reformat: drop TextureManager based sprite rendering, directly hold Textures

This commit is contained in:
2023-09-08 23:37:32 -05:00
parent 443afb1223
commit b46a51bc76
2 changed files with 45 additions and 10 deletions
+1 -2
View File
@@ -5,8 +5,7 @@ use sdl2::{
}; };
pub struct AnimatedTexture<'a> { pub struct AnimatedTexture<'a> {
raw_texture: &'a Texture<'a>, raw_texture: Texture<'a>,
current_frame: u32,
frame_count: u32, frame_count: u32,
frame_width: u32, frame_width: u32,
frame_height: u32, frame_height: u32,
+44 -8
View File
@@ -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 sdl2::{pixels::Color, render::Canvas, video::Window};
use crate::constants::{MapTile, BOARD, BOARD_HEIGHT, BOARD_WIDTH}; use crate::constants::{MapTile, BOARD, BOARD_HEIGHT, BOARD_WIDTH};
use crate::pacman::Pacman; use crate::direction::Direction;
use crate::textures::TextureManager; use crate::entity::Entity;
use crate::pacman::{Pacman};
pub struct Game<'a> { pub struct Game<'a> {
pub textures: TextureManager<'a>,
canvas: &'a mut Canvas<Window>, canvas: &'a mut Canvas<Window>,
map_texture: Texture<'a>,
pacman: Pacman<'a>, pacman: Pacman<'a>,
debug: bool, debug: bool,
} }
@@ -14,19 +19,47 @@ pub struct Game<'a> {
impl Game<'_> { impl Game<'_> {
pub fn new<'a>( pub fn new<'a>(
canvas: &'a mut Canvas<Window>, canvas: &'a mut Canvas<Window>,
texture_manager: TextureManager<'a>, texture_creator: &'a TextureCreator<WindowContext>,
) -> Game<'a> { ) -> 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 { Game {
canvas, canvas,
textures: texture_manager,
pacman: pacman, pacman: pacman,
debug: true, 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) { pub fn draw(&mut self) {
// Clear the screen (black) // Clear the screen (black)
@@ -34,9 +67,12 @@ impl Game<'_> {
self.canvas.clear(); self.canvas.clear();
self.canvas self.canvas
.copy(&self.textures.map, None, None) .copy(&self.map_texture, None, None)
.expect("Could not render texture on canvas"); .expect("Could not render texture on canvas");
// Render the pacman
self.pacman.render(self.canvas);
// Draw a grid // Draw a grid
for x in 0..BOARD_WIDTH { for x in 0..BOARD_WIDTH {
for y in 0..BOARD_HEIGHT { for y in 0..BOARD_HEIGHT {