Skip to content

Commit

Permalink
fix(image cache): 🐛 properly unload textures
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Nov 30, 2023
1 parent ab0607a commit 3501004
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 21 deletions.
14 changes: 4 additions & 10 deletions crates/components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use itertools::Itertools;

#[derive(Debug)]
pub struct MapView {
/// Toggle to display the visible region in-game.
pub visible_display: bool,
Expand Down Expand Up @@ -92,25 +91,20 @@ impl MapView {
let events = map
.events
.iter()
.map(|(id, e)| {
.map(|(id, e)| -> anyhow::Result<_> {
let sprite = luminol_graphics::Event::new(
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
);
)?;
let preview_sprite = luminol_graphics::Event::new(
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
);
let Ok(sprite) = sprite else {
return Err(sprite.unwrap_err());
};
let Ok(preview_sprite) = preview_sprite else {
return Err(preview_sprite.unwrap_err());
};
)?;

Ok(if let Some(sprite) = sprite {
preview_sprite.map(|preview_sprite| (id, (sprite, preview_sprite)))
} else {
Expand Down
2 changes: 0 additions & 2 deletions crates/components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use itertools::Itertools;
use std::sync::Arc;
use std::time::Duration;

#[derive(Debug)]
pub struct Tilepicker {
pub selected_tiles_left: i16,
pub selected_tiles_top: i16,
Expand All @@ -33,7 +32,6 @@ pub struct Tilepicker {
ani_time: Option<f64>,
}

#[derive(Debug)]
struct Resources {
tiles: luminol_graphics::tiles::Tiles,
collision: luminol_graphics::collision::Collision,
Expand Down
2 changes: 1 addition & 1 deletion crates/graphics/src/atlas_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.
use crate::{tiles::Atlas, GraphicsState};

#[derive(Default, Debug)]
#[derive(Default)]
pub struct Cache {
atlases: dashmap::DashMap<usize, Atlas>,
}
Expand Down
1 change: 0 additions & 1 deletion crates/graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::sync::Arc;

use crate::{quad::Quad, sprite::Sprite, tiles::Atlas, viewport::Viewport, GraphicsState};

#[derive(Debug)]
pub struct Event {
sprite: Arc<Sprite>,
viewport: Arc<Viewport>,
Expand Down
2 changes: 0 additions & 2 deletions crates/graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::time::Duration;

use crate::{collision::Collision, tiles::Tiles, viewport::Viewport, GraphicsState, Plane};

#[derive(Debug)]
pub struct Map {
resources: Arc<Resources>,
viewport: Arc<Viewport>,
Expand All @@ -33,7 +32,6 @@ pub struct Map {
pub enabled_layers: Vec<bool>,
}

#[derive(Debug)]
struct Resources {
tiles: Tiles,
panorama: Option<Plane>,
Expand Down
1 change: 0 additions & 1 deletion crates/graphics/src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::{quad::Quad, sprite::Sprite, viewport::Viewport, GraphicsState, Texture};
use std::sync::Arc;

#[derive(Debug)]
pub struct Plane {
sprite: Sprite,
}
Expand Down
1 change: 0 additions & 1 deletion crates/graphics/src/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub(crate) mod graphic;
pub(crate) mod shader;
mod vertices;

#[derive(Debug)]
pub struct Sprite {
pub texture: Arc<Texture>,
pub graphic: graphic::Graphic,
Expand Down
14 changes: 13 additions & 1 deletion crates/graphics/src/texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ pub struct TextureLoader {
render_state: egui_wgpu::RenderState,
}

#[derive(Debug)]
pub struct Texture {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub texture_id: egui::TextureId,

render_state: egui_wgpu::RenderState,
}

impl Drop for Texture {
fn drop(&mut self) {
let mut renderer = self.render_state.renderer.write();
renderer.free_texture(&self.texture_id);
}
}

pub const TEXTURE_LOADER_ID: &str = egui::load::generate_loader_id!(TextureLoader);
Expand Down Expand Up @@ -165,6 +173,8 @@ impl TextureLoader {
texture,
view,
texture_id,

render_state: self.render_state.clone(),
}),
);
}
Expand Down Expand Up @@ -227,6 +237,8 @@ impl TextureLoader {
texture,
view,
texture_id,

render_state: self.render_state.clone(),
});
self.loaded_textures.insert(path, texture.clone());
texture
Expand Down
2 changes: 1 addition & 1 deletion crates/graphics/src/tiles/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub const AUTOTILE_FRAME_WIDTH: u32 = AUTOTILE_FRAME_COLS * TILE_SIZE; // This i
use image::GenericImageView;
use std::sync::Arc;

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Atlas {
pub atlas_texture: Arc<Texture>,
pub autotile_width: u32,
Expand Down
1 change: 0 additions & 1 deletion crates/graphics/src/tiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod instance;
pub(crate) mod opacity;
pub(crate) mod shader;

#[derive(Debug)]
pub struct Tiles {
pub autotiles: Autotiles,
pub atlas: Atlas,
Expand Down

0 comments on commit 3501004

Please sign in to comment.