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
This commit is contained in:
reo 2025-09-07 17:00:04 +03:00
parent 3fd5b09a94
commit 15122b8ebd
20 changed files with 344 additions and 117 deletions

View file

@ -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,

40
core/src/debug_ui.rs Normal file
View file

@ -0,0 +1,40 @@
pub enum UICommand {
Text(String),
Separator,
}
pub struct DebugUIBuffer {
cmds: Vec<UICommand>,
}
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![];
}
}

View file

@ -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<RefCell<DebugUIBuffer>>;
fn reset_debug_ui_buffer(&mut self);
fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc<RefCell<DebugUIBuffer>>);
}

View file

@ -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::*;

View file

@ -17,6 +17,8 @@ impl Scene {
}
}
impl Scene {}
impl AsRef<Scene> for Scene {
fn as_ref(&self) -> &Scene {
&self

View file

@ -1,38 +0,0 @@
use crate::Scene;
use indexmap::IndexMap;
use winit::event::Event;
use crate::context::PlatformContext;
pub struct SystemContext<'a> {
// TODO: time delta etc.
pub scene: &'a mut Scene,
pub platform_context: PlatformContext,
}
pub trait System {
/// Initialize the system.
fn initialize(&mut self);
/// Spawn the first entities of the world.
fn load_world(&mut self, ctx: &mut SystemContext);
fn update(&mut self, ctx: &mut SystemContext);
}
pub type SystemID = &'static str;
pub struct SystemManager {
pub systems: IndexMap<SystemID, Box<dyn System>>,
}
impl SystemManager {
pub fn new() -> Self {
let systems = IndexMap::default();
Self { systems }
}
pub fn add_system(&mut self, id: SystemID, system: Box<dyn System>) {
self.systems.insert(id, system);
}
pub fn remove_system(&mut self, id: SystemID) {
self.systems.shift_remove(&id);
}
}