aboutsummaryrefslogtreecommitdiff
path: root/src/text.rs
blob: a27ca497f87046393cba693e3c80e982a6128c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use sdl2::ttf;
use sdl2::render::{Texture, TextureCreator};
use sdl2::pixels::Color;

const WHITE: Color = Color::RGB(255, 255, 255);

pub struct Factory<'a, T> {
    font: &'a ttf::Font<'a, 'a>,
    creator: &'a TextureCreator<T>,
    bg: Color,
}

impl<'a, T> Factory<'a, T> {
    pub fn new(font: &'a ttf::Font,
            creator: &'a TextureCreator<T>,
            bg: Color
              ) -> Factory<'a, T>
    {
        Factory { font, creator, bg }
    }

    pub fn from_string(&self, s: &String) -> Texture
    {
        let surface = self.font.render(s).shaded(WHITE, self.bg).unwrap();
        self.creator.create_texture_from_surface(&surface).unwrap()
    }

    pub fn set_bg(&mut self, bg: Color) {
        self.bg = bg;
    }
}


// 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()
//     }
// }