slightly faster copying blocks data
This commit is contained in:
+17
-25
@@ -28,34 +28,26 @@ impl App {
|
||||
};
|
||||
|
||||
|
||||
let timer = Instant::now();
|
||||
for x in 0..16 {
|
||||
for z in 0..16 {
|
||||
let pos = &Point3{x,y:0,z};
|
||||
let block = app.world.gen_block(pos);
|
||||
content.write(pos, block);
|
||||
}
|
||||
}
|
||||
println!("Generated in {}", timer.elapsed().as_secs_f32());
|
||||
|
||||
let timer = Instant::now();
|
||||
app.world.save();
|
||||
println!("Saved in {}", timer.elapsed().as_secs_f32());
|
||||
// let timer = Instant::now();
|
||||
// for x in 0..16 {
|
||||
// for z in 0..16 {
|
||||
// let pos = &Point3{x,y:0,z};
|
||||
// let block = app.world.gen_block(pos);
|
||||
// content.write(pos, block);
|
||||
// }
|
||||
// }
|
||||
// println!("Generated in {}", timer.elapsed().as_secs_f32());
|
||||
|
||||
// let timer = Instant::now();
|
||||
// app.world.load();
|
||||
// for (pos, block) in app.world.all_blocks() {
|
||||
// content.write(&pos, block);
|
||||
// }
|
||||
// println!("Loaded in {}", timer.elapsed().as_secs_f32());
|
||||
// app.world.save();
|
||||
// println!("Saved in {}", timer.elapsed().as_secs_f32());
|
||||
|
||||
// let pos = &Point3{x:1,y:0,z:0};
|
||||
// let block = app.world.gen_block(pos);
|
||||
// content.write(pos, block);
|
||||
|
||||
// let pos = &Point3{x:0,y:0,z:1};
|
||||
// let block = app.world.gen_block(pos);
|
||||
// content.write(pos, block);
|
||||
let timer = Instant::now();
|
||||
app.world.load();
|
||||
for (pos, block) in app.world.all_blocks() {
|
||||
content.write(&pos, block);
|
||||
}
|
||||
println!("Loaded in {}", timer.elapsed().as_secs_f32());
|
||||
|
||||
println!("App initialized");
|
||||
app
|
||||
|
||||
+29
-30
@@ -1,3 +1,4 @@
|
||||
use std::time::Instant;
|
||||
use flate2::Compression;
|
||||
use flate2::write::DeflateEncoder;
|
||||
use flate2::read::DeflateDecoder;
|
||||
@@ -26,6 +27,34 @@ impl WorldChunk {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: Vec<u8>) -> Self {
|
||||
let mut instance = Self::new();
|
||||
let (node_bytes, block_bytes) = bytes.split_at((SIZE_QB*4) as usize);
|
||||
|
||||
// Nodes
|
||||
let mut num_blocks = 0;
|
||||
for i in 0..SIZE_QB {
|
||||
let idx = (i*4) as usize;
|
||||
let val = u32::from_le_bytes([node_bytes[idx], node_bytes[idx+1], node_bytes[idx+2], node_bytes[idx+3]]);
|
||||
instance.nodes[i as usize] = val;
|
||||
if val > 0 { num_blocks+=1; }
|
||||
}
|
||||
|
||||
// Blocks
|
||||
let mut decoder = DeflateDecoder::new(block_bytes);
|
||||
let mut block_bytes = Vec::with_capacity(SIZE_QB);
|
||||
decoder.read_to_end(&mut block_bytes).expect("Failed to decode");
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub fn all_blocks(&self) -> Vec<(Point3<u32>, &WorldBlock)> {
|
||||
self.nodes.iter().enumerate()
|
||||
.filter(|(_, n)| **n != 0)
|
||||
@@ -76,34 +105,4 @@ impl WorldChunk {
|
||||
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: Vec<u8>) -> Self {
|
||||
let mut instance = Self::new();
|
||||
let (node_bytes, block_bytes) = bytes.split_at((SIZE_QB*4) as usize);
|
||||
|
||||
// Nodes
|
||||
let mut block_num = 0;
|
||||
for i in 0..SIZE_QB {
|
||||
let idx = (i*4) as usize;
|
||||
let val = u32::from_le_bytes([node_bytes[idx], node_bytes[idx+1], node_bytes[idx+2], node_bytes[idx+3]]);
|
||||
instance.nodes[i as usize] = val;
|
||||
if val > 0 { block_num+=1; }
|
||||
}
|
||||
|
||||
// Blocks
|
||||
let mut decoder = DeflateDecoder::new(block_bytes);
|
||||
let mut block_bytes = Vec::new();
|
||||
decoder.read_to_end(&mut block_bytes).expect("Failed to decode");
|
||||
|
||||
for i in 0..block_num {
|
||||
let mut block = WorldBlock::new();
|
||||
let idx = i * block::SIZE_QB;
|
||||
for j in 0..block::SIZE_QB {
|
||||
block.materials[j] = block_bytes[idx+j];
|
||||
}
|
||||
instance.blocks.push(block);
|
||||
}
|
||||
|
||||
instance
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod generator;
|
||||
pub mod chunk;
|
||||
pub mod block;
|
||||
use std::time::Instant;
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::path::Path;
|
||||
|
||||
Reference in New Issue
Block a user