diff options
Diffstat (limited to 'src/text.rs')
| -rw-r--r-- | src/text.rs | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/src/text.rs b/src/text.rs index a27ca49..e62db3d 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,6 +1,7 @@ use sdl2::ttf; -use sdl2::render::{Texture, TextureCreator}; +use sdl2::render::{Canvas, Texture, TextureCreator}; use sdl2::pixels::Color; +use sdl2::rect::Rect; const WHITE: Color = Color::RGB(255, 255, 255); @@ -8,18 +9,20 @@ pub struct Factory<'a, T> { font: &'a ttf::Font<'a, 'a>, creator: &'a TextureCreator<T>, bg: Color, + font_size: u32, } impl<'a, T> Factory<'a, T> { pub fn new(font: &'a ttf::Font, - creator: &'a TextureCreator<T>, - bg: Color + creator: &'a TextureCreator<T>, + bg: Color, + font_size: u32 ) -> Factory<'a, T> { - Factory { font, creator, bg } + Factory { font, creator, bg, font_size } } - pub fn from_string(&self, s: &String) -> Texture + pub fn from_string(&self, s: &str) -> Texture { let surface = self.font.render(s).shaded(WHITE, self.bg).unwrap(); self.creator.create_texture_from_surface(&surface).unwrap() @@ -30,14 +33,32 @@ impl<'a, T> Factory<'a, T> { } } +struct Frame<'a> { + width: u32, + height: u32, + texture: Texture<'a>, +} + +impl<'a> Frame<'a> { + fn new<T>(s: &'a str, factory: &'a Factory<T>) -> Frame<'a> { + Frame{ + width: s.len() as u32 * factory.font_size, + height: factory.font_size, + texture: factory.from_string(s) + } + } -// pub trait TextTexture { -// fn to_texture<T>(&self, factory: &Factory<T>) -> Texture; -// } -// -// impl TextTexture for String { -// fn to_texture<T>(&self, factory: &Factory<T>) -> Texture { -// let surface = factory.font.render(self).shaded(WHITE, factory.bg).unwrap(); -// factory.creator.create_texture_from_surface(&surface).unwrap() -// } -// } + fn to_rect(&self, x: i32, y: i32) -> Rect { + Rect::new(x, y, self.width, self.height) + } +} + +trait Framable { + fn to_frame<'a>(&'a self) -> Frame<'a>; + + fn put_canvas<T: sdl2::render::RenderTarget>(&self, canvas: &mut Canvas<T>, x: i32, y: i32) { + let frame = self.to_frame(); + let rect = frame.to_rect(x, y); + canvas.copy(&frame.texture, None, rect); + } +} |
