WINDOW RUNS IN THE GAME LOOP; THIS IS NOT A DRILL :3
This commit is contained in:
parent
06bee7d20c
commit
5be528cf07
7 changed files with 42 additions and 192 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
//! Base functionality for a Moonhare Game Engine Project
|
//! Base functionality for a Moonhare Game Engine Project
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use moonhare_log::*;
|
use moonhare_log::*;
|
||||||
use moonhare_window::{Window, WindowRenderContext};
|
use moonhare_window::{glfw, platforms::glfw_window::GLFWWindow, Window, WindowRenderContext};
|
||||||
|
|
||||||
pub mod basic;
|
pub mod basic;
|
||||||
|
|
||||||
|
|
@ -11,6 +13,7 @@ pub struct Game {
|
||||||
pub is_running: bool,
|
pub is_running: bool,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub context: WindowRenderContext,
|
pub context: WindowRenderContext,
|
||||||
|
pub glfw_window: Option<GLFWWindow>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Game {
|
impl Default for Game {
|
||||||
|
|
@ -18,7 +21,8 @@ impl Default for Game {
|
||||||
Self {
|
Self {
|
||||||
is_running: true,
|
is_running: true,
|
||||||
name: default_game_name(),
|
name: default_game_name(),
|
||||||
context: WindowRenderContext::OPENGLGLFW
|
context: WindowRenderContext::OPENGLGLFW,
|
||||||
|
glfw_window: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -28,16 +32,32 @@ impl Game {
|
||||||
Game::default()
|
Game::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&self) {
|
/*
|
||||||
info("Running Game...");
|
|
||||||
while self.is_running {
|
|
||||||
|
|
||||||
|
pub fn run_window(mut glfw_w: GLFWWindow) {
|
||||||
|
while !glfw_w.glfw_window.should_close() {
|
||||||
|
glfw_w.glfw.poll_events();
|
||||||
|
for(_, event) in glfw::flush_messages(&glfw_w.events) {
|
||||||
|
Self::handle_window_event(&mut glfw_w.glfw_window, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
pub fn run(self) {
|
||||||
|
info("Running Game...");
|
||||||
|
let mut a = self.glfw_window.unwrap();
|
||||||
|
|
||||||
|
while self.is_running {
|
||||||
|
a.glfw_window.glfw.poll_events();
|
||||||
|
for (_, event) in glfw::flush_messages(&a.events) {
|
||||||
|
GLFWWindow::handle_window_event(&mut a.glfw_window, event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_window(&mut self) {
|
pub fn add_window(&mut self) {
|
||||||
moonhare_log::info(format!("Adding window to {:?}", self));
|
moonhare_log::info(format!("Adding window to {:?}", self));
|
||||||
Window::create(self.context);
|
self.glfw_window =Some(Window::create(self.context).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
//! Provides functionality to create either a vulkan or opengl window
|
//! Provides functionality to create either a vulkan or opengl window
|
||||||
pub mod window_config;
|
pub mod window_config;
|
||||||
pub mod platforms;
|
pub mod platforms;
|
||||||
|
pub use glfw as glfw;
|
||||||
|
|
||||||
|
use crate::platforms::glfw_window::GLFWWindow;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum WindowRenderContext {
|
pub enum WindowRenderContext {
|
||||||
VULKANGTK, // TODO
|
|
||||||
OPENGLGTK,
|
|
||||||
OPENGLWINIT,
|
|
||||||
OPENGLGLFW,
|
OPENGLGLFW,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -14,7 +14,7 @@ pub trait WindowResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait MoonhareWindow {
|
pub trait MoonhareWindow {
|
||||||
fn init();
|
fn init() -> GLFWWindow;
|
||||||
fn on_update();
|
fn on_update();
|
||||||
fn shutdown();
|
fn shutdown();
|
||||||
}
|
}
|
||||||
|
|
@ -24,30 +24,14 @@ pub struct Window {
|
||||||
|
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
/// creates a gtk4 window while spaning a new thread that the window runs on.
|
/// creates a glfw window while spawning a new thread that the window runs on.
|
||||||
/// here: gtk sends engine events when _things happen_ with the window that other engine parts can interact with
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub fn create(context: WindowRenderContext) {
|
pub fn create(context: WindowRenderContext) -> GLFWWindow {
|
||||||
match context {
|
match context {
|
||||||
WindowRenderContext::VULKANGTK => {
|
|
||||||
todo!()
|
|
||||||
},
|
|
||||||
WindowRenderContext::OPENGLGTK => {
|
|
||||||
todo!()
|
|
||||||
},
|
|
||||||
WindowRenderContext::OPENGLWINIT => {
|
|
||||||
use crate::platforms::winit_window::WinitWindow;
|
|
||||||
|
|
||||||
moonhare_log::info("Creating Winit OpenGL Window");
|
|
||||||
WinitWindow::init();
|
|
||||||
},
|
|
||||||
WindowRenderContext::OPENGLGLFW => {
|
WindowRenderContext::OPENGLGLFW => {
|
||||||
std::thread::spawn(|| {
|
|
||||||
use crate::platforms::glfw_window::GLFWWindow;
|
use crate::platforms::glfw_window::GLFWWindow;
|
||||||
moonhare_log::info("Creating GLFW OpenGL Window");
|
moonhare_log::info("Creating GLFW OpenGL Window");
|
||||||
GLFWWindow::init();
|
GLFWWindow::init()
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,10 @@ use crate::{window_config, MoonhareWindow};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct GLFWWindow {
|
pub struct GLFWWindow {
|
||||||
glfw_window: PWindow,
|
pub glfw_window: PWindow,
|
||||||
events: GlfwReceiver<(f64, WindowEvent)>,
|
pub events: GlfwReceiver<(f64, WindowEvent)>,
|
||||||
glfw: Glfw,
|
pub glfw: Glfw,
|
||||||
is_running: bool,
|
pub is_running: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
const APP_ID: &str = "de.lunarakai.moonhare_engine";
|
const APP_ID: &str = "de.lunarakai.moonhare_engine";
|
||||||
|
|
@ -38,16 +38,8 @@ impl GLFWWindow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
pub fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
|
||||||
match event {
|
match event {
|
||||||
glfw::WindowEvent::Close => {
|
glfw::WindowEvent::Close => {
|
||||||
WindowCloseEvent::emit();
|
WindowCloseEvent::emit();
|
||||||
|
|
@ -55,13 +47,12 @@ impl GLFWWindow {
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MoonhareWindow for GLFWWindow {
|
impl MoonhareWindow for GLFWWindow {
|
||||||
fn init() {
|
fn init() -> GLFWWindow {
|
||||||
let mut window = GLFWWindow::new();
|
let window = GLFWWindow::new();
|
||||||
GLFWWindow::run_window(&mut window);
|
window
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
use gtk4::{gio::{prelude::{ActionMapExtManual, ApplicationExt}, ActionEntry}, glib, prelude::{GtkWindowExt, WidgetExt}, Application, ApplicationWindow};
|
|
||||||
use moonhare_event::{event::Event, events::window_events::window_close_event::WindowCloseEvent};
|
|
||||||
|
|
||||||
use crate::{window_config, MoonhareWindow};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct GTKWindow {
|
|
||||||
application: Application,
|
|
||||||
}
|
|
||||||
|
|
||||||
const APP_ID: &str = "de.lunarakai.moonhare_engine";
|
|
||||||
|
|
||||||
impl GTKWindow {
|
|
||||||
pub fn get_application(self) -> Application {
|
|
||||||
self.application
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_ui(application: &Application) {
|
|
||||||
let window = ApplicationWindow::new(application);
|
|
||||||
let window_config = window_config::WindowConfig::default();
|
|
||||||
window.set_title(Some(format!("{} GTK", window_config.title).as_str()));
|
|
||||||
window.set_default_size(window_config.width as i32, window_config.height as i32);
|
|
||||||
window.set_visible(window_config.visble);
|
|
||||||
|
|
||||||
// Add action "close" to `window` taking no parameter
|
|
||||||
let action_close = ActionEntry::builder("close")
|
|
||||||
.activate(|window: &ApplicationWindow, _, _| {
|
|
||||||
GTKWindow::shutdown();
|
|
||||||
window.close();
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
window.add_action_entries([action_close]);
|
|
||||||
|
|
||||||
window.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MoonhareWindow for GTKWindow {
|
|
||||||
fn init() {
|
|
||||||
let app = Application::builder().application_id(APP_ID).build();
|
|
||||||
|
|
||||||
app.connect_activate(GTKWindow::build_ui);
|
|
||||||
|
|
||||||
Self {
|
|
||||||
application: app
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn on_update() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn shutdown() {
|
|
||||||
// todo: emit WindowCloseEvent
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,3 +1 @@
|
||||||
pub mod gtk_window;
|
|
||||||
pub mod winit_window;
|
|
||||||
pub mod glfw_window;
|
pub mod glfw_window;
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
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 {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn on_update() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn shutdown() {
|
|
||||||
// todo: emit WindowCloseEvent
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue