mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-13 10:12:19 -06:00
fix: continue removing lifetime annotations
This commit is contained in:
25
src/game.rs
25
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
|
/// This struct contains all the information necessary to run the game, including
|
||||||
/// the canvas, textures, fonts, game objects, and the current score.
|
/// the canvas, textures, fonts, game objects, and the current score.
|
||||||
pub struct Game<'a> {
|
pub struct Game {
|
||||||
canvas: &'a mut Canvas<Window>,
|
canvas: &'static mut Canvas<Window>,
|
||||||
map_texture: Texture<'a>,
|
map_texture: Texture<'static>,
|
||||||
pellet_texture: Rc<AtlasTexture>,
|
pellet_texture: Rc<AtlasTexture>,
|
||||||
power_pellet_texture: Rc<AtlasTexture>,
|
power_pellet_texture: Rc<AtlasTexture>,
|
||||||
font: Font<'a, 'static>,
|
font: Font<'static, 'static>,
|
||||||
pacman: Rc<RefCell<Pacman>>,
|
pacman: Rc<RefCell<Pacman>>,
|
||||||
map: Rc<RefCell<Map>>,
|
map: Rc<RefCell<Map>>,
|
||||||
debug_mode: DebugMode,
|
debug_mode: DebugMode,
|
||||||
@@ -46,7 +46,7 @@ pub struct Game<'a> {
|
|||||||
edibles: Vec<Edible>,
|
edibles: Vec<Edible>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Game<'a> {
|
impl Game {
|
||||||
/// Creates a new `Game` instance.
|
/// Creates a new `Game` instance.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
@@ -56,11 +56,11 @@ impl<'a> Game<'a> {
|
|||||||
/// * `ttf_context` - The SDL TTF context.
|
/// * `ttf_context` - The SDL TTF context.
|
||||||
/// * `_audio_subsystem` - The SDL audio subsystem (currently unused).
|
/// * `_audio_subsystem` - The SDL audio subsystem (currently unused).
|
||||||
pub fn new(
|
pub fn new(
|
||||||
canvas: &'a mut Canvas<Window>,
|
canvas: &'static mut Canvas<Window>,
|
||||||
texture_creator: &'a TextureCreator<WindowContext>,
|
texture_creator: &TextureCreator<WindowContext>,
|
||||||
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
|
ttf_context: &sdl2::ttf::Sdl2TtfContext,
|
||||||
_audio_subsystem: &'a sdl2::AudioSubsystem,
|
_audio_subsystem: &sdl2::AudioSubsystem,
|
||||||
) -> Game<'a> {
|
) -> Game {
|
||||||
let map = Rc::new(RefCell::new(Map::new(RAW_BOARD)));
|
let map = Rc::new(RefCell::new(Map::new(RAW_BOARD)));
|
||||||
|
|
||||||
// Load Pacman texture from asset API
|
// Load Pacman texture from asset API
|
||||||
@@ -125,6 +125,7 @@ impl<'a> Game<'a> {
|
|||||||
.load_texture_bytes(&map_bytes)
|
.load_texture_bytes(&map_bytes)
|
||||||
.expect("Could not load map texture from asset API");
|
.expect("Could not load map texture from asset API");
|
||||||
map_texture.set_color_mod(0, 0, 255);
|
map_texture.set_color_mod(0, 0, 255);
|
||||||
|
let map_texture = unsafe { texture_to_static(map_texture) };
|
||||||
|
|
||||||
let edibles = reconstruct_edibles(
|
let edibles = reconstruct_edibles(
|
||||||
Rc::clone(&map),
|
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 = 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_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");
|
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)
|
.load_font_from_rwops(font_rwops, 24)
|
||||||
.expect("Could not load font from asset API")
|
.expect("Could not load font from asset API")
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -123,7 +123,8 @@ pub fn main() {
|
|||||||
.expect("Could not set logical size");
|
.expect("Could not set logical size");
|
||||||
|
|
||||||
let texture_creator = canvas.texture_creator();
|
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<sdl2::video::Window> = 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));
|
game.audio.set_mute(cfg!(debug_assertions));
|
||||||
|
|
||||||
let mut event_pump = sdl_context.event_pump().expect("Could not get SDL EventPump");
|
let mut event_pump = sdl_context.event_pump().expect("Could not get SDL EventPump");
|
||||||
|
|||||||
Reference in New Issue
Block a user