Simple renderer

This commit is contained in:
2026-01-24 14:17:05 +01:00
parent 0515087f0d
commit 979af999e4
3 changed files with 43 additions and 25 deletions
+1 -1
View File
@@ -34,6 +34,6 @@ fn app(cx: &Arc<Context>) -> View {
}); });
Arc::new(move |ctx| { Arc::new(move |ctx| {
println!("Count: {count}, size: {:?}", ctx.available); ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}"));
}) })
} }
+16 -12
View File
@@ -3,8 +3,8 @@ use std::sync::{Arc, Mutex};
use crate::{prelude::Context, DrawContext, View}; use crate::{prelude::Context, DrawContext, View};
pub trait Engine { pub trait Engine {
fn render(&self, ctx: &mut DrawContext, view: View); fn render_view(&self, ctx: &DrawContext, view: View);
fn draw_text(&self, ctx: &mut DrawContext, text: &str); fn draw_context(&self, ctx: &DrawContext);
} }
pub struct Console { pub struct Console {
@@ -35,24 +35,28 @@ impl Console {
}); });
} }
let (width, height) = crossterm::terminal::size().unwrap();
loop { 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)); std::thread::sleep(std::time::Duration::from_millis(16));
} }
} }
} }
impl Engine for Console { impl Engine for Console {
fn render(&self, ctx: &mut DrawContext, view: View) { fn render_view(&self, ctx: &DrawContext, view: View) {
view(&mut DrawContext::new( let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height);
ctx.available.width, view(&mut context);
ctx.available.height, self.draw_context(&context);
));
} }
fn draw_text(&self, _ctx: &mut DrawContext, text: &str) { fn draw_context(&self, ctx: &DrawContext) {
println!("{}", text); 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),
}
}
} }
} }
+26 -12
View File
@@ -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)] #[derive(Debug, Clone)]
pub struct Size { pub struct Size {
@@ -9,33 +13,43 @@ pub struct Size {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DrawContext { pub struct Point {
pub available: Size, pub x: u16,
pub used: Size, pub y: u16,
pub drawing: Vec<Arc<dyn DrawInstruction>>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RenderContext {} pub struct DrawContext {
pub available: Size,
pub allocated: Size,
pub parent: Point,
pub drawing: Vec<DrawInstruction>,
}
impl DrawContext { impl DrawContext {
pub fn new(width: u16, height: u16) -> Self { pub fn new(width: u16, height: u16) -> Self {
Self { Self {
available: Size { width, height }, available: Size { width, height },
used: Size { allocated: Size {
width: 0, width: 0,
height: 0, height: 0,
}, },
parent: Point { x: 0, y: 0 },
drawing: Vec::new(), drawing: Vec::new(),
} }
} }
pub fn allocate(&mut self, width: u16, height: u16) { pub fn allocate(&mut self, width: u16, height: u16) {
self.used.width += width; self.allocated.width += width;
self.used.height += height; self.allocated.height += height;
} }
pub fn draw<I: DrawInstruction + 'static>(&mut self, inst: I) { pub fn draw(&mut self, inst: DrawInstruction) {
self.drawing.push(Arc::new(inst)); self.drawing.push(inst);
}
pub fn draw_text(&mut self, point: Point, text: &str) {
self.drawing
.push(DrawInstruction::Text(point, text.to_string()));
} }
} }