1786006835
This commit is contained in:
27
src/main.rs
27
src/main.rs
@@ -1,18 +1,39 @@
|
||||
mod rgba;
|
||||
mod trekstor;
|
||||
|
||||
use embedded_graphics::{prelude::*, primitives::*};
|
||||
use evdev::KeyCode;
|
||||
|
||||
use crate::trekstor::Trekstor;
|
||||
|
||||
fn main() {
|
||||
let mut trekstor = Trekstor::new(60);
|
||||
struct AppState {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
trekstor.update(|ctx| {
|
||||
fn main() {
|
||||
let app_state = AppState { x: 0, y: 0 };
|
||||
|
||||
let mut trekstor = Trekstor::new(60, app_state);
|
||||
|
||||
trekstor.update(|ctx, state| {
|
||||
// retuyrningf false will exit loop
|
||||
if ctx.input.is_just_released(KeyCode::BTN_TOUCH) {
|
||||
println!("Released {}x{}", ctx.input.x, ctx.input.y);
|
||||
state.x = ctx.input.x;
|
||||
state.y = ctx.input.y;
|
||||
}
|
||||
|
||||
ctx.screen.clear((0, 0, 0, 255).into()).ok();
|
||||
|
||||
Rectangle::new(
|
||||
Point::new(state.x, state.y),
|
||||
Size::new(ctx.screen.width(), 1),
|
||||
)
|
||||
.into_styled(PrimitiveStyle::with_fill((255, 0, 0, 255).into()))
|
||||
.draw(&mut ctx.screen)
|
||||
.ok();
|
||||
|
||||
true
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
|
||||
use self::input::InputState;
|
||||
use self::screen::Screen;
|
||||
|
||||
type UpdateFn = fn(&mut Context) -> bool;
|
||||
type UpdateFn<S> = fn(&mut Context, &mut S) -> bool;
|
||||
|
||||
mod input;
|
||||
mod screen;
|
||||
@@ -14,19 +14,21 @@ pub struct Context {
|
||||
pub input: InputState,
|
||||
}
|
||||
|
||||
pub struct Trekstor {
|
||||
pub struct Trekstor<S> {
|
||||
state: S,
|
||||
ctx: Context,
|
||||
last_time: Instant,
|
||||
current_time: Instant,
|
||||
update_timestep: Duration,
|
||||
}
|
||||
|
||||
impl Trekstor {
|
||||
pub fn new(update_fps: i32) -> Self {
|
||||
impl<S> Trekstor<S> {
|
||||
pub fn new(update_fps: i32, state: S) -> Self {
|
||||
let screen = Screen::new();
|
||||
let input = InputState::new(&screen);
|
||||
|
||||
Self {
|
||||
state,
|
||||
ctx: Context { screen, input },
|
||||
current_time: Instant::now(),
|
||||
last_time: Instant::now(),
|
||||
@@ -36,7 +38,7 @@ impl Trekstor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, update: UpdateFn) {
|
||||
pub fn update(&mut self, update: UpdateFn<S>) {
|
||||
loop {
|
||||
self.ctx.input.handle_event();
|
||||
self.current_time = Instant::now();
|
||||
@@ -47,7 +49,7 @@ impl Trekstor {
|
||||
sleep(self.update_timestep - dt);
|
||||
}
|
||||
|
||||
if !update(&mut self.ctx) {
|
||||
if !update(&mut self.ctx, &mut self.state) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user