Got rid of frameskipping because it wasn't working, fixed inaccuracies in delta time, added relative time for interpolation

This commit is contained in:
skyeshroom 2021-08-09 13:24:06 -07:00
parent 56448eceed
commit 3315491b0f
3 changed files with 20 additions and 22 deletions

2
Cargo.lock generated
View File

@ -103,7 +103,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "loopcore" name = "hypoloop"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",

View File

@ -1,5 +1,5 @@
[package] [package]
name = "loopcore" name = "hypoloop"
version = "0.1.0" version = "0.1.0"
edition = "2018" edition = "2018"

View File

@ -1,6 +1,5 @@
use std::time::Instant; use std::time::Instant;
const UPDATE_INTERVAL: u32 = 40; const UPDATE_INTERVAL: u32 = 80;
const MAX_FRAMESKIP: u32 = 5;
fn main() { fn main() {
// track the delta time (the duration of the previous loop in milliseconds) // track the delta time (the duration of the previous loop in milliseconds)
@ -12,13 +11,23 @@ fn main() {
// start per-loop delay timer // start per-loop delay timer
let timer = Instant::now(); let timer = Instant::now();
// track the number of update ticks // update
let mut ticks: u32 = 0; if delta_time >= UPDATE_INTERVAL {
println!("Updating");
update();
update(&mut delta_time, &mut ticks); // DEBUG
// reset the delta time
delta_time = timer.elapsed().as_millis() as u32;
}
// support interpolation via fractional "relative time"
let relative_time: f32 = delta_time as f32 / UPDATE_INTERVAL as f32;
println!("Displaying | Delta Time: {}ms | Relative Time: {}", delta_time, relative_time);
display(); display();
// update the delta time // update the delta time
let elapsed_time: u32 = timer.elapsed().as_millis() as u32; let elapsed_time: u32 = timer.elapsed().as_millis() as u32;
delta_time += elapsed_time; delta_time += elapsed_time;
@ -27,24 +36,13 @@ fn main() {
// update function // update function
// TODO - I think I'm resetting delta time in the wrong place or just using it incorrectly; frameskips aren't happening at all // TODO - I think I'm resetting delta time in the wrong place or just using it incorrectly; frameskips aren't happening at all
fn update(delta_time: &mut u32, ticks: &mut u32) { fn update() {
// keep going even if frames aren't displaying, but halt if there are too many frameskips waste_time(64);
while *delta_time >= UPDATE_INTERVAL && *ticks < MAX_FRAMESKIP {
println!("Updating...");
waste_time(64);
// record the tick
*ticks += 1;
// reset the delta time
*delta_time = 0;
}
} }
// render function // render function
fn display() { fn display() {
println!("Displaying..."); waste_time(4);
waste_time(32);
} }
// 1D linear interpolation // 1D linear interpolation