From 15122b8ebd56014eae41334e4ca969bb163d0f21 Mon Sep 17 00:00:00 2001 From: reo Date: Sun, 7 Sep 2025 17:00:04 +0300 Subject: [PATCH] wip: Changes of the week - Move engine to a different crate - Add engine trait - Refactor the rest of the codebase to work with these changes - Add debug ui buffer, use it to finish imgui support --- Cargo.lock | 114 ++++++++++++++++++++++++++ Cargo.toml | 3 +- core/Cargo.toml | 1 + core/src/context.rs | 7 +- core/src/debug_ui.rs | 40 +++++++++ core/src/engine.rs | 64 +++------------ core/src/lib.rs | 11 +-- core/src/scene.rs | 2 + engine/Cargo.toml | 12 +++ engine/src/engine.rs | 83 +++++++++++++++++++ engine/src/lib.rs | 4 + {core => engine}/src/system.rs | 8 +- game/Cargo.toml | 1 + game/src/main.rs | 8 +- glium_platform/Cargo.toml | 1 + glium_platform/src/platform.rs | 27 +++--- glium_platform/src/render/basic.rs | 2 +- glium_platform/src/render/debug_ui.rs | 60 +++++++------- glium_platform/src/system.rs | 7 +- platform/src/platform.rs | 6 +- 20 files changed, 344 insertions(+), 117 deletions(-) create mode 100644 core/src/debug_ui.rs create mode 100644 engine/Cargo.toml create mode 100644 engine/src/engine.rs create mode 100644 engine/src/lib.rs rename {core => engine}/src/system.rs (80%) diff --git a/Cargo.lock b/Cargo.lock index 0c0aafe..adf963e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,6 +245,12 @@ dependencies = [ "libc", ] +[[package]] +name = "chlorine" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c00d31b1d19317b4777ec879192d3745bd97d05262b4b19cb1dda284b9d22f19" + [[package]] name = "combine" version = "4.6.7" @@ -642,6 +648,51 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "imgui" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8addafa5cecf0515812226e806913814e02ce38d10215778082af5174abe5669" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "imgui-sys", + "mint", + "parking_lot", +] + +[[package]] +name = "imgui-glium-renderer" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "781e53a393214a132c89c62e6e4b817645ad38832d3e334b2ad543f2c3df5106" +dependencies = [ + "glium", + "imgui", +] + +[[package]] +name = "imgui-sys" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ead193f9f4b60398e8b8f4ab1483e2321640d87aeebdaa3e5f44c55633ccd804" +dependencies = [ + "cc", + "cfg-if", + "chlorine", + "mint", +] + +[[package]] +name = "imgui-winit-support" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff7fcccfa9efab56c94274c0fec9939bb14149342b49e6a425883a5b7dda6a3f" +dependencies = [ + "imgui", + "winit", +] + [[package]] name = "indexmap" version = "2.10.0" @@ -757,6 +808,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" +[[package]] +name = "lock_api" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.27" @@ -797,6 +858,12 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mint" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff" + [[package]] name = "ndk" version = "0.9.0" @@ -1137,6 +1204,29 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "parking_lot" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.17", + "smallvec", + "windows-targets 0.52.6", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -1253,6 +1343,7 @@ name = "raidillon_core" version = "0.1.0" dependencies = [ "hecs", + "imgui", "indexmap", "raidillon_assets", "winit", @@ -1266,6 +1357,18 @@ dependencies = [ "raidillon_assets", ] +[[package]] +name = "raidillon_engine" +version = "0.1.0" +dependencies = [ + "hecs", + "indexmap", + "raidillon_assets", + "raidillon_core", + "raidillon_platform", + "winit", +] + [[package]] name = "raidillon_game" version = "0.1.0" @@ -1274,6 +1377,7 @@ dependencies = [ "raidillon_assets", "raidillon_core", "raidillon_ecs", + "raidillon_engine", "raidillon_glium", "raidillon_platform", ] @@ -1286,10 +1390,14 @@ dependencies = [ "glam", "glium", "gltf", + "imgui", + "imgui-glium-renderer", + "imgui-winit-support", "indexmap", "raidillon_assets", "raidillon_core", "raidillon_ecs", + "raidillon_engine", "raidillon_platform", "winit", ] @@ -1387,6 +1495,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "sctk-adwaita" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index aedf63e..102844c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ members = [ "platform", "asset", "game", - "ecs" + "ecs", + "engine" ] diff --git a/core/Cargo.toml b/core/Cargo.toml index 7b8bf94..fc6b73f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -8,3 +8,4 @@ hecs = "0.10.5" indexmap = "2.10.0" raidillon_assets = { path = "../asset" } winit = "0.30.12" +imgui = "0.12.0" \ No newline at end of file diff --git a/core/src/context.rs b/core/src/context.rs index a89d46c..f1cdffe 100644 --- a/core/src/context.rs +++ b/core/src/context.rs @@ -1,16 +1,11 @@ use std::cell::RefCell; use std::rc::Rc; +use std::sync::Arc; use winit::event::Event; use raidillon_assets::{ModelManagerRef, ModelManager}; -// TODO: Find a way to move this to raidillon_platform as it belongs there. -// TODO: The name "Context" doesn't imply the wide role of this structure. - -/// This provides a bridge between the game logic systems (raidillon_core::System) and -/// platform-related data/utilities. #[derive(Clone)] pub struct PlatformContext { - /// The latest winit event. pub current_event: Event<()>, pub asset_manager: ModelManagerRef, pub frame_width: f32, diff --git a/core/src/debug_ui.rs b/core/src/debug_ui.rs new file mode 100644 index 0000000..18d846f --- /dev/null +++ b/core/src/debug_ui.rs @@ -0,0 +1,40 @@ +pub enum UICommand { + Text(String), + Separator, +} + +pub struct DebugUIBuffer { + cmds: Vec, +} + +impl DebugUIBuffer { + pub fn new() -> DebugUIBuffer { + DebugUIBuffer { cmds: vec![] } + } + + // Commands + pub fn text(&mut self, text: String) { + self.cmds.push(UICommand::Text(text)); + } + + pub fn separator(&mut self) { + self.cmds.push(UICommand::Separator); + } + + pub fn write_buffer(&self, ui: &imgui::Ui) { + for cmd in &self.cmds { + match cmd { + UICommand::Text(s) => { + ui.text(s); + } + UICommand::Separator => { + ui.separator(); + } + } + } + } + + pub fn reset_buffer(&mut self) { + self.cmds = vec![]; + } +} diff --git a/core/src/engine.rs b/core/src/engine.rs index 6eb000f..4c73815 100644 --- a/core/src/engine.rs +++ b/core/src/engine.rs @@ -1,59 +1,15 @@ use std::cell::RefCell; use std::rc::Rc; -use winit::event::Event; -use crate::SceneManager; -use crate::system::{SystemContext, SystemManager}; -use raidillon_assets::{ModelManager, ModelManagerRef}; use crate::context::PlatformContext; +use crate::DebugUIBuffer; +use crate::scene::Scene; -pub struct Engine { - pub scene_manager: SceneManager, - pub system_manager: SystemManager, -} - -impl Engine { - pub fn new() -> Self { - let scene_manager = SceneManager::new(); - let system_manager = SystemManager::new(); - Self { - scene_manager, - system_manager, - } - } - - /// Initialize systems, load the world. - pub fn initialize(&mut self, platform_context: PlatformContext) { - // Engine Loading Stage 1: initialize systems - for (system_id, system) in self.system_manager.systems.iter_mut() { - system.initialize(); - } - - let mut ctx = SystemContext { - scene: self.scene_manager.current_mut(), - platform_context, - }; - - // Engine Loading Stage 2: load world - for (system_id, system) in self.system_manager.systems.iter_mut() { - system.load_world(&mut ctx); - } - } - - /// Update the engine - pub fn update(&mut self, platform_context: PlatformContext) { - let mut ctx = SystemContext { - scene: self.scene_manager.current_mut(), - platform_context, - }; - - for (system_id, system) in self.system_manager.systems.iter_mut() { - system.update(&mut ctx); - } - } - - // pub fn build_system_context(&mut self) -> SystemContext { - // SystemContext { - // scene: self.scene_manager.current_mut(), - // } - // } +pub trait EngineTrait { + fn new() -> Self; + fn initialize(&mut self, platform_context: PlatformContext); + fn update(&mut self, platform_context: PlatformContext); + fn current_scene_mut(&mut self) -> &mut Scene; + fn get_debug_ui_buffer(&self) -> Rc>; + fn reset_debug_ui_buffer(&mut self); + fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc>); } diff --git a/core/src/lib.rs b/core/src/lib.rs index 6fddfd0..b6fc074 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,8 +1,5 @@ -mod scene; -mod engine; -pub mod system; +pub mod engine; +pub mod scene; pub mod context; - -pub use scene::{Scene, SceneManager}; -pub use engine::Engine; -pub use system::{System, SystemManager}; +pub mod debug_ui; +pub use debug_ui::*; \ No newline at end of file diff --git a/core/src/scene.rs b/core/src/scene.rs index 94b49fa..9d1a7d4 100644 --- a/core/src/scene.rs +++ b/core/src/scene.rs @@ -17,6 +17,8 @@ impl Scene { } } +impl Scene {} + impl AsRef for Scene { fn as_ref(&self) -> &Scene { &self diff --git a/engine/Cargo.toml b/engine/Cargo.toml new file mode 100644 index 0000000..726ac44 --- /dev/null +++ b/engine/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "raidillon_engine" +version = "0.1.0" +edition = "2024" + +[dependencies] +raidillon_assets = { path = "../asset" } +raidillon_core = { path = "../core" } +raidillon_platform = { path = "../platform" } +winit = "0.30.12" +hecs = "0.10.5" +indexmap = "2.10.0" diff --git a/engine/src/engine.rs b/engine/src/engine.rs new file mode 100644 index 0000000..ea6a894 --- /dev/null +++ b/engine/src/engine.rs @@ -0,0 +1,83 @@ +use std::cell::RefCell; +use std::rc::Rc; +use winit::event::Event; +use raidillon_core::scene::{Scene, SceneManager}; +use crate::system::{SystemContext, SystemManager}; +use raidillon_assets::{ModelManager, ModelManagerRef}; +use raidillon_core::context::PlatformContext; +use raidillon_core::DebugUIBuffer; +use raidillon_core::engine::{EngineTrait}; + +pub struct Engine { + pub scene_manager: SceneManager, + pub system_manager: SystemManager, + debug_ui_buffer: Rc>, +} + +impl EngineTrait for Engine { + fn new() -> Self { + let scene_manager = SceneManager::new(); + let system_manager = SystemManager::new(); + Self { + scene_manager, + system_manager, + debug_ui_buffer: Rc::new(RefCell::new(DebugUIBuffer::new())), + } + } + + /// Initialize systems, load the world. + fn initialize(&mut self, platform_context: PlatformContext) { + // Engine Loading Stage 1: initialize systems + for (system_id, system) in self.system_manager.systems.iter_mut() { + system.initialize(); + } + + let mut ctx = SystemContext { + scene: self.scene_manager.current_mut(), + platform_context, + debug_ui_buffer: self.debug_ui_buffer.clone(), + }; + + // Engine Loading Stage 2: load world + for (system_id, system) in self.system_manager.systems.iter_mut() { + system.load_world(&mut ctx); + } + } + + /// Update the engine + fn update(&mut self, platform_context: PlatformContext) { + self.debug_ui_buffer.borrow_mut().reset_buffer(); + let mut ctx = SystemContext { + scene: self.scene_manager.current_mut(), + platform_context, + debug_ui_buffer: self.debug_ui_buffer.clone(), + }; + + for (system_id, system) in self.system_manager.systems.iter_mut() { + system.update(&mut ctx); + } + } + + // pub fn build_system_context(&mut self) -> SystemContext { + // SystemContext { + // scene: self.scene_manager.current_mut(), + // } + // } + + fn current_scene_mut(&mut self) -> &mut Scene { + self.scene_manager.current_mut() + } + + fn get_debug_ui_buffer(&self) -> Rc> { + self.debug_ui_buffer.clone() + } + + fn reset_debug_ui_buffer(&mut self) { + self.debug_ui_buffer.borrow_mut().reset_buffer(); + } + + fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc>) { + let (sm, dub) = (&mut self.scene_manager, &mut self.debug_ui_buffer); + (sm.current_mut(), dub.clone()) + } +} diff --git a/engine/src/lib.rs b/engine/src/lib.rs new file mode 100644 index 0000000..e17307d --- /dev/null +++ b/engine/src/lib.rs @@ -0,0 +1,4 @@ +pub mod engine; +pub mod system; + +pub use crate::engine::Engine; \ No newline at end of file diff --git a/core/src/system.rs b/engine/src/system.rs similarity index 80% rename from core/src/system.rs rename to engine/src/system.rs index 4412d7b..18eb01e 100644 --- a/core/src/system.rs +++ b/engine/src/system.rs @@ -1,12 +1,16 @@ -use crate::Scene; +use std::cell::RefCell; +use std::rc::Rc; +use raidillon_core::scene::Scene; use indexmap::IndexMap; use winit::event::Event; -use crate::context::PlatformContext; +use raidillon_core::context::PlatformContext; +use raidillon_core::DebugUIBuffer; pub struct SystemContext<'a> { // TODO: time delta etc. pub scene: &'a mut Scene, pub platform_context: PlatformContext, + pub debug_ui_buffer: Rc>, } pub trait System { diff --git a/game/Cargo.toml b/game/Cargo.toml index e8fac5f..0e1d7e9 100644 --- a/game/Cargo.toml +++ b/game/Cargo.toml @@ -12,5 +12,6 @@ raidillon_core = { path = "../core" } raidillon_platform = { path = "../platform" } raidillon_assets = { path = "../asset" } raidillon_ecs = { path = "../ecs" } +raidillon_engine = { path = "../engine" } raidillon_glium = { path = "../glium_platform", optional = true } glam = "0.30.5" diff --git a/game/src/main.rs b/game/src/main.rs index e72db32..c19ca94 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -1,13 +1,16 @@ use std::path::Path; use glam::{Quat, Vec3}; -use raidillon_core::{Engine, Scene, System}; -use raidillon_core::system::SystemContext; +use raidillon_engine::{Engine, system::System}; +use raidillon_engine::system::SystemContext; use raidillon_platform::{Platform, Camera}; use raidillon_assets::model_path; +use raidillon_core::engine::EngineTrait; use raidillon_ecs::components::ModelHandle; use raidillon_ecs::Transform; +use raidillon_core::scene::Scene; #[cfg(feature = "glium")] use raidillon_glium::GliumPlatform; +use raidillon_core::DebugUIBuffer; const TEST_GLTF: &str = "pink-monkey.gltf"; @@ -48,6 +51,7 @@ impl System for RenderingTestSystem { // if let Some(mut debug_ui) = ctx.platform_context.imgui_ui.as_ref().map(|ui| ui.borrow_mut()) { // debug_ui.text("Hello World!"); // } + ctx.debug_ui_buffer.borrow_mut().text("Hello World!".to_owned()); } } diff --git a/glium_platform/Cargo.toml b/glium_platform/Cargo.toml index ebae599..0c0ecc2 100644 --- a/glium_platform/Cargo.toml +++ b/glium_platform/Cargo.toml @@ -12,6 +12,7 @@ raidillon_platform = { path = "../platform" } raidillon_core = { path = "../core" } raidillon_assets = { path = "../asset" } raidillon_ecs = { path = "../ecs" } +raidillon_engine = { path = "../engine" } winit = "0.30.12" indexmap = "2.10.0" imgui = "0.12.0" diff --git a/glium_platform/src/platform.rs b/glium_platform/src/platform.rs index bf1aeac..c01cf60 100644 --- a/glium_platform/src/platform.rs +++ b/glium_platform/src/platform.rs @@ -12,25 +12,26 @@ use glium::Surface; use crate::system::{RenderingSystemManager, RenderingSystem, RenderingContext, SystemID}; use winit::event::{Event, WindowEvent}; use raidillon_assets::{ModelManager, ModelManagerRef}; -use raidillon_core::Engine; +use raidillon_core::engine::EngineTrait; +use raidillon_engine::Engine; use crate::{GliumAssetManager}; use crate::render::debug_ui::ImguiBridge; use crate::render::BasicMeshRenderingSystem; pub const MESH_RENDERER: &str = "mesh_renderer"; +pub const DEBUG_UI_RENDERER: &str = "debug_ui_renderer"; -pub struct GliumPlatform { +pub struct GliumPlatform { event_loop: EventLoop<()>, window: Window, display: Display, rendering_system_manager: RenderingSystemManager, asset_manager: ModelManagerRef, - engine: Engine, - imgui_bridge: ImguiBridge, + engine: E, } -impl Platform for GliumPlatform { - fn initialize(mut engine: Engine, title: String, width: u32, height: u32) -> Self { +impl Platform for GliumPlatform { + fn initialize(mut engine: E, title: String, width: u32, height: u32) -> Self { let event_loop = glium::winit::event_loop::EventLoop::builder() .build() .expect("create event-loop"); @@ -46,10 +47,13 @@ impl Platform for GliumPlatform { // Install rendering systems rendering_system_manager.add_system( MESH_RENDERER, - Box::new(BasicMeshRenderingSystem::initialize(&display)) + Box::new(BasicMeshRenderingSystem::initialize(&display, &window)) ); - let imgui_bridge = ImguiBridge::new(&display, &window).unwrap(); + rendering_system_manager.add_system( + DEBUG_UI_RENDERER, + Box::new(ImguiBridge::initialize(&display, &window)), + ); Self { event_loop, @@ -58,7 +62,6 @@ impl Platform for GliumPlatform { rendering_system_manager, asset_manager, engine, - imgui_bridge, } } @@ -81,11 +84,13 @@ impl Platform for GliumPlatform { WindowEvent::RedrawRequested => { let mut target = self.display.draw(); target.clear_color_and_depth((0.1, 0.1, 0.15, 1.0), 1.0); - // TODO: let mut context; + let (scene_mut, debug_ui_buffer) = self.engine.scene_and_debug_ui_buffer_mut(); let mut context = RenderingContext { - scene: self.engine.scene_manager.current_mut(), + scene: scene_mut, target: &mut target, asset_manager: self.asset_manager.clone(), + window: &mut self.window, + debug_ui_buffer, }; for (system_id, system) in self.rendering_system_manager.systems.iter_mut() { diff --git a/glium_platform/src/render/basic.rs b/glium_platform/src/render/basic.rs index 60e7621..0f1db93 100644 --- a/glium_platform/src/render/basic.rs +++ b/glium_platform/src/render/basic.rs @@ -20,7 +20,7 @@ pub struct BasicMeshRenderingSystem { } impl RenderingSystem for BasicMeshRenderingSystem { - fn initialize(display: &Display) -> Self { + fn initialize(display: &Display, _window: &glium::winit::window::Window) -> Self { const VERT_SRC: &str = include_shader!("gl_textured.vert"); const FRAG_SRC: &str = include_shader!("gl_textured.frag"); diff --git a/glium_platform/src/render/debug_ui.rs b/glium_platform/src/render/debug_ui.rs index 796be6d..3f141c5 100644 --- a/glium_platform/src/render/debug_ui.rs +++ b/glium_platform/src/render/debug_ui.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::rc::Rc; +use std::sync::Arc; use std::time::Instant; use glium::Display; use glium::glutin::surface::WindowSurface; @@ -8,10 +9,11 @@ use imgui_winit_support::{HiDpiMode, WinitPlatform}; use imgui_glium_renderer::Renderer as ImguiGliumRenderer; use winit::raw_window_handle::DisplayHandle; use winit::window::Window; -use anyhow::Result; +use anyhow::{Result}; use crate::RenderingSystem; use crate::system::RenderingContext; use glium::Frame; +use raidillon_core::DebugUIBuffer; pub struct ImguiBridge { imgui: ImguiContext, @@ -20,21 +22,6 @@ pub struct ImguiBridge { } impl ImguiBridge { - pub fn new(display: &Display, window: &Window) -> Result { - let mut imgui = ImguiContext::create(); - imgui.set_ini_filename(None); - let mut platform = WinitPlatform::new(&mut imgui); - platform.attach_window(imgui.io_mut(), window, HiDpiMode::Default); - imgui.fonts().add_font(&[imgui::FontSource::DefaultFontData { config: None }]); - let renderer = ImguiGliumRenderer::new(&mut imgui, display)?; - - Ok(Self { - imgui, - platform, - renderer, - }) - } - pub fn render(&mut self, target: &mut Frame, window: &Window, build_ui: F) where F: FnOnce(&imgui::Ui), @@ -52,26 +39,43 @@ impl ImguiBridge { .expect("imgui rendering failed"); } - pub fn initialize_frame(&mut self) -> &mut imgui::Ui { - self.imgui.frame() - } - - pub fn finish_frame(&mut self, target: &mut Frame, window: &Window, ui: imgui::Ui) { - self.platform.prepare_render(&ui, window); - let draw_data = self.imgui.render(); - self.renderer.render(target, draw_data).expect("imgui rendering failed"); - } + // pub fn initialize_frame(&mut self) -> &mut imgui::Ui { + // self.imgui.frame() + // } + // + // pub fn finish_frame(&mut self, target: &mut Frame, window: &Window, ui: imgui::Ui) { + // self.platform.prepare_render(&ui, window); + // let draw_data = self.imgui.render(); + // self.renderer.render(target, draw_data).expect("imgui rendering failed"); + // } } impl RenderingSystem for ImguiBridge { fn render(&mut self, ctx: &mut RenderingContext) { - todo!() + let mut ui = self.imgui.frame(); + + ctx.debug_ui_buffer.borrow_mut().write_buffer(&mut ui); + + self.platform.prepare_render(&ui, ctx.window); + let draw_data = self.imgui.render(); + self.renderer.render(ctx.target, draw_data).expect("imgui rendering failed"); } - fn initialize(display: &Display) -> Self + fn initialize(display: &Display, window: &glium::winit::window::Window) -> Self where Self: Sized, { - todo!() + let mut imgui = ImguiContext::create(); + imgui.set_ini_filename(None); + let mut platform = WinitPlatform::new(&mut imgui); + platform.attach_window(imgui.io_mut(), window, HiDpiMode::Default); + imgui.fonts().add_font(&[imgui::FontSource::DefaultFontData { config: None }]); + let renderer = ImguiGliumRenderer::new(&mut imgui, display).unwrap(); + + Self { + imgui, + platform, + renderer, + } } } \ No newline at end of file diff --git a/glium_platform/src/system.rs b/glium_platform/src/system.rs index 63d58b4..3c21a04 100644 --- a/glium_platform/src/system.rs +++ b/glium_platform/src/system.rs @@ -1,23 +1,26 @@ use std::cell::RefCell; use std::rc::Rc; -use raidillon_core::Scene; +use raidillon_core::scene::Scene; use glium::{Display, Frame}; use glium::glutin::surface::WindowSurface; use indexmap::IndexMap; use raidillon_assets::ModelManagerRef; +use raidillon_core::DebugUIBuffer; use crate::GliumAssetManager; pub struct RenderingContext<'a> { pub scene: &'a Scene, pub target: &'a mut Frame, + pub window: &'a mut glium::winit::window::Window, pub asset_manager: ModelManagerRef, + pub debug_ui_buffer: Rc>, } /// The internal "rendering system" trait of glium_platform. /// This is unrelated to the main System trait in core. pub trait RenderingSystem { fn render(&mut self, ctx: &mut RenderingContext); - fn initialize(display: &Display) -> Self where Self: Sized; + fn initialize(display: &Display, window: &glium::winit::window::Window) -> Self where Self: Sized; } pub type SystemID = &'static str; diff --git a/platform/src/platform.rs b/platform/src/platform.rs index 3e0309f..44b799f 100644 --- a/platform/src/platform.rs +++ b/platform/src/platform.rs @@ -1,9 +1,9 @@ use raidillon_assets::ModelManager; -use raidillon_core::Engine; +use raidillon_core::engine::EngineTrait; use crate::PlatformContext; -pub trait Platform { +pub trait Platform { /// Initialize platform. - fn initialize(engine: Engine, title: String, width: u32, height: u32) -> Self; + fn initialize(engine: E, title: String, width: u32, height: u32) -> Self; fn run(self); }