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
+23 -10
View File
@@ -41,12 +41,14 @@ impl Game {
for x in -RENDER_DIST..RENDER_DIST+1 {
for z in -RENDER_DIST..RENDER_DIST+1 {
let pos = Vector3{x, y: 0, z};
if instance.world.load_chunk(pos) {
let chunk = instance.world.get_chunk(&pos).unwrap();
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
println!("Writing chunk to {:?}", pos * world::chunk::SIZE as i32);
count += 1;
}
instance.world.load_chunk_async(pos);
count += 1;
//if instance.world.load_chunk(pos) {
//let chunk = instance.world.get_chunk(&pos).unwrap();
//chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
//println!("Writing chunk to {:?}", pos * world::chunk::SIZE as i32);
//count += 1;
//}
}
}
println!("Preloaded {} chunks in {} ms (about {} voxels)", count, timer.elapsed().as_millis(), count * world::block::SIZE_QB);
@@ -102,14 +104,25 @@ impl Game {
for a in -RENDER_DIST..RENDER_DIST+1 {
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
self.world.load_chunk(pos);
let chunk = self.world.get_chunk(&pos).unwrap();
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
self.world.load_chunk_async(pos);
// if self.world.load_chunk(pos) {
// self.world.get_chunk(&pos).unwrap().dirty = true;
// }
}
}
self.prev_chunk_pos = Some(chunk_pos);
}
// Send dirty chunks to renderer
// TODO: Only send chunks in rendering range
// TODO: Unload chunks above certain radius
self.world.update();
for (pos, chunk) in self.world.all_chunks() {
if chunk.dirty {
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
chunk.dirty = false;
}
}
}
pub fn input(&mut self, event: &Event<()>) {