diff --git a/moonhare_engine/src/game.rs b/moonhare_engine/src/game.rs index 430382c..7a1992b 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}}, 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; @@ -8,7 +8,7 @@ pub struct Game { running: bool, game_plugin: Option>, window: Option, - display: Arc>>>, + display: Option>, event_loop: EventLoop<()> } @@ -18,7 +18,7 @@ impl Game { running: true, game_plugin: None, window: None, - display: Arc::new(Mutex::new(None)), + display: None, event_loop: init_event_loop(), } } @@ -36,7 +36,7 @@ impl Game { let (_window, _display) = return_window(&self.event_loop); self.window = Some(_window); - self.display = Arc::new(Mutex::new(Some(_display))); + self.display = Some(_display); } pub fn update(&mut self) { @@ -48,7 +48,7 @@ impl Game { } 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(); if let Some(ref mut game_plugin) = self.game_plugin { @@ -79,6 +79,14 @@ impl Game { self.cleanup(); } + + pub fn get_display(&self) -> &glium::Display{ + self.display.as_ref().unwrap() + } + + pub fn get_window(self) -> Option { + return self.window; + } } impl ApplicationHandler for Game { diff --git a/moonhare_engine/src/game_plugin.rs b/moonhare_engine/src/game_plugin.rs index 17fcb4f..09d13ab 100644 --- a/moonhare_engine/src/game_plugin.rs +++ b/moonhare_engine/src/game_plugin.rs @@ -1,5 +1,6 @@ use glium::Frame; + pub trait GamePlugin { fn init(&mut self); fn update(&mut self); diff --git a/playground/src/main.rs b/playground/src/main.rs index 9bdcc40..aa4ce41 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -1,5 +1,9 @@ +use std::cell::RefCell; 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::{program, uniform, Display, Surface}; use moonhare_engine::{game::Game, game_plugin::GamePlugin, vertex::Vertex}; @@ -63,7 +67,10 @@ impl GamePlugin for PlaygroundGame { 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( 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) // Isn't strictly necessary but, makes tge drawing operation faster - let vertex_buffer = glium::VertexBuffer::new(&display, &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 @@ -95,14 +103,14 @@ fn main() { // send shader source code to glium let program = glium::Program::from_source( - glium::Display::new(context, surface), + g, &vertex_shader_src, &fragment_shader_src, None ).unwrap(); - let mut pg_game = PlaygroundGame { + let pg_game = PlaygroundGame { t: 0.0, shape: shape, vertex_buffer: vertex_buffer, @@ -110,7 +118,8 @@ fn main() { 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(); } \ No newline at end of file