summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs149
1 files changed, 108 insertions, 41 deletions
diff --git a/src/main.rs b/src/main.rs
index 483bfb2..d248a75 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use egui::Id;
use egui_wgpu::{RendererOptions, ScreenDescriptor};
use egui_winit::egui::{self, Context};
use futures::executor;
+use rand::random_range;
use std::{
sync::Arc,
time::{Duration, Instant},
@@ -26,7 +27,7 @@ use winit::{
use crate::{
camera::Camera,
config::WINDOW_TITLE,
- sim::{materials::MaterialId, sim::sim_tick, world::World},
+ sim::{cell::Cell, materials::MaterialId, sim::sim_tick, world::World},
ui::draw_egui,
};
@@ -56,6 +57,11 @@ struct Diagnostics {
fps: f32,
}
+struct RendererChunk {
+ texture: wgpu::Texture,
+ bind_group: wgpu::BindGroup,
+}
+
struct RendererState {
window: Arc<Window>,
surface: wgpu::Surface<'static>,
@@ -69,6 +75,8 @@ struct RendererState {
egui_state: egui_winit::State,
egui_renderer: egui_wgpu::Renderer,
+ // world pixels
+ renderer_chunks: Vec<RendererChunk>,
pixels_pipeline: wgpu::RenderPipeline,
pixels_bind_group: wgpu::BindGroup,
texture: wgpu::Texture,
@@ -168,13 +176,6 @@ impl RendererState {
}],
});
- let pixels_pipeline_layout =
- device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
- label: None,
- immediate_size: 0,
- bind_group_layouts: &[Some(&pixels_bind_group_layout)],
- });
-
let pixels_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &pixels_bind_group_layout,
@@ -197,6 +198,13 @@ impl RendererState {
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
});
+ let pixels_pipeline_layout =
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: None,
+ immediate_size: 0,
+ bind_group_layouts: &[Some(&pixels_bind_group_layout)],
+ });
+
let pixels_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(&pixels_pipeline_layout),
@@ -231,6 +239,67 @@ impl RendererState {
cache: None,
});
+ let mut renderer_chunks: Vec<RendererChunk> = Vec::new();
+
+ // match number of world chunks
+ // TODO refactor so that this implicit
+ for _ in -10..10 {
+ for _ in -10..10 {
+ let texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: None,
+ mip_level_count: 1,
+ sample_count: 1,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
+ format: wgpu_types::TextureFormat::Rgba8UnormSrgb,
+ size: wgpu::Extent3d {
+ width: size.width,
+ height: size.height,
+ depth_or_array_layers: 1,
+ },
+ dimension: wgpu::TextureDimension::D2,
+ view_formats: &[],
+ });
+
+ let bind_group_layout =
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: None,
+ entries: &[wgpu::BindGroupLayoutEntry {
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ binding: 0,
+ count: None,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ }],
+ });
+
+ let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: None,
+ layout: &bind_group_layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&texture.create_view(
+ &wgpu::TextureViewDescriptor {
+ dimension: Some(wgpu::TextureViewDimension::D2),
+ usage: Some(
+ wgpu::TextureUsages::TEXTURE_BINDING
+ | wgpu::TextureUsages::COPY_DST,
+ ),
+ ..wgpu::TextureViewDescriptor::default()
+ },
+ )),
+ }],
+ });
+
+ renderer_chunks.push(RendererChunk {
+ texture,
+ bind_group,
+ })
+ }
+ }
+
RendererState {
window,
surface,
@@ -243,6 +312,7 @@ impl RendererState {
egui_state,
egui_renderer,
+ renderer_chunks: Vec::new(),
texture,
frame_view: vec![0; size.width as usize * size.height as usize * 4],
pixels_pipeline,
@@ -576,11 +646,9 @@ impl ApplicationHandler for App {
}
WindowEvent::CursorMoved { position, .. } => {
self.input.last_mouse_pos_on_screen = Some((position.x, position.y));
- // self.input.last_mouse_pos_on_board = pixels
- // .window_pos_to_pixel((position.x as f32, position.y as f32))
- // .map(|v| camera.screen_position_to_world(v.0 as f64, v.1 as f64))
- // .map(|v| (v.0 as i32, v.1 as i32))
- // .ok();
+ let world_pos = camera.screen_position_to_world(position.x, position.y);
+ self.input.last_mouse_pos_on_board =
+ Some((world_pos.0 as i32, world_pos.1 as i32))
}
WindowEvent::MouseInput { state, button, .. } => {
if button == MouseButton::Left {
@@ -611,35 +679,34 @@ impl ApplicationHandler for App {
camera.handle_camera_input(&self.input, delta_time);
// // --TEST DRAWING--
- // if self.input.is_lmb_pressed
- // && let Some(lm) = self.input.last_mouse_pos_on_board
- // {
- // // start with the bounding box of the drawing brush circle + some margin
- // // clamp the bounding box to the board sie
- // // TODO better way to do this without unwrap?
- // let bb_xl = lm.0 - self.config.brush_radius as i32;
- // let bb_xu = lm.0 + self.config.brush_radius as i32;
- // let bb_yl = lm.1 - self.config.brush_radius as i32;
- // let bb_yu = lm.1 + self.config.brush_radius as i32;
+ if self.input.is_lmb_pressed
+ && let Some(lm) = self.input.last_mouse_pos_on_board
+ {
+ // start with the bounding box of the drawing brush circle + some margin
+ // clamp the bounding box to the board sie
+ let bb_xl = lm.0 - self.config.brush_radius as i32;
+ let bb_xu = lm.0 + self.config.brush_radius as i32;
+ let bb_yl = lm.1 - self.config.brush_radius as i32;
+ let bb_yu = lm.1 + self.config.brush_radius as i32;
- // // for each point, check if the distance is less than the brush size and write the pixel
- // for x in bb_xl..bb_xu {
- // for y in bb_yl..bb_yu {
- // let r = random_range(0.0..1.0);
- // if ((x - lm.0).pow(2) + (y - lm.1).pow(2))
- // < (self.config.brush_radius as i32).pow(2)
- // && r > 0.9
- // {
- // let mut cell = Cell::from_material(self.config.brush_material);
- // cell.flags = (self.sim_seqno as u8) & 0b1;
- // world.set_cell_from_game_position(
- // x, y, cell, // wake the chunk
- // false,
- // );
- // }
- // }
- // }
- // }
+ // for each point, check if the distance is less than the brush size and write the pixel
+ for x in bb_xl..bb_xu {
+ for y in bb_yl..bb_yu {
+ let r = random_range(0.0..1.0);
+ if ((x - lm.0).pow(2) + (y - lm.1).pow(2))
+ < (self.config.brush_radius as i32).pow(2)
+ && r > 0.9
+ {
+ let mut cell = Cell::from_material(self.config.brush_material);
+ cell.flags = (self.sim_seqno as u8) & 0b1;
+ world.set_cell_from_game_position(
+ x, y, cell, // wake the chunk
+ false,
+ );
+ }
+ }
+ }
+ }
// TODO check if we need to run another sim tick given the sim speed + delta_time
// SIM logic