Make TextureManager use a HashMap
This commit is contained in:
parent
ce3efce2d9
commit
0091bf51ee
2 changed files with 39 additions and 37 deletions
12
src/main.rs
12
src/main.rs
|
@ -53,13 +53,10 @@ fn update(event_pump: &mut EventPump, game_state: &mut GameState) -> bool {
|
|||
Event::Quit { .. } => return false,
|
||||
|
||||
Event::KeyDown {
|
||||
keycode: Some(keycode),
|
||||
keycode: Some(Keycode::Escape),
|
||||
..
|
||||
} => {
|
||||
match keycode {
|
||||
Keycode::Escape => return false,
|
||||
_ => {}
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
Event::MouseButtonDown {
|
||||
|
@ -125,7 +122,6 @@ fn generate_map() -> Vec<Vec<Tile>> {
|
|||
for (y, row) in map.iter_mut().enumerate() {
|
||||
for (x, tile) in row.iter_mut().enumerate() {
|
||||
let height = perlin.get2d([x as f64 / 16.0, y as f64 / 16.0]) * 320.0;
|
||||
println!("({}, {}) = {}", x, y, height);
|
||||
tile.height = height as i32;
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +151,9 @@ pub fn main() {
|
|||
let texture_creator = canvas.texture_creator();
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
|
||||
let texture_manager = TextureManager::new(&texture_creator);
|
||||
let mut texture_manager = TextureManager::new();
|
||||
texture_manager.load_textures(&texture_creator);
|
||||
|
||||
let map = generate_map();
|
||||
let mut game_state = GameState::new();
|
||||
|
||||
|
|
|
@ -1,50 +1,54 @@
|
|||
use sdl2::image::LoadTexture;
|
||||
use sdl2::render::{Texture, TextureCreator};
|
||||
use sdl2::video::WindowContext;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Index;
|
||||
|
||||
pub struct TextureManager<'a> {
|
||||
bridge_east: Texture<'a>,
|
||||
bridge_north: Texture<'a>,
|
||||
crossroad: Texture<'a>,
|
||||
dirt: Texture<'a>,
|
||||
dirt_high: Texture<'a>,
|
||||
grass: Texture<'a>,
|
||||
textures: HashMap<&'a str, Texture<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> TextureManager<'a> {
|
||||
pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> TextureManager<'a> {
|
||||
TextureManager {
|
||||
bridge_east: texture_creator
|
||||
.load_texture("data/png/bridgeEast.png")
|
||||
.unwrap(),
|
||||
bridge_north: texture_creator
|
||||
.load_texture("data/png/bridgeNorth.png")
|
||||
.unwrap(),
|
||||
crossroad: texture_creator
|
||||
.load_texture("data/png/crossroad.png")
|
||||
.unwrap(),
|
||||
dirt: texture_creator.load_texture("data/png/dirt.png").unwrap(),
|
||||
dirt_high: texture_creator
|
||||
.load_texture("data/png/dirtHigh.png")
|
||||
.unwrap(),
|
||||
grass: texture_creator.load_texture("data/png/grass.png").unwrap(),
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
textures: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_texture(
|
||||
&mut self,
|
||||
texture_creator: &'a TextureCreator<WindowContext>,
|
||||
name: &'a str,
|
||||
path: &str,
|
||||
) {
|
||||
let texture = texture_creator.load_texture(path).unwrap();
|
||||
self.textures.insert(name, texture);
|
||||
}
|
||||
|
||||
pub fn load_textures(&mut self, texture_creator: &'a TextureCreator<WindowContext>) {
|
||||
self.load_texture(texture_creator, "bridge_east", "data/png/bridgeEast.png");
|
||||
self.load_texture(texture_creator, "bridge_north", "data/png/bridgeNorth.png");
|
||||
self.load_texture(texture_creator, "crossroad", "data/png/crossroad.png");
|
||||
self.load_texture(texture_creator, "dirt", "data/png/dirt.png");
|
||||
self.load_texture(texture_creator, "dirt_high", "data/png/dirtHigh.png");
|
||||
self.load_texture(texture_creator, "grass", "data/png/grass.png");
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Index<usize> for TextureManager<'a> {
|
||||
type Output = Texture<'a>;
|
||||
|
||||
fn index(&self, index: usize) -> &Texture<'a> {
|
||||
match index {
|
||||
1 => &self.bridge_east,
|
||||
2 => &self.bridge_north,
|
||||
3 => &self.crossroad,
|
||||
4 => &self.dirt,
|
||||
5 => &self.dirt_high,
|
||||
6 => &self.grass,
|
||||
let key = match index {
|
||||
1 => "bridge_east",
|
||||
2 => "bridge_north",
|
||||
3 => "crossroad",
|
||||
4 => "dirt",
|
||||
5 => "dirt_high",
|
||||
6 => "grass",
|
||||
_ => panic!("invalid texture ID"),
|
||||
}
|
||||
};
|
||||
|
||||
&self.textures[key]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue