From 55b325a257f4d06c5999dfb2103605e8d6989850 Mon Sep 17 00:00:00 2001 From: memdmp Date: Sun, 12 Jan 2025 01:51:48 +0100 Subject: feat: it worky --- src/font.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/font.rs') diff --git a/src/font.rs b/src/font.rs index d82d5a9..056c691 100644 --- a/src/font.rs +++ b/src/font.rs @@ -41,6 +41,42 @@ pub trait BakedFont { } } } +impl RenderableCharacter { + /** Alpha value of colour is currently ignored! */ + pub fn to_surface(&self, colour: Color) -> Surface<'static> { + let mut surface = Surface::new( + self.width.into(), + self.height.into(), + PixelFormatEnum::RGBA32, + ) + .unwrap(); + surface.with_lock_mut(|buffer: &mut [u8]| { + let mut idx: usize = 0; + print!("{} ({}x{})", self.data.len() * 4, self.width, self.height); + for pixel in self.data { + let index = idx * 4; + buffer[index] = colour.r; // Red + buffer[index + 1] = colour.g; // Green + buffer[index + 2] = colour.b; // Blue + buffer[index + 3] = *pixel; // Alpha + idx += 1; + } + }); + surface + } + /** Colour Alpha Channel is ignored */ + pub fn to_texture<'a>( + &self, + texture_creator: &'a TextureCreator, + colour: Color, + ) -> Result, TextureValueError> { + let surface = self.to_surface(colour); + surface.as_texture(texture_creator) + } + pub fn to_rect(&self, x: i32, y: i32) -> Rect { + Rect::new(x, y, self.width.into(), self.height.into()) + } +} pub struct Font<'a> { pub texture: &'a [u8], pub glyphs: Vec<*const Glyph>, @@ -151,4 +187,9 @@ mod sprite_sheets { use std::str::Bytes; +use sdl2::pixels::{Color, PixelFormatEnum}; +use sdl2::rect::Rect; +use sdl2::render::{Canvas, Texture, TextureCreator, TextureValueError}; +use sdl2::surface::Surface; +use sdl2::video::WindowContext; pub use sprite_sheets::UwUFont; -- cgit v1.2.3