1786006567
This commit is contained in:
@@ -8,10 +8,10 @@ use crate::trekstor::Trekstor;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut trekstor = Trekstor::new(60);
|
let mut trekstor = Trekstor::new(60);
|
||||||
|
|
||||||
trekstor.update(|screen, input| {
|
trekstor.update(|ctx| {
|
||||||
// retuyrningf false will exit loop
|
// retuyrningf false will exit loop
|
||||||
if input.is_just_released(KeyCode::BTN_TOUCH) {
|
if ctx.input.is_just_released(KeyCode::BTN_TOUCH) {
|
||||||
println!("Released {}x{}", input.x, input.y);
|
println!("Released {}x{}", ctx.input.x, ctx.input.y);
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,15 +4,18 @@ use std::time::{Duration, Instant};
|
|||||||
use self::input::InputState;
|
use self::input::InputState;
|
||||||
use self::screen::Screen;
|
use self::screen::Screen;
|
||||||
|
|
||||||
type UpdateFn = fn(&mut Screen, &InputState) -> bool;
|
type UpdateFn = fn(&mut Context) -> bool;
|
||||||
|
|
||||||
mod input;
|
mod input;
|
||||||
mod screen;
|
mod screen;
|
||||||
|
|
||||||
pub struct Trekstor {
|
pub struct Context {
|
||||||
pub screen: Screen,
|
pub screen: Screen,
|
||||||
pub input: InputState,
|
pub input: InputState,
|
||||||
accumulator: Duration,
|
}
|
||||||
|
|
||||||
|
pub struct Trekstor {
|
||||||
|
ctx: Context,
|
||||||
last_time: Instant,
|
last_time: Instant,
|
||||||
current_time: Instant,
|
current_time: Instant,
|
||||||
update_timestep: Duration,
|
update_timestep: Duration,
|
||||||
@@ -24,9 +27,7 @@ impl Trekstor {
|
|||||||
let input = InputState::new(&screen);
|
let input = InputState::new(&screen);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
screen,
|
ctx: Context { screen, input },
|
||||||
input,
|
|
||||||
accumulator: Duration::default(),
|
|
||||||
current_time: Instant::now(),
|
current_time: Instant::now(),
|
||||||
last_time: Instant::now(),
|
last_time: Instant::now(),
|
||||||
update_timestep: Duration::from_nanos(
|
update_timestep: Duration::from_nanos(
|
||||||
@@ -37,7 +38,7 @@ impl Trekstor {
|
|||||||
|
|
||||||
pub fn update(&mut self, update: UpdateFn) {
|
pub fn update(&mut self, update: UpdateFn) {
|
||||||
loop {
|
loop {
|
||||||
self.input.handle_event();
|
self.ctx.input.handle_event();
|
||||||
self.current_time = Instant::now();
|
self.current_time = Instant::now();
|
||||||
let dt = self.current_time - self.last_time;
|
let dt = self.current_time - self.last_time;
|
||||||
self.last_time = self.current_time;
|
self.last_time = self.current_time;
|
||||||
@@ -46,14 +47,12 @@ impl Trekstor {
|
|||||||
sleep(self.update_timestep - dt);
|
sleep(self.update_timestep - dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// println!("FPS: {}", 1.0 / dt.as_secs_f64());
|
if !update(&mut self.ctx) {
|
||||||
|
|
||||||
if !update(&mut self.screen, &self.input) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.screen.flush();
|
self.ctx.screen.flush();
|
||||||
self.input.finish();
|
self.ctx.input.finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user