refactor: use 'unsafe_textures' sdl2 feature to hide lifetimes & obscure leaks into upstream

This commit is contained in:
Ryan Walters
2025-09-02 12:59:06 -05:00
parent d0a68faa51
commit 7cdd1b6ad9
6 changed files with 13 additions and 18 deletions

View File

@@ -42,16 +42,14 @@ windows-sys = { version = "0.60.2", features = ["Win32_System_Console"] }
[target.'cfg(not(target_os = "windows"))'.dependencies] [target.'cfg(not(target_os = "windows"))'.dependencies]
libc = "0.2" 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] [target.'cfg(not(target_os = "emscripten"))'.dependencies.sdl2]
version = "0.38" version = "0.38"
default-features = false 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] [package.metadata.vcpkg]
dependencies = ["sdl2", "sdl2-image", "sdl2-ttf", "sdl2-gfx", "sdl2-mixer"] 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" } x86_64-apple-darwin = { triplet = "x64-osx" }
aarch64-apple-darwin = { triplet = "arm64-osx" } aarch64-apple-darwin = { triplet = "arm64-osx" }
[target.'cfg(target_os = "emscripten")'.dependencies]
libc = "0.2.175"
[build-dependencies] [build-dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@@ -23,7 +23,7 @@ fn f32_to_u8(value: f32) -> u8 {
} }
/// Resource to hold the debug texture for persistent rendering /// 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 /// Resource to hold the debug font
pub struct DebugFontResource(pub Font<'static, 'static>); pub struct DebugFontResource(pub Font<'static, 'static>);

View File

@@ -100,10 +100,10 @@ pub fn linear_render_system(dt: Res<DeltaTime>, 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. /// 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. /// 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. /// Renders the HUD (score, lives, etc.) on top of the game.
pub fn hud_render_system( pub fn hud_render_system(

View File

@@ -79,7 +79,7 @@ impl AtlasTile {
/// and optional default color modulation configuration. /// and optional default color modulation configuration.
pub struct SpriteAtlas { pub struct SpriteAtlas {
/// The combined texture containing all sprite frames /// The combined texture containing all sprite frames
texture: Texture<'static>, texture: Texture,
/// Mapping from sprite names to their pixel coordinates within the texture /// Mapping from sprite names to their pixel coordinates within the texture
tiles: HashMap<String, MapperFrame>, tiles: HashMap<String, MapperFrame>,
default_color: Option<Color>, default_color: Option<Color>,
@@ -88,7 +88,7 @@ pub struct SpriteAtlas {
} }
impl SpriteAtlas { impl SpriteAtlas {
pub fn new(texture: Texture<'static>, mapper: AtlasMapper) -> Self { pub fn new(texture: Texture, mapper: AtlasMapper) -> Self {
Self { Self {
texture, texture,
tiles: mapper.frames, tiles: mapper.frames,
@@ -117,7 +117,7 @@ impl SpriteAtlas {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn texture(&self) -> &Texture<'static> { pub fn texture(&self) -> &Texture {
&self.texture &self.texture
} }

View File

@@ -7,7 +7,7 @@ use pacman::{
}; };
use sdl2::{ use sdl2::{
image::LoadTexture, image::LoadTexture,
render::{Canvas, Texture, TextureCreator}, render::{Canvas, TextureCreator},
video::{Window, WindowContext}, video::{Window, WindowContext},
Sdl, Sdl,
}; };
@@ -31,7 +31,6 @@ pub fn create_atlas(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>) -> S
let atlas_bytes = get_asset_bytes(Asset::AtlasImage).unwrap(); let atlas_bytes = get_asset_bytes(Asset::AtlasImage).unwrap();
let texture = texture_creator.load_texture_bytes(&atlas_bytes).unwrap(); let texture = texture_creator.load_texture_bytes(&atlas_bytes).unwrap();
let texture: Texture<'static> = unsafe { std::mem::transmute(texture) };
let atlas_mapper = AtlasMapper { let atlas_mapper = AtlasMapper {
frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(), frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(),

View File

@@ -3,7 +3,7 @@ use pacman::texture::sprite::{AtlasMapper, AtlasTile, MapperFrame, SpriteAtlas};
use sdl2::pixels::Color; use sdl2::pixels::Color;
use std::collections::HashMap; use std::collections::HashMap;
fn mock_texture() -> sdl2::render::Texture<'static> { fn mock_texture() -> sdl2::render::Texture {
unsafe { std::mem::transmute(0usize) } unsafe { std::mem::transmute(0usize) }
} }