From b46a51bc766a9fe4ca61397b45cb02e835d714dd Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 8 Sep 2023 23:37:32 -0500 Subject: [PATCH] reformat: drop TextureManager based sprite rendering, directly hold Textures --- src/animation.rs | 3 +-- src/game.rs | 52 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/animation.rs b/src/animation.rs index 9099b19..61c24bb 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -5,8 +5,7 @@ use sdl2::{ }; pub struct AnimatedTexture<'a> { - raw_texture: &'a Texture<'a>, - current_frame: u32, + raw_texture: Texture<'a>, frame_count: u32, frame_width: u32, frame_height: u32, diff --git a/src/game.rs b/src/game.rs index 71f1473..11e5c26 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,12 +1,17 @@ +use sdl2::image::LoadTexture; +use sdl2::keyboard::Keycode; +use sdl2::render::{TextureCreator, Texture}; +use sdl2::video::WindowContext; use sdl2::{pixels::Color, render::Canvas, video::Window}; use crate::constants::{MapTile, BOARD, BOARD_HEIGHT, BOARD_WIDTH}; -use crate::pacman::Pacman; -use crate::textures::TextureManager; +use crate::direction::Direction; +use crate::entity::Entity; +use crate::pacman::{Pacman}; pub struct Game<'a> { - pub textures: TextureManager<'a>, canvas: &'a mut Canvas, + map_texture: Texture<'a>, pacman: Pacman<'a>, debug: bool, } @@ -14,19 +19,47 @@ pub struct Game<'a> { impl Game<'_> { pub fn new<'a>( canvas: &'a mut Canvas, - texture_manager: TextureManager<'a>, + texture_creator: &'a TextureCreator, ) -> Game<'a> { - let pacman = Pacman::new(None, &texture_manager.pacman); + let pacman_atlas = texture_creator + .load_texture("assets/32/pacman.png") + .expect("Could not load pacman texture"); + let pacman = Pacman::new(None, pacman_atlas); Game { canvas, - textures: texture_manager, pacman: pacman, debug: true, + map_texture: texture_creator + .load_texture("assets/map.png") + .expect("Could not load pacman texture"), } } - pub fn tick(&mut self) {} + pub fn keyboard_event(&mut self, keycode: Keycode) { + match keycode { + Keycode::D => { + self.pacman.direction = Direction::Right; + } + Keycode::A => { + self.pacman.direction = Direction::Left; + } + Keycode::W => { + self.pacman.direction = Direction::Up; + } + Keycode::S => { + self.pacman.direction = Direction::Down; + } + Keycode::Space => { + self.debug = !self.debug; + } + _ => {} + } + } + + pub fn tick(&mut self) { + self.pacman.tick(); + } pub fn draw(&mut self) { // Clear the screen (black) @@ -34,9 +67,12 @@ impl Game<'_> { self.canvas.clear(); self.canvas - .copy(&self.textures.map, None, None) + .copy(&self.map_texture, None, None) .expect("Could not render texture on canvas"); + // Render the pacman + self.pacman.render(self.canvas); + // Draw a grid for x in 0..BOARD_WIDTH { for y in 0..BOARD_HEIGHT {