figuring out windows, eventually...

This commit is contained in:
LunarAkai 2025-08-03 11:28:34 +02:00
commit ceac5a09bb
9 changed files with 2033 additions and 8 deletions

1930
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -14,8 +14,8 @@ impl Event for KeyPressedEvent {
EventType::KeyPressed
}
fn emit(&self) -> &impl Event{
self
fn emit() {
}
}

View file

@ -18,7 +18,7 @@ impl Default for Game {
Self {
is_running: true,
name: default_game_name(),
context: WindowRenderContext::OPENGLGTK
context: WindowRenderContext::OPENGLWINIT
}
}
}

View file

@ -5,6 +5,8 @@ version.workspace = true
authors.workspace = true
[dependencies]
bevy_ecs = "0.16.1"
moonhare_event = { path = "../moonhare_event" }
moonhare_game = { path = "../moonhare_game" }
moonhare_graphics = { path = "../moonhare_graphics" }

View file

@ -1,3 +1,6 @@
// Writing an ecs from scratch seems hard, might do it in the future though
pub use bevy_ecs as ecs;
pub use moonhare_event as event;
pub use moonhare_game as game;
pub use moonhare_graphics as graphics;

View file

@ -7,4 +7,5 @@ authors.workspace = true
[dependencies]
moonhare_log = { path = "../moonhare_log" }
moonhare_event = { path = "../moonhare_event" }
gtk4 = "*"
gtk4 = "*"
winit = "0.30"

View file

@ -6,6 +6,7 @@ pub mod platforms;
pub enum WindowRenderContext {
VULKANGTK, // TODO
OPENGLGTK,
OPENGLWINIT,
}
pub trait WindowResult {
@ -39,11 +40,17 @@ impl Window {
use crate::platforms::gtk_window::GTKWindow;
thread::spawn(|| {
moonhare_log::info("Created Window thread");
moonhare_log::info("Created GTK Window thread");
let application = GTKWindow::init();
application.get_application().run();
});
},
WindowRenderContext::OPENGLWINIT => {
use crate::platforms::winit_window::WinitWindow;
moonhare_log::info("Creating Winit OpenGL Winit");
let application = WinitWindow::init();
}
}
}

View file

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

View file

@ -0,0 +1,85 @@
use std::sync::Arc;
use winit::{application::ApplicationHandler, event::WindowEvent, event_loop::{ActiveEventLoop, ControlFlow, EventLoop}, window::Window};
use crate::MoonhareWindow;
#[derive(Debug, Default)]
pub struct WinitWindow {
window: Option<Arc<Window>>,
}
impl WinitWindow {
pub fn new() -> Self {
Self {
window: None,
}
}
}
const APP_ID: &str = "de.lunarakai.moonhare_engine";
impl ApplicationHandler for WinitWindow {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
self.window = Some(Arc::new(event_loop.create_window(Window::default_attributes()).unwrap()));
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
println!("The close button was pressed; stopping");
event_loop.exit();
},
WindowEvent::RedrawRequested => {
// Redraw the application.
//
// It's preferable for applications that do not render continuously to render in
// this event rather than in AboutToWait, since rendering in here allows
// the program to gracefully handle redraws requested by the OS.
// Draw.
// Queue a RedrawRequested event.
//
// You only need to call this if you've determined that you need to redraw in
// applications which do not always need to. Applications that redraw continuously
// can render here instead.
self.window.as_ref().unwrap().request_redraw();
}
_ => (),
}
}
}
impl MoonhareWindow for WinitWindow {
type WindowResult = WinitWindow;
fn init() -> Self::WindowResult {
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
}
fn on_update() {
}
fn shutdown() {
// todo: emit WindowCloseEvent
}
}