Commit | Line | Data |
---|---|---|
cdf8f998 TW |
1 | use std::collections::HashMap; |
2 | ||
3 | use sdl2::image::LoadTexture; | |
4 | use sdl2::render::Texture; | |
5 | use sdl2::render::TextureCreator; | |
6 | use sdl2::video::WindowContext; | |
7 | ||
8 | pub struct SpriteManager<'canvas> { | |
9 | texture_creator: &'canvas TextureCreator<WindowContext>, // can't make the lifetimes work when this is owned instead of borrowed | |
10 | textures: HashMap<&'static str, Texture<'canvas>>, | |
11 | } | |
12 | ||
13 | impl<'canvas> SpriteManager<'canvas> { | |
14 | pub fn new(texture_creator: &'canvas TextureCreator<WindowContext>) -> SpriteManager<'canvas> { | |
15 | SpriteManager { | |
16 | texture_creator, | |
17 | textures: HashMap::new(), | |
18 | } | |
19 | } | |
20 | ||
21 | pub fn load(&mut self, name: &'static str, file: &str) { | |
22 | self.textures.insert(name, self.texture_creator.load_texture(file).unwrap()); | |
23 | } | |
24 | ||
25 | pub fn get(&self, name: &str) -> &Texture { | |
26 | self.textures.get(name).expect(format!("The sprite '{}' was not found", name).as_str()) | |
27 | } | |
28 | } |