1786006567
This commit is contained in:
@@ -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
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user