From 0ee17ac3d909536ccf09761ec3f4f665ecfd6102 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 10 Oct 2023 02:40:24 -0700 Subject: [PATCH] timers --- src/main.rs | 1 + src/timers.rs | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/timers.rs diff --git a/src/main.rs b/src/main.rs index 2d39d3e..8178ad7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use instruction::{TimedInstruction, InstructionConsumer}; mod bvh; mod aabb; mod model; +mod timers; mod zeroes; mod worker; mod physics; diff --git a/src/timers.rs b/src/timers.rs new file mode 100644 index 0000000..8bab1e5 --- /dev/null +++ b/src/timers.rs @@ -0,0 +1,150 @@ +type TIME=crate::physics::TIME; + +pub struct Timescale{ + num:i64, + den:std::num::NonZeroU64, +} + +pub struct Paused{} +pub struct Unpaused{} +pub struct PausedScaled{scale:Timescale} +pub struct UnpausedScaled{scale:Timescale} + +pub trait TimerState{} +impl TimerState for Paused{} +impl TimerState for Unpaused{} +impl TimerState for PausedScaled{} +impl TimerState for UnpausedScaled{} + +pub trait IsPaused{} +impl IsPaused for Paused{} +impl IsPaused for PausedScaled{} + +pub trait IsUnpaused{} +impl IsUnpaused for Unpaused{} +impl IsUnpaused for UnpausedScaled{} + +pub trait IsScaled{} +impl IsScaled for PausedScaled{} +impl IsScaled for UnpausedScaled{} + +pub trait IsUnscaled{} +impl IsUnscaled for Paused{} +impl IsUnscaled for Unpaused{} + +pub struct Timer{ + offset:crate::physics::TIME, + state:State, +} +fn get_offset(time:TIME,write_time:TIME)->TIME{ + write_time-time +} +fn get_offset_scaled(time:TIME,write_time:TIME,scale:&Timescale)->TIME{ + write_time-time*scale.num/scale.den.get() as i64 +} + +fn paused()->Timer{ + Timer{ + offset:0, + state:Paused{}, + } +} +fn unpaused()->Timer{ + Timer{ + offset:0, + state:Unpaused{}, + } +} +fn paused_scaled(scale:Timescale)->Timer{ + Timer{ + offset:0, + state:PausedScaled{scale}, + } +} +fn unpaused_scaled(scale:Timescale)->Timer{ + Timer{ + offset:0, + state:UnpausedScaled{scale}, + } +} +impl Timer{ + pub fn time(&self)->TIME{ + self.offset + } + pub fn unpause(self,time:TIME)->Timer{ + Timer{ + offset:get_offset(time,self.time()), + state:Unpaused{}, + } + } + pub fn set_time(&mut self,time:TIME,write_time:TIME){ + self.offset=get_offset(time,write_time); + } + pub fn set_scale(self,time:TIME,scale:Timescale)->Timer{ + Timer{ + offset:get_offset_scaled(time,self.time(),&scale), + state:PausedScaled{scale}, + } + } +} +impl Timer{ + pub fn time(&self,time:TIME)->TIME{ + self.offset+time + } + pub fn pause(self,time:TIME)->Timer{ + Timer{ + offset:self.time(time), + state:Paused{}, + } + } + pub fn set_time(&mut self,time:TIME,write_time:TIME){ + self.offset=get_offset(time,write_time); + } + pub fn set_scale(self,time:TIME,scale:Timescale)->Timer{ + Timer{ + offset:get_offset_scaled(time,self.time(time),&scale), + state:UnpausedScaled{scale}, + } + } +} +impl Timer{ + pub fn time(&self)->TIME{ + self.offset + } + pub fn unpause(self,time:TIME)->Timer{ + Timer{ + offset:get_offset_scaled(time,self.time(),&self.state.scale), + state:UnpausedScaled{scale:self.state.scale}, + } + } + pub fn set_time(&mut self,time:TIME,write_time:TIME){ + self.offset=get_offset_scaled(time,write_time,&self.state.scale); + } + pub fn set_scale(self,time:TIME,scale:Timescale)->Timer{ + Timer{ + offset:get_offset_scaled(time,self.time(),&scale), + state:PausedScaled{scale}, + } + } +} +impl Timer{ + pub fn time(&self,time:TIME)->TIME{ + self.offset+time*self.state.scale.num/self.state.scale.den.get() as i64 + } + pub fn pause(self,time:TIME)->Timer{ + Timer{ + offset:self.time(time), + state:PausedScaled{scale:self.state.scale}, + } + } + pub fn set_time(&mut self,time:TIME,write_time:TIME){ + self.offset=get_offset_scaled(time,write_time,&self.state.scale); + } + pub fn set_scale(self,time:TIME,scale:Timescale)->Timer{ + Timer{ + offset:get_offset_scaled(time,self.time(time),&scale), + //self.offset+time*self.state.scale.num/self.state.scale.den.get() as i64-time*scale.num/scale.den.get() as i64 + state:UnpausedScaled{scale}, + } + } +} \ No newline at end of file