♻️ Extract timestep.rs
parent
f8ceec6b9f
commit
593784917e
54
src/main.rs
54
src/main.rs
|
@ -4,17 +4,19 @@ use sdl2::event::Event;
|
||||||
use sdl2::keyboard::{Keycode, Scancode};
|
use sdl2::keyboard::{Keycode, Scancode};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::ffi::{c_void, CStr, CString};
|
use std::ffi::{c_void, CString};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration};
|
||||||
|
|
||||||
mod iqm;
|
mod iqm;
|
||||||
mod shader;
|
mod shader;
|
||||||
|
mod timestep;
|
||||||
|
|
||||||
use iqm::Model;
|
use iqm::Model;
|
||||||
use shader::{ShaderProgram, ShaderObject};
|
use shader::{ShaderProgram, ShaderObject};
|
||||||
|
use timestep::TimeStep;
|
||||||
|
|
||||||
pub fn load_small_file <P> (name: P) -> Vec <u8>
|
pub fn load_small_file <P> (name: P) -> Vec <u8>
|
||||||
where P: AsRef <Path>
|
where P: AsRef <Path>
|
||||||
|
@ -222,54 +224,6 @@ impl WorldState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TimeStep {
|
|
||||||
last_frame_time: Instant,
|
|
||||||
// Milliseconds
|
|
||||||
accum: u128,
|
|
||||||
fps_num: u16,
|
|
||||||
fps_den: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TimeStep {
|
|
||||||
pub fn new (fps_num: u16, fps_den: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
last_frame_time: Instant::now (),
|
|
||||||
accum: 0,
|
|
||||||
fps_num,
|
|
||||||
fps_den,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Automatically gets monotonic system time from Instant
|
|
||||||
// If you need something fancy just rewrite this.
|
|
||||||
// Returns: How many logics steps to run. Typically 0 or 1.
|
|
||||||
|
|
||||||
pub fn step (&mut self) -> u16 {
|
|
||||||
let frame_time = Instant::now ();
|
|
||||||
|
|
||||||
let fps_num_128: u128 = self.fps_num.into ();
|
|
||||||
let fps_den_128: u128 = self.fps_den.into ();
|
|
||||||
|
|
||||||
self.accum += (frame_time - self.last_frame_time).as_millis () * fps_num_128;
|
|
||||||
|
|
||||||
let mut result = 0;
|
|
||||||
const MAX_FRAMES: u16 = 4;
|
|
||||||
|
|
||||||
for _ in 0..MAX_FRAMES {
|
|
||||||
if self.accum > fps_den_128 {
|
|
||||||
result += 1;
|
|
||||||
self.accum -= fps_den_128;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.last_frame_time = frame_time;
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main () {
|
fn main () {
|
||||||
let sdl_context = sdl2::init ().unwrap ();
|
let sdl_context = sdl2::init ().unwrap ();
|
||||||
let video_subsystem = sdl_context.video ().unwrap ();
|
let video_subsystem = sdl_context.video ().unwrap ();
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub struct TimeStep {
|
||||||
|
last_frame_time: Instant,
|
||||||
|
// Milliseconds
|
||||||
|
accum: u128,
|
||||||
|
fps_num: u16,
|
||||||
|
fps_den: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TimeStep {
|
||||||
|
pub fn new (fps_num: u16, fps_den: u16) -> Self {
|
||||||
|
Self {
|
||||||
|
last_frame_time: Instant::now (),
|
||||||
|
accum: 0,
|
||||||
|
fps_num,
|
||||||
|
fps_den,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Automatically gets monotonic system time from Instant
|
||||||
|
// If you need something fancy just rewrite this.
|
||||||
|
// Returns: How many logics steps to run. Typically 0 or 1.
|
||||||
|
|
||||||
|
pub fn step (&mut self) -> u16 {
|
||||||
|
let frame_time = Instant::now ();
|
||||||
|
|
||||||
|
let fps_num_128: u128 = self.fps_num.into ();
|
||||||
|
let fps_den_128: u128 = self.fps_den.into ();
|
||||||
|
|
||||||
|
self.accum += (frame_time - self.last_frame_time).as_millis () * fps_num_128;
|
||||||
|
|
||||||
|
let mut result = 0;
|
||||||
|
const MAX_FRAMES: u16 = 4;
|
||||||
|
|
||||||
|
for _ in 0..MAX_FRAMES {
|
||||||
|
if self.accum > fps_den_128 {
|
||||||
|
result += 1;
|
||||||
|
self.accum -= fps_den_128;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.last_frame_time = frame_time;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue