From 61ca5379096fc19c8e3a6fc1fa670bf8ff913d9d Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 24 Jul 2025 16:09:25 -0500 Subject: [PATCH] fix: continue removing lifetime annotations --- src/game.rs | 25 ++++++++++++++----------- src/main.rs | 3 ++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/game.rs b/src/game.rs index 7daa3ce..cf80367 100644 --- a/src/game.rs +++ b/src/game.rs @@ -31,12 +31,12 @@ use crate::texture::atlas::{texture_to_static, AtlasTexture}; /// /// This struct contains all the information necessary to run the game, including /// the canvas, textures, fonts, game objects, and the current score. -pub struct Game<'a> { - canvas: &'a mut Canvas, - map_texture: Texture<'a>, +pub struct Game { + canvas: &'static mut Canvas, + map_texture: Texture<'static>, pellet_texture: Rc, power_pellet_texture: Rc, - font: Font<'a, 'static>, + font: Font<'static, 'static>, pacman: Rc>, map: Rc>, debug_mode: DebugMode, @@ -46,7 +46,7 @@ pub struct Game<'a> { edibles: Vec, } -impl<'a> Game<'a> { +impl Game { /// Creates a new `Game` instance. /// /// # Arguments @@ -56,11 +56,11 @@ impl<'a> Game<'a> { /// * `ttf_context` - The SDL TTF context. /// * `_audio_subsystem` - The SDL audio subsystem (currently unused). pub fn new( - canvas: &'a mut Canvas, - texture_creator: &'a TextureCreator, - ttf_context: &'a sdl2::ttf::Sdl2TtfContext, - _audio_subsystem: &'a sdl2::AudioSubsystem, - ) -> Game<'a> { + canvas: &'static mut Canvas, + texture_creator: &TextureCreator, + ttf_context: &sdl2::ttf::Sdl2TtfContext, + _audio_subsystem: &sdl2::AudioSubsystem, + ) -> Game { let map = Rc::new(RefCell::new(Map::new(RAW_BOARD))); // Load Pacman texture from asset API @@ -125,6 +125,7 @@ impl<'a> Game<'a> { .load_texture_bytes(&map_bytes) .expect("Could not load map texture from asset API"); map_texture.set_color_mod(0, 0, 255); + let map_texture = unsafe { texture_to_static(map_texture) }; let edibles = reconstruct_edibles( Rc::clone(&map), @@ -138,7 +139,9 @@ impl<'a> Game<'a> { let font_bytes = get_asset_bytes(Asset::FontKonami).expect("Failed to load asset").into_owned(); let font_bytes_static: &'static [u8] = Box::leak(font_bytes.into_boxed_slice()); let font_rwops = RWops::from_bytes(font_bytes_static).expect("Failed to create RWops for font"); - ttf_context + // Leak the ttf_context to get a 'static lifetime + let ttf_context_static: &'static sdl2::ttf::Sdl2TtfContext = unsafe { std::mem::transmute(ttf_context) }; + ttf_context_static .load_font_from_rwops(font_rwops, 24) .expect("Could not load font from asset API") }; diff --git a/src/main.rs b/src/main.rs index 904de9b..44ecf98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,7 +123,8 @@ pub fn main() { .expect("Could not set logical size"); let texture_creator = canvas.texture_creator(); - let mut game = Game::new(&mut canvas, &texture_creator, &ttf_context, &audio_subsystem); + let canvas_static: &'static mut sdl2::render::Canvas = Box::leak(Box::new(canvas)); + let mut game = Game::new(canvas_static, &texture_creator, &ttf_context, &audio_subsystem); game.audio.set_mute(cfg!(debug_assertions)); let mut event_pump = sdl_context.event_pump().expect("Could not get SDL EventPump");