This commit is contained in:
LunarAkai 2025-08-05 13:15:19 +02:00
commit b873398a79
8 changed files with 121 additions and 68 deletions

8
Cargo.lock generated
View file

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

View file

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

View file

@ -0,0 +1 @@
pub mod world;

View file

@ -0,0 +1,30 @@
use crate::nodes::node::Node;
#[derive(Debug, Clone)]
pub struct World {
nodes: Vec<Box<dyn Node>>
}
impl World {
pub fn new() -> Self {
Self {
nodes: vec![]
}
}
pub fn add_node(&mut self, node: Box<dyn Node>) {
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();
}
}
}

View file

@ -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] -> <Systems> -> <Nodes> (-> <Nodes> -> ... )
//-------------
// [ ] => only 1 --- < > => allow multiple
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Game {
pub systems: Option<Vec<Box<dyn System>>>,
pub world: Vec<World>,
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<dyn System>) {
if self.systems.is_none() {
let a: Vec<Box<dyn System>> = vec![];
self.systems = Some(a);
pub fn get_worlds(self) -> Vec<World> {
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<moonhare_graphics::glium::backend::Context>;
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 {
<World as Clone>::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<Context>) {
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);
}

View file

@ -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<T> Node for Box<T> {
impl Debug for dyn Node {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
writeln!(f, "")
}
}
impl<T: Clone> Node for Box<T> {
fn init(&mut self) {
todo!()
}
fn update(&mut self) {
todo!()
}
}

View file

@ -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<String>
#[derive(Node, Clone)]
pub struct Window {
context: moonhare_window::WindowRenderContext,
glfw_window: Option<moonhare_window::platforms::glfw_window::GLFWWindow>,
render_context: Option<Rc<Context>>
}
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<Context>) {
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);
}

View file

@ -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()));
}