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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use sdl2::image::LoadTexture;
use sdl2::pixels::Color;
use sdl2::render::{Canvas, Texture, TextureQuery};
use sdl2::ttf::{FontStyle, Sdl2TtfContext};
use sdl2::video::Window;
use std::collections::HashMap;
use std::path::Path;
pub struct TextureCache {
images: HashMap<String, Texture>,
ttf_context: Sdl2TtfContext,
}
impl TextureCache {
pub fn new() -> Self {
Self {
images: HashMap::new(),
ttf_context: sdl2::ttf::init().map_err(|e| e.to_string()).unwrap(),
}
}
pub fn get_ttf_context(&self) -> &Sdl2TtfContext {
&self.ttf_context
}
pub fn get_image(&mut self, c: &mut Canvas<Window>, image_name: String) -> &Texture {
self.images.entry(image_name.clone()).or_insert({
c.texture_creator()
.load_texture(Path::new(&image_name))
.unwrap()
})
}
pub fn render_text(
&mut self,
c: &mut Canvas<Window>,
font_name: String,
font_size: u16,
font_style: FontStyle,
font_string: String,
font_color: Color,
width: u32,
) -> (Texture, u32, u32) {
let ttf_context = self.get_ttf_context();
let texture_creator = c.texture_creator();
let mut font = ttf_context
.load_font(Path::new(&font_name), font_size as u16)
.unwrap();
font.set_style(font_style);
let surface = font
.render(&font_string)
.blended_wrapped(font_color, width)
.map_err(|e| e.to_string())
.unwrap();
let font_texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())
.unwrap();
let TextureQuery { width, height, .. } = font_texture.query();
(font_texture, width, height)
}
}
impl Default for TextureCache {
fn default() -> Self {
Self::new()
}
}