diff --git a/kubi/src/world/loading.rs b/kubi/src/world/loading.rs index 7320f3b..a248857 100644 --- a/kubi/src/world/loading.rs +++ b/kubi/src/world/loading.rs @@ -170,61 +170,69 @@ fn process_completed_tasks( mut meshes: NonSendSync>, renderer: NonSendSync> ) { - for _ in 0..MAX_CHUNK_OPS { - if let Some(res) = task_manager.receive() { - match res { - ChunkTaskResponse::LoadedChunk { position, chunk_data } => { - //check if chunk exists - let Some(chunk) = world.chunks.get_mut(&position) else { - log::warn!("blocks data discarded: chunk doesn't exist"); - return - }; + let mut ops: usize = 0; + while let Some(res) = task_manager.receive() { + match res { + ChunkTaskResponse::LoadedChunk { position, chunk_data } => { + //check if chunk exists + let Some(chunk) = world.chunks.get_mut(&position) else { + log::warn!("blocks data discarded: chunk doesn't exist"); + return + }; - //check if chunk still wants it - if !matches!(chunk.desired_state, DesiredChunkState::Loaded | DesiredChunkState::Rendered) { - log::warn!("block data discarded: state undesirable: {:?}", chunk.desired_state); - return - } - - //set the block data - chunk.block_data = Some(ChunkData { - blocks: chunk_data - }); - - //update chunk state - chunk.current_state = CurrentChunkState::Loaded; - }, - ChunkTaskResponse::GeneratedMesh { position, vertices, indexes } => { - //check if chunk exists - let Some(chunk) = world.chunks.get_mut(&position) else { - log::warn!("mesh discarded: chunk doesn't exist"); - return - }; - - //check if chunk still wants it - if chunk.desired_state != DesiredChunkState::Rendered { - log::warn!("mesh discarded: state undesirable: {:?}", chunk.desired_state); - return - } - - //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 = ChunkMesh { - vertex_buffer, - index_buffer, - }; - 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; + //check if chunk still wants it + if !matches!(chunk.desired_state, DesiredChunkState::Loaded | DesiredChunkState::Rendered) { + log::warn!("block data discarded: state undesirable: {:?}", chunk.desired_state); + return } + + //set the block data + chunk.block_data = Some(ChunkData { + blocks: chunk_data + }); + + //update chunk state + chunk.current_state = CurrentChunkState::Loaded; + + //increase ops counter + ops += 1; + }, + ChunkTaskResponse::GeneratedMesh { position, vertices, indexes } => { + //check if chunk exists + let Some(chunk) = world.chunks.get_mut(&position) else { + log::warn!("mesh discarded: chunk doesn't exist"); + return + }; + + //check if chunk still wants it + if chunk.desired_state != DesiredChunkState::Rendered { + log::warn!("mesh discarded: state undesirable: {:?}", chunk.desired_state); + return + } + + //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 = ChunkMesh { + vertex_buffer, + index_buffer, + }; + 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; + + //increase ops counter + ops += 1; } } + if ops >= MAX_CHUNK_OPS { + break + } } }