summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-06-03 20:48:07 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-08-05 23:02:47 +0200
commitdada176e8400d1c3cc8fb51cb1d7864485851059 (patch)
treeabcfe80d35f1ba62fd806d0698bfa79632df3b58
parent406a1be081e6144f38402bfe64b0dc221bb3dbdd (diff)
Changed time to use Rust time rather than SDL time
-rw-r--r--gate/Cargo.toml1
-rw-r--r--gate/src/core/sdl/app_clock.rs56
-rw-r--r--gate/src/core/sdl/event_handler.rs2
-rw-r--r--gate/src/core/sdl/mod.rs3
-rw-r--r--gate/src/lib.rs3
5 files changed, 31 insertions, 34 deletions
diff --git a/gate/Cargo.toml b/gate/Cargo.toml
index 98e48f6..d092137 100644
--- a/gate/Cargo.toml
+++ b/gate/Cargo.toml
@@ -15,3 +15,4 @@ byteorder = "1.1.0"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
gl = "0.6.1"
sdl2 = { version = "0.29", default-features = false, features = ["image", "mixer"] }
+time = "0.1.40" \ No newline at end of file
diff --git a/gate/src/core/sdl/app_clock.rs b/gate/src/core/sdl/app_clock.rs
index b45cb88..49fc67b 100644
--- a/gate/src/core/sdl/app_clock.rs
+++ b/gate/src/core/sdl/app_clock.rs
@@ -12,59 +12,53 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use sdl2::TimerSubsystem;
-
use app_info::AppInfo;
-use std::u32;
+use std::u64;
+use std::thread;
+
+use std::time::Duration;
+use time;
pub struct AppClock {
- timer: TimerSubsystem,
- last_ticks: u32,
- frame_dur_millis: u32,
+ last_ticks: u64,
+ frame_dur_nanos: u32,
work_load_sum: f64,
work_load_max: f64,
work_load_frames: u64,
- last_print_workload_ticks: u32,
+ last_print_workload_ticks: u64,
}
impl AppClock {
- pub fn new(mut timer: TimerSubsystem, info: &AppInfo) -> AppClock {
+ pub fn new(info: &AppInfo) -> AppClock {
AppClock {
- last_ticks: timer.ticks(),
- timer,
- frame_dur_millis: (1000. / info.target_fps).round() as u32,
+ last_ticks: time::precise_time_ns(),
+ frame_dur_nanos: (1_000_000_000. / info.target_fps).round() as u32,
work_load_sum: 0.0,
work_load_max: 0.0,
work_load_frames: 0,
- last_print_workload_ticks: if info.print_workload_info { 1 } else { u32::MAX - 100_000 },
+ last_print_workload_ticks: if info.print_workload_info { 1 } else { u64::MAX - 100_000 },
}
}
pub fn step(&mut self) -> f64 {
- let mut elapsed;
- let mut first_iter = true;
- loop {
- let now = self.timer.ticks();
- let dt = now - self.last_ticks;
- elapsed = dt as f64 / 1_000.0;
+ let now_before_delay = time::precise_time_ns();
- if first_iter {
- first_iter = false;
- self.append_workload(now, dt);
- }
+ let dt_before_delay = (now_before_delay - self.last_ticks) as u32;
- if dt < self.frame_dur_millis {
- self.timer.delay(self.frame_dur_millis - dt);
- } else {
- break;
- }
+ if dt_before_delay < self.frame_dur_nanos {
+ thread::sleep(Duration::new(0, self.frame_dur_nanos - dt_before_delay));
}
- self.last_ticks = self.timer.ticks();
- elapsed
+
+ let now_after_delay = time::precise_time_ns();
+ let dt_after_delay = (now_after_delay - self.last_ticks) as u32;
+
+ self.last_ticks = now_after_delay;
+
+ dt_after_delay as f64 / 1_000_000_000.0
}
- fn append_workload(&mut self, now: u32, dt: u32) {
- let work_load = dt as f64 / self.frame_dur_millis as f64;
+ fn append_workload(&mut self, now: u64, dt: u32) {
+ let work_load = dt as f64 / self.frame_dur_nanos as f64;
self.work_load_sum += work_load;
self.work_load_max = self.work_load_max.max(work_load);
self.work_load_frames += 1;
diff --git a/gate/src/core/sdl/event_handler.rs b/gate/src/core/sdl/event_handler.rs
index 6664f24..9bf27a8 100644
--- a/gate/src/core/sdl/event_handler.rs
+++ b/gate/src/core/sdl/event_handler.rs
@@ -122,7 +122,7 @@ fn sdl_to_gate_key(sdl: SdlKeyCode) -> Option<KeyCode> {
SdlKeyCode::Left => Some(KeyCode::Left),
SdlKeyCode::Down => Some(KeyCode::Down),
SdlKeyCode::Up => Some(KeyCode::Up),
- SdlKeyCode::Return => Some(KeyCode::Return),
+ SdlKeyCode::Return | SdlKeyCode::Return2 | SdlKeyCode::KpEnter => Some(KeyCode::Return),
SdlKeyCode::Space => Some(KeyCode::Space),
SdlKeyCode::Backspace => Some(KeyCode::Backspace),
SdlKeyCode::Delete => Some(KeyCode::Delete),
diff --git a/gate/src/core/sdl/mod.rs b/gate/src/core/sdl/mod.rs
index 9129477..767eb38 100644
--- a/gate/src/core/sdl/mod.rs
+++ b/gate/src/core/sdl/mod.rs
@@ -64,7 +64,6 @@ pub fn run<AS: AppAssetId, AP: App<AS>>(info: AppInfo, mut app: AP) {
mixer_setup();
gl_hints(video.gl_attr());
- let timer = sdl_context.timer().unwrap();
let mut event_handler = EventHandler::new(sdl_context.event_pump().unwrap());
let window = video.window(info.title, info.window_pixels.0, info.window_pixels.1)
@@ -87,7 +86,7 @@ pub fn run<AS: AppAssetId, AP: App<AS>>(info: AppInfo, mut app: AP) {
app.start(&mut ctx);
- let mut clock = AppClock::new(timer, &info);
+ let mut clock = AppClock::new(&info);
loop {
unsafe {
diff --git a/gate/src/lib.rs b/gate/src/lib.rs
index 96cd463..f0188b1 100644
--- a/gate/src/lib.rs
+++ b/gate/src/lib.rs
@@ -53,6 +53,9 @@
#[cfg(not(target_arch = "wasm32"))] extern crate sdl2;
#[cfg(not(target_arch = "wasm32"))] extern crate gl;
+#[cfg(not(target_arch = "wasm32"))] extern crate time;
+
+#[macro_use] extern crate lazy_static;
extern crate byteorder;
pub mod asset_id;