engine structure
This commit is contained in:
+2
-4
@@ -8,12 +8,10 @@ fn main() {
|
||||
engine.thread("Main.tick", || {});
|
||||
|
||||
let my_rect = engine.entity(|r| {
|
||||
r.draw_rect(8, 3, 11, 5, 0x0000ff);
|
||||
r.draw_rect(0, 0, 2, 1, 0x0000ff);
|
||||
});
|
||||
|
||||
terminal::clear().unwrap();
|
||||
|
||||
my_rect.render(&mut osmium::renderer::Renderer {});
|
||||
engine.start();
|
||||
|
||||
terminal::show_cursor().unwrap();
|
||||
}
|
||||
|
||||
@@ -29,4 +29,10 @@ impl Entity {
|
||||
pub fn render(&mut self, renderer: &mut Renderer) {
|
||||
(self.render)(renderer)
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
for comp in &mut self.components {
|
||||
// comp.tick(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+32
@@ -18,6 +18,10 @@ pub struct Engine {
|
||||
entities: Vec<entity::Entity>,
|
||||
}
|
||||
|
||||
pub fn sleep(ms: u64) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(ms));
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn thread<F: FnMut() + Send + Sync + 'static>(&mut self, label: &str, f: F) {
|
||||
self.threads.insert(label.to_string(), Box::new(f));
|
||||
@@ -30,4 +34,32 @@ impl Engine {
|
||||
.last_mut()
|
||||
.expect("Unable to obtain entity (unspawned)")
|
||||
}
|
||||
|
||||
pub fn render(&mut self) {
|
||||
terminal::clear().unwrap();
|
||||
|
||||
let mut renderer = renderer::Renderer::new();
|
||||
for entity in &mut self.entities {
|
||||
entity.render(&mut renderer);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
terminal::clear().unwrap();
|
||||
|
||||
for entity in &mut self.entities {
|
||||
entity.tick();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(&mut self) {
|
||||
for thr in &mut self.threads.values() {
|
||||
// std::thread::spawn(thr);
|
||||
}
|
||||
|
||||
loop {
|
||||
self.render();
|
||||
sleep(16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use crate::terminal::{hex_bg, print_liner};
|
||||
pub struct Renderer {}
|
||||
|
||||
impl Renderer {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn draw_rect(&self, x: u16, y: u16, width: u16, height: u16, color: u32) {
|
||||
let rl = " ".repeat(width as usize) + "\x1b[0m";
|
||||
let rect = (rl + "\n").repeat(height as usize);
|
||||
|
||||
Reference in New Issue
Block a user