aboutsummaryrefslogtreecommitdiffstats
path: root/src/font.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/font.rs')
-rw-r--r--src/font.rs41
1 files changed, 41 insertions, 0 deletions
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<WindowContext>,
+ colour: Color,
+ ) -> Result<Texture<'a>, 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;