diff --git a/Cargo.lock b/Cargo.lock index 91fec58..76b94f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -402,6 +402,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "cmake" +version = "0.1.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" +dependencies = [ + "cc", +] + [[package]] name = "colored" version = "2.2.0" @@ -844,6 +853,28 @@ dependencies = [ "serde", ] +[[package]] +name = "glfw" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8c61a3f08ac5eb93c8dc0e9f2e6b2c7a7d14da089db39d43d696bc4fd025d4c" +dependencies = [ + "bitflags 1.3.2", + "glfw-sys", + "objc2", + "raw-window-handle", + "winapi", +] + +[[package]] +name = "glfw-sys" +version = "5.0.0+3.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dfc32d45fb58ff38b112696907963a7d671e9cf742b16f882062169a053cf88" +dependencies = [ + "cmake", +] + [[package]] name = "glib" version = "0.21.0" @@ -1238,6 +1269,7 @@ dependencies = [ name = "moonhare_window" version = "0.1.0" dependencies = [ + "glfw", "gtk4", "moonhare_event", "moonhare_log", @@ -2362,6 +2394,22 @@ dependencies = [ "web-sys", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -2371,6 +2419,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/crates/moonhare_game/src/lib.rs b/crates/moonhare_game/src/lib.rs index 3d25f60..a95fcd1 100644 --- a/crates/moonhare_game/src/lib.rs +++ b/crates/moonhare_game/src/lib.rs @@ -18,7 +18,7 @@ impl Default for Game { Self { is_running: true, name: default_game_name(), - context: WindowRenderContext::OPENGLWINIT + context: WindowRenderContext::OPENGLGLFW } } } diff --git a/crates/moonhare_window/Cargo.toml b/crates/moonhare_window/Cargo.toml index ae37cc2..8c47bf0 100644 --- a/crates/moonhare_window/Cargo.toml +++ b/crates/moonhare_window/Cargo.toml @@ -8,4 +8,5 @@ authors.workspace = true moonhare_log = { path = "../moonhare_log" } moonhare_event = { path = "../moonhare_event" } gtk4 = "*" -winit = "0.30" \ No newline at end of file +winit = "0.30" +glfw = "*" \ No newline at end of file diff --git a/crates/moonhare_window/src/lib.rs b/crates/moonhare_window/src/lib.rs index 988be09..6312215 100644 --- a/crates/moonhare_window/src/lib.rs +++ b/crates/moonhare_window/src/lib.rs @@ -7,14 +7,14 @@ pub enum WindowRenderContext { VULKANGTK, // TODO OPENGLGTK, OPENGLWINIT, + OPENGLGLFW, } pub trait WindowResult { } pub trait MoonhareWindow { - type WindowResult; - fn init() -> Self::WindowResult; + fn init(); fn on_update(); fn shutdown(); } @@ -33,23 +33,21 @@ impl Window { todo!() }, WindowRenderContext::OPENGLGTK => { - use std::thread; - - use gtk4::gio::prelude::ApplicationExtManual; - - use crate::platforms::gtk_window::GTKWindow; - - thread::spawn(|| { - moonhare_log::info("Created GTK Window thread"); - let application = GTKWindow::init(); - application.get_application().run(); - }); + todo!() }, WindowRenderContext::OPENGLWINIT => { use crate::platforms::winit_window::WinitWindow; - moonhare_log::info("Creating Winit OpenGL Winit"); - let application = WinitWindow::init(); + moonhare_log::info("Creating Winit OpenGL Window"); + WinitWindow::init(); + }, + WindowRenderContext::OPENGLGLFW => { + std::thread::spawn(|| { + use crate::platforms::glfw_window::GLFWWindow; + moonhare_log::info("Creating GLFW OpenGL Window"); + GLFWWindow::init(); + }); + } } } diff --git a/crates/moonhare_window/src/platforms/glfw_window.rs b/crates/moonhare_window/src/platforms/glfw_window.rs new file mode 100644 index 0000000..116ee9b --- /dev/null +++ b/crates/moonhare_window/src/platforms/glfw_window.rs @@ -0,0 +1,77 @@ +use std::sync::Arc; + +use glfw::{Context, Glfw, GlfwReceiver, PWindow, WindowEvent}; +use gtk4::gdk::Key; +use moonhare_event::{event::{self, Event}, events::window_events::window_close_event::WindowCloseEvent}; + +use crate::{window_config, MoonhareWindow}; + +#[derive(Debug)] +pub struct GLFWWindow { + glfw_window: PWindow, + events: GlfwReceiver<(f64, WindowEvent)>, + glfw: Glfw, + is_running: bool, +} + +const APP_ID: &str = "de.lunarakai.moonhare_engine"; + +impl GLFWWindow { + fn new() -> Self { + let mut glfw = glfw::init(glfw::fail_on_errors).unwrap(); + let config = window_config::WindowConfig::default(); + let (mut window, events) = glfw.create_window( + config.width, + config.height, + format!("{} GLFW", config.title).as_str(), + glfw::WindowMode::Windowed) + .unwrap(); + + window.set_key_polling(true); + window.make_current(); + + Self { + glfw_window: window, + events: events, + glfw: glfw, + is_running: true + } + } + + fn run_window(&mut self) { + while !self.glfw_window.should_close() { + self.glfw.poll_events(); + for(_, event) in glfw::flush_messages(&self.events) { + Self::handle_window_event(&mut self.glfw_window, event); + } + } + } + + fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { + match event { + glfw::WindowEvent::Close => { + WindowCloseEvent::emit(); + } + _ => {}, + } + } + +} + +impl MoonhareWindow for GLFWWindow { + fn init() { + let mut window = GLFWWindow::new(); + GLFWWindow::run_window(&mut window); + } + + + fn on_update() { + + } + + fn shutdown() { + // todo: emit WindowCloseEvent + + } +} + diff --git a/crates/moonhare_window/src/platforms/gtk_window.rs b/crates/moonhare_window/src/platforms/gtk_window.rs index af21ae3..3990b33 100644 --- a/crates/moonhare_window/src/platforms/gtk_window.rs +++ b/crates/moonhare_window/src/platforms/gtk_window.rs @@ -39,15 +39,14 @@ impl GTKWindow { } impl MoonhareWindow for GTKWindow { - type WindowResult = GTKWindow; - fn init() -> Self::WindowResult { + fn init() { let app = Application::builder().application_id(APP_ID).build(); app.connect_activate(GTKWindow::build_ui); Self { application: app - } + }; } diff --git a/crates/moonhare_window/src/platforms/mod.rs b/crates/moonhare_window/src/platforms/mod.rs index d502f48..3e038fd 100644 --- a/crates/moonhare_window/src/platforms/mod.rs +++ b/crates/moonhare_window/src/platforms/mod.rs @@ -1,2 +1,3 @@ pub mod gtk_window; -pub mod winit_window; \ No newline at end of file +pub mod winit_window; +pub mod glfw_window; \ No newline at end of file diff --git a/crates/moonhare_window/src/platforms/winit_window.rs b/crates/moonhare_window/src/platforms/winit_window.rs index 9094209..9eb5491 100644 --- a/crates/moonhare_window/src/platforms/winit_window.rs +++ b/crates/moonhare_window/src/platforms/winit_window.rs @@ -61,15 +61,11 @@ impl ApplicationHandler for WinitWindow { impl MoonhareWindow for WinitWindow { - type WindowResult = WinitWindow; - fn init() -> Self::WindowResult { + fn init() { let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Poll); let mut app = WinitWindow::default(); let _ = event_loop.run_app(&mut app); - - // Need to find a better way because this will not return until the window is closed - app }