mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 04:07:52 -06:00
chore: move ttf context out of game.rs, remove unnecessary window event logging
This commit is contained in:
12
src/app.rs
12
src/app.rs
@@ -13,10 +13,6 @@ use sdl2::{AudioSubsystem, Sdl};
|
|||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
/// Main application wrapper that manages SDL initialization, window lifecycle, and the game loop.
|
/// Main application wrapper that manages SDL initialization, window lifecycle, and the game loop.
|
||||||
///
|
|
||||||
/// Handles platform-specific setup, maintains consistent frame timing, and delegates
|
|
||||||
/// game logic to the contained `Game` instance. The app manages focus state to
|
|
||||||
/// optimize CPU usage when the window loses focus.
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub game: Game,
|
pub game: Game,
|
||||||
last_tick: Instant,
|
last_tick: Instant,
|
||||||
@@ -29,16 +25,13 @@ pub struct App {
|
|||||||
impl App {
|
impl App {
|
||||||
/// Initializes SDL subsystems, creates the game window, and sets up the game state.
|
/// Initializes SDL subsystems, creates the game window, and sets up the game state.
|
||||||
///
|
///
|
||||||
/// Performs comprehensive initialization including video/audio subsystems,
|
|
||||||
/// window creation with proper scaling, and canvas configuration. All SDL
|
|
||||||
/// resources are leaked to maintain 'static lifetimes required by the game architecture.
|
|
||||||
///
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns `GameError::Sdl` if any SDL initialization step fails, or propagates
|
/// Returns `GameError::Sdl` if any SDL initialization step fails, or propagates
|
||||||
/// errors from `Game::new()` during game state setup.
|
/// errors from `Game::new()` during game state setup.
|
||||||
pub fn new() -> GameResult<Self> {
|
pub fn new() -> GameResult<Self> {
|
||||||
let sdl_context = sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
let sdl_context = sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
let ttf_context = sdl2::ttf::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
let video_subsystem = sdl_context.video().map_err(|e| GameError::Sdl(e.to_string()))?;
|
let video_subsystem = sdl_context.video().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
let audio_subsystem = sdl_context.audio().map_err(|e| GameError::Sdl(e.to_string()))?;
|
let audio_subsystem = sdl_context.audio().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
let event_pump = sdl_context.event_pump().map_err(|e| GameError::Sdl(e.to_string()))?;
|
let event_pump = sdl_context.event_pump().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
@@ -102,8 +95,7 @@ impl App {
|
|||||||
|
|
||||||
let texture_creator = canvas.texture_creator();
|
let texture_creator = canvas.texture_creator();
|
||||||
|
|
||||||
let game = Game::new(canvas, texture_creator, event_pump)?;
|
let game = Game::new(canvas, ttf_context, texture_creator, event_pump)?;
|
||||||
// game.audio.set_mute(cfg!(debug_assertions));
|
|
||||||
|
|
||||||
Ok(App {
|
Ok(App {
|
||||||
game,
|
game,
|
||||||
|
|||||||
@@ -88,13 +88,12 @@ impl Game {
|
|||||||
/// errors, or entity initialization issues.
|
/// errors, or entity initialization issues.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
mut canvas: Canvas<Window>,
|
mut canvas: Canvas<Window>,
|
||||||
|
ttf_context: sdl2::ttf::Sdl2TtfContext,
|
||||||
texture_creator: TextureCreator<WindowContext>,
|
texture_creator: TextureCreator<WindowContext>,
|
||||||
mut event_pump: EventPump,
|
mut event_pump: EventPump,
|
||||||
) -> GameResult<Game> {
|
) -> GameResult<Game> {
|
||||||
Self::disable_sdl_events(&mut event_pump);
|
Self::disable_sdl_events(&mut event_pump);
|
||||||
|
|
||||||
let ttf_context = Box::leak(Box::new(sdl2::ttf::init().map_err(|e| GameError::Sdl(e.to_string()))?));
|
|
||||||
|
|
||||||
let (backbuffer, mut map_texture, debug_texture, ttf_atlas) =
|
let (backbuffer, mut map_texture, debug_texture, ttf_atlas) =
|
||||||
Self::setup_textures_and_fonts(&mut canvas, &texture_creator, ttf_context)?;
|
Self::setup_textures_and_fonts(&mut canvas, &texture_creator, ttf_context)?;
|
||||||
|
|
||||||
@@ -188,7 +187,7 @@ impl Game {
|
|||||||
fn setup_textures_and_fonts(
|
fn setup_textures_and_fonts(
|
||||||
canvas: &mut Canvas<Window>,
|
canvas: &mut Canvas<Window>,
|
||||||
texture_creator: &TextureCreator<WindowContext>,
|
texture_creator: &TextureCreator<WindowContext>,
|
||||||
ttf_context: &'static sdl2::ttf::Sdl2TtfContext,
|
ttf_context: sdl2::ttf::Sdl2TtfContext,
|
||||||
) -> GameResult<(
|
) -> GameResult<(
|
||||||
sdl2::render::Texture,
|
sdl2::render::Texture,
|
||||||
sdl2::render::Texture,
|
sdl2::render::Texture,
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use sdl2::{
|
|||||||
EventPump,
|
EventPump,
|
||||||
};
|
};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use tracing::{debug, info};
|
|
||||||
|
|
||||||
use crate::systems::components::DeltaTime;
|
use crate::systems::components::DeltaTime;
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -300,15 +299,10 @@ pub fn input_system(
|
|||||||
}
|
}
|
||||||
Event::Window { win_event, .. } => match win_event {
|
Event::Window { win_event, .. } => match win_event {
|
||||||
WindowEvent::Resized(w, h) => {
|
WindowEvent::Resized(w, h) => {
|
||||||
info!("Window resized to {}x{}", w, h);
|
tracing::info!("Window resized to {}x{}", w, h);
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
debug!("Window event: {:?}", win_event);
|
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
},
|
},
|
||||||
Event::RenderTargetsReset { .. } => {
|
|
||||||
// No-op
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
tracing::warn!("Unhandled event, consider disabling: {:?}", event);
|
tracing::warn!("Unhandled event, consider disabling: {:?}", event);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user