fn main in playground has no errors currently

This commit is contained in:
LunarAkai 2025-07-27 11:31:25 +02:00
commit 01ff7d39b1
3 changed files with 29 additions and 11 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}}, Display}; use glium::{glutin::surface::WindowSurface, winit::{application::ApplicationHandler, event::{Event, WindowEvent}, event_loop::{self, EventLoop}, window::{self, Window, WindowAttributes}}, Display};
use crate::game_plugin::GamePlugin; use crate::game_plugin::GamePlugin;
@ -8,7 +8,7 @@ pub struct Game {
running: bool, running: bool,
game_plugin: Option<Box<dyn GamePlugin>>, game_plugin: Option<Box<dyn GamePlugin>>,
window: Option<Window>, window: Option<Window>,
display: Arc<Mutex<Option<glium::Display<WindowSurface>>>>, display: Option<glium::Display<WindowSurface>>,
event_loop: EventLoop<()> event_loop: EventLoop<()>
} }
@ -18,7 +18,7 @@ impl Game {
running: true, running: true,
game_plugin: None, game_plugin: None,
window: None, window: None,
display: Arc::new(Mutex::new(None)), display: None,
event_loop: init_event_loop(), event_loop: init_event_loop(),
} }
} }
@ -36,7 +36,7 @@ impl Game {
let (_window, _display) = return_window(&self.event_loop); let (_window, _display) = return_window(&self.event_loop);
self.window = Some(_window); self.window = Some(_window);
self.display = Arc::new(Mutex::new(Some(_display))); self.display = Some(_display);
} }
pub fn update(&mut self) { pub fn update(&mut self) {
@ -48,7 +48,7 @@ impl Game {
} }
pub fn render(&mut self) { pub fn render(&mut self) {
if let Some(ref display) = self.display.into() { if let Some(ref display) = self.display{
let mut target = display.draw(); let mut target = display.draw();
if let Some(ref mut game_plugin) = self.game_plugin { if let Some(ref mut game_plugin) = self.game_plugin {
@ -79,6 +79,14 @@ impl Game {
self.cleanup(); self.cleanup();
} }
pub fn get_display(&self) -> &glium::Display<WindowSurface>{
self.display.as_ref().unwrap()
}
pub fn get_window(self) -> Option<Window> {
return self.window;
}
} }
impl ApplicationHandler for Game { impl ApplicationHandler for Game {

View file

@ -1,5 +1,6 @@
use glium::Frame; use glium::Frame;
pub trait GamePlugin { pub trait GamePlugin {
fn init(&mut self); fn init(&mut self);
fn update(&mut self); fn update(&mut self);

View file

@ -1,5 +1,9 @@
use std::cell::RefCell;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use glium::glutin::surface::WindowSurface;
use glium::{index::NoIndices, Frame, Program, VertexBuffer}; use glium::{index::NoIndices, Frame, Program, VertexBuffer};
use glium::{program, uniform, Display, Surface}; use glium::{program, uniform, Display, Surface};
use moonhare_engine::{game::Game, game_plugin::GamePlugin, vertex::Vertex}; use moonhare_engine::{game::Game, game_plugin::GamePlugin, vertex::Vertex};
@ -63,7 +67,10 @@ impl GamePlugin for PlaygroundGame {
fn main() { fn main() {
let mut game: Game = Game::new(); let game = RefCell::new(Game::new());
let binding = game.borrow();
let g = binding.get_display();
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] },
@ -73,7 +80,8 @@ 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 = glium::VertexBuffer::new(&display, &shape).unwrap();
let vertex_buffer: VertexBuffer<Vertex> = 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 // Complex shapes consist of hundreds/thousands of vertices -> need to have a list of vertices and tell OpenGL how to link these
@ -95,14 +103,14 @@ fn main() {
// send shader source code to glium // send shader source code to glium
let program = glium::Program::from_source( let program = glium::Program::from_source(
glium::Display::new(context, surface), g,
&vertex_shader_src, &vertex_shader_src,
&fragment_shader_src, &fragment_shader_src,
None None
).unwrap(); ).unwrap();
let mut pg_game = PlaygroundGame { let pg_game = PlaygroundGame {
t: 0.0, t: 0.0,
shape: shape, shape: shape,
vertex_buffer: vertex_buffer, vertex_buffer: vertex_buffer,
@ -110,7 +118,8 @@ fn main() {
program: program, program: program,
}; };
game.register_plugin(Box::new(pg_game)); let mut a = game.borrow_mut();
a.register_plugin(Box::new(pg_game));
game.run(); a.run();
} }