This commit is contained in:
Piotrek
2021-05-16 17:19:23 +02:00
parent 76e952995d
commit 67bb4fddd0
4 changed files with 37 additions and 81 deletions
+17 -17
View File
@@ -80,28 +80,28 @@ impl Game {
}
// Chunk position
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
// if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
// renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
// let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
// if dir.x != 0 || dir.y != 0 || dir.z != 0 {
// Shift world
renderer.shift(dir * world::chunk::SIZE as i32);
// // Shift world
// renderer.shift(dir * world::chunk::SIZE as i32);
// Load chunks at the edge
let center = chunk_pos.clone() + RENDER_DIST*dir;
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
println!("Load at {:?}", center);
// // Load chunks at the edge
// let center = chunk_pos.clone() + RENDER_DIST*dir;
// let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
// println!("Load at {:?}", center);
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);
}
}
self.prev_chunk_pos = Some(chunk_pos);
}
// 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);
// }
// }
// self.prev_chunk_pos = Some(chunk_pos);
// }
// Send dirty chunks to renderer
// TODO: Unload chunks above certain radius
+8 -27
View File
@@ -56,35 +56,16 @@ impl WorldChunk {
// Blocks
let mut block_bytes = vec![0_u8; num_blocks*block::SIZE_QB];
lz4::decompress(&block_bytes_compressed, &mut block_bytes[0..num_blocks*block::SIZE_QB]).unwrap();
let block_bytes = Arc::new(block_bytes);
// Spawn workers
let (tx, rx) = mpsc::channel();
let per_worker = num_blocks / NUM_WORKERS;
let mut workers = vec![];
for t in 0..NUM_WORKERS {
let by = block_bytes.clone();
let tx = tx.clone();
// Each worker will process their part
workers.push(thread::spawn(move || {
for i in t*per_worker..(t+1)*per_worker {
let mut block = WorldBlock::new();
let idx = i * block::SIZE_QB;
block.materials.clone_from_slice(&by[idx..idx+block::SIZE_QB]);
tx.send((i, block)).unwrap();
}
}));
if lz4::decompress(&block_bytes_compressed, &mut block_bytes[0..num_blocks*block::SIZE_QB]).is_err() {
}
// Wait for workers to finish.
for handle in workers { handle.join().unwrap(); }
let mut blocks: Vec<(usize, WorldBlock)> = rx.try_iter().collect();
// Sort and add result blocks to chunk
blocks.sort_by(|a, b| (*a).0.cmp(&(*b).0));
for b in blocks { instance.blocks.push(b.1); }
for i in 0..num_blocks {
let mut block = WorldBlock::new();
let idx = i * block::SIZE_QB;
block.materials.clone_from_slice(&block_bytes[idx..idx+block::SIZE_QB]);
instance.blocks.push(block);
}
instance
}
+1 -1
View File
@@ -61,7 +61,7 @@ impl ChunkLoader {
* Schedule loading chunks
*/
pub fn load(&mut self, pos: ChunkPos) {
self.queue.send(pos);
self.queue.send(pos).unwrap();
}
/*