Pausing rendering, warnings cleanup, more statistics

This commit is contained in:
Piotrek
2021-05-10 15:36:11 +02:00
parent 4eaa0fab56
commit a09f446ecd
10 changed files with 84 additions and 79 deletions
+10 -3
View File
@@ -3,7 +3,7 @@ mod player;
pub mod world;
use std::time::Instant;
use crate::renderer::content_view::ContentView;
use cgmath::{SquareMatrix, Point3, InnerSpace};
use cgmath::Point3;
use winit::event::Event;
use crate::renderer::{camera::CameraTransform};
use player::Player;
@@ -16,6 +16,7 @@ pub enum GameState {
}
pub struct Game {
state: GameState,
player: Player,
world: World
}
@@ -23,6 +24,7 @@ pub struct Game {
impl Game {
pub fn new(content: &mut dyn ContentView) -> Self {
let mut app = Self {
state: GameState::Paused,
player: Player::new(),
world: World::new("default".to_string())
};
@@ -62,7 +64,7 @@ impl Game {
self.player.camera_controller.handle_input(event);
}
pub fn update_state(&mut self, requested_state: GameState) -> GameState {
pub fn update_state(&mut self, requested_state: GameState) -> &GameState {
match requested_state {
GameState::Paused => {
self.player.camera_controller.set_enabled(false);
@@ -73,7 +75,12 @@ impl Game {
};
// Currently we are not preventing any state changes, just return requested state
requested_state
self.state = requested_state;
&self.state
}
pub fn get_state(&self) -> &GameState {
&self.state
}
pub fn update_camera(&mut self, delta: f32, camera_transform: &mut dyn CameraTransform) {
-3
View File
@@ -1,7 +1,4 @@
use cgmath::Point3;
use std::iter::FromIterator;
use std::io::Write;
use flate2::{write::DeflateEncoder, Compression};
pub const SIZE: usize = 32;
pub const SIZE_QB: usize = SIZE*SIZE*SIZE;
+9 -15
View File
@@ -1,20 +1,20 @@
use std::time::Instant;
use flate2::Compression;
use flate2::write::DeflateEncoder;
use flate2::read::DeflateDecoder;
use byteorder::LittleEndian;
use byteorder::ByteOrder;
use cgmath::Point3;
use std::io::Write;
use std::convert::TryInto;
use lzzzz::lz4;
use std::io::Read;
use crate::game::world::{WorldBlock, block};
pub const SIZE: usize = 32;
const SIZE_SQ: usize = SIZE*SIZE;
const SIZE_QB: usize = SIZE*SIZE*SIZE;
// 1 chunk times on i7-5930K
// alg | size | compr | decom
// raw 32.89kB 0.06s 1.19s
// deflate fast 1.23kB 1.22s 5.46s
// lz4 1.6kB 0.07s 1.23s
pub struct WorldChunk {
nodes: Box<[u32]>,
blocks: Vec<WorldBlock>
@@ -30,15 +30,15 @@ impl WorldChunk {
pub fn from_bytes(bytes: Vec<u8>) -> Self {
let mut instance = Self::new();
let (header_bytes, bytes) = bytes.split_at(128);
let (node_bytes, block_bytes_compressed) = bytes.split_at((SIZE_QB*4) as usize);
// Header
let (header_bytes, bytes) = bytes.split_at(128);
let file_ver = header_bytes[0];
let num_blocks = LittleEndian::read_u32(&header_bytes[1..5]) as usize;
assert_eq!(file_ver, 1);
// Nodes
let (node_bytes, block_bytes_compressed) = bytes.split_at((SIZE_QB*4) as usize);
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]]);
@@ -79,12 +79,6 @@ impl WorldChunk {
}
lz4::compress_to_vec(&buf, &mut bytes, lz4::ACC_LEVEL_DEFAULT).unwrap();
// alg | size | compr | decom
// raw 32.89kB 0.06s 1.19s
// deflate fast 1.23kB 1.22s 5.46s
// lz4 1.6kB 0.07s 1.23s
bytes
}
+1 -1
View File
@@ -44,7 +44,7 @@ impl WorldGen {
let noise = Perlin::new();
// Generate height map
let (map, max) = WorldGen::gen_height_map(block_pos.x as usize, block_pos.z as usize, &mut rng, &noise);
let (map, _max) = WorldGen::gen_height_map(block_pos.x as usize, block_pos.z as usize, &mut rng, &noise);
// Fill
for x in 0..block::SIZE {
+4 -6
View File
@@ -1,19 +1,17 @@
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;
use std::io::Write;
use std::io::Read;
use std::fs;
use cgmath::{Point3, InnerSpace};
use cgmath::Point3;
use std::collections::HashMap;
use generator::WorldGen;
use chunk::WorldChunk;
use block::WorldBlock;
use yaml_rust::{Yaml, YamlLoader, YamlEmitter};
use yaml_rust::{Yaml, YamlEmitter};
pub struct World {
@@ -101,7 +99,7 @@ impl World {
file.read_to_end(&mut bytes).expect("Failed to read file");
let chunk = WorldChunk::from_bytes(bytes);
println!("loaded {:?}", pos);
println!("Load: Chunk {}, {}, {}", pos.x, pos.y, pos.z);
self.chunks.insert(pos, chunk);
count += 1;
}
@@ -117,7 +115,7 @@ impl World {
// Create chunk if does not exist
if let None = self.chunks.get(&ch_pos) {
self.chunks.insert(ch_pos.clone(), WorldChunk::new());
println!("Alloc chunk [{}, {}, {}]", ch_pos.x, ch_pos.y, ch_pos.z);
println!("Alloc: Chunk [{}, {}, {}]", ch_pos.x, ch_pos.y, ch_pos.z);
}
// Generate block for chunk
let chunk = self.chunks.get_mut(&ch_pos).unwrap();