From 148c32ea0541ac54404ffa2b0127f6866330f212 Mon Sep 17 00:00:00 2001 From: hakolao Date: Fri, 14 Jan 2022 15:27:19 +0200 Subject: [PATCH] Renames, docs & comments --- corrode/src/renderer/render_pass/deferred.rs | 1 + .../renderer/render_pass/place_over_frame.rs | 1 + corrode/src/renderer/vertices.rs | 8 ----- sandbox/src/app.rs | 29 +++++++++++++++---- sandbox/src/main.rs | 4 +-- sandbox/src/object/deformation_utils.rs | 1 + sandbox/src/sim/simulation.rs | 12 ++++---- sandbox/src/sim/simulation_chunk_manager.rs | 3 ++ 8 files changed, 37 insertions(+), 22 deletions(-) diff --git a/corrode/src/renderer/render_pass/deferred.rs b/corrode/src/renderer/render_pass/deferred.rs index 0b07c2e..a6cceb6 100644 --- a/corrode/src/renderer/render_pass/deferred.rs +++ b/corrode/src/renderer/render_pass/deferred.rs @@ -35,6 +35,7 @@ pub struct Pipelines { } /// System that contains the necessary facilities for rendering a single frame. +/// This is a stripped down version of https://github.com/vulkano-rs/vulkano/blob/master/examples/src/bin/deferred/main.rs pub struct RenderPassDeferred { gfx_queue: Arc, render_pass: Arc, diff --git a/corrode/src/renderer/render_pass/place_over_frame.rs b/corrode/src/renderer/render_pass/place_over_frame.rs index 766d6ec..26f353f 100644 --- a/corrode/src/renderer/render_pass/place_over_frame.rs +++ b/corrode/src/renderer/render_pass/place_over_frame.rs @@ -12,6 +12,7 @@ use vulkano::{ use crate::renderer::{pipelines::FullFrameImagePipeline, DeviceImageView, FinalImageView}; +/// A simple render pass that places an image filling the whole frame pub struct RenderPassPlaceOverFrame { gfx_queue: Arc, render_pass: Arc, diff --git a/corrode/src/renderer/vertices.rs b/corrode/src/renderer/vertices.rs index 70cd19e..29d14e6 100644 --- a/corrode/src/renderer/vertices.rs +++ b/corrode/src/renderer/vertices.rs @@ -1,13 +1,5 @@ use cgmath::Vector2; -/// A B C D -pub const VERTICES_PER_QUAD: usize = 4; -/// 0, 2, 1, 0, 3, 2 -pub const INDICES_PER_QUAD: usize = 6; - -/// 0, 1, 1, 2, 2, 3, 3, 4 (Line list) -pub const VERTICES_PER_LINE: usize = 2; - /// Vertex for textured quads #[repr(C)] #[derive(Default, Debug, Clone, Copy)] diff --git a/sandbox/src/app.rs b/sandbox/src/app.rs index 7d60419..85cb34a 100644 --- a/sandbox/src/app.rs +++ b/sandbox/src/app.rs @@ -31,24 +31,27 @@ pub enum InputAction { ToggleFullScreen, } -pub struct App { +pub struct SandboxApp { + // Main structs simulation: Option, editor: Editor, gui_state: GuiState, settings: AppSettings, + // Bools is_running_simulation: bool, is_step: bool, is_debug: bool, time_since_last_step: f64, time_since_last_perf: f64, + // Performance metrics simulation_timer: PerformanceTimer, render_timer: PerformanceTimer, frame_timer: PerformanceTimer, } -impl App { - pub fn new() -> Result { - Ok(App { +impl SandboxApp { + pub fn new() -> Result { + Ok(SandboxApp { simulation: None, editor: Editor::new()?, gui_state: GuiState::new(), @@ -84,6 +87,7 @@ impl App { log_world_performance(self.simulation.as_ref().unwrap()); } + /// Step the simulation pub fn step(&mut self, api: &mut EngineApi) -> Result<()> { self.simulation_timer.start(); let canvas_mouse_state = CanvasMouseState::new(&api.main_camera, &api.inputs[0]); @@ -97,39 +101,47 @@ impl App { } } -impl Engine for App { +impl Engine for SandboxApp { fn start( &mut self, _event_loop: &EventLoop, api: &mut EngineApi, ) -> Result<()> { + // Zoom to desired level api.main_camera.zoom_to_fit_canvas(WORLD_UNIT_SIZE); + // Read matter definitions let matter_definitions = if let Some(defs) = read_matter_definitions_file() { defs } else { default_matter_definitions() }; validate_matter_definitions(&matter_definitions); + // Create simulator self.simulation = Some(Simulation::new( api.renderer.compute_queue(), matter_definitions, api.renderer.image_format(), )?); + // Register gui images (for editor windows in gui) self.editor .register_gui_images(api, self.simulation.as_ref().unwrap()); + // Update settings based on read information from renderer self.settings .update_based_on_device_info_and_env(&api.renderer); + // Toggle fullscreen api.renderer.toggle_fullscreen(); Ok(()) } fn update(&mut self, api: &mut EngineApi) -> Result<()> { + // Update editor & handle inputs there self.editor.update( api, self.simulation.as_mut().unwrap(), &mut self.is_running_simulation, &mut self.is_step, )?; + // Step if desired if self.should_step() { if self.is_running_simulation { self.step(api)?; @@ -174,7 +186,9 @@ impl Engine for App { while let Some(pass) = frame.next_pass()? { after_future = match pass { Pass::Deferred(mut dp) => { + // Render canvas first draw_canvas(simulation, &mut dp)?; + // Debug renders if self.is_debug { draw_contours(ecs_world, physics_world, simulation, &mut dp)?; draw_grid(simulation, &mut dp, [0.5; 4])?; @@ -185,6 +199,7 @@ impl Engine for App { ])?; } } + // Render line from dragged object if let Some((obj_id, _)) = self.editor.dragger.dragged_object { ecs_world .query_one::<(&Position, &Angle)>(obj_id) @@ -200,6 +215,7 @@ impl Engine for App { }); } + // Render circle when painting if self.editor.mode == EditorMode::Paint || self.editor.mode == EditorMode::ObjectPaint { @@ -220,6 +236,7 @@ impl Engine for App { dp.draw_circle(pos, radius, color_f32)?; } + // Draw painted object image if self.editor.mode == EditorMode::ObjectPaint && self.editor.draw_state.started() { @@ -237,7 +254,7 @@ impl Engine for App { } fn gui_content(&mut self, api: &mut EngineApi) -> Result<()> { - let App { + let SandboxApp { simulation: simulator, gui_state, is_running_simulation, diff --git a/sandbox/src/main.rs b/sandbox/src/main.rs index de0c66b..dda9347 100644 --- a/sandbox/src/main.rs +++ b/sandbox/src/main.rs @@ -33,7 +33,7 @@ use corrode::{ use simplelog::LevelFilter; use winit::event::VirtualKeyCode; -use crate::app::{App, InputAction}; +use crate::app::{InputAction, SandboxApp}; /// This is an example for using doc comment attributes /// Canvas plane scale (1.0 means our world is between -1.0 and 1.0) @@ -90,7 +90,7 @@ fn main() -> Result<()> { initialize_logger(LevelFilter::Info)?; Corrode::run( - App::new()?, + SandboxApp::new()?, EngineOptions { render_options: RenderOptions { v_sync: false, diff --git a/sandbox/src/object/deformation_utils.rs b/sandbox/src/object/deformation_utils.rs index be9d226..a96f6bc 100644 --- a/sandbox/src/object/deformation_utils.rs +++ b/sandbox/src/object/deformation_utils.rs @@ -53,6 +53,7 @@ fn depth_first_label_mark( } /// Go through inputted bitmap & extract new bitmaps of connected pixels (components) +/// See: https://en.wikipedia.org/wiki/Connected-component_labeling pub fn extract_connected_components_from_bitmap( bitmap: &[f64], width: u32, diff --git a/sandbox/src/sim/simulation.rs b/sandbox/src/sim/simulation.rs index 31feaa6..17473fc 100644 --- a/sandbox/src/sim/simulation.rs +++ b/sandbox/src/sim/simulation.rs @@ -95,12 +95,11 @@ impl Simulation { Ok(()) } - /* - 1. Write objects to CA grid - 2. Step CA (multiple steps if needed). Updates solid etc. bitmaps - 3. Form contours & physics utils from CA Grid - 4. Step physics simulation - */ + /// 1. Write objects to CA grid + /// 2. Step CA (multiple steps if needed). Updates solid etc. bitmaps + /// 3. Remove object pixels from grid + /// 4. Form contours for new deformed physics objects + /// 5. Step physics simulation pub fn step( &mut self, api: &mut EngineApi, @@ -147,6 +146,7 @@ impl Simulation { Ok(()) } + /// Update object ecs data after physics calculation fn update_dynamic_physics_objects(&mut self, api: &mut EngineApi) -> Result<()> { let EngineApi { ecs_world, diff --git a/sandbox/src/sim/simulation_chunk_manager.rs b/sandbox/src/sim/simulation_chunk_manager.rs index 6aef537..6eecabe 100644 --- a/sandbox/src/sim/simulation_chunk_manager.rs +++ b/sandbox/src/sim/simulation_chunk_manager.rs @@ -177,6 +177,9 @@ impl GpuChunk { } } +/// The purpose of this manager is to organize map chunk loading and unloading when camera is moved +/// This is required for a potential endless 2d world. In this simulator it's not that useful though. +/// More like a tech demo part. pub struct SimulationChunkManager { pub queue: Arc, canvas_pos: Vector2,