Replace Contexts with Resources #7
16 changed files with 287 additions and 93 deletions
|
|
@ -6,5 +6,5 @@ members = [
|
|||
"asset",
|
||||
"game",
|
||||
"ecs",
|
||||
"engine"
|
||||
"engine",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ pub trait EngineTrait {
|
|||
fn fixed_update(&mut self, platform_context: Self::PlatformCtx);
|
||||
fn handle_event(&mut self, platform_context: Self::PlatformCtx);
|
||||
fn current_scene_mut(&mut self) -> &mut Scene;
|
||||
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>>;
|
||||
fn current_scene(&self) -> &Scene;
|
||||
fn get_debug_ui_buffer(&self) -> &DebugUIBuffer;
|
||||
fn reset_debug_ui_buffer(&mut self);
|
||||
fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc<RefCell<DebugUIBuffer>>);
|
||||
// fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Self::Scene, &DebugUIBuffer);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
pub mod engine;
|
||||
pub mod scene;
|
||||
pub mod debug_ui;
|
||||
pub mod time;
|
||||
pub mod utils;
|
||||
pub mod scene;
|
||||
|
||||
pub use debug_ui::*;
|
||||
|
|
|
|||
|
|
@ -1,19 +1,31 @@
|
|||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use crate::{define_typemap, DebugUIBuffer};
|
||||
|
||||
pub struct Scene {
|
||||
pub title: String,
|
||||
pub world: hecs::World,
|
||||
pub skybox_texture_path: Option<PathBuf>,
|
||||
pub resources: SceneResources,
|
||||
}
|
||||
|
||||
define_typemap!(SceneResources,);
|
||||
|
||||
impl Scene {
|
||||
pub fn new(title: String, skybox_texture_path: Option<PathBuf>) -> Self {
|
||||
Self {
|
||||
let mut s = Self {
|
||||
title,
|
||||
world: hecs::World::new(),
|
||||
skybox_texture_path,
|
||||
resources: SceneResources::new(),
|
||||
};
|
||||
s.load_default_resources();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn load_default_resources(&mut self) {
|
||||
let dbg = DebugUIBuffer::new();
|
||||
self.resources.insert(dbg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
44
core/src/utils/managers.rs
Normal file
44
core/src/utils/managers.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/// Unused as of now.
|
||||
#[macro_export]
|
||||
macro_rules! create_manager {
|
||||
($manager_name:ident, $trait_name:ident) => {
|
||||
pub struct $manager_name {
|
||||
systems: ::indexmap::IndexMap<::std::any::TypeId, Box<dyn $trait_name>>,
|
||||
}
|
||||
|
||||
impl $manager_name {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
systems: ::indexmap::IndexMap::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add<S: $trait_name + Default + 'static>(&mut self) {
|
||||
self.systems
|
||||
.insert(::std::any::TypeId::of::<S>(), Box::new(S::default()));
|
||||
}
|
||||
|
||||
pub fn remove<S: 'static>(&mut self) {
|
||||
self.systems.shift_remove(&::std::any::TypeId::of::<S>());
|
||||
}
|
||||
|
||||
pub fn for_each_value<F>(&self, mut f: F)
|
||||
where
|
||||
F: FnMut(&dyn $trait_name),
|
||||
{
|
||||
for value in self.systems.values() {
|
||||
f(value.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_each_value_mut<F>(&mut self, mut f: F)
|
||||
where
|
||||
F: FnMut(&mut dyn $trait_name),
|
||||
{
|
||||
for value in self.systems.values_mut() {
|
||||
f(value.as_mut());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
2
core/src/utils/mod.rs
Normal file
2
core/src/utils/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod typemap;
|
||||
mod managers;
|
||||
122
core/src/utils/typemap.rs
Normal file
122
core/src/utils/typemap.rs
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#[macro_export]
|
||||
macro_rules! define_typemap {
|
||||
($name:ident, $($trait_bound:tt)*) => {
|
||||
pub struct $name {
|
||||
map: std::collections::HashMap<std::any::TypeId, Box<dyn std::any::Any>>,
|
||||
}
|
||||
|
||||
impl $name {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert<T>(&mut self, value: T) -> Option<Box<T>>
|
||||
where
|
||||
T: std::any::Any + 'static + $($trait_bound)*,
|
||||
{
|
||||
let type_id = std::any::TypeId::of::<T>();
|
||||
self.map
|
||||
.insert(type_id, Box::new(value))
|
||||
.and_then(|boxed| boxed.downcast().ok())
|
||||
}
|
||||
|
||||
pub fn get<T>(&self) -> Option<&T>
|
||||
where
|
||||
T: std::any::Any + 'static + $($trait_bound)*,
|
||||
{
|
||||
self.map
|
||||
.get(&std::any::TypeId::of::<T>())
|
||||
.and_then(|any| any.downcast_ref::<T>())
|
||||
}
|
||||
|
||||
pub fn get_mut<T>(&mut self) -> Option<&mut T>
|
||||
where
|
||||
T: std::any::Any + 'static + $($trait_bound)*,
|
||||
{
|
||||
self.map
|
||||
.get_mut(&std::any::TypeId::of::<T>())
|
||||
.and_then(|any| any.downcast_mut::<T>())
|
||||
}
|
||||
|
||||
pub fn remove<T>(&mut self) -> Option<Box<T>>
|
||||
where
|
||||
T: std::any::Any + 'static + $($trait_bound)*,
|
||||
{
|
||||
self.map
|
||||
.remove(&std::any::TypeId::of::<T>())
|
||||
.and_then(|boxed| boxed.downcast().ok())
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.is_empty()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.map.clear();
|
||||
}
|
||||
|
||||
pub fn contains<T>(&self) -> bool
|
||||
where
|
||||
T: std::any::Any + 'static + $($trait_bound)*,
|
||||
{
|
||||
self.map.contains_key(&std::any::TypeId::of::<T>())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for $name {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// pub struct TypeMap {
|
||||
// map: HashMap<TypeId, Box<dyn Any>>,
|
||||
// }
|
||||
//
|
||||
// impl TypeMap {
|
||||
// pub fn new() -> Self {
|
||||
// Self {
|
||||
// map: HashMap::new(),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// pub fn insert<T: Any + 'static>(&mut self, value: T) -> Option<Box<T>> {
|
||||
// let type_id = TypeId::of::<T>();
|
||||
// self.map
|
||||
// .insert(type_id, Box::new(value))
|
||||
// .and_then(|boxed| boxed.downcast().ok())
|
||||
// }
|
||||
//
|
||||
// pub fn get<T>(&self) -> Option<&T>
|
||||
// where
|
||||
// T: Any + 'static,
|
||||
// {
|
||||
// self.map.get(&TypeId::of::<T>())
|
||||
// .and_then(|any| any.downcast_ref::<T>())
|
||||
// }
|
||||
//
|
||||
// pub fn get_mut<T>(&mut self) -> Option<&mut T>
|
||||
// where
|
||||
// T: Any + 'static,
|
||||
// {
|
||||
// self.map.get_mut(&TypeId::of::<T>()).and_then(|any| any.downcast_mut::<T>())
|
||||
// }
|
||||
//
|
||||
// pub fn remove<T: Any>(&mut self) -> Option<Box<T>> {
|
||||
// unimplemented!()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// impl Default for TypeMap {
|
||||
// fn default() -> Self {
|
||||
// Self::new()
|
||||
// }
|
||||
// }
|
||||
|
|
@ -3,15 +3,22 @@ use std::rc::Rc;
|
|||
use raidillon_core::scene::{Scene, SceneManager};
|
||||
use crate::system::{SystemContext, SystemManager};
|
||||
use raidillon_platform::PlatformContext;
|
||||
use raidillon_core::DebugUIBuffer;
|
||||
use raidillon_core::{define_typemap, DebugUIBuffer};
|
||||
use raidillon_core::engine::EngineTrait;
|
||||
use crate::input::InputState;
|
||||
use crate::resources::EngineResources;
|
||||
|
||||
pub struct Engine {
|
||||
pub scene_manager: SceneManager,
|
||||
pub system_manager: SystemManager,
|
||||
debug_ui_buffer: Rc<RefCell<DebugUIBuffer>>,
|
||||
input_state: Rc<RefCell<InputState>>,
|
||||
pub resources: EngineResources,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
fn load_default_resources(&mut self) {
|
||||
let input = InputState::default();
|
||||
self.resources.insert(input);
|
||||
}
|
||||
}
|
||||
|
||||
impl EngineTrait for Engine {
|
||||
|
|
@ -19,12 +26,13 @@ impl EngineTrait for Engine {
|
|||
fn new() -> Self {
|
||||
let scene_manager = SceneManager::new();
|
||||
let system_manager = SystemManager::new();
|
||||
Self {
|
||||
let mut s = Self {
|
||||
scene_manager,
|
||||
system_manager,
|
||||
debug_ui_buffer: Rc::new(RefCell::new(DebugUIBuffer::new())),
|
||||
input_state: Default::default(),
|
||||
}
|
||||
resources: EngineResources::new(),
|
||||
};
|
||||
s.load_default_resources();
|
||||
s
|
||||
}
|
||||
|
||||
/// Initialize systems, load the world.
|
||||
|
|
@ -33,60 +41,35 @@ impl EngineTrait for Engine {
|
|||
for system in self.system_manager.systems.values_mut() {
|
||||
system.initialize();
|
||||
}
|
||||
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
self.resources.insert(platform_context);
|
||||
// Engine Loading Stage 2: load world
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.load_world(&mut ctx);
|
||||
system.load_world(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the engine
|
||||
fn frame_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(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
self.scene_manager.current_mut().resources.get_mut::<DebugUIBuffer>().unwrap().reset_buffer();
|
||||
self.resources.insert(platform_context);
|
||||
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.frame_update(&mut ctx);
|
||||
system.frame_update(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
fn fixed_update(&mut self, platform_context: PlatformContext) {
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
self.resources.insert(platform_context);
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.fixed_update(&mut ctx);
|
||||
system.fixed_update(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, platform_context: PlatformContext) {
|
||||
self.input_state.borrow_mut().handle_event(&platform_context.current_event);
|
||||
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
// self.input_state.borrow_mut().handle_event(&platform_context.current_event);
|
||||
self.resources.insert(platform_context);
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.handle_event(&mut ctx);
|
||||
system.handle_event(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -100,16 +83,20 @@ impl EngineTrait for Engine {
|
|||
self.scene_manager.current_mut()
|
||||
}
|
||||
|
||||
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>> {
|
||||
self.debug_ui_buffer.clone()
|
||||
fn current_scene(&self) -> &Scene {
|
||||
self.scene_manager.current()
|
||||
}
|
||||
|
||||
fn get_debug_ui_buffer(&self) -> &DebugUIBuffer {
|
||||
self.scene_manager.current().resources.get::<DebugUIBuffer>().unwrap()
|
||||
}
|
||||
|
||||
fn reset_debug_ui_buffer(&mut self) {
|
||||
self.debug_ui_buffer.borrow_mut().reset_buffer();
|
||||
self.scene_manager.current_mut().resources.get_mut::<DebugUIBuffer>().unwrap().reset_buffer();
|
||||
}
|
||||
|
||||
fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc<RefCell<DebugUIBuffer>>) {
|
||||
let (sm, dub) = (&mut self.scene_manager, &mut self.debug_ui_buffer);
|
||||
(sm.current_mut(), dub.clone())
|
||||
}
|
||||
// fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, &DebugUIBuffer) {
|
||||
// let (sm, dub) = (&mut self.scene_manager, self.scene_manager.current().resources.get::<DebugUIBuffer>().unwrap());
|
||||
// (sm.current_mut(), dub)
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,5 +2,7 @@ pub mod engine;
|
|||
pub mod system;
|
||||
mod input;
|
||||
pub mod systems;
|
||||
mod resources;
|
||||
|
||||
pub use crate::engine::Engine;
|
||||
pub use crate::resources::EngineResources;
|
||||
|
|
|
|||
3
engine/src/resources.rs
Normal file
3
engine/src/resources.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
use raidillon_core::define_typemap;
|
||||
|
||||
define_typemap!(EngineResources,);
|
||||
|
|
@ -6,6 +6,7 @@ use std::any::TypeId;
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use crate::input::InputState;
|
||||
use crate::resources::EngineResources;
|
||||
|
||||
pub struct SystemContext<'a> {
|
||||
pub scene: &'a mut Scene,
|
||||
|
|
@ -18,10 +19,10 @@ 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 handle_event(&mut self, _ctx: &mut SystemContext) {}
|
||||
fn fixed_update(&mut self, _ctx: &mut SystemContext) {}
|
||||
fn frame_update(&mut self, _ctx: &mut SystemContext) {}
|
||||
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
|
||||
|
|
||||
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
|
||||
fn fixed_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
|
||||
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
|
||||
}
|
||||
|
||||
pub struct SystemManager {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,10 @@ use winit::event::{ElementState, Event, MouseButton, WindowEvent};
|
|||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::CursorGrabMode;
|
||||
use raidillon_assets::model_path;
|
||||
use raidillon_platform::Camera;
|
||||
use raidillon_platform::{Camera, PlatformContext};
|
||||
use crate::input::InputState;
|
||||
use crate::resources::EngineResources;
|
||||
use raidillon_core::scene::Scene;
|
||||
|
||||
pub struct FPSDebugCameraSystem {
|
||||
mouse_delta: (f64, f64),
|
||||
|
|
@ -32,20 +35,22 @@ impl Default for FPSDebugCameraSystem {
|
|||
}
|
||||
|
||||
impl System for FPSDebugCameraSystem {
|
||||
fn load_world(&mut self, ctx: &mut SystemContext) {
|
||||
ctx.scene.world.spawn((Camera {
|
||||
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
scene.world.spawn((Camera {
|
||||
eye: Vec3::new(0.0, 0.0, 2.0),
|
||||
center: Vec3::ZERO,
|
||||
up: Vec3::Y,
|
||||
fovy: 60_f32.to_radians(),
|
||||
aspect: ctx.platform_context.frame_width / ctx.platform_context.frame_height,
|
||||
aspect: pctx.frame_width / pctx.frame_height,
|
||||
znear: 0.1,
|
||||
zfar: 100.0,
|
||||
},));
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, ctx: &mut SystemContext) {
|
||||
let event2 = ctx.platform_context.current_event.clone();
|
||||
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
let event2 = pctx.current_event.clone();
|
||||
match event2 {
|
||||
Event::DeviceEvent { device_id, event} => {
|
||||
match event {
|
||||
|
|
@ -60,7 +65,7 @@ impl System for FPSDebugCameraSystem {
|
|||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
if button == MouseButton::Right {
|
||||
// blood and tear
|
||||
let window = ctx.platform_context.window.lock().unwrap();
|
||||
let window = pctx.window.lock().unwrap();
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
if window
|
||||
|
|
@ -86,7 +91,10 @@ impl System for FPSDebugCameraSystem {
|
|||
}
|
||||
}
|
||||
|
||||
fn frame_update(&mut self, ctx: &mut SystemContext) {
|
||||
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
let input = res.get::<InputState>().unwrap();
|
||||
|
||||
if self.mouse_enabled {
|
||||
self.yaw += self.mouse_delta.0 as f32 * self.sensitivity;
|
||||
self.pitch -= self.mouse_delta.1 as f32 * self.sensitivity;
|
||||
|
|
@ -95,22 +103,21 @@ impl System for FPSDebugCameraSystem {
|
|||
|
||||
let front = self.front();
|
||||
let right_vec = front.cross(Vec3::Y).normalize();
|
||||
let input = ctx.input_state.borrow_mut();
|
||||
|
||||
if input.key_held(KeyCode::KeyW) {
|
||||
self.position += front * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position += front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyS) {
|
||||
self.position -= front * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position -= front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyA) {
|
||||
self.position -= right_vec * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position -= right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyD) {
|
||||
self.position += right_vec * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position += right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
|
||||
ctx.scene.world.query_mut::<&mut Camera>().into_iter().for_each(|(_, camera)| {
|
||||
scene.world.query_mut::<&mut Camera>().into_iter().for_each(|(_, camera)| {
|
||||
camera.eye = self.position;
|
||||
camera.center = self.position + front;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
reo marked this conversation as resolved
reo
commented
Looking at this mess makes me want to invent a simple domain-specific language to specify which resources a system needs, and then make a macro to generate this code. I've never made a list of the most over-engineered things I've created. If I were to start now I wouldn't have trouble deciding which one gets the top spot. Looking at this mess makes me want to invent a simple domain-specific language to specify which resources a system needs, and then make a macro to generate this code.
```rust
res!("pctx = PlatformContext, dbg_ui = DebugUIBuffer");
// generates:
// let pctx = res.get::<PlatformContext>().unwrap().clone();
// let dbg_ui = scene.resources.get_mut::<DebugUIBuffer>().unwrap();
```
I've never made a list of the most over-engineered things I've created. If I were to start now I wouldn't have trouble deciding which one gets the top spot.
reo
commented
Solved by implementing Solved by implementing [`get_many`](https://git.reoco.de/reo/raidillon/commit/1a48e58a1cfca6baea4cd60437abfca69fe0d6dd)
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -98,9 +98,12 @@ impl<E: EngineTrait<PlatformCtx = PlatformContext>> Platform<E> for GliumPlatfor
|
|||
WindowEvent::RedrawRequested => {
|
||||
let mut target = self.display.draw();
|
||||
target.clear_color_and_depth((0.1, 0.1, 0.15, 1.0), 1.0);
|
||||
let (scene_mut, debug_ui_buffer) = self.engine.scene_and_debug_ui_buffer_mut();
|
||||
let (scene, debug_ui_buffer) = (
|
||||
self.engine.current_scene(),
|
||||
self.engine.get_debug_ui_buffer(),
|
||||
);
|
||||
let mut context = RenderingContext {
|
||||
scene: scene_mut,
|
||||
scene,
|
||||
target: &mut target,
|
||||
asset_manager: self.asset_manager.clone(),
|
||||
window: self.window.clone(),
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ impl RenderingSystem for ImguiBridge {
|
|||
self.rendered_this_frame = true;
|
||||
|
||||
let ui = self.imgui.frame();
|
||||
ctx.debug_ui_buffer.borrow().write_buffer(&ui);
|
||||
ctx.debug_ui_buffer.write_buffer(&ui);
|
||||
|
||||
{
|
||||
let window = ctx.window.lock().unwrap();
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use indexmap::IndexMap;
|
|||
use glium::{Display, Frame};
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use raidillon_assets::ModelManagerRef;
|
||||
use raidillon_core::DebugUIBuffer;
|
||||
use raidillon_core::{define_typemap, DebugUIBuffer};
|
||||
use raidillon_core::scene::Scene;
|
||||
use glam::Vec3;
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ pub struct RenderingContext<'a> {
|
|||
pub target: &'a mut Frame,
|
||||
pub window: Arc<Mutex<glium::winit::window::Window>>,
|
||||
pub asset_manager: ModelManagerRef,
|
||||
pub debug_ui_buffer: Rc<RefCell<DebugUIBuffer>>,
|
||||
pub debug_ui_buffer: &'a DebugUIBuffer,
|
||||
pub env_light_dir: Vec3,
|
||||
}
|
||||
|
||||
|
|
@ -35,6 +35,8 @@ pub trait RenderingSystem {
|
|||
Self: Sized;
|
||||
}
|
||||
|
||||
// define_typemap!(RenderingSystemManager, RenderingSystem);
|
||||
|
||||
pub struct RenderingSystemManager {
|
||||
pub systems: IndexMap<TypeId, Box<dyn RenderingSystem>>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue
I'm wondering if I should put scene inside EngineResources instead 🤔