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