From 7cdd1b6ad9fca5fc6a07ba62245c3e0a9c0a4765 Mon Sep 17 00:00:00 2001 From: Ryan Walters Date: Tue, 2 Sep 2025 12:59:06 -0500 Subject: [PATCH] refactor: use 'unsafe_textures' sdl2 feature to hide lifetimes & obscure leaks into upstream --- Cargo.toml | 14 +++++--------- src/systems/debug.rs | 2 +- src/systems/render.rs | 4 ++-- src/texture/sprite.rs | 6 +++--- tests/common/mod.rs | 3 +-- tests/sprite.rs | 2 +- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 582f9ae..91fef56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,16 +42,14 @@ windows-sys = { version = "0.60.2", features = ["Win32_System_Console"] } [target.'cfg(not(target_os = "windows"))'.dependencies] libc = "0.2" - -[target.'cfg(target_os = "emscripten")'.dependencies.sdl2] -version = "0.38" -default-features = false -features = ["ttf","image","gfx","mixer"] - [target.'cfg(not(target_os = "emscripten"))'.dependencies.sdl2] version = "0.38" default-features = false -features = ["ttf","image","gfx","mixer","static-link","use-vcpkg"] +features = ["image", "ttf", "gfx", "mixer", "unsafe_textures", "static-link", "use-vcpkg"] + +[target.'cfg(target_os = "emscripten")'.dependencies] +sdl2 = { version = "0.38", default-features = false, features = ["image", "ttf", "gfx", "mixer", "unsafe_textures"] } +libc = "0.2.175" # TODO: Describe why this is required. [package.metadata.vcpkg] dependencies = ["sdl2", "sdl2-image", "sdl2-ttf", "sdl2-gfx", "sdl2-mixer"] @@ -64,8 +62,6 @@ x86_64-unknown-linux-gnu = { triplet = "x64-linux" } x86_64-apple-darwin = { triplet = "x64-osx" } aarch64-apple-darwin = { triplet = "arm64-osx" } -[target.'cfg(target_os = "emscripten")'.dependencies] -libc = "0.2.175" [build-dependencies] serde = { version = "1.0", features = ["derive"] } diff --git a/src/systems/debug.rs b/src/systems/debug.rs index e65d8c5..e4c25f1 100644 --- a/src/systems/debug.rs +++ b/src/systems/debug.rs @@ -23,7 +23,7 @@ fn f32_to_u8(value: f32) -> u8 { } /// Resource to hold the debug texture for persistent rendering -pub struct DebugTextureResource(pub Texture<'static>); +pub struct DebugTextureResource(pub Texture); /// Resource to hold the debug font pub struct DebugFontResource(pub Font<'static, 'static>); diff --git a/src/systems/render.rs b/src/systems/render.rs index 3d4a47d..7ce4ac8 100644 --- a/src/systems/render.rs +++ b/src/systems/render.rs @@ -100,10 +100,10 @@ pub fn linear_render_system(dt: Res, mut query: Query<(&mut LinearAni } /// A non-send resource for the map texture. This just wraps the texture with a type so it can be differentiated when exposed as a resource. -pub struct MapTextureResource(pub Texture<'static>); +pub struct MapTextureResource(pub Texture); /// A non-send resource for the backbuffer texture. This just wraps the texture with a type so it can be differentiated when exposed as a resource. -pub struct BackbufferResource(pub Texture<'static>); +pub struct BackbufferResource(pub Texture); /// Renders the HUD (score, lives, etc.) on top of the game. pub fn hud_render_system( diff --git a/src/texture/sprite.rs b/src/texture/sprite.rs index 815ba33..e1d11e0 100644 --- a/src/texture/sprite.rs +++ b/src/texture/sprite.rs @@ -79,7 +79,7 @@ impl AtlasTile { /// and optional default color modulation configuration. pub struct SpriteAtlas { /// The combined texture containing all sprite frames - texture: Texture<'static>, + texture: Texture, /// Mapping from sprite names to their pixel coordinates within the texture tiles: HashMap, default_color: Option, @@ -88,7 +88,7 @@ pub struct SpriteAtlas { } impl SpriteAtlas { - pub fn new(texture: Texture<'static>, mapper: AtlasMapper) -> Self { + pub fn new(texture: Texture, mapper: AtlasMapper) -> Self { Self { texture, tiles: mapper.frames, @@ -117,7 +117,7 @@ impl SpriteAtlas { } #[allow(dead_code)] - pub fn texture(&self) -> &Texture<'static> { + pub fn texture(&self) -> &Texture { &self.texture } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 092ef9e..98e211a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -7,7 +7,7 @@ use pacman::{ }; use sdl2::{ image::LoadTexture, - render::{Canvas, Texture, TextureCreator}, + render::{Canvas, TextureCreator}, video::{Window, WindowContext}, Sdl, }; @@ -31,7 +31,6 @@ pub fn create_atlas(canvas: &mut sdl2::render::Canvas) -> S let atlas_bytes = get_asset_bytes(Asset::AtlasImage).unwrap(); let texture = texture_creator.load_texture_bytes(&atlas_bytes).unwrap(); - let texture: Texture<'static> = unsafe { std::mem::transmute(texture) }; let atlas_mapper = AtlasMapper { frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(), diff --git a/tests/sprite.rs b/tests/sprite.rs index 338ef52..8662dda 100644 --- a/tests/sprite.rs +++ b/tests/sprite.rs @@ -3,7 +3,7 @@ use pacman::texture::sprite::{AtlasMapper, AtlasTile, MapperFrame, SpriteAtlas}; use sdl2::pixels::Color; use std::collections::HashMap; -fn mock_texture() -> sdl2::render::Texture<'static> { +fn mock_texture() -> sdl2::render::Texture { unsafe { std::mem::transmute(0usize) } }