Got rid of frameskipping because it wasn't working, fixed inaccuracies in delta time, added relative time for interpolation
This commit is contained in:
parent
56448eceed
commit
3315491b0f
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -103,7 +103,7 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "loopcore"
|
||||
name = "hypoloop"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"futures",
|
||||
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "loopcore"
|
||||
name = "hypoloop"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
|
36
src/main.rs
36
src/main.rs
@ -1,6 +1,5 @@
|
||||
use std::time::Instant;
|
||||
const UPDATE_INTERVAL: u32 = 40;
|
||||
const MAX_FRAMESKIP: u32 = 5;
|
||||
const UPDATE_INTERVAL: u32 = 80;
|
||||
|
||||
fn main() {
|
||||
// track the delta time (the duration of the previous loop in milliseconds)
|
||||
@ -12,11 +11,21 @@ fn main() {
|
||||
// start per-loop delay timer
|
||||
let timer = Instant::now();
|
||||
|
||||
// track the number of update ticks
|
||||
let mut ticks: u32 = 0;
|
||||
// update
|
||||
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();
|
||||
|
||||
// update the delta time
|
||||
@ -27,24 +36,13 @@ fn main() {
|
||||
|
||||
// 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
|
||||
fn update(delta_time: &mut u32, ticks: &mut u32) {
|
||||
// keep going even if frames aren't displaying, but halt if there are too many frameskips
|
||||
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;
|
||||
}
|
||||
fn update() {
|
||||
waste_time(64);
|
||||
}
|
||||
|
||||
// render function
|
||||
fn display() {
|
||||
println!("Displaying...");
|
||||
waste_time(32);
|
||||
waste_time(4);
|
||||
}
|
||||
|
||||
// 1D linear interpolation
|
||||
|
Loading…
Reference in New Issue
Block a user