diff --git a/moonhare_engine/src/game.rs b/moonhare_engine/src/game.rs index 63dc883..3445474 100644 --- a/moonhare_engine/src/game.rs +++ b/moonhare_engine/src/game.rs @@ -1,6 +1,6 @@ use std::sync::{Arc, Mutex}; -use glium::{glutin::surface::WindowSurface, winit::{application::ApplicationHandler, event::{Event, WindowEvent}, event_loop::{self, EventLoop}, window::{self, Window, WindowAttributes}}, Display}; +use glium::{glutin::surface::WindowSurface, winit::{application::ApplicationHandler, event::{Event, WindowEvent}, event_loop::{self, EventLoop, EventLoopBuilder}, window::{self, Window, WindowAttributes}}, Display}; use crate::game_plugin::GamePlugin; @@ -9,7 +9,7 @@ pub struct Game { pub game_plugin: Option>, pub window: Option, pub display: Option>, - pub event_loop: EventLoop<()> + pub event_loop: Option> } impl Game { @@ -19,7 +19,7 @@ impl Game { game_plugin: None, window: None, display: None, - event_loop: init_event_loop(), + event_loop: None, } } @@ -33,8 +33,9 @@ impl Game { } else { //todo!("Default Impl init") } - - let (_window, _display) = return_window(&self.event_loop); + self.event_loop = Some(EventLoop::builder().build().expect("event loop building")); + + let (_window, _display) = return_window(&self.event_loop.as_ref().unwrap()); self.window = Some(_window); self.display = Some(_display); } @@ -70,8 +71,6 @@ impl Game { } pub fn run(&mut self) { - self.init(); - while self.running { self.update(); self.render(); diff --git a/moonhare_engine/src/vertex.rs b/moonhare_engine/src/vertex.rs index d087b63..10e3bd0 100644 --- a/moonhare_engine/src/vertex.rs +++ b/moonhare_engine/src/vertex.rs @@ -8,13 +8,6 @@ pub struct Vertex { implement_vertex!(Vertex, position, color); impl Vertex { - pub fn new() -> Self { - Self { - position: [0.0, 0.0], - color: [0.0, 0.0, 0.0], - } - } - pub fn define_shape(v1: Vertex, v2: Vertex, v3: Vertex) -> Vec { let shape = vec![ v1, diff --git a/playground/src/main.rs b/playground/src/main.rs index a250243..858b1f4 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -74,7 +74,7 @@ fn main() { let binding = Some(a.display.clone()).unwrap().unwrap(); // todo: unwraps on none - let g = binding; + let display = binding; let shape = Vertex::define_shape( Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] }, @@ -86,7 +86,7 @@ fn main() { // "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(&display, &shape).unwrap(); // Complex shapes consist of hundreds/thousands of vertices -> need to have a list of vertices and tell OpenGL how to link these @@ -103,16 +103,44 @@ 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("playground/src/shaders/vertex_shader.glsl").unwrap(); - let fragment_shader_src = read_to_string("playground/src/shaders/fragment_shader.glsl").unwrap(); + let vertex_shader_src = r#" + #version 140 + + in vec2 position; + in vec3 color; + out vec3 vertex_color; + + uniform mat4 matrix; + + void main() { + //vertex_color = color; + gl_Position = matrix * vec4(position, 0.0, 1.0); + } + "#; + let fragment_shader_src = r#" + #version 140 + + in vec3 vertex_color; + out vec4 color; + + void main() { + color = vec4(1.0, 0.0, 0.0, 1.0); + } + "#; // send shader source code to glium - let program = glium::Program::from_source( - &g, - &vertex_shader_src, - &fragment_shader_src, + let program = match glium::Program::from_source( + &display, + vertex_shader_src, + fragment_shader_src, None - ).unwrap(); + ) { + Ok(program) => program, + Err(err) => { + eprintln!("Shader compilation error: {:?}", err); + panic!("Failed to compile shaders") + } + }; let pg_game = PlaygroundGame {