Skip to content

Commit

Permalink
chunk loading (1/3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakingLead committed May 31, 2024
1 parent 0cb994f commit 210f8c7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 47 deletions.
40 changes: 25 additions & 15 deletions blockworld-client/game/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub const CHUNK_HEIGHT: usize = 256;
pub const CHUNK_BLOCK_NUM: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT;

/// from `net/minecraft/util/math/ChunkPos.java`
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub struct ChunkPos {
pub x: i32,
pub z: i32,
Expand Down Expand Up @@ -38,6 +38,30 @@ pub struct Chunk {
}

impl Chunk {
pub fn new(chunk_x: i32, chunk_z: i32) -> Self {
let mut blocks = Box::new([Block::default(); CHUNK_BLOCK_NUM]);
for x in 0..CHUNK_SIZE as i32 {
for y in 0..3 {
for z in 0..CHUNK_SIZE as i32 {
blocks[Chunk::index(x, y, z)] = match y {
_ => Block { id: 1 },
}
}
}
}
for y in 5..(5 + chunk_x) {
blocks[Chunk::index(7, y, 7)] = Block { id: 2 };
}
for y in 5..(5 + chunk_z) {
blocks[Chunk::index(8, y * 2, 8)] = Block { id: 2 };
blocks[Chunk::index(8, y * 2 + 1, 8)] = Block { id: 2 };
}
Self {
blocks,
pos: ChunkPos::new(chunk_x, chunk_z),
}
}

/// Reference: [https://minecraft.wiki/w/Chunk_format]
///
/// Format: YZX
Expand Down Expand Up @@ -125,23 +149,9 @@ impl Default for Chunk {
}
}
}
for x in 0..CHUNK_SIZE as i32 {
for y in 5..40 {
for z in 0..CHUNK_SIZE as i32 {
if (vec3(x as f32, y as f32, z as f32) - vec3(7.0, 15.0, 7.0)).length() <= 7.0 {
blocks[Chunk::index(x, y, z)] = match y {
_ => Block { id: 2 },
}
}
}
}
}

Self {
blocks,
pos: ChunkPos::new(0, 0),
}
}
}

pub struct ChunkProvider;
22 changes: 20 additions & 2 deletions blockworld-client/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
use std::rc::Rc;

use chunk_provider::ClientChunkProvider;
use world::ClientWorld;

use crate::io::input_helper::InputState;

use self::player_state::PlayerState;

pub mod block;
pub mod chunk;
pub mod chunk_array;
pub mod chunk_provider;
pub mod console_instr;
pub mod player_state;
pub mod register;
pub mod save;
pub mod settings;
pub mod world;

#[derive(Default, Debug)]
pub struct Game {
pub player_state: PlayerState,
pub client_world: Rc<ClientWorld>,
pub chunk_provider: ClientChunkProvider,
}
impl Default for Game {
fn default() -> Self {
let client_world = Rc::new(ClientWorld);
Self {
player_state: Default::default(),
client_world: client_world.clone(),
// ! TEMP
chunk_provider: ClientChunkProvider::new(client_world.clone(), 16),
}
}
}

impl Game {
Expand Down
2 changes: 2 additions & 0 deletions blockworld-client/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(int_roundings)]

use anyhow::*;
use clap::Parser;

Expand Down
28 changes: 12 additions & 16 deletions blockworld-client/render/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
};

use super::camera::{Camera, MatrixData};
use super::render_array::RenderArray;
use super::texture::Texture;
use super::uniform::*;
use super::{
Expand All @@ -50,7 +51,7 @@ pub struct State<'a> {
pub main_pipeline: RegularPipeline,
pub wireframe_pipeline: WireframePipeline,

pub render_chunk: RenderChunk,
pub render_array: RenderArray,

pub texture: Texture,
pub texture_bind_group: wgpu::BindGroup,
Expand Down Expand Up @@ -84,7 +85,6 @@ pub struct State<'a> {

impl<'a> State<'a> {
pub async fn new(event_loop: &EventLoop<()>, boot_args: &BootArgs) -> Result<State<'a>> {
// /-------------------../assets/atlas.png
// Create the window
let mut window_attrs = Window::default_attributes().with_title("Blockworld Indev");
// set screen size based on boot_args
Expand All @@ -101,9 +101,7 @@ impl<'a> State<'a> {
let _player_state: PlayerState = Default::default();

let size = window.inner_size();
// \-------------------

// /-------------------
// Instance is the way to create surface and adapter.
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
Expand Down Expand Up @@ -276,10 +274,8 @@ impl<'a> State<'a> {
},
)?;

let chunk = Chunk::default();
let render_chunk = RenderChunk::new(&device, &chunk, &register_table);

let game = Game::default();
let mut game = Game::default();
let render_array = RenderArray::new(&mut game.chunk_provider, &device, &register_table);
let input_state = InputState::default();

let settings = Settings {
Expand Down Expand Up @@ -324,7 +320,7 @@ impl<'a> State<'a> {

depth_texture,

render_chunk,
render_array,

camera,
matrix_uniform,
Expand Down Expand Up @@ -426,9 +422,9 @@ impl<'a> State<'a> {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
r: 0.0,
g: 1.0,
b: 239.0 / 255.0,
a: 1.0,
}),
store: wgpu::StoreOp::Store,
Expand Down Expand Up @@ -458,8 +454,10 @@ impl<'a> State<'a> {
render_pass.set_bind_group(0, &self.texture_bind_group, &[]);
render_pass.set_bind_group(1, &self.matrix_uniform.bind_group, &[]);

render_pass.set_vertex_buffer(0, self.render_chunk.vertex_buffer.slice(..));
render_pass.draw(0..self.render_chunk.vertex_count, 0..1);
for (i, chunk) in self.render_array.chunks().iter().enumerate() {
render_pass.set_vertex_buffer(0, chunk.vertex_buffer.slice(..));
render_pass.draw(0..chunk.vertex_count, 0..1);
}
self.brush.draw(&mut render_pass);
}

Expand Down Expand Up @@ -491,13 +489,11 @@ impl<'a> Debug for State<'a> {
.field("size", &self.size)
.field("main_pipeline", &self.main_pipeline)
.field("wireframe_pipeline", &self.wireframe_pipeline)
.field("render_chunk", &self.render_chunk)
.field("texture", &self.texture)
.field("texture_bind_group", &self.texture_bind_group)
.field("depth_texture", &self.depth_texture)
.field("camera", &self.camera)
.field("input_state", &self.input_state)
.field("game", &self.game)
.field("fps", &self.fps)
.field("dt_timer", &self.dt_timer)
.field("global_timer", &self.global_timer)
Expand Down
3 changes: 2 additions & 1 deletion blockworld-client/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pub mod camera;
pub mod draw;
pub mod pipeline;
pub mod render_array;
pub mod render_block;
pub mod render_chunk;
pub mod render_chunk_pool;
pub mod texture;
pub mod uniform;
pub mod utils;
pub mod vertex;
pub mod window_init;
pub mod world_renderer;
2 changes: 2 additions & 0 deletions blockworld-client/render/render_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::render_block::*;
use glam::*;
use log::info;
use wgpu::{util::DeviceExt, Device};

use crate::game::{chunk::*, register::RegisterTable};
Expand All @@ -14,6 +15,7 @@ pub struct RenderChunk {

impl RenderChunk {
pub fn new(device: &Device, chunk: &Chunk, register_table: &RegisterTable) -> Self {
info!("New renderchunk in {:?}", chunk.pos);
let mut vertices: Vec<Vertex> = Vec::new();
for x in 0..CHUNK_SIZE as i32 {
for y in 0..CHUNK_HEIGHT as i32 {
Expand Down
13 changes: 0 additions & 13 deletions blockworld-client/render/render_chunk_pool.rs

This file was deleted.

0 comments on commit 210f8c7

Please sign in to comment.