Vsync instead of fps + print display modes
[kaka/rust-sdl-test.git] / src / sprites.rs
CommitLineData
cdf8f998
TW
1use std::collections::HashMap;
2
3use sdl2::image::LoadTexture;
4use sdl2::render::Texture;
5use sdl2::render::TextureCreator;
6use sdl2::video::WindowContext;
7
fcbc5786
TW
8pub struct SpriteManager {
9 texture_creator: TextureCreator<WindowContext>, // can't make the lifetimes work when this is owned instead of borrowed
1e322944 10 textures: HashMap<String, Texture>,
cdf8f998
TW
11}
12
fcbc5786
TW
13impl SpriteManager {
14 pub fn new(texture_creator: TextureCreator<WindowContext>) -> SpriteManager {
cdf8f998
TW
15 SpriteManager {
16 texture_creator,
17 textures: HashMap::new(),
18 }
19 }
20
1e322944
TW
21 pub fn load(&mut self, name: &str, file: &str) {
22 self.textures.insert(name.to_string(), self.texture_creator.load_texture(file).unwrap());
cdf8f998
TW
23 }
24
25 pub fn get(&self, name: &str) -> &Texture {
55eb2b7d 26 self.textures.get(name).unwrap_or_else(|| panic!("The sprite '{}' was not found", name))
cdf8f998
TW
27 }
28}