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

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