From 7096849b65faf503792d0d1db057d080a41192a1 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 26 Nov 2025 20:55:44 +0100 Subject: [PATCH] Improved a few things (i don't remember them) --- examples/components/mod.rs | 31 ++++++++++++++++++++++ examples/components/threads.rs | 28 ++++++++++++++++++++ examples/cube.rs | 47 +++------------------------------- src/renderer.rs | 6 ++--- 4 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 examples/components/mod.rs create mode 100644 examples/components/threads.rs diff --git a/examples/components/mod.rs b/examples/components/mod.rs new file mode 100644 index 0000000..74b9827 --- /dev/null +++ b/examples/components/mod.rs @@ -0,0 +1,31 @@ +pub mod threads; + +use osmium::{entity::Component, renderer::Point}; + +pub struct Velocity(pub Point); + +impl Component for Velocity { + fn tick(&mut self, entity: &std::sync::Arc, tick: u16) { + let mut position = entity.position(); + apply_velocity(tick, self.0.x, &mut position.x); + apply_velocity(tick, self.0.y, &mut position.y); + } +} + +fn apply_velocity(tick: u16, velocity: i32, x: &mut u16) -> bool { + let abs_v = velocity.abs(); + if abs_v == 0 { + return false; + } + + let interval = (1000 / abs_v).max(1); + if tick as i32 % interval == 0 { + if velocity > 0 { + *x = x.saturating_add(1); + } else { + *x = x.saturating_sub(1); + } + return true; + } + false +} diff --git a/examples/components/threads.rs b/examples/components/threads.rs new file mode 100644 index 0000000..10a5312 --- /dev/null +++ b/examples/components/threads.rs @@ -0,0 +1,28 @@ +use std::sync::Arc; + +use osmium::{Engine, sleep}; + +pub fn tick(engine: &Arc) { + engine.thread("Main.tick", |engine| { + let mut tick = 0; + loop { + if tick == 60000 { + tick = 0; + } + + engine.tick(tick); + + tick += 1; + sleep(4); + } + }); +} + +pub fn key(engine: &Arc) { + engine.thread("Main.key", |engine| { + loop { + // Pass an event to the thread + + } + }); +} diff --git a/examples/cube.rs b/examples/cube.rs index 80d8e74..c13362b 100644 --- a/examples/cube.rs +++ b/examples/cube.rs @@ -1,60 +1,21 @@ -use osmium::{entity::Component, sleep, terminal}; +pub mod components; + +use osmium::{renderer::Point, terminal}; fn main() { terminal::hide_cursor().unwrap(); let engine = osmium::init(); - engine.thread("Main.tick", |engine| { - let mut tick = 0; - loop { - if tick == 60000 { - tick = 0; - } - - engine.tick(tick); - - tick += 1; - sleep(4); - } - }); - let my_rect = engine.entity(|r| { r.draw_rect(0, 0, 1, 2, 0xff0000); r.draw_rect(1, 0, 1, 2, 0x00ff00); r.draw_rect(2, 0, 1, 2, 0x0000ff); }); - my_rect.component(Velocity(300)); + my_rect.component(components::Velocity(Point { x: 0, y: 0 })); engine.start(); terminal::show_cursor().unwrap(); } - -pub struct Velocity(pub i32); - -impl Component for Velocity { - fn tick(&mut self, entity: &std::sync::Arc, tick: u16) { - let mut position = entity.position(); - apply_velocity(tick, self.0, &mut position.x); - } -} - -fn apply_velocity(tick: u16, velocity: i32, x: &mut u16) -> bool { - let abs_v = velocity.abs(); - if abs_v == 0 { - return false; - } - - let interval = (1000 / abs_v).max(1); - if tick as i32 % interval == 0 { - if velocity > 0 { - *x = x.saturating_add(1); - } else { - *x = x.saturating_sub(1); - } - return true; - } - false -} diff --git a/src/renderer.rs b/src/renderer.rs index 6f237d6..f485a98 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,9 +1,9 @@ use crate::terminal::{hex_bg, print_liner}; #[derive(Debug, Clone)] -pub struct Point { - pub x: u16, - pub y: u16, +pub struct Point { + pub x: I, + pub y: I, } pub struct Renderer {