Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkBlock committed May 31, 2024
2 parents e632cdf + 97b53bb commit 039b4c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 72 deletions.
4 changes: 2 additions & 2 deletions blockworld-client/io/input_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;

use winit::{
event::{ElementState, KeyEvent},
keyboard::Key,
keyboard::{Key, NamedKey},
};

/// Tracker for the pressing keys
Expand All @@ -16,7 +16,7 @@ impl InputState {
self.pressing_keys.contains(&key)
}

pub fn handle_event(&mut self, event: &KeyEvent) {
pub fn handle_event<'a>(&mut self, event: &'a KeyEvent) {
let key = &event.logical_key;
match event.state {
ElementState::Pressed => {
Expand Down
112 changes: 44 additions & 68 deletions blockworld-client/render/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use crate::{
BootArgs,
};

use super::render_chunk::RenderChunk;
use super::{
pipeline::{RegularPipeline, WireframePipeline},
render_chunk::RenderChunk,
};

/// state contains all things the game needs
pub struct State<'a> {
Expand All @@ -46,7 +49,8 @@ pub struct State<'a> {
pub queue: wgpu::Queue,
pub config: wgpu::SurfaceConfiguration,
pub size: winit::dpi::PhysicalSize<u32>,
pub render_pipeline: wgpu::RenderPipeline,
pub main_pipeline: RegularPipeline,
pub wireframe_pipeline: WireframePipeline,

pub render_chunk: RenderChunk,

Expand Down Expand Up @@ -79,6 +83,9 @@ pub struct State<'a> {
pub settings: Settings<'a>,

pub register_table: RegisterTable,

// Debug
pub debug_mode: bool,
}

impl<'a> State<'a> {
Expand Down Expand Up @@ -129,7 +136,7 @@ impl<'a> State<'a> {
.request_device(
&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::empty(),
required_features: wgpu::Features::POLYGON_MODE_LINE,
required_limits: wgpu::Limits::default(),
},
None,
Expand Down Expand Up @@ -193,7 +200,7 @@ impl<'a> State<'a> {
binding: 30,
resource: matrix_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
label: Some("Blockworld Prespective Matrix Bind Group"),
});
// \-------------------

Expand Down Expand Up @@ -250,58 +257,23 @@ impl<'a> State<'a> {
// \-------------------

let shader = device.create_shader_module(include_wgsl!("shaders/default_shader.wgsl"));
let wireframe_shader =
device.create_shader_module(include_wgsl!("shaders/debug_shader.wgsl"));

let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &matrix_bind_group_layout],
push_constant_ranges: &[],
});
let main_pipeline = RegularPipeline::new(
&device,
&[&texture_bind_group_layout, &matrix_bind_group_layout],
&shader,
&config,
);

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Vertex::desc()],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
format: config.format,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
});
let wireframe_pipeline = WireframePipeline::new(
&device,
&[&texture_bind_group_layout, &matrix_bind_group_layout],
&wireframe_shader,
&config,
);

dbg!("Help me!");
// -------------------
// | Game Initialize |
// -------------------
Expand Down Expand Up @@ -359,7 +331,6 @@ impl<'a> State<'a> {
.with_screen_position((50.0, config.height as f32 * 0.5))
.to_owned();

dbg!("Help me!");
Ok(Self {
window,

Expand All @@ -368,7 +339,8 @@ impl<'a> State<'a> {
queue,
config,
size,
render_pipeline,
main_pipeline,
wireframe_pipeline,

texture,
texture_bind_group,
Expand All @@ -394,6 +366,7 @@ impl<'a> State<'a> {
game,

register_table,
debug_mode: false,
})
}

Expand Down Expand Up @@ -442,6 +415,7 @@ impl<'a> State<'a> {

// Camera Update
self.camera.update(&self.game.player_state);

self.matrix_uniform.update_matrix(&self.camera);
self.queue.write_buffer(
&self.matrix_buffer,
Expand All @@ -460,21 +434,15 @@ impl<'a> State<'a> {
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
label: Some("Blockworld Render Encoder"),
});

// match self
// .brush
// .queue(&self.device, &self.queue, vec![self.fps_text_section])
// {
// Ok(_) => (),
// Err(err) => {
// panic!("{err}");
// }
// };
// self.brush
// .queue(&self.device, &self.queue, vec![&self.fps_text_section])
// .unwrap();
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
label: Some("Blockworld Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
Expand All @@ -499,14 +467,22 @@ impl<'a> State<'a> {
timestamp_writes: None,
occlusion_query_set: None,
});
render_pass.set_pipeline(&self.render_pipeline);

// check debug mode
if self.debug_mode {
// render with wireframe
render_pass.set_pipeline(&self.wireframe_pipeline.pipeline);
} else {
// render with texture
render_pass.set_pipeline(&self.main_pipeline.pipeline);
}

render_pass.set_bind_group(0, &self.texture_bind_group, &[]);
render_pass.set_bind_group(1, &self.matrix_bind_group, &[]);

render_pass.set_vertex_buffer(0, self.render_chunk.vertex_buffer.slice(..));
self.brush.draw(&mut render_pass);
render_pass.draw(0..self.render_chunk.vertex_count, 0..1);
self.brush.draw(&mut render_pass);
}

self.queue.submit(std::iter::once(encoder.finish()));
Expand Down
2 changes: 2 additions & 0 deletions blockworld-client/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod camera;
pub mod draw;
pub mod pipeline;
pub mod render_block;
pub mod render_chunk;
pub mod render_chunk_pool;
pub mod texture;
pub mod uniform_utils;
pub mod vertex;
pub mod window_init;
11 changes: 9 additions & 2 deletions blockworld-client/render/window_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use anyhow::*;
use clap::Parser;
use log::*;
use winit::application::ApplicationHandler;
use winit::event::{DeviceEvent, WindowEvent};
use winit::event::{DeviceEvent, ElementState, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::KeyCode;
use winit::keyboard::{Key, KeyCode, NamedKey};
use winit::window::WindowId;

impl<'a> ApplicationHandler for State<'a> {
Expand Down Expand Up @@ -65,6 +65,13 @@ impl<'a> ApplicationHandler for State<'a> {
event_loop.exit();
}
self.input_state.handle_event(&event);
let key = event.logical_key;

// ! NOT IDEAL
// ! FIX LATER
if key == Key::Named(NamedKey::F1) && event.state == ElementState::Released {
self.debug_mode = !self.debug_mode;
}
}
_ => (),
}
Expand Down

0 comments on commit 039b4c9

Please sign in to comment.