Loading and generating chunks in a separate threads

This commit is contained in:
Piotrek
2021-05-15 20:48:59 +02:00
parent e6a8712de0
commit f8760e876c
3 changed files with 98 additions and 24 deletions
+5 -12
View File
@@ -24,14 +24,16 @@ const NUM_WORKERS: usize = 8;
pub struct WorldChunk {
nodes: Box<[u32]>,
blocks: Vec<WorldBlock>
blocks: Vec<WorldBlock>,
pub dirty: bool // true by default, set to false when uploaded to renderer. should be set to true when unloaded (TODO)
}
impl WorldChunk {
pub fn new() -> Self {
Self {
nodes: vec![0_u32; SIZE_QB].into_boxed_slice(),
blocks: Vec::new()
blocks: Vec::new(),
dirty: true
}
}
@@ -110,7 +112,6 @@ impl WorldChunk {
bytes
}
#[allow(unused)]
pub fn all_blocks(&self) -> Vec<(Vector3<i32>, &WorldBlock)> {
self.nodes.iter().enumerate()
.filter(|(_, n)| **n != 0)
@@ -133,7 +134,7 @@ impl WorldChunk {
// Get block index
let index = (block_pos.x + SIZE as i32 * (block_pos.y + SIZE as i32 * block_pos.z)) as usize;
let mut value = self.nodes[index];
// Allocate new block
// Allocate new block if needed
if value == 0 {
value = (self.blocks.len() + 1) as u32;
self.nodes[index] = value;
@@ -143,14 +144,6 @@ impl WorldChunk {
&mut self.blocks[(value-1) as usize]
}
pub fn get_block(&self, block_pos: &Vector3<i32>) -> &WorldBlock {
// Get block index
let index = (block_pos.x + SIZE as i32 * (block_pos.y + SIZE as i32 * block_pos.z)) as usize;
let value = self.nodes[index];
// Return block reference
&self.blocks[(value-1) as usize]
}
pub fn write_to(&self, renderer: &mut dyn RendererView, offset: Vector3<i32>) {
for (blpos, block) in self.all_blocks() {
renderer.write(&(offset + blpos), block);