From 021d79d568d44696d9c7838f6b121e5b0f5f0692 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 29 Jan 2023 01:28:00 +0100 Subject: [PATCH] fix a memory leak and most warnings --- src/block_placement.rs | 1 - src/rendering/selection_box.rs | 1 - src/world.rs | 1 + src/world/loading.rs | 11 ++++++++--- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/block_placement.rs b/src/block_placement.rs index 89cdf99..e9adce0 100644 --- a/src/block_placement.rs +++ b/src/block_placement.rs @@ -1,4 +1,3 @@ -use glam::Vec3; use shipyard::{UniqueViewMut, UniqueView, View, IntoIter}; use crate::{ player::MainPlayer, diff --git a/src/rendering/selection_box.rs b/src/rendering/selection_box.rs index 7f0ad56..21858d5 100644 --- a/src/rendering/selection_box.rs +++ b/src/rendering/selection_box.rs @@ -51,7 +51,6 @@ pub fn render_selection_box( lookat: View, camera: View, mut target: NonSendSync>, - display: NonSendSync>, program: NonSendSync>, buffers: NonSendSync>, ) { diff --git a/src/world.rs b/src/world.rs index ff75456..2e5bdf2 100644 --- a/src/world.rs +++ b/src/world.rs @@ -85,6 +85,7 @@ impl ChunkMeshStorage { } pub fn insert(&mut self, mesh: ChunkMesh) -> usize { let index = self.index; + debug_assert!(self.meshes.get(&index).is_none()); self.meshes.insert_unique_unchecked(index, mesh); self.index += 1; index diff --git a/src/world/loading.rs b/src/world/loading.rs index a686316..d360777 100644 --- a/src/world/loading.rs +++ b/src/world/loading.rs @@ -210,11 +210,16 @@ fn process_completed_tasks( //apply the mesh let vertex_buffer = VertexBuffer::new(&renderer.display, &vertices).unwrap(); let index_buffer = IndexBuffer::new(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap(); - let mesh_index = meshes.insert(ChunkMesh { + let mesh = ChunkMesh { vertex_buffer, index_buffer, - }); - chunk.mesh_index = Some(mesh_index); + }; + if let Some(index) = chunk.mesh_index { + meshes.update(index, mesh).expect("Mesh update failed"); + } else { + let mesh_index = meshes.insert(mesh); + chunk.mesh_index = Some(mesh_index); + } //update chunk state chunk.current_state = CurrentChunkState::Rendered;