red triangle yippie

This commit is contained in:
LunarAkai 2025-07-27 13:24:32 +02:00
commit 11d192836c
3 changed files with 43 additions and 23 deletions

View file

@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex}; 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; use crate::game_plugin::GamePlugin;
@ -9,7 +9,7 @@ pub struct Game {
pub game_plugin: Option<Box<dyn GamePlugin>>, pub game_plugin: Option<Box<dyn GamePlugin>>,
pub window: Option<Window>, pub window: Option<Window>,
pub display: Option<glium::Display<WindowSurface>>, pub display: Option<glium::Display<WindowSurface>>,
pub event_loop: EventLoop<()> pub event_loop: Option<EventLoop<()>>
} }
impl Game { impl Game {
@ -19,7 +19,7 @@ impl Game {
game_plugin: None, game_plugin: None,
window: None, window: None,
display: None, display: None,
event_loop: init_event_loop(), event_loop: None,
} }
} }
@ -33,8 +33,9 @@ impl Game {
} else { } else {
//todo!("Default Impl init") //todo!("Default Impl init")
} }
self.event_loop = Some(EventLoop::builder().build().expect("event loop building"));
let (_window, _display) = return_window(&self.event_loop);
let (_window, _display) = return_window(&self.event_loop.as_ref().unwrap());
self.window = Some(_window); self.window = Some(_window);
self.display = Some(_display); self.display = Some(_display);
} }
@ -70,8 +71,6 @@ impl Game {
} }
pub fn run(&mut self) { pub fn run(&mut self) {
self.init();
while self.running { while self.running {
self.update(); self.update();
self.render(); self.render();

View file

@ -8,13 +8,6 @@ pub struct Vertex {
implement_vertex!(Vertex, position, color); implement_vertex!(Vertex, position, color);
impl Vertex { 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<Vertex> { pub fn define_shape(v1: Vertex, v2: Vertex, v3: Vertex) -> Vec<Vertex> {
let shape = vec![ let shape = vec![
v1, v1,

View file

@ -74,7 +74,7 @@ fn main() {
let binding = Some(a.display.clone()).unwrap().unwrap(); let binding = Some(a.display.clone()).unwrap().unwrap();
// todo: unwraps on none // todo: unwraps on none
let g = binding; let display = binding;
let shape = Vertex::define_shape( let shape = Vertex::define_shape(
Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] }, 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) // "Upload" shape to the memory of the GPU (Vertex Buffer)
// Isn't strictly necessary but, makes tge drawing operation faster // Isn't strictly necessary but, makes tge drawing operation faster
let vertex_buffer: VertexBuffer<Vertex> = VertexBuffer::new(&g, &shape).unwrap(); let vertex_buffer: VertexBuffer<Vertex> = 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 // 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 // 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 // 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 vertex_shader_src = r#"
let fragment_shader_src = read_to_string("playground/src/shaders/fragment_shader.glsl").unwrap(); #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 // send shader source code to glium
let program = glium::Program::from_source( let program = match glium::Program::from_source(
&g, &display,
&vertex_shader_src, vertex_shader_src,
&fragment_shader_src, fragment_shader_src,
None None
).unwrap(); ) {
Ok(program) => program,
Err(err) => {
eprintln!("Shader compilation error: {:?}", err);
panic!("Failed to compile shaders")
}
};
let pg_game = PlaygroundGame { let pg_game = PlaygroundGame {