From cdf8f998d161abafcff3cd8503c9ec81e9c372f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Tue, 12 Feb 2019 20:30:24 +0100 Subject: [PATCH] Move some sprite logic into a SpriteManager --- src/main.rs | 34 ++++++++++++++++------------------ src/sprites.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 src/sprites.rs diff --git a/src/main.rs b/src/main.rs index d99da89..7216479 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,31 +2,29 @@ extern crate rand; extern crate sdl2; extern crate time; -use std::collections::hash_map::RandomState; -use std::collections::HashMap; use std::f32::consts::PI; use rand::Rng; use sdl2::event::Event; use sdl2::EventPump; use sdl2::gfx::primitives::DrawRenderer; -use sdl2::image::LoadTexture; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::rect::Rect; use sdl2::render::BlendMode; use sdl2::render::Canvas; -use sdl2::render::Texture; -use sdl2::render::TextureCreator; use sdl2::video::Window; -use sdl2::video::WindowContext; use time::PreciseTime; -use boll::{Boll, SquareBoll, CircleBoll}; +use boll::{Boll, CircleBoll, SquareBoll}; use common::Point2D; +use sprites::SpriteManager; +use sdl2::video::WindowContext; +use sdl2::render::TextureCreator; #[macro_use] mod common; mod boll; +mod sprites; const SCREEN_WIDTH: u32 = 1280; const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32; @@ -54,11 +52,11 @@ fn init() -> (Canvas, EventPump) { (canvas, event_pump) } -fn load_textures(texture_creator: &TextureCreator) -> HashMap<&str, Texture, RandomState> { - let mut textures = HashMap::new(); - textures.insert("block", texture_creator.load_texture("res/block.bmp").unwrap()); - textures.insert("mario", texture_creator.load_texture("res/mario-trans.png").unwrap()); - textures +fn load_sprites(texture_creator: &TextureCreator) -> SpriteManager { + let mut texman = SpriteManager::new(texture_creator); + texman.load("block", "res/block.bmp"); + texman.load("mario", "res/mario-trans.png"); + texman } fn main() { @@ -72,7 +70,7 @@ fn main() { let mut boll_size = 1; let texture_creator = canvas.texture_creator(); - let textures = load_textures(&texture_creator); + let sprites = load_sprites(&texture_creator); let mut mario_angle = 0.0; 'running: loop { @@ -84,10 +82,10 @@ fn main() { let size = 32; let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2); for i in 0..blocks { - canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap(); + canvas.copy(sprites.get("block"), None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap(); + canvas.copy(sprites.get("block"), None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap(); + canvas.copy(sprites.get("block"), None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap(); + canvas.copy(sprites.get("block"), None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap(); } } { @@ -96,7 +94,7 @@ fn main() { let radius = 110.0 + size as f32 * 0.5; let angle = (mario_angle as f32 - 90.0) * PI / 180.0; let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32); - canvas.copy_ex(&(textures.get("mario").unwrap()), None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32), mario_angle, sdl2::rect::Point::new(size / 2, size / 2), false, false).unwrap(); + canvas.copy_ex(sprites.get("mario"), None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32), mario_angle, sdl2::rect::Point::new(size / 2, size / 2), false, false).unwrap(); mario_angle += 1.0; if mario_angle >= 360.0 { mario_angle -= 360.0 } } diff --git a/src/sprites.rs b/src/sprites.rs new file mode 100644 index 0000000..82dd6dc --- /dev/null +++ b/src/sprites.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; + +use sdl2::image::LoadTexture; +use sdl2::render::Texture; +use sdl2::render::TextureCreator; +use sdl2::video::WindowContext; + +pub struct SpriteManager<'canvas> { + texture_creator: &'canvas TextureCreator, // can't make the lifetimes work when this is owned instead of borrowed + textures: HashMap<&'static str, Texture<'canvas>>, +} + +impl<'canvas> SpriteManager<'canvas> { + pub fn new(texture_creator: &'canvas TextureCreator) -> SpriteManager<'canvas> { + SpriteManager { + texture_creator, + textures: HashMap::new(), + } + } + + pub fn load(&mut self, name: &'static str, file: &str) { + self.textures.insert(name, self.texture_creator.load_texture(file).unwrap()); + } + + pub fn get(&self, name: &str) -> &Texture { + self.textures.get(name).expect(format!("The sprite '{}' was not found", name).as_str()) + } +} -- 2.11.0