mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 16:07:52 -06:00
feat: smarter winapi-based console window handling
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -94,6 +94,7 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
"tracing-error",
|
"tracing-error",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
spin_sleep = "1.1.1"
|
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-error = "0.2.0"
|
||||||
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
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]
|
[dependencies.sdl2]
|
||||||
version = "0.38"
|
version = "0.38"
|
||||||
|
|||||||
BIN
assets/wav/eating.wav
Normal file
BIN
assets/wav/eating.wav
Normal file
Binary file not shown.
BIN
assets/wav/waka_ka.wav
Normal file
BIN
assets/wav/waka_ka.wav
Normal file
Binary file not shown.
BIN
assets/wav/waka_wa.wav
Normal file
BIN
assets/wav/waka_wa.wav
Normal file
Binary file not shown.
47
src/main.rs
47
src/main.rs
@@ -1,3 +1,5 @@
|
|||||||
|
#![windows_subsystem = "windows"]
|
||||||
|
|
||||||
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
|
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
|
||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
use sdl2::event::{Event, WindowEvent};
|
use sdl2::event::{Event, WindowEvent};
|
||||||
@@ -7,7 +9,46 @@ use tracing::event;
|
|||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
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 animation;
|
||||||
|
mod audio;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod direction;
|
mod direction;
|
||||||
mod entity;
|
mod entity;
|
||||||
@@ -18,8 +59,14 @@ mod modulation;
|
|||||||
mod pacman;
|
mod pacman;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
#[cfg(windows)]
|
||||||
|
unsafe {
|
||||||
|
attach_console();
|
||||||
|
}
|
||||||
|
|
||||||
let sdl_context = sdl2::init().unwrap();
|
let sdl_context = sdl2::init().unwrap();
|
||||||
let video_subsystem = sdl_context.video().unwrap();
|
let video_subsystem = sdl_context.video().unwrap();
|
||||||
|
let audio_subsystem = sdl_context.audio().unwrap();
|
||||||
let ttf_context = sdl2::ttf::init().unwrap();
|
let ttf_context = sdl2::ttf::init().unwrap();
|
||||||
|
|
||||||
// Setup tracing
|
// Setup tracing
|
||||||
|
|||||||
Reference in New Issue
Block a user