From bd637547734228fa18113c26eee30f13e32c8cd2 Mon Sep 17 00:00:00 2001 From: Avii Date: Thu, 6 Aug 2026 10:56:07 +0200 Subject: [PATCH] 1786006567 --- src/main.rs | 6 +++--- src/trekstor.rs | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6ee1667..1982d12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,10 @@ use crate::trekstor::Trekstor; fn main() { let mut trekstor = Trekstor::new(60); - trekstor.update(|screen, input| { + trekstor.update(|ctx| { // retuyrningf false will exit loop - if input.is_just_released(KeyCode::BTN_TOUCH) { - println!("Released {}x{}", input.x, input.y); + if ctx.input.is_just_released(KeyCode::BTN_TOUCH) { + println!("Released {}x{}", ctx.input.x, ctx.input.y); } true }); diff --git a/src/trekstor.rs b/src/trekstor.rs index 8b70f2d..2849241 100644 --- a/src/trekstor.rs +++ b/src/trekstor.rs @@ -4,15 +4,18 @@ use std::time::{Duration, Instant}; use self::input::InputState; use self::screen::Screen; -type UpdateFn = fn(&mut Screen, &InputState) -> bool; +type UpdateFn = fn(&mut Context) -> bool; mod input; mod screen; -pub struct Trekstor { +pub struct Context { pub screen: Screen, pub input: InputState, - accumulator: Duration, +} + +pub struct Trekstor { + ctx: Context, last_time: Instant, current_time: Instant, update_timestep: Duration, @@ -24,9 +27,7 @@ impl Trekstor { let input = InputState::new(&screen); Self { - screen, - input, - accumulator: Duration::default(), + ctx: Context { screen, input }, current_time: Instant::now(), last_time: Instant::now(), update_timestep: Duration::from_nanos( @@ -37,7 +38,7 @@ impl Trekstor { pub fn update(&mut self, update: UpdateFn) { loop { - self.input.handle_event(); + self.ctx.input.handle_event(); self.current_time = Instant::now(); let dt = self.current_time - self.last_time; self.last_time = self.current_time; @@ -46,14 +47,12 @@ impl Trekstor { sleep(self.update_timestep - dt); } - // println!("FPS: {}", 1.0 / dt.as_secs_f64()); - - if !update(&mut self.screen, &self.input) { + if !update(&mut self.ctx) { return; } - self.screen.flush(); - self.input.finish(); + self.ctx.screen.flush(); + self.ctx.input.finish(); } } }