i think i'm sticking with glfw until i change my mind lol

This commit is contained in:
LunarAkai 2025-08-03 13:48:44 +02:00
commit 06bee7d20c
8 changed files with 152 additions and 26 deletions

View file

@ -8,4 +8,5 @@ authors.workspace = true
moonhare_log = { path = "../moonhare_log" }
moonhare_event = { path = "../moonhare_event" }
gtk4 = "*"
winit = "0.30"
winit = "0.30"
glfw = "*"

View file

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

View file

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

View file

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

View file

@ -1,2 +1,3 @@
pub mod gtk_window;
pub mod winit_window;
pub mod winit_window;
pub mod glfw_window;

View file

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