feat: current canvas, window & mainloop

This commit is contained in:
2023-09-08 22:47:36 -05:00
parent d90785a61a
commit ccde1b6538

View File

@@ -1,17 +1,22 @@
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH}; use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use sdl2::event::{Event, WindowEvent}; use crate::game::Game;
use sdl2::image::LoadTexture; use crate::textures::TextureManager;
use sdl2::event::{Event};
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::render::{Texture, Canvas}; use sdl2::render::{Canvas, Texture};
use std::time::Duration; use std::time::{Duration, Instant};
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
pub mod emscripten; pub mod emscripten;
mod board;
mod constants; mod constants;
mod direction;
mod game; mod game;
mod pacman;
mod textures;
mod entity;
mod animation;
fn redraw(canvas: &mut Canvas<sdl2::video::Window>, tex: &Texture, i: u8) { fn redraw(canvas: &mut Canvas<sdl2::video::Window>, tex: &Texture, i: u8) {
canvas.set_draw_color(Color::RGB(i, i, i)); canvas.set_draw_color(Color::RGB(i, i, i));
@@ -28,7 +33,6 @@ pub fn main() {
let window = video_subsystem let window = video_subsystem
.window("Pac-Man", WINDOW_WIDTH, WINDOW_HEIGHT) .window("Pac-Man", WINDOW_WIDTH, WINDOW_HEIGHT)
.position_centered() .position_centered()
.resizable()
.build() .build()
.expect("Could not initialize window"); .expect("Could not initialize window");
@@ -36,22 +40,21 @@ pub fn main() {
.into_canvas() .into_canvas()
.build() .build()
.expect("Could not build canvas"); .expect("Could not build canvas");
let texture_creator = canvas.texture_creator();
let map_texture = texture_creator
.load_texture("assets/map.png")
.expect("Could not load pacman texture");
canvas canvas
.copy(&map_texture, None, None) .set_logical_size(WINDOW_WIDTH, WINDOW_HEIGHT)
.expect("Could not render texture on canvas"); .expect("Could not set logical size");
let mut i = 0u8; let texture_creator = canvas.texture_creator();
let mut game = Game::new(&mut canvas, TextureManager::new(&texture_creator));
let mut event_pump = sdl_context let mut event_pump = sdl_context
.event_pump() .event_pump()
.expect("Could not get SDL EventPump"); .expect("Could not get SDL EventPump");
game.draw();
game.tick();
let mut main_loop = || { let mut main_loop = || {
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
@@ -63,20 +66,31 @@ pub fn main() {
} => return false, } => return false,
event @ Event::KeyDown { .. } => { event @ Event::KeyDown { .. } => {
println!("{:?}", event); println!("{:?}", event);
}, }
Event::Window { win_event, .. } => {
if let WindowEvent::Resized(width, height) = win_event {
i = i.wrapping_add(1);
canvas.set_logical_size(width as u32, height as u32).unwrap();
redraw(&mut canvas, &map_texture, i);
}
},
_ => {} _ => {}
} }
} }
canvas.present(); let tick_time = {
let start = Instant::now();
game.tick();
start.elapsed()
};
let draw_time = {
let start = Instant::now();
game.draw();
start.elapsed()
};
// Alert if tick time exceeds 10ms
if tick_time > Duration::from_millis(3) {
println!("Tick took: {:?}", tick_time);
}
if draw_time > Duration::from_millis(3) {
println!("Draw took: {:?}", draw_time);
}
::std::thread::sleep(Duration::from_millis(10)); ::std::thread::sleep(Duration::from_millis(10));
true true
}; };