mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-11 04:07:55 -06:00
reformat: strict tick timing with lag prints
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -46,7 +46,7 @@ pub fn main() {
|
|||||||
.expect("Could not set logical size");
|
.expect("Could not set logical size");
|
||||||
|
|
||||||
let texture_creator = canvas.texture_creator();
|
let texture_creator = canvas.texture_creator();
|
||||||
let mut game = Game::new(&mut canvas, TextureManager::new(&texture_creator));
|
let mut game = Game::new(&mut canvas, &texture_creator);
|
||||||
|
|
||||||
let mut event_pump = sdl_context
|
let mut event_pump = sdl_context
|
||||||
.event_pump()
|
.event_pump()
|
||||||
@@ -55,7 +55,11 @@ pub fn main() {
|
|||||||
game.draw();
|
game.draw();
|
||||||
game.tick();
|
game.tick();
|
||||||
|
|
||||||
|
let loop_time = Duration::from_millis(1000 / 60);
|
||||||
|
|
||||||
let mut main_loop = || {
|
let mut main_loop = || {
|
||||||
|
let start = Instant::now();
|
||||||
|
|
||||||
for event in event_pump.poll_iter() {
|
for event in event_pump.poll_iter() {
|
||||||
match event {
|
match event {
|
||||||
// Handle quitting keys or window close
|
// Handle quitting keys or window close
|
||||||
@@ -64,9 +68,9 @@ pub fn main() {
|
|||||||
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
|
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
|
||||||
..
|
..
|
||||||
} => return false,
|
} => return false,
|
||||||
event @ Event::KeyDown { .. } => {
|
Event::KeyDown { keycode , .. } => {
|
||||||
println!("{:?}", event);
|
game.keyboard_event(keycode.unwrap());
|
||||||
}
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,15 +87,12 @@ pub fn main() {
|
|||||||
start.elapsed()
|
start.elapsed()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Alert if tick time exceeds 10ms
|
if start.elapsed() < loop_time {
|
||||||
if tick_time > Duration::from_millis(3) {
|
::std::thread::sleep(loop_time - start.elapsed());
|
||||||
println!("Tick took: {:?}", tick_time);
|
} else {
|
||||||
}
|
println!("Game loop behind schedule by: {:?}", start.elapsed() - loop_time);
|
||||||
if draw_time > Duration::from_millis(3) {
|
|
||||||
println!("Draw took: {:?}", draw_time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
::std::thread::sleep(Duration::from_millis(10));
|
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user