From 979af999e42a8e1b92ebd5ba6c00a5f8030e1381 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 14:17:05 +0100 Subject: [PATCH] Simple renderer --- examples/hello_world.rs | 2 +- src/engine.rs | 28 ++++++++++++++++------------ src/render.rs | 38 ++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 2535b11..7e30dd4 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -34,6 +34,6 @@ fn app(cx: &Arc) -> View { }); Arc::new(move |ctx| { - println!("Count: {count}, size: {:?}", ctx.available); + ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}")); }) } diff --git a/src/engine.rs b/src/engine.rs index 22dfc45..7f4f5e4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,8 +3,8 @@ use std::sync::{Arc, Mutex}; use crate::{prelude::Context, DrawContext, View}; pub trait Engine { - fn render(&self, ctx: &mut DrawContext, view: View); - fn draw_text(&self, ctx: &mut DrawContext, text: &str); + fn render_view(&self, ctx: &DrawContext, view: View); + fn draw_context(&self, ctx: &DrawContext); } pub struct Console { @@ -35,24 +35,28 @@ impl Console { }); } - let (width, height) = crossterm::terminal::size().unwrap(); - loop { - cx.get_view()(&mut DrawContext::new(width, height)); + let (width, height) = crossterm::terminal::size().unwrap(); + let mut ctx = DrawContext::new(width, height); + self.render_view(&mut ctx, cx.get_view()); std::thread::sleep(std::time::Duration::from_millis(16)); } } } impl Engine for Console { - fn render(&self, ctx: &mut DrawContext, view: View) { - view(&mut DrawContext::new( - ctx.available.width, - ctx.available.height, - )); + fn render_view(&self, ctx: &DrawContext, view: View) { + let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height); + view(&mut context); + self.draw_context(&context); } - fn draw_text(&self, _ctx: &mut DrawContext, text: &str) { - println!("{}", text); + fn draw_context(&self, ctx: &DrawContext) { + for inst in &ctx.drawing { + match inst { + crate::render::DrawInstruction::Text(point, text) => println!("{text}"), + crate::render::DrawInstruction::Child(point, child) => self.draw_context(child), + } + } } } diff --git a/src/render.rs b/src/render.rs index f08e96d..01c5353 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,6 +1,10 @@ -use std::{fmt::Debug, sync::Arc}; +use std::fmt::Debug; -pub trait DrawInstruction: Debug {} +#[derive(Debug, Clone)] +pub enum DrawInstruction { + Text(Point, String), + Child(Point, DrawContext), +} #[derive(Debug, Clone)] pub struct Size { @@ -9,33 +13,43 @@ pub struct Size { } #[derive(Debug, Clone)] -pub struct DrawContext { - pub available: Size, - pub used: Size, - pub drawing: Vec>, +pub struct Point { + pub x: u16, + pub y: u16, } #[derive(Debug, Clone)] -pub struct RenderContext {} +pub struct DrawContext { + pub available: Size, + pub allocated: Size, + pub parent: Point, + pub drawing: Vec, +} impl DrawContext { pub fn new(width: u16, height: u16) -> Self { Self { available: Size { width, height }, - used: Size { + allocated: Size { width: 0, height: 0, }, + parent: Point { x: 0, y: 0 }, drawing: Vec::new(), } } pub fn allocate(&mut self, width: u16, height: u16) { - self.used.width += width; - self.used.height += height; + self.allocated.width += width; + self.allocated.height += height; } - pub fn draw(&mut self, inst: I) { - self.drawing.push(Arc::new(inst)); + pub fn draw(&mut self, inst: DrawInstruction) { + self.drawing.push(inst); + } + + pub fn draw_text(&mut self, point: Point, text: &str) { + self.drawing + .push(DrawInstruction::Text(point, text.to_string())); } }