From 0252961d6b04bb0b775a7409f761808f29d3a44c Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 23 Mar 2026 17:22:28 +0800 Subject: [PATCH] add a Dynamic trait wrapper around the FrameBuffer type this allows us to pass it through APIs that need dynamic traits. also fix typo on 0,0 location --- libs/ux-api/src/minigfx/mod.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/libs/ux-api/src/minigfx/mod.rs b/libs/ux-api/src/minigfx/mod.rs index e742b58f0..ef4e60604 100644 --- a/libs/ux-api/src/minigfx/mod.rs +++ b/libs/ux-api/src/minigfx/mod.rs @@ -36,7 +36,7 @@ use crate::platform; /// Abstract trait for a FrameBuffer. Slower than native manipulation /// of the [u8] contents of a frame buffer, but more portable. pub trait FrameBuffer { - /// Puts a pixel of ColorNative at x, y. (0, 0) is defined as the lower left corner. + /// Puts a pixel of ColorNative at x, y. (0, 0) is defined as the top left corner. fn put_pixel(&mut self, p: Point, color: ColorNative); /// For higher-performance loops that have guaranteed bounds checking in the point location. unsafe fn put_pixel_unchecked(&mut self, p: Point, on: ColorNative); @@ -56,6 +56,27 @@ pub trait FrameBuffer { unsafe fn raw_mut(&mut self) -> &mut platform::FbRaw; } +// Newtype wrapping the FrameBuffer, to allow for sized passing of the object around +pub struct DynFb<'a>(pub &'a mut dyn FrameBuffer); + +impl FrameBuffer for DynFb<'_> { + fn put_pixel(&mut self, p: Point, c: ColorNative) { self.0.put_pixel(p, c) } + + fn xor_pixel(&mut self, p: Point) { self.0.xor_pixel(p) } + + unsafe fn put_pixel_unchecked(&mut self, p: Point, on: ColorNative) { self.0.put_pixel_unchecked(p, on); } + + fn get_pixel(&self, p: Point) -> Option { self.0.get_pixel(p) } + + fn draw(&mut self) { self.0.draw(); } + + fn clear(&mut self) { self.0.clear(); } + + fn dimensions(&self) -> Point { self.0.dimensions() } + + unsafe fn raw_mut(&mut self) -> &mut platform::FbRaw { self.0.raw_mut() } +} + /// A TypesetWord is a Word that has beet turned into sprites and placed at a specific location on the canvas, /// defined by its `bb` record. The intention is that this abstract representation can be passed directly to /// a rasterizer for rendering.