Not parallel but functional!
Huge improvements to the design
This commit is contained in:
parent
cbd45ffc92
commit
3ff89e0065
60
src/main.rs
60
src/main.rs
@ -1,62 +1,44 @@
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
const UPDATE_INTERVAL: u32 = 40;
|
const UPDATE_INTERVAL: u32 = 40;
|
||||||
|
const MAX_FRAMESKIP: u32 = 5;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// track the update delay's exponential moving average (EMA)
|
// track the delta time (the duration of the previous loop in milliseconds)
|
||||||
let mut frame_delay_ema: f64 = 0.0;
|
let mut delta_time: u32 = 0;
|
||||||
|
|
||||||
// track the latest delays
|
|
||||||
let mut frame_delay: u32 = 0;
|
|
||||||
let mut tick_delay: u32 = 0;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// start per-loop delay timer
|
// start per-loop delay timer
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
|
||||||
// DEBUG - track if a tick has happened on this frame
|
// track the number of update ticks
|
||||||
let mut ticked: bool = false;
|
let mut ticks: u32 = 0;
|
||||||
|
|
||||||
// update (game logic, etc.)
|
update(&mut delta_time, &mut ticks);
|
||||||
// executes every UPDATE_INTERVAL milliseconds at maximum
|
|
||||||
if tick_delay >= UPDATE_INTERVAL {
|
|
||||||
// perform all per-tick logic here
|
|
||||||
update();
|
|
||||||
|
|
||||||
// reset the tick delay
|
display();
|
||||||
tick_delay = 0;
|
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
ticked = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// display (rendering)
|
// update the delta time
|
||||||
// executes as fast as possible
|
let elapsed_time: u32 = timer.elapsed().as_millis() as u32;
|
||||||
{
|
delta_time += elapsed_time;
|
||||||
display();
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the current frame delay, update the frame delay EMA and the tick delay
|
|
||||||
frame_delay = timer.elapsed().as_millis() as u32;
|
|
||||||
frame_delay_ema = lerp_1d(frame_delay_ema, frame_delay as f64, 0.1f64);
|
|
||||||
tick_delay += frame_delay;
|
|
||||||
|
|
||||||
// debug
|
|
||||||
let frame_rate = (1000.0f64 / frame_delay_ema as f64) as u32;
|
|
||||||
if ticked {
|
|
||||||
println!("Frame Delay: {}ms | Frame Rate: {}Hz | Ticked!", frame_delay, frame_rate);
|
|
||||||
} else {
|
|
||||||
println!("Frame Delay: {}ms | Frame Rate: {}Hz", frame_delay, frame_rate);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update function
|
// update function
|
||||||
fn update() {
|
fn update(delta_time: &mut u32, ticks: &mut u32) {
|
||||||
waste_time(64);
|
// 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 and reset the delta time
|
||||||
|
*ticks += 1;
|
||||||
|
*delta_time = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// render function
|
// render function
|
||||||
fn display() {
|
fn display() {
|
||||||
|
println!("Displaying...");
|
||||||
waste_time(16);
|
waste_time(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user