From 92c0278ef033c4b458ffd7c1b3ff47dc44fcb5da Mon Sep 17 00:00:00 2001 From: Tony Klink Date: Sun, 31 Mar 2024 14:09:38 -0600 Subject: [PATCH] Move egui layer to a separate DebugGuiStack structure --- src/core/debug_gui.rs | 50 +++++++++++++++++++++++++++++++++++++ src/core/mod.rs | 2 ++ src/core/module.rs | 9 ++++--- src/core/time.rs | 43 +++++++++++++++++++++++++++++++ src/modules/graphics/mod.rs | 9 +++---- 5 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 src/core/debug_gui.rs create mode 100644 src/core/time.rs diff --git a/src/core/debug_gui.rs b/src/core/debug_gui.rs new file mode 100644 index 0000000..07c745c --- /dev/null +++ b/src/core/debug_gui.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; + +use vulkano_util::renderer::VulkanoWindowRenderer; +use winit::{event_loop::EventLoopWindowTarget, window::WindowId}; + +use crate::modules::graphics::egui::{Gui, GuiConfig}; + +#[derive(Default)] +pub struct DebugGuiStack { + guis: HashMap, +} + +impl DebugGuiStack { + pub fn add_gui( + &mut self, + window_id: WindowId, + event_loop: &EventLoopWindowTarget, + renderer: &VulkanoWindowRenderer, + is_overlay: bool, + allow_srgb_render_target: bool, + ) where + T: Clone + Send + Sync, + { + let gui = Gui::new( + event_loop, + renderer.surface().clone(), + renderer.graphics_queue().clone(), + renderer.swapchain_format(), + GuiConfig { + is_overlay, + allow_srgb_render_target, + ..Default::default() + }, + ); + + self.guis.insert(window_id, gui); + } + + pub fn remove_gui(&mut self, window_id: WindowId) { + self.guis.remove(&window_id).unwrap(); + } + + pub fn get(&mut self, window_id: WindowId) -> Option<&Gui> { + self.guis.get(&window_id) + } + + pub fn get_mut(&mut self, window_id: WindowId) -> Option<&mut Gui> { + self.guis.get_mut(&window_id) + } +} diff --git a/src/core/mod.rs b/src/core/mod.rs index bd43ed8..a89e05c 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,2 +1,4 @@ pub mod events; pub mod module; +pub mod time; +pub mod debug_gui; diff --git a/src/core/module.rs b/src/core/module.rs index 670b666..1d609c7 100644 --- a/src/core/module.rs +++ b/src/core/module.rs @@ -1,10 +1,11 @@ -use std::{collections::HashMap, time::Duration}; +use std::time::Duration; use anyhow::Result; use flax::World; -use winit::window::WindowId; -use crate::{core::events::Events, modules::graphics::egui::Gui}; +use crate::core::events::Events; + +use super::debug_gui::DebugGuiStack; pub trait Module { fn on_update(&mut self, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>; @@ -67,7 +68,7 @@ impl<'a> IntoIterator for &'a mut ModulesStack { // THREAD LOCAL STUFF pub trait RenderModule { - fn on_update(&mut self, gui: &mut HashMap, vk_context: &mut vulkano_util::context::VulkanoContext, vk_windows: &mut vulkano_util::window::VulkanoWindows, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>; + fn on_update(&mut self, gui_stack: &mut DebugGuiStack, vk_context: &mut vulkano_util::context::VulkanoContext, vk_windows: &mut vulkano_util::window::VulkanoWindows, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>; } pub struct RenderModulesStack { diff --git a/src/core/time.rs b/src/core/time.rs new file mode 100644 index 0000000..04f2c86 --- /dev/null +++ b/src/core/time.rs @@ -0,0 +1,43 @@ +//! Provides time related functionality for Clocks. +use std::time::{Duration, Instant}; + +use flax::component; + +component! { + pub clock: Clock, +} + +/// Measures high precision time +#[allow(dead_code)] +pub struct Clock { + start: Instant, +} + +#[allow(dead_code)] +impl Clock { + // Creates and starts a new clock + pub fn new() -> Self { + Clock { + start: Instant::now(), + } + } + + // Returns the elapsed time + pub fn elapsed(&self) -> Duration { + Instant::now() - self.start + } + + // Resets the clock and returns the elapsed time + pub fn reset(&mut self) -> Duration { + let elapsed = self.elapsed(); + + self.start = Instant::now(); + elapsed + } +} + +impl Default for Clock { + fn default() -> Self { + Self::new() + } +} diff --git a/src/modules/graphics/mod.rs b/src/modules/graphics/mod.rs index 2cdcca9..ca85195 100644 --- a/src/modules/graphics/mod.rs +++ b/src/modules/graphics/mod.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; use flax::{entity_ids, BoxedSystem, Query, QueryBorrow, Schedule, System, World}; use vulkano::{ @@ -15,9 +15,8 @@ use vulkano::{ use vulkano_util::{ context::VulkanoContext, renderer::VulkanoWindowRenderer, window::VulkanoWindows, }; -use winit::window::WindowId; -use crate::core::module::RenderModule as ThreadLocalModule; +use crate::core::{debug_gui::DebugGuiStack, module::RenderModule as ThreadLocalModule}; use self::{egui::Gui, test_pipeline::test_pipeline}; @@ -65,7 +64,7 @@ impl RenderModule { impl ThreadLocalModule for RenderModule { fn on_update( &mut self, - guis: &mut HashMap, + gui_stack: &mut DebugGuiStack, vk_context: &mut VulkanoContext, vk_windows: &mut vulkano_util::window::VulkanoWindows, world: &mut World, @@ -77,7 +76,7 @@ impl ThreadLocalModule for RenderModule { let viewport = &mut self.viewport; for (window_id, renderer) in vk_windows.iter_mut() { - let gui = guis.get_mut(window_id).unwrap(); + let gui = gui_stack.get_mut(*window_id).unwrap(); draw( self.command_buffer_allocator.clone(), viewport,