opengl window

This commit is contained in:
LunarAkai 2025-08-01 12:14:43 +02:00
commit 30ff2325f2
8 changed files with 367 additions and 102 deletions

View file

@ -5,5 +5,6 @@ version.workspace = true
authors.workspace = true
[dependencies]
winit = "0.30.12"
ash = "0.38"
glium = "0.36"
moonhare_log = { path = "../moonhare_log" }

View file

@ -1,3 +1,35 @@
//! Provides an abstraction above Winit functionality
//! Provides functionality to create either a vulkan or opengl window
use crate::opengl_window::create_open_gl_window;
pub mod window_config;
pub mod winit_window;
pub mod opengl_window;
#[derive(Debug, Clone, Copy)]
pub enum WindowRenderContext {
VULKAN, // TODO
OPENGL,
}
#[derive(Debug, Clone, Copy)]
pub struct MoonhareWindow {
render_context: WindowRenderContext
}
impl MoonhareWindow {
pub fn define_context(context: WindowRenderContext) -> Self {
Self {
render_context: context
}
}
pub fn create_window_from_context(self) {
match self.render_context {
WindowRenderContext::VULKAN => {
todo!("Vulkan not implemented yet")
},
WindowRenderContext::OPENGL => {
create_open_gl_window();
}
}
}
}

View file

@ -0,0 +1,32 @@
use glium::{winit::dpi::{LogicalPosition, LogicalSize}, Surface};
use crate::window_config::WindowConfig;
pub fn create_open_gl_window() {
let event_loop = glium::winit::event_loop::EventLoopBuilder::new().build().expect("event loop building");
let (mut _window, display) = glium::backend::glutin::SimpleWindowBuilder::new().build(&event_loop);
let config = WindowConfig::default();
_window.set_title(format!("{} OpenGL", &config.title).as_str());
_window.set_decorations(config.decorations);
_window.set_visible(config.visble);
//_window.set_min_inner_size(Some(LogicalSize::new(config.width, config.height)));
let mut frame = display.draw();
frame.clear_color(0.0, 0.0, 1.0, 1.0);
frame.finish().unwrap();
let _ = event_loop.run(move | event, window_target| {
match event {
glium::winit::event::Event::WindowEvent { event, .. } => match event {
glium::winit::event::WindowEvent::CloseRequested => {
window_target.exit()
},
_ => (),
}
_ => (),
}
});
}

View file

@ -1,62 +0,0 @@
use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::{self, ActiveEventLoop, EventLoop}, window::{Window, WindowAttributes}};
use crate::window_config::WindowConfig;
#[derive(Debug)]
pub struct WinitWindow {
pub window: Window,
}
impl WinitWindow {
pub fn new(config: WindowConfig) -> Self {
moonhare_log::trace("Im inside the create window function in winit");
let event_loop = EventLoop::new().unwrap();
let mut window_attributes = WindowAttributes::default()
.with_title(config.title);
let logical_size = LogicalSize::new(config.width, config.height);
window_attributes = window_attributes.with_inner_size(logical_size);
window_attributes = window_attributes.with_max_inner_size(logical_size);
// Set Visible
window_attributes = window_attributes.with_visible(config.visble);
window_attributes = window_attributes.with_decorations(config.decorations);
let window = match event_loop.create_window(window_attributes) {
Ok(window) => window,
Err(err) => {
moonhare_log::error("Error creating window: {err}");
return;
},
};
moonhare_log::info(format!("Winit WIndow: {:?}", window));
Self { window: window }
}
}
impl ApplicationHandler for WinitWindow {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
event_loop.exit();
},
WindowEvent::RedrawRequested => {
},
_ => (),
}
}
}