From 237dd614a2960246c9845253b8fc5d5f7115c4b8 Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Mon, 28 Jul 2025 19:50:49 +0200 Subject: [PATCH] added log wrapper --- .gitignore | 1 + Cargo.lock | 53 +++++++++++- Cargo.toml | 1 + moonhare_engine/Cargo.toml | 5 +- moonhare_engine/src/core/game.rs | 60 +++---------- moonhare_engine/src/window/winit_window.rs | 28 ++++-- moonhare_graphics/Cargo.toml | 7 ++ .../src/gpu_buffer.rs | 0 moonhare_graphics/src/graphics_server.rs | 0 moonhare_graphics/src/lib.rs | 4 + moonhare_graphics/src/shader.rs | 4 + moonhare_log/Cargo.toml | 10 +++ moonhare_log/src/lib.rs | 86 +++++++++++++++++++ playground/Cargo.toml | 1 + playground/src/main.rs | 12 ++- 15 files changed, 213 insertions(+), 59 deletions(-) create mode 100644 moonhare_graphics/Cargo.toml rename moonhare_engine/src/graphics/mod.rs => moonhare_graphics/src/gpu_buffer.rs (100%) create mode 100644 moonhare_graphics/src/graphics_server.rs create mode 100644 moonhare_graphics/src/lib.rs create mode 100644 moonhare_graphics/src/shader.rs create mode 100644 moonhare_log/Cargo.toml create mode 100644 moonhare_log/src/lib.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..1a4595e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +*.log diff --git a/Cargo.lock b/Cargo.lock index c20498d..b8a5d84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,6 +221,16 @@ dependencies = [ "libc", ] +[[package]] +name = "colored" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" +dependencies = [ + "lazy_static", + "windows-sys 0.52.0", +] + [[package]] name = "combine" version = "4.6.7" @@ -345,6 +355,16 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "fern" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4316185f709b23713e41e3195f90edef7fb00c3ed4adc79769cf09cc762a3b29" +dependencies = [ + "colored", + "log", +] + [[package]] name = "fnv" version = "1.0.7" @@ -512,6 +532,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" +[[package]] +name = "humantime" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" + [[package]] name = "indexmap" version = "2.10.0" @@ -570,6 +596,12 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.174" @@ -652,7 +684,23 @@ dependencies = [ name = "moonhare_engine" version = "0.1.0" dependencies = [ + "fern", "glium", + "log", + "winit", +] + +[[package]] +name = "moonhare_graphics" +version = "0.1.0" + +[[package]] +name = "moonhare_log" +version = "0.1.0" +dependencies = [ + "fern", + "humantime", + "log", ] [[package]] @@ -1030,6 +1078,7 @@ version = "0.1.0" dependencies = [ "glium", "moonhare_engine", + "moonhare_log", ] [[package]] @@ -1877,9 +1926,9 @@ checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" [[package]] name = "winit" -version = "0.30.11" +version = "0.30.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4409c10174df8779dc29a4788cac85ed84024ccbc1743b776b21a520ee1aaf4" +checksum = "c66d4b9ed69c4009f6321f762d6e61ad8a2389cd431b97cb1e146812e9e6c732" dependencies = [ "ahash", "android-activity", diff --git a/Cargo.toml b/Cargo.toml index c75a41c..2288eab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ resolver = "3" members = [ "moonhare_engine", + "moonhare_graphics", "moonhare_log", "playground" ] diff --git a/moonhare_engine/Cargo.toml b/moonhare_engine/Cargo.toml index 7102990..898d396 100644 --- a/moonhare_engine/Cargo.toml +++ b/moonhare_engine/Cargo.toml @@ -4,4 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -glium = "0.36.0" \ No newline at end of file +glium = "0.36.0" +log = "0.4" +fern = "0.7" +winit = "0.30.12" diff --git a/moonhare_engine/src/core/game.rs b/moonhare_engine/src/core/game.rs index 1d5228c..de40a67 100644 --- a/moonhare_engine/src/core/game.rs +++ b/moonhare_engine/src/core/game.rs @@ -1,30 +1,28 @@ use std::{ops::{ControlFlow, DerefMut}, sync::Mutex}; -use glium::{glutin::surface::WindowSurface, winit::{self, event::{self, WindowEvent}, event_loop::{self, EventLoop}, window::Window}}; +use glium::{glutin::{api::egl::surface::Surface, surface::WindowSurface}, winit::{self, event::{self, WindowEvent}, event_loop::{self, EventLoop}, window::Window}, Display}; +use winit::application::ApplicationHandler; -use crate::{game_plugin::GamePlugin, window::winit_window::WinitWindow, ENGINE_NAME}; +use crate::{core::game, game_plugin::GamePlugin, window::winit_window::WinitWindow, ENGINE_NAME}; pub struct Game { pub running: bool, pub game_plugin: Option>, - pub window: Window, - pub display: glium::Display, - pub event_loop: EventLoop<()> + pub window: WinitWindow, } impl Game { pub fn new() -> Self { let _event_loop: EventLoop<()> = EventLoop::new().unwrap(); - let _window = WinitWindow::construct_window(&_event_loop); + let mut game_window = WinitWindow::default(); + _event_loop.run_app(&mut game_window); Game { running: true, game_plugin: None, - window: _window.0, - display: _window.1, - event_loop: _event_loop, + window: game_window, } } @@ -33,8 +31,6 @@ impl Game { } pub fn init(&mut self) { - self.window.set_fullscreen(None); - self.window.set_decorations(true); if let Some(ref mut game_plugin) = self.game_plugin { game_plugin.init(); } else { @@ -49,13 +45,13 @@ impl Game { } pub fn render(&mut self) { - let mut target = self.display.draw(); + //let mut target = display.draw(); - if let Some(ref mut game_plugin) = self.game_plugin { - game_plugin.render(&mut target); - } + //if let Some(ref mut game_plugin) = self.game_plugin { + // game_plugin.render(&mut target); + //} - target.finish().unwrap(); + //target.finish().unwrap(); } pub fn cleanup(&mut self) { @@ -72,36 +68,4 @@ impl Game { self.cleanup(); } - - pub fn get_display(&self) -> &glium::Display{ - &self.display - } - - pub fn get_window(self) -> Window { - self.window - } - - pub fn handle_events(&mut self, event: winit::event::Event<()>) { - if let Some(ref mut game_plugin) = self.game_plugin { - game_plugin.handle_events(); - } - match event { - glium::winit::event::Event::WindowEvent { event, .. } => match event { - glium::winit::event::WindowEvent::CloseRequested => { - - }, - glium::winit::event::WindowEvent::Resized(window_size) => { - - }, - glium::winit::event::WindowEvent::RedrawRequested => { - - }, - _ => (), - } - glium::winit::event::Event::AboutToWait => { - - }, - _ => (), - } - } } diff --git a/moonhare_engine/src/window/winit_window.rs b/moonhare_engine/src/window/winit_window.rs index 359b86a..7cec57c 100644 --- a/moonhare_engine/src/window/winit_window.rs +++ b/moonhare_engine/src/window/winit_window.rs @@ -1,15 +1,19 @@ -use glium::{backend::glutin::SimpleWindowBuilder, glutin::{display::GetGlDisplay, surface::WindowSurface}, winit::{self, dpi::LogicalSize, event_loop::{ActiveEventLoop, EventLoop}, raw_window_handle::HasDisplayHandle, window::{Window, WindowAttributes}}, Display}; +use glium::{backend::{glutin::SimpleWindowBuilder, Context}, glutin::{display::GetGlDisplay, prelude::GlContext, surface::WindowSurface}, winit::{self, dpi::LogicalSize, event_loop::{ActiveEventLoop, EventLoop}, raw_window_handle::HasDisplayHandle, window::{Window, WindowAttributes}}, Display}; +use winit::application::ApplicationHandler; use crate::window::window_config::WindowConfig; use crate::ENGINE_NAME; -pub struct WinitWindow {} +#[derive(Default)] +pub struct WinitWindow { + pub window: Option, +} -impl WinitWindow { - /// constructs a new winit window - pub fn construct_window(event_loop: &EventLoop<()>) -> (Window, Display) { + +impl ApplicationHandler for WinitWindow { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { let config = WindowConfig::default(); let mut window_attributes = WindowAttributes::default(); @@ -25,8 +29,18 @@ impl WinitWindow { window_attributes = window_attributes.with_visible(true); window_attributes = winit::platform::wayland::WindowAttributesExtWayland::with_name(window_attributes, ENGINE_NAME, ""); - - SimpleWindowBuilder::new().set_window_builder(window_attributes).build(event_loop) + + + self.window = Some(event_loop.create_window(window_attributes).unwrap()); + } + + fn window_event( + &mut self, + event_loop: &ActiveEventLoop, + window_id: winit::window::WindowId, + event: winit::event::WindowEvent, + ) { + todo!() } } diff --git a/moonhare_graphics/Cargo.toml b/moonhare_graphics/Cargo.toml new file mode 100644 index 0000000..e49a21d --- /dev/null +++ b/moonhare_graphics/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "moonhare_graphics" +edition = "2024" +version.workspace = true +authors.workspace = true + +[dependencies] diff --git a/moonhare_engine/src/graphics/mod.rs b/moonhare_graphics/src/gpu_buffer.rs similarity index 100% rename from moonhare_engine/src/graphics/mod.rs rename to moonhare_graphics/src/gpu_buffer.rs diff --git a/moonhare_graphics/src/graphics_server.rs b/moonhare_graphics/src/graphics_server.rs new file mode 100644 index 0000000..e69de29 diff --git a/moonhare_graphics/src/lib.rs b/moonhare_graphics/src/lib.rs new file mode 100644 index 0000000..f3412ac --- /dev/null +++ b/moonhare_graphics/src/lib.rs @@ -0,0 +1,4 @@ +/// Crate for providing an abstraction layer over different graphics APIs + +pub mod graphics_server; +pub mod shader; \ No newline at end of file diff --git a/moonhare_graphics/src/shader.rs b/moonhare_graphics/src/shader.rs new file mode 100644 index 0000000..8d5f719 --- /dev/null +++ b/moonhare_graphics/src/shader.rs @@ -0,0 +1,4 @@ +pub enum ShaderType { + Vertex, + Fragment, +} \ No newline at end of file diff --git a/moonhare_log/Cargo.toml b/moonhare_log/Cargo.toml new file mode 100644 index 0000000..5995ea6 --- /dev/null +++ b/moonhare_log/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "moonhare_log" +edition = "2024" +version.workspace = true +authors.workspace = true + +[dependencies] +log = "*" +fern = { version = "0.7", features = ["colored"] } +humantime = "*" \ No newline at end of file diff --git a/moonhare_log/src/lib.rs b/moonhare_log/src/lib.rs new file mode 100644 index 0000000..88791d9 --- /dev/null +++ b/moonhare_log/src/lib.rs @@ -0,0 +1,86 @@ +/// Wrapper around `log` and `fern` crates +/// +use std::{fmt::Display, io, time::SystemTime}; + +/// Configures the Log Output Settings +pub fn configere_logger() -> Result<(), fern::InitError>{ + let base_config = fern::Dispatch::new(); + + // configure colors for the whole line + let colors_line = fern::colors::ColoredLevelConfig::new() + .error(fern::colors::Color::Red) + .warn(fern::colors::Color::Yellow) + // we actually don't need to specify the color for debug and info, they are white by default + .info(fern::colors::Color::TrueColor { r: 85, g: 85, b: 85 }) + .debug(fern::colors::Color::White) + // depending on the terminals color scheme, this is the same as the background color + .trace(fern::colors::Color::Black); + + // Separate file config so we can include year, month and day in file logs + let file_config = fern::Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "[{} {} {}] {}", + humantime::format_rfc3339_seconds(SystemTime::now()), + record.level(), + record.target(), + message + )) + }) + .chain(fern::log_file("moonhare_engine.log")?); + + + let stdout_config = fern::Dispatch::new() + .format(move |out, message, record| { + // special format for debug messages coming from our own crate. + if record.level() > log::LevelFilter::Info && record.target() == "cmd_program" { + out.finish(format_args!( + "DEBUG @ {}: {}", + humantime::format_rfc3339_seconds(SystemTime::now()), + message + )) + } else { + out.finish(format_args!( + "{color_line}[{date} {level} {target}] {message}\x1B[0m", + color_line = format_args!( + "\x1B[{}m", + colors_line.get_color(&record.level()).to_fg_str() + ), + date = humantime::format_rfc3339_seconds(SystemTime::now()), + level = record.level(), + target = record.target(), + message = message + )) + } + }) + .chain(io::stdout()); + + base_config + .chain(file_config) + .chain(stdout_config) + .apply()?; + + Ok(()) +} + +pub fn mh_info(arg: T) { + log::info!("{}", arg); +} + +pub fn mh_warn(arg: T) { + log::warn!("{}", arg); +} + +pub fn mh_debug(arg: T) { + log::debug!("{}", arg); +} + +pub fn mh_trace(arg: T) { + log::trace!("{}", arg); +} + +pub fn mh_error(arg: T) { + log::error!("{}", arg); +} + + diff --git a/playground/Cargo.toml b/playground/Cargo.toml index 6e08b67..76ef8c0 100644 --- a/playground/Cargo.toml +++ b/playground/Cargo.toml @@ -5,4 +5,5 @@ edition = "2024" [dependencies] moonhare_engine = { path = "../moonhare_engine" } +moonhare_log = { path = "../moonhare_log" } glium = "0.36.0" diff --git a/playground/src/main.rs b/playground/src/main.rs index f5f61b5..51bb8e1 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -3,6 +3,7 @@ use std::fs::read_to_string; use glium::{index::NoIndices, Frame, Program, VertexBuffer}; use glium::{uniform, Surface}; use moonhare_engine::{core::game::Game, game_plugin::GamePlugin, vertex::Vertex}; +use moonhare_log::{mh_debug, mh_error, mh_info, mh_trace, mh_warn}; struct PlaygroundGame { t: f32, @@ -65,7 +66,14 @@ impl GamePlugin for PlaygroundGame { fn main() { - + let _ = moonhare_log::configere_logger(); + mh_info("Blub"); + mh_debug("blub"); + mh_warn("blug"); + mh_trace("trace"); + mh_error("error"); + println!("Blaa"); +/* let mut game = Game::new(); game.register_plugin(Box::new(PlaygroundGame{ t: 0.0, shape: Default::default(), vertex_buffer: None, indices: None, program: None })); game.init(); @@ -131,4 +139,6 @@ fn main() { game.register_plugin(Box::new(pg_game)); game.run(); + +*/ } \ No newline at end of file