Replace contexts with resources

- Implements a new macro to generate code for a new structure: TypeMap
- TypeMaps are wrappers for HashMaps that use TypeIDs as keys.
- Refactor the entire codebase to use the new resource structures.
- This commit is the first step towards getting rid of "god context objects everywhere".
This commit is contained in:
reo 2025-10-15 22:33:04 +03:00
parent 6e42d94b44
commit ef055a1bda
16 changed files with 287 additions and 93 deletions

View file

@ -1,8 +1,8 @@
use std::fmt::format;
use glam::{Quat, Vec3};
use raidillon_engine::{Engine, system::System};
use raidillon_engine::{Engine, system::System, EngineResources};
use raidillon_engine::system::SystemContext;
use raidillon_platform::{Platform, Camera};
use raidillon_platform::{Platform, Camera, PlatformContext};
use raidillon_assets::model_path;
use raidillon_core::engine::EngineTrait;
use raidillon_ecs::components::ModelHandle;
@ -13,6 +13,7 @@ use raidillon_glium::GliumPlatform;
use winit::event::{ElementState, Event, WindowEvent};
use winit::event::DeviceEvent::MouseMotion;
use winit::keyboard::{KeyCode, PhysicalKey};
use raidillon_core::DebugUIBuffer;
use raidillon_engine::systems::fps_camera::FPSDebugCameraSystem;
const TEST_GLTF: &str = "pink-monkey.gltf";
@ -22,11 +23,13 @@ const MAIN_SCENE_ID: &str = "main_scene";
#[derive(Default)]
struct UpdateAspectRatioSystem;
impl System for UpdateAspectRatioSystem {
fn handle_event(&mut self, ctx: &mut SystemContext) {
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {
let pctx = res.get::<PlatformContext>().unwrap();
if let Event::WindowEvent { event: WindowEvent::Resized(sz), .. } =
&ctx.platform_context.current_event
pctx.current_event
{
ctx.scene.world
scene.world
.query_mut::<&mut Camera>()
.into_iter()
.for_each(|(_, cam)| {
@ -42,14 +45,16 @@ struct RenderingTestSystem {
}
impl System for RenderingTestSystem {
fn load_world(&mut self, ctx: &mut SystemContext) {
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {
let pctx = res.get::<PlatformContext>().unwrap();
self.rotation_speed = std::rc::Rc::new(std::cell::RefCell::new(5.0));
let mut am = ctx.platform_context.asset_manager.borrow_mut();
let mut am = pctx.asset_manager.borrow_mut();
am.load_gltf(TEST_GLTF, &model_path(TEST_GLTF));
ctx.scene.world.spawn((
scene.world.spawn((
Transform {
translation: Vec3::new(0.0, 0.0, 0.0),
rotation: Quat::IDENTITY,
@ -59,17 +64,19 @@ impl System for RenderingTestSystem {
));
}
fn frame_update(&mut self, ctx: &mut SystemContext) {
let mut dbg_ui = ctx.debug_ui_buffer.borrow_mut();
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
let pctx = res.get::<PlatformContext>().unwrap().clone();
let dbg_ui = scene.resources.get_mut::<DebugUIBuffer>().unwrap();
dbg_ui.text("Hello World!".to_owned());
dbg_ui.text(format!("Frame Delta: {}", ctx.platform_context.time_ctx.frame_dt));
dbg_ui.text(format!("Fixed Delta: {}", ctx.platform_context.time_ctx.fixed_dt));
dbg_ui.text(format!("Frame Delta: {}", pctx.time_ctx.frame_dt));
dbg_ui.text(format!("Fixed Delta: {}", pctx.time_ctx.fixed_dt));
dbg_ui.slider_f32("Rotation Speed", -10.0, 10.0, self.rotation_speed.clone());
}
fn fixed_update(&mut self, ctx: &mut SystemContext) {
ctx.scene.world.query_mut::<(&mut Transform, &ModelHandle)>().into_iter().for_each(|(_, (t, _))| {
t.rotation *= Quat::from_rotation_y(*self.rotation_speed.borrow() * ctx.platform_context.time_ctx.fixed_dt);
fn fixed_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
let pctx = res.get::<PlatformContext>().unwrap();
scene.world.query_mut::<(&mut Transform, &ModelHandle)>().into_iter().for_each(|(_, (t, _))| {
t.rotation *= Quat::from_rotation_y(*self.rotation_speed.borrow() * pctx.time_ctx.fixed_dt);
});
}