From 4b2d2a9dc4a7ee141df39c35c53260c834c9901f Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Sun, 27 Jul 2025 12:15:34 +0200 Subject: [PATCH] window opens again but a lot of panicking D: --- moonhare_engine/src/game.rs | 24 +++++++++++++----------- moonhare_engine/src/lib.rs | 8 ++++++++ playground/src/main.rs | 25 +++++++++++++++---------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/moonhare_engine/src/game.rs b/moonhare_engine/src/game.rs index 7a1992b..63dc883 100644 --- a/moonhare_engine/src/game.rs +++ b/moonhare_engine/src/game.rs @@ -5,11 +5,11 @@ use glium::{glutin::surface::WindowSurface, winit::{application::ApplicationHand use crate::game_plugin::GamePlugin; pub struct Game { - running: bool, - game_plugin: Option>, - window: Option, - display: Option>, - event_loop: EventLoop<()> + pub running: bool, + pub game_plugin: Option>, + pub window: Option, + pub display: Option>, + pub event_loop: EventLoop<()> } impl Game { @@ -31,7 +31,7 @@ impl Game { if let Some(ref mut game_plugin) = self.game_plugin { game_plugin.init(); } else { - todo!("Default Impl init") + //todo!("Default Impl init") } let (_window, _display) = return_window(&self.event_loop); @@ -43,7 +43,7 @@ impl Game { if let Some(ref mut game_plugin) = self.game_plugin { game_plugin.update(); } else { - todo!("Default Impl update") + //todo!("Default Impl update") } } @@ -54,10 +54,10 @@ impl Game { if let Some(ref mut game_plugin) = self.game_plugin { game_plugin.render(&mut target); } else { - todo!("Default Impl render") + //todo!("Default Impl render") } - - target.finish().unwrap(); + + let _ = &target.finish().unwrap(); } } @@ -65,7 +65,7 @@ impl Game { if let Some(ref mut game_plugin) = self.game_plugin { game_plugin.cleanup(); } else { - todo!("Default Impl cleanup") + //todo!("Default Impl cleanup") } } @@ -89,6 +89,7 @@ impl Game { } } + impl ApplicationHandler for Game { fn resumed(&mut self, event_loop: &event_loop::ActiveEventLoop) { self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap()) @@ -120,6 +121,7 @@ impl ApplicationHandler for Game { } + fn init_event_loop() -> EventLoop<()> { let event_loop = glium::winit::event_loop::EventLoop::builder() .build() diff --git a/moonhare_engine/src/lib.rs b/moonhare_engine/src/lib.rs index 4fa0028..60f0ef7 100644 --- a/moonhare_engine/src/lib.rs +++ b/moonhare_engine/src/lib.rs @@ -8,6 +8,14 @@ use glium::{glutin::surface::WindowSurface, uniform, uniforms, winit::{event::{s const ENGINE_NAME: &str = "Moonhare Engine"; +pub struct CPointer(T); + +impl Drop for CPointer { + fn drop(&mut self) { + println!("Dropping") + } +} + // rescaling: position *= factor; // rotating: new_position = vec2(pos.x * cos(angle) - pos.y * sin(angle), pos.x * sin(single) + pos.y * cos(angle)); // skewing: position.x += position.y * factor; diff --git a/playground/src/main.rs b/playground/src/main.rs index aa4ce41..a250243 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::fs::read_to_string; +use std::ops::Deref; use std::rc::Rc; use std::sync::{Arc, Mutex}; @@ -26,7 +27,7 @@ impl GamePlugin for PlaygroundGame { } fn render(&mut self, target: &mut Frame) { - + target.clear_color( 0.0, 0.0, @@ -57,20 +58,23 @@ impl GamePlugin for PlaygroundGame { &uniforms, &Default::default() ).unwrap(); - - target.finish().unwrap(); } fn cleanup(&mut self) { } } + + fn main() { let game = RefCell::new(Game::new()); + let mut a = game.borrow_mut(); + a.init(); - let binding = game.borrow(); - let g = binding.get_display(); + let binding = Some(a.display.clone()).unwrap().unwrap(); + // todo: unwraps on none + let g = binding; let shape = Vertex::define_shape( Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] }, @@ -78,10 +82,11 @@ fn main() { Vertex { position: [ 0.5, -0.25], color: [0.0, 0.0, 1.0] } ); + // "Upload" shape to the memory of the GPU (Vertex Buffer) // Isn't strictly necessary but, makes tge drawing operation faster - let vertex_buffer: VertexBuffer = VertexBuffer::new(g, &shape).unwrap(); + let vertex_buffer: VertexBuffer = VertexBuffer::new(&g, &shape).unwrap(); // Complex shapes consist of hundreds/thousands of vertices -> need to have a list of vertices and tell OpenGL how to link these @@ -98,12 +103,12 @@ fn main() { // Important to write matrix * vertex -> Matrix operations produce different results depending on the order // out: defines a variable that is going to be passed along to the fragment shader - let vertex_shader_src = read_to_string("./shaders/vertex_shader.glsl").unwrap(); - let fragment_shader_src = read_to_string("./shaders/fragment_shader.glsl").unwrap(); + let vertex_shader_src = read_to_string("playground/src/shaders/vertex_shader.glsl").unwrap(); + let fragment_shader_src = read_to_string("playground/src/shaders/fragment_shader.glsl").unwrap(); // send shader source code to glium let program = glium::Program::from_source( - g, + &g, &vertex_shader_src, &fragment_shader_src, None @@ -118,7 +123,7 @@ fn main() { program: program, }; - let mut a = game.borrow_mut(); + //let mut a = game.borrow_mut(); a.register_plugin(Box::new(pg_game)); a.run();