From b873398a79b6d801a9af22c6762b9576a946b4ad Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Tue, 5 Aug 2025 13:15:19 +0200 Subject: [PATCH] hmm --- Cargo.lock | 8 ++- crates/moonhare_game/Cargo.toml | 3 +- crates/moonhare_game/src/basic/mod.rs | 1 + crates/moonhare_game/src/basic/world.rs | 30 +++++++++++ crates/moonhare_game/src/lib.rs | 68 +++++++----------------- crates/moonhare_game/src/nodes/node.rs | 20 +++++-- crates/moonhare_game/src/nodes/window.rs | 45 +++++++++++++--- playground/src/main.rs | 14 ++--- 8 files changed, 121 insertions(+), 68 deletions(-) create mode 100644 crates/moonhare_game/src/basic/mod.rs create mode 100644 crates/moonhare_game/src/basic/world.rs diff --git a/Cargo.lock b/Cargo.lock index cec3774..c70ac3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,6 +363,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + [[package]] name = "equivalent" version = "1.0.2" @@ -800,8 +806,8 @@ version = "0.1.0" name = "moonhare_game" version = "0.1.0" dependencies = [ + "dyn-clone", "moonhare_derives", - "moonhare_ecs", "moonhare_graphics", "moonhare_log", "moonhare_window", diff --git a/crates/moonhare_game/Cargo.toml b/crates/moonhare_game/Cargo.toml index 1d5c8e2..4491996 100644 --- a/crates/moonhare_game/Cargo.toml +++ b/crates/moonhare_game/Cargo.toml @@ -6,8 +6,7 @@ authors.workspace = true [dependencies] moonhare_derives = { path = "../moonhare_derives" } -moonhare_ecs = { path = "../moonhare_ecs" } moonhare_graphics = { path = "../moonhare_graphics" } moonhare_log = { path = "../moonhare_log" } moonhare_window = { path = "../moonhare_window" } - +dyn-clone = "1.0.20" diff --git a/crates/moonhare_game/src/basic/mod.rs b/crates/moonhare_game/src/basic/mod.rs new file mode 100644 index 0000000..4627b64 --- /dev/null +++ b/crates/moonhare_game/src/basic/mod.rs @@ -0,0 +1 @@ +pub mod world; \ No newline at end of file diff --git a/crates/moonhare_game/src/basic/world.rs b/crates/moonhare_game/src/basic/world.rs new file mode 100644 index 0000000..2669bae --- /dev/null +++ b/crates/moonhare_game/src/basic/world.rs @@ -0,0 +1,30 @@ +use crate::nodes::node::Node; + +#[derive(Debug, Clone)] +pub struct World { + nodes: Vec> +} + +impl World { + pub fn new() -> Self { + Self { + nodes: vec![] + } + } + + pub fn add_node(&mut self, node: Box) { + self.nodes.push(node) + } + + pub fn init(self) { + for mut node in self.nodes { + node.init(); + } + } + + pub fn update(self) { + for mut node in self.nodes { + node.update(); + } + } +} \ No newline at end of file diff --git a/crates/moonhare_game/src/lib.rs b/crates/moonhare_game/src/lib.rs index eb81bdf..4b8f00a 100644 --- a/crates/moonhare_game/src/lib.rs +++ b/crates/moonhare_game/src/lib.rs @@ -7,9 +7,10 @@ use moonhare_graphics::{color::Color, glium::{backend::Context, glutin::api::egl use moonhare_log::*; use moonhare_window::{platforms::glfw_window::GLFWWindow}; -use crate::systems::system::{BaseSystems, System}; +use crate::{basic::world::{self, World}, systems::system::{BaseSystems, System}}; pub mod systems; +pub mod basic; pub mod nodes; /* #[derive(Debug)] pub struct Game { @@ -28,9 +29,9 @@ pub struct Game { // [Game] -> -> (-> -> ... ) //------------- // [ ] => only 1 --- < > => allow multiple -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Game { - pub systems: Option>>, + pub world: Vec, pub is_running: bool } @@ -38,7 +39,7 @@ pub struct Game { impl Default for Game { fn default() -> Self { Self { - systems: None, + world: vec![], is_running: true } } @@ -58,63 +59,34 @@ impl Game { pub fn new() -> Self { Game::default() } - - pub fn add_base_systems(&mut self) { - + pub fn add_world(&mut self, world: World) { + self.world.push(world); } - pub fn add_system(&mut self, system: Box) { - if self.systems.is_none() { - let a: Vec> = vec![]; - self.systems = Some(a); + pub fn get_worlds(self) -> Vec { + self.world + } + + pub fn init(&self) { + for world in &self.world { + world.clone().init(); } - &self.systems.as_mut().unwrap().push(system); } - pub fn run(self) { - - - info("Running Game..."); - //------------------------------ - // Run Init on all Systems - //------------------------------ - let glfw_window_unwrapped = self.glfw_window; - let mut graphics_handler: GraphicsHandler = GraphicsHandler { ..Default::default() }; - let context: std::rc::Rc; - - context = moonhare_graphics::build_context(glfw_window_unwrapped.clone().unwrap().glfw_window); - - graphics_handler.context = Some(context.clone()); - let mut value = glfw_window_unwrapped; + pub fn run(&mut self) { + let worlds = self.world.clone(); while self.is_running { - handle_window_event(value.as_mut().unwrap()); - render(context.clone()); - - self.base_systems.game_loop(); - // update(); + for world in &worlds { + ::clone(&world).update(); + } } } - pub fn add_window(&mut self) { - moonhare_log::info(format!("Adding window to {:?}", self)); - self.glfw_window =Some(moonhare_window::Window::create(self.context).into()); - } } + fn default_game_name() -> String { "Moonhare Game".to_owned() } -/// Deals with GLFW Window Events (in `monhare_window`) -fn handle_window_event(glfw_window: &GLFWWindow) { - glfw_window.glfw_window.borrow_mut().glfw.poll_events(); - for (_, event) in moonhare_window::glfw::flush_messages(&glfw_window.events.borrow()) { - moonhare_window::platforms::glfw_window::GLFWWindow::handle_window_event(&glfw_window, event); - } -} - -fn render(context: Rc) { - let target = moonhare_graphics::glium::Frame::new(context.clone(), context.get_framebuffer_dimensions()); - moonhare_graphics::draw_background_color(Color::color_from_rgb(255, 255, 255), target); -} diff --git a/crates/moonhare_game/src/nodes/node.rs b/crates/moonhare_game/src/nodes/node.rs index 8ef7393..3408688 100644 --- a/crates/moonhare_game/src/nodes/node.rs +++ b/crates/moonhare_game/src/nodes/node.rs @@ -1,16 +1,28 @@ +use dyn_clone::DynClone; use moonhare_derives::Node; +use std::fmt::{Debug, Formatter, Result}; -pub trait Node { +pub trait Node: DynClone { fn init(&mut self); fn update(&mut self); } +dyn_clone::clone_trait_object!(Node); -impl Node for Box { + + + +impl Debug for dyn Node { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + writeln!(f, "") + } +} + +impl Node for Box { fn init(&mut self) { - todo!() + } fn update(&mut self) { - todo!() + } } diff --git a/crates/moonhare_game/src/nodes/window.rs b/crates/moonhare_game/src/nodes/window.rs index 7dbc7b2..56b7020 100644 --- a/crates/moonhare_game/src/nodes/window.rs +++ b/crates/moonhare_game/src/nodes/window.rs @@ -1,17 +1,50 @@ +use std::rc::Rc; + use moonhare_derives::Node; +use moonhare_graphics::{color::Color, glium::backend::Context}; +use moonhare_window::{glfw::RenderContext, platforms::glfw_window::{self, GLFWWindow}}; use crate::nodes::node::Node; -#[derive(Node)] -struct Window { - a: Box +#[derive(Node, Clone)] +pub struct Window { + context: moonhare_window::WindowRenderContext, + glfw_window: Option, + render_context: Option> +} + +impl Default for Window { + fn default() -> Self { + Self { + context: moonhare_window::WindowRenderContext::OPENGLGLFW, + glfw_window: None, + render_context: None + } + } } impl Window { - fn init(&mut self) { - + fn init(&mut self) -> Self { + Self { + context: moonhare_window::WindowRenderContext::OPENGLGLFW, + glfw_window: Some(moonhare_window::Window::create(self.context)), + render_context: Some(moonhare_graphics::build_context(self.glfw_window.clone().unwrap().glfw_window)) + } } fn update(&mut self) { - + handle_window_event(&self.glfw_window.as_mut().unwrap()); + render(self.render_context.clone().unwrap()); } +} + +fn handle_window_event(glfw_window: &GLFWWindow) { + glfw_window.glfw_window.borrow_mut().glfw.poll_events(); + for (_, event) in moonhare_window::glfw::flush_messages(&glfw_window.events.borrow()) { + moonhare_window::platforms::glfw_window::GLFWWindow::handle_window_event(&glfw_window, event); + } +} + +fn render(context: Rc) { + let target = moonhare_graphics::glium::Frame::new(context.clone(), context.get_framebuffer_dimensions()); + moonhare_graphics::draw_background_color(Color::color_from_rgb(255, 255, 255), target); } \ No newline at end of file diff --git a/playground/src/main.rs b/playground/src/main.rs index d195a49..47c3069 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -1,6 +1,6 @@ use std::fs::File; -use moonhare_engine::{game::Game, log}; +use moonhare_engine::{game::{basic::world::World, nodes::window::Window, Game}, log}; fn main() { @@ -8,10 +8,10 @@ fn main() { log::info("test"); let mut game = Game::new(); - game.add_window(); - - log::info(format!("Game: {:?}", game)); - - // Enters Loop - game.run(); + let mut world = World::new(); + + let window = Window::default(); + world.add_node(Box::new(window)); + game.add_world(world.clone()); + log::info(format!("{:?}", game.get_worlds())); } \ No newline at end of file