Compare commits

...

2 Commits

Author SHA1 Message Date
f5ff90cb11 feat: smarter winapi-based console window handling 2025-07-21 21:54:22 -05:00
a0f65b551c feat: board reset, store original map matrix 2025-07-18 20:21:16 -05:00
8 changed files with 92 additions and 8 deletions

1
Cargo.lock generated
View File

@@ -94,6 +94,7 @@ dependencies = [
"tracing",
"tracing-error",
"tracing-subscriber",
"winapi",
]
[[package]]

View File

@@ -8,9 +8,10 @@ edition = "2021"
[dependencies]
lazy_static = "1.4.0"
spin_sleep = "1.1.1"
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_warn"]}
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
tracing-error = "0.2.0"
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
winapi = { version = "0.3", features = ["consoleapi", "fileapi", "handleapi", "processenv", "winbase", "wincon", "winnt", "winuser", "windef", "minwindef"] }
[dependencies.sdl2]
version = "0.38"

BIN
assets/wav/eating.wav Normal file
View File

Binary file not shown.

BIN
assets/wav/waka_ka.wav Normal file
View File

Binary file not shown.

BIN
assets/wav/waka_wa.wav Normal file
View File

Binary file not shown.

View File

@@ -73,12 +73,34 @@ impl Game<'_> {
if keycode == Keycode::Space {
self.debug = !self.debug;
}
// Reset game
if keycode == Keycode::R {
self.reset();
}
}
pub fn add_score(&mut self, points: u32) {
self.score += points;
}
pub fn reset(&mut self) {
// Reset the map to restore all pellets
{
let mut map = self.map.borrow_mut();
map.reset();
}
// Reset the score
self.score = 0;
// Reset Pacman position (you might want to customize this)
// For now, we'll keep Pacman where he is, but you could add:
// self.pacman.position = Map::cell_to_pixel((1, 1));
event!(tracing::Level::INFO, "Game reset - map and score cleared");
}
pub fn tick(&mut self) {
self.pacman.tick();
self.check_pellet_eating();

View File

@@ -1,3 +1,5 @@
#![windows_subsystem = "windows"]
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use crate::game::Game;
use sdl2::event::{Event, WindowEvent};
@@ -7,7 +9,46 @@ use tracing::event;
use tracing_error::ErrorLayer;
use tracing_subscriber::layer::SubscriberExt;
#[cfg(windows)]
use winapi::{
shared::{ntdef::NULL, windef::HWND},
um::{
fileapi::{CreateFileA, OPEN_EXISTING},
handleapi::INVALID_HANDLE_VALUE,
processenv::SetStdHandle,
winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE},
wincon::{AttachConsole, GetConsoleWindow},
winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
},
};
#[cfg(windows)]
unsafe fn attach_console() {
if GetConsoleWindow() != std::ptr::null_mut() as HWND {
return;
}
if AttachConsole(winapi::um::wincon::ATTACH_PARENT_PROCESS) != 0 {
let handle = CreateFileA(
"CONOUT$\0".as_ptr() as *const i8,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null_mut(),
OPEN_EXISTING,
0,
NULL,
);
if handle != INVALID_HANDLE_VALUE {
SetStdHandle(STD_OUTPUT_HANDLE, handle);
SetStdHandle(STD_ERROR_HANDLE, handle);
}
}
// Do NOT call AllocConsole here - we don't want a console when launched from Explorer
}
mod animation;
mod audio;
mod constants;
mod direction;
mod entity;
@@ -18,8 +59,14 @@ mod modulation;
mod pacman;
pub fn main() {
#[cfg(windows)]
unsafe {
attach_console();
}
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let audio_subsystem = sdl_context.audio().unwrap();
let ttf_context = sdl2::ttf::init().unwrap();
// Setup tracing

View File

@@ -1,13 +1,14 @@
use crate::constants::MapTile;
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH};
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
pub struct Map {
inner: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
current: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
default: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
}
impl Map {
pub fn new(raw_board: [&str; BOARD_HEIGHT as usize]) -> Map {
let mut inner = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
let mut map = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
for y in 0..BOARD_HEIGHT as usize {
let line = raw_board[y];
@@ -35,11 +36,23 @@ impl Map {
_ => panic!("Unknown character in board: {}", character),
};
inner[x as usize][y as usize] = tile;
map[x as usize][y as usize] = tile;
}
}
Map { inner: inner }
Map {
current: map,
default: map.clone(),
}
}
pub fn reset(&mut self) {
// Restore the map to its original state
for x in 0..BOARD_WIDTH as usize {
for y in 0..BOARD_HEIGHT as usize {
self.current[x][y] = self.default[x][y];
}
}
}
pub fn get_tile(&self, cell: (i32, i32)) -> Option<MapTile> {
@@ -50,7 +63,7 @@ impl Map {
return None;
}
Some(self.inner[x][y])
Some(self.current[x][y])
}
pub fn set_tile(&mut self, cell: (i32, i32), tile: MapTile) -> bool {
@@ -61,7 +74,7 @@ impl Map {
return false;
}
self.inner[x][y] = tile;
self.current[x][y] = tile;
true
}