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| {
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};
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),
}
}
}
}
+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)]
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<Arc<dyn DrawInstruction>>,
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<DrawInstruction>,
}
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<I: DrawInstruction + 'static>(&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()));
}
}