From 0091bf51ee726ba286e2cd9647110d6d7170ba59 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 29 Jul 2018 23:40:43 +0200 Subject: [PATCH] Make TextureManager use a HashMap --- src/main.rs | 12 ++++---- src/texture_manager.rs | 64 ++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index 45055cb..266ade3 100644 --- a/src/main.rs +++ b/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> { 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(); diff --git a/src/texture_manager.rs b/src/texture_manager.rs index 74e7154..a7003ab 100644 --- a/src/texture_manager.rs +++ b/src/texture_manager.rs @@ -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) -> 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, + 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) { + 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 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] } }