summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRaymond Hogenson <rhogenson@posteo.net>2018-08-20 00:27:28 -0400
committerRaymond Hogenson <rhogenson@posteo.net>2018-08-20 00:29:34 -0400
commit5bd92c7df63002982a6936bbd55eca329391ce68 (patch)
tree1aa74e5e13b329b9c4571cee121c82316b837a30 /src
parentMove some code around and improve abstraction (diff)
downloadstallman-shooter-5bd92c7df63002982a6936bbd55eca329391ce68.tar.zst
Count frames more precisely
Instead of calculating the time from the beginning, we'll just check the time since the frame was started. This means if you are slow and then fast, it won't freak out. This was inspired by an app I wrote for Clank which counts clanks frame by frame and I needed some similar mechanism.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs8
1 files changed, 3 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs
index 6e1322a..9089daf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -84,9 +84,7 @@ fn main() {
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
- let start = std::time::Instant::now();
let frame_duration = std::time::Duration::new(0, 1_000_000_000u32 / 60);
- let mut frame_count = 0;
let mut state: state::State<gates::Gates, gates::GatesBullet> = state::State::new();
let mut won = false;
let player_surface: sdl2::surface::Surface = sdl2::image::LoadSurface::from_file("assets/stallman.png").unwrap();
@@ -100,6 +98,7 @@ fn main() {
let pbullet_texture = texture_creator.create_texture_from_surface(pbullet_surface).unwrap();
let surfaces = Textures { player: player_texture, enemy: enemy_texture, pbullet: pbullet_texture, ebullet: ebullet_texture };
'running: loop {
+ let start = std::time::Instant::now();
if won {
for event in event_pump.poll_iter() {
match event {
@@ -127,11 +126,10 @@ fn main() {
}
}
}
- let calc_time = frame_duration * frame_count;
- match calc_time.checked_sub(start.elapsed()) {
+ /* Make sure this iteration took a whole frame */
+ match frame_duration.checked_sub(start.elapsed()) {
Some(t) => std::thread::sleep(t),
None => (),
}
- frame_count += 1;
}
}