| 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 { |
| 9 | texture_creator: TextureCreator<WindowContext>, // can't make the lifetimes work when this is owned instead of borrowed |
| 10 | textures: HashMap<String, Texture>, |
| 11 | } |
| 12 | |
| 13 | impl SpriteManager { |
| 14 | pub fn new(texture_creator: TextureCreator<WindowContext>) -> SpriteManager { |
| 15 | SpriteManager { |
| 16 | texture_creator, |
| 17 | textures: HashMap::new(), |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub fn load(&mut self, name: &str, file: &str) { |
| 22 | self.textures.insert( |
| 23 | name.to_string(), |
| 24 | self.texture_creator.load_texture(file).unwrap(), |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | pub fn get(&self, name: &str) -> &Texture { |
| 29 | self.textures |
| 30 | .get(name) |
| 31 | .unwrap_or_else(|| panic!("The sprite '{}' was not found", name)) |
| 32 | } |
| 33 | } |