Massive update, exposed a ton of functions
This commit is contained in:
parent
1e29ee2476
commit
bf6c66e3b9
@ -6,4 +6,4 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hypoloop = {version = "0.1.1", path = "D:/Code/hypoloop"}
|
hypoloop = {version = "0.1.1", path = "D:/OneDrive/Code/hypoloop"}
|
@ -1,16 +1,20 @@
|
|||||||
use hypoloop::Simulation;
|
use hypoloop::core::Loop;
|
||||||
|
|
||||||
// look into using closures for this
|
// look into using closures for this
|
||||||
fn main() {
|
fn main() {
|
||||||
// create sim and configure it
|
// create sim and configure it
|
||||||
let mut sim = Simulation::new();
|
let mut sim = Loop::new();
|
||||||
sim.set_update_function(test);
|
|
||||||
//sim.set_realtime(false);
|
//sim.set_realtime(false);
|
||||||
|
|
||||||
// run sim
|
// test variable
|
||||||
sim.run();
|
let mut x: f32 = 0.0;
|
||||||
}
|
|
||||||
|
|
||||||
fn test() {
|
// run the simulation using custom update logic
|
||||||
println!("Test");
|
sim.run(|state| {
|
||||||
|
state.debug_tick();
|
||||||
|
|
||||||
|
x += 2.0 * state.get_timescale();
|
||||||
|
|
||||||
|
//println!("Delta time: {} | Timescale: {} | Sim time: {} | x: {}", state.get_delta_time(), state.get_timescale(), state.get_sim_time().as_millis(), x);
|
||||||
|
});
|
||||||
}
|
}
|
186
src/lib.rs
186
src/lib.rs
@ -1,114 +1,153 @@
|
|||||||
|
pub mod core {
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
// debug constants
|
/// Contains mutable simulation state which can be changed via callback functions
|
||||||
const DEBUG_LOOP: bool = false;
|
#[derive(Copy, Clone)]
|
||||||
const DEBUG_TIME: bool = true;
|
pub struct State {
|
||||||
|
|
||||||
/// Contains all per-simulation logic and state
|
|
||||||
// update_interval is the minimum delay (in milliseconds) between update ticks
|
|
||||||
// timescale is the rate of the simulation proportional to real-time
|
|
||||||
// if realtime is false, the simulation runs as fast as possible and doesn't run the display function
|
|
||||||
pub struct Simulation {
|
|
||||||
update_interval: u32,
|
update_interval: u32,
|
||||||
timescale: f32,
|
timescale: f32,
|
||||||
realtime: bool,
|
|
||||||
simulate: bool,
|
simulate: bool,
|
||||||
update_function: fn(),
|
delta_time: u32,
|
||||||
display_function: fn()
|
irl_time: Duration,
|
||||||
|
sim_time: Duration,
|
||||||
|
last_tick: Instant
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Simulation {
|
impl State {
|
||||||
/// Creates a new simulation with default values
|
/// Creates a default State object
|
||||||
pub fn new() -> Simulation {
|
pub fn new() -> State {
|
||||||
Simulation {
|
// Create default state object
|
||||||
|
let mut new_state = State {
|
||||||
update_interval: 40,
|
update_interval: 40,
|
||||||
timescale: 1.0,
|
timescale: 1.0,
|
||||||
realtime: true,
|
|
||||||
simulate: true,
|
simulate: true,
|
||||||
update_function: default_update,
|
delta_time: 0,
|
||||||
display_function: default_display
|
irl_time: Duration::new(0,0),
|
||||||
|
sim_time: Duration::new(0,0),
|
||||||
|
last_tick: Instant::now()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make sure that delta_time always starts the same as update_interval
|
||||||
|
new_state.delta_time = new_state.update_interval;
|
||||||
|
|
||||||
|
// Return this default state
|
||||||
|
new_state
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current "delta time", the time elapsed since the last update tick in milliseconds
|
||||||
|
pub fn get_delta_time(self) -> u32 {
|
||||||
|
self.delta_time
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current IRL time elapsed since the start of the simulation
|
||||||
|
pub fn get_irl_time(self) -> Duration {
|
||||||
|
self.irl_time
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current simulation time elapsed since the start of the simulation
|
||||||
|
pub fn get_sim_time(self) -> Duration {
|
||||||
|
self.sim_time
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current "timescale", the speed of simulation time relative to real time
|
||||||
|
pub fn get_timescale(self) -> f32 {
|
||||||
|
self.timescale
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the time of the last tick
|
||||||
|
pub fn get_last_tick(self) -> Instant {
|
||||||
|
self.last_tick
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pauses the simulation
|
||||||
|
pub fn pause(&mut self) {
|
||||||
|
self.simulate = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resumes the simulation
|
||||||
|
pub fn resume(&mut self) {
|
||||||
|
self.simulate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the simulation timescale
|
||||||
|
pub fn set_timescale(&mut self, timescale: f32) {
|
||||||
|
self.timescale = timescale;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prints a string of information about the current tick
|
||||||
|
pub fn debug_tick(self) {
|
||||||
|
let elapsed_time = Instant::now().duration_since(self.last_tick);
|
||||||
|
let loop_delay_ms = elapsed_time.as_nanos() as f32 / 1_000_000.0;
|
||||||
|
let loop_rate_hz = 1000.0 / loop_delay_ms;
|
||||||
|
println!("IRL time: {}ms | Sim time: {}ms | Tick delay/rate: {}ms/{}hz", self.irl_time.as_millis(), self.sim_time.as_millis(), loop_delay_ms, loop_rate_hz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows the user to pass in a custom update function
|
/// The simulation loop itself
|
||||||
pub fn set_update_function(&mut self, user_function: fn()) {
|
pub struct Loop {
|
||||||
self.update_function = user_function;
|
state: State,
|
||||||
|
realtime: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows the user to pass in a custom display function
|
impl Loop {
|
||||||
pub fn set_display_function(&mut self, user_function: fn()) {
|
/// Creates a new simulation with default values
|
||||||
self.display_function = user_function;
|
pub fn new() -> Loop {
|
||||||
|
// Return a Loop object with a default State
|
||||||
|
Loop {
|
||||||
|
state: State::new(),
|
||||||
|
realtime: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows the user to turn realtime mode on/off
|
/// Initializes and runs the simulation using a user-supplied callback as the update logic
|
||||||
pub fn set_realtime(&mut self, realtime: bool) {
|
pub fn run(&mut self, mut update_callback: impl FnMut(&mut State)) {
|
||||||
self.realtime = realtime;
|
// Make sure the simulation will run
|
||||||
}
|
self.state.simulate = true;
|
||||||
|
|
||||||
/// Initializes and runs the simulation
|
|
||||||
pub fn run(&self) {
|
|
||||||
// start the clock to keep track of real time
|
// start the clock to keep track of real time
|
||||||
let clock_start = Instant::now();
|
let clock_start = Instant::now();
|
||||||
|
|
||||||
// keep track of the last tick time
|
while self.state.simulate {
|
||||||
let mut last_tick = Instant::now();
|
|
||||||
|
|
||||||
// real-time and sim-time clocks
|
|
||||||
let mut irl_time = Duration::new(0, 0);
|
|
||||||
let mut sim_time = Duration::new(0, 0);
|
|
||||||
|
|
||||||
while self.simulate {
|
|
||||||
// TODO - support frameskips
|
// TODO - support frameskips
|
||||||
if !self.realtime || delta_time(last_tick) >= self.update_interval {
|
if !self.realtime || delta_time(self.state.last_tick) >= self.state.update_interval {
|
||||||
// mutable delta time and timescale for flexibility
|
// mutable delta time and timescale for flexibility
|
||||||
let mut current_timescale: f32;
|
let elapsed_time = Instant::now().duration_since(self.state.last_tick);
|
||||||
let mut current_delta_time: u32;
|
|
||||||
let elapsed_time = Instant::now().duration_since(last_tick);
|
|
||||||
|
|
||||||
// update clocks
|
// update clocks
|
||||||
if self.realtime {
|
if self.realtime {
|
||||||
current_timescale = self.timescale;
|
self.state.delta_time = delta_time(self.state.last_tick);
|
||||||
current_delta_time = delta_time(last_tick);
|
self.state.sim_time += elapsed_time.mul_f32(self.state.timescale);
|
||||||
sim_time += elapsed_time.mul_f32(self.timescale);
|
self.state.irl_time += elapsed_time;
|
||||||
irl_time += elapsed_time;
|
|
||||||
} else {
|
} else {
|
||||||
current_timescale = 1.0;
|
self.state.delta_time = self.state.update_interval;
|
||||||
current_delta_time = self.update_interval;
|
self.state.sim_time += Duration::from_millis(self.state.update_interval as u64);
|
||||||
sim_time += Duration::from_millis(self.update_interval as u64);
|
self.state.irl_time = Instant::now().duration_since(clock_start);
|
||||||
irl_time = Instant::now().duration_since(clock_start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
if DEBUG_TIME {
|
|
||||||
let loop_delay_ms = elapsed_time.as_nanos() as f32 / 1_000_000.0;
|
|
||||||
let loop_rate_hz = 1000.0 / loop_delay_ms;
|
|
||||||
println!("Realtime: {} | IRL time: {}ms | Sim time: {}ms | Tick delay/rate: {}ms/{}hz", self.realtime, irl_time.as_millis(), sim_time.as_millis(), loop_delay_ms, loop_rate_hz);
|
|
||||||
}
|
|
||||||
|
|
||||||
// record last tick time
|
|
||||||
last_tick = Instant::now();
|
|
||||||
|
|
||||||
// update
|
// update
|
||||||
update(self.update_function, current_delta_time, current_timescale);
|
update_callback(&mut self.state);
|
||||||
|
|
||||||
|
// record last tick time
|
||||||
|
self.state.last_tick = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
// display
|
// display
|
||||||
if self.realtime {
|
if self.realtime {
|
||||||
display(self.display_function, delta_time(last_tick), self.timescale, self.update_interval);
|
display(delta_time(self.state.last_tick), self.state.timescale, self.state.update_interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Turns real-time mode on/off
|
||||||
|
pub fn set_realtime(&mut self, realtime: bool) {
|
||||||
|
self.realtime = realtime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// update function
|
// update function
|
||||||
// this is where all your per-tick logic should go
|
// this is where all your per-tick logic should go
|
||||||
fn update(user_function: fn(), delta_time: u32, timescale: f32) {
|
fn update(user_function: fn(), delta_time: u32, timescale: f32) {
|
||||||
// DEBUG
|
|
||||||
if DEBUG_LOOP {
|
|
||||||
println!("Updating...");
|
|
||||||
}
|
|
||||||
|
|
||||||
// use timestep to scale per-tick calculations appropriately
|
// use timestep to scale per-tick calculations appropriately
|
||||||
let timestep: f32 = delta_time as f32 / 1000.0 * timescale;
|
let timestep: f32 = delta_time as f32 / 1000.0 * timescale;
|
||||||
|
|
||||||
@ -118,17 +157,9 @@ fn update(user_function: fn(), delta_time: u32, timescale: f32) {
|
|||||||
|
|
||||||
// display function
|
// display function
|
||||||
// this is where you should call a render function
|
// this is where you should call a render function
|
||||||
fn display(user_function: fn(), delta_time: u32, timescale: f32, update_interval: u32) {
|
fn display(delta_time: u32, timescale: f32, update_interval: u32) {
|
||||||
// DEBUG
|
|
||||||
if DEBUG_LOOP {
|
|
||||||
println!("Displaying...");
|
|
||||||
}
|
|
||||||
|
|
||||||
// use interpolation to smooth display values between ticks
|
// use interpolation to smooth display values between ticks
|
||||||
let interpolation: f32 = delta_time as f32 / update_interval as f32 * timescale;
|
let interpolation: f32 = delta_time as f32 / update_interval as f32 * timescale;
|
||||||
|
|
||||||
// call user display function
|
|
||||||
user_function();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gets the time in milliseconds that's elapsed since the earlier Instant
|
// gets the time in milliseconds that's elapsed since the earlier Instant
|
||||||
@ -143,3 +174,4 @@ fn default_update() {
|
|||||||
// default display function (does nothing)
|
// default display function (does nothing)
|
||||||
fn default_display() {
|
fn default_display() {
|
||||||
}
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user