Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Created September 25, 2014 09:48
Show Gist options
  • Save bvssvni/3cdc297616aa37b7526f to your computer and use it in GitHub Desktop.
Save bvssvni/3cdc297616aa37b7526f to your computer and use it in GitHub Desktop.
impl<'a, W: Window>
Iterator<Event>
for EventIterator<'a, W> {
/// Returns the next game event.
fn next(&mut self) -> Option<Event> {
loop {
self.state = match self.state {
RenderState => {
if self.window.should_close() { return None; }
let start_render = time::precise_time_ns();
self.last_frame = start_render;
let (w, h) = self.window.get_size();
if w != 0 && h != 0 {
// Swap buffers next time.
self.state = SwapBuffersState;
return Some(Render(
// Extrapolate time forward to allow smooth motion.
ext_dt: (start_render - self.last_update) as f64 / billion as f64,
width: w,
height: h,
));
}
UpdateLoopState
}
SwapBuffersState => {
self.window.swap_buffers();
UpdateLoopState
}
UpdateLoopState => {
let current_time = time::precise_time_ns();
let next_frame = self.last_frame + self.dt_frame_in_ns;
let next_update = self.last_update + self.dt_update_in_ns;
let next_event = cmp::min(next_frame, next_update);
if next_event > current_time {
sleep( Duration::nanoseconds((next_event - current_time) as i64) );
UpdateLoopState
} else if next_event == next_frame {
RenderState
} else {
HandleEventsState
}
}
HandleEventsState => {
// Handle all events before updating.
match self.window.poll_event() {
None => UpdateState,
Some(x) => { return Some(Input(x)); },
}
}
UpdateState => {
self.state = UpdateLoopState;
self.last_update += self.dt_update_in_ns;
return Some(Update(dt: self.dt));
}
};
}
}
}
impl<'a, W: Window>
Iterator<Event>
for EventIterator<'a, W> {
/// Returns the next game event.
fn next(&mut self) -> Option<Event> {
loop {
self.state = match self.state {
RenderState => {
if self.window.should_close() { return None; }
let start_render = time::precise_time_ns();
self.last_frame = start_render;
let (w, h) = self.window.get_size();
if w != 0 && h != 0 {
// Swap buffers next time.
self.state = SwapBuffersState;
return Some(Render(RenderArgs {
// Extrapolate time forward to allow smooth motion.
ext_dt: (start_render - self.last_update) as f64 / billion as f64,
width: w,
height: h,
}));
}
UpdateLoopState
}
SwapBuffersState => {
self.window.swap_buffers();
UpdateLoopState
}
UpdateLoopState => {
let current_time = time::precise_time_ns();
let next_frame = self.last_frame + self.dt_frame_in_ns;
let next_update = self.last_update + self.dt_update_in_ns;
let next_event = cmp::min(next_frame, next_update);
if next_event > current_time {
sleep( Duration::nanoseconds((next_event - current_time) as i64) );
UpdateLoopState
} else if next_event == next_frame {
RenderState
} else {
HandleEventsState
}
}
HandleEventsState => {
// Handle all events before updating.
match self.window.poll_event() {
None => UpdateState,
Some(x) => { return Some(Input(x)); },
}
}
UpdateState => {
self.state = UpdateLoopState;
self.last_update += self.dt_update_in_ns;
return Some(Update(UpdateArgs{ dt: self.dt }));
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment