cleanup, but window border still missing
This commit is contained in:
parent
fb49d28498
commit
d93d7f10b4
4 changed files with 63 additions and 74 deletions
|
|
@ -1,25 +1,26 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use glium::{glutin::surface::WindowSurface, winit::{application::ApplicationHandler, event::{Event, WindowEvent}, event_loop::{self, EventLoop, EventLoopBuilder}, window::{self, Window, WindowAttributes}}, Display};
|
||||
use glium::{backend::glutin::SimpleWindowBuilder, glutin::surface::WindowSurface, winit::{event_loop::EventLoop, window::Window}, Surface};
|
||||
|
||||
use crate::{game_plugin::GamePlugin, ENGINE_NAME};
|
||||
|
||||
pub struct Game {
|
||||
pub running: bool,
|
||||
pub game_plugin: Option<Box<dyn GamePlugin>>,
|
||||
pub window: Option<Window>,
|
||||
pub display: Option<glium::Display<WindowSurface>>,
|
||||
pub event_loop: Option<EventLoop<()>>
|
||||
pub window: Window,
|
||||
pub display: glium::Display<WindowSurface>,
|
||||
pub event_loop: EventLoop<()>
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
let _event_loop = EventLoop::new().unwrap();
|
||||
let _window = SimpleWindowBuilder::new().with_title(ENGINE_NAME).build(&_event_loop);
|
||||
|
||||
Game {
|
||||
running: true,
|
||||
game_plugin: None,
|
||||
window: None,
|
||||
display: None,
|
||||
event_loop: None,
|
||||
window: _window.0,
|
||||
display: _window.1,
|
||||
event_loop: _event_loop,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -31,42 +32,29 @@ impl Game {
|
|||
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||
game_plugin.init();
|
||||
} else {
|
||||
//todo!("Default Impl init")
|
||||
panic!("Needs Game Plugin to run!");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||
game_plugin.update();
|
||||
} else {
|
||||
//todo!("Default Impl update")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self) {
|
||||
if let Some(ref display) = self.display{
|
||||
let mut target = display.draw();
|
||||
let mut target = self.display.draw();
|
||||
|
||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||
game_plugin.render(&mut target);
|
||||
} else {
|
||||
//todo!("Default Impl render")
|
||||
}
|
||||
|
||||
let _ = &target.finish().unwrap();
|
||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||
game_plugin.render(&mut target);
|
||||
}
|
||||
|
||||
target.finish().unwrap();
|
||||
}
|
||||
|
||||
pub fn cleanup(&mut self) {
|
||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||
game_plugin.cleanup();
|
||||
} else {
|
||||
//todo!("Default Impl cleanup")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,18 +68,18 @@ impl Game {
|
|||
}
|
||||
|
||||
pub fn get_display(&self) -> &glium::Display<WindowSurface>{
|
||||
self.display.as_ref().unwrap()
|
||||
&self.display
|
||||
}
|
||||
|
||||
pub fn get_window(self) -> Option<Window> {
|
||||
return self.window;
|
||||
pub fn get_window(self) -> Window {
|
||||
self.window
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ApplicationHandler for 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())
|
||||
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
|
|
@ -102,30 +90,34 @@ impl ApplicationHandler for Game {
|
|||
) {
|
||||
let _ = window_id;
|
||||
|
||||
|
||||
|
||||
match event {
|
||||
|
||||
WindowEvent::CloseRequested => {
|
||||
event_loop.exit();
|
||||
},
|
||||
WindowEvent::Resized(window_size) => {
|
||||
self.display.as_ref().unwrap().resize(window_size.into());
|
||||
if let Some(window) = &self.window {
|
||||
if let Some(display) = &self.display {
|
||||
display.resize(window_size.into());
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
WindowEvent::RedrawRequested => {
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
if let Some(window) = &self.window {
|
||||
window.request_redraw();
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
||||
}
|
||||
fn about_to_wait(&mut self, _: &event_loop::ActiveEventLoop) {
|
||||
if let Some(window) = &self.window {
|
||||
window.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn return_window(event_loop: &EventLoop<()>) -> (Window, Display<WindowSurface>) {
|
||||
let (_window, display) = glium::backend::glutin::SimpleWindowBuilder::new()
|
||||
.with_title(crate::ENGINE_NAME)
|
||||
.build(event_loop);
|
||||
|
||||
return (_window, display);
|
||||
}
|
||||
} */
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
pub mod vertex;
|
||||
pub mod game;
|
||||
pub mod game_plugin;
|
||||
|
||||
use glium::{glutin::surface::WindowSurface, uniform, uniforms, winit::{event::{self, StartCause}, event_loop::{self, EventLoop, EventLoopBuilder}, window::{self, Window}}, Display, Surface};
|
||||
|
||||
|
||||
pub mod window;
|
||||
|
||||
const ENGINE_NAME: &str = "Moonhare Engine";
|
||||
|
||||
|
|
|
|||
5
moonhare_engine/src/window.rs
Normal file
5
moonhare_engine/src/window.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub struct WindowConfig {
|
||||
pub title: &'static str,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
|
@ -1,20 +1,15 @@
|
|||
use std::cell::RefCell;
|
||||
use std::fs::read_to_string;
|
||||
use std::ops::Deref;
|
||||
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 glium::{uniform, Surface};
|
||||
use moonhare_engine::{game::Game, game_plugin::GamePlugin, vertex::Vertex};
|
||||
|
||||
struct PlaygroundGame {
|
||||
t: f32,
|
||||
shape: Vec<Vertex>,
|
||||
vertex_buffer: VertexBuffer<Vertex>,
|
||||
indices: NoIndices,
|
||||
program: Program,
|
||||
vertex_buffer: Option<VertexBuffer<Vertex>>,
|
||||
indices: Option<NoIndices>,
|
||||
program: Option<Program>,
|
||||
}
|
||||
|
||||
impl GamePlugin for PlaygroundGame {
|
||||
|
|
@ -52,9 +47,9 @@ impl GamePlugin for PlaygroundGame {
|
|||
};
|
||||
|
||||
target.draw(
|
||||
&self.vertex_buffer,
|
||||
&self.indices,
|
||||
&self.program,
|
||||
self.vertex_buffer.as_ref().unwrap(),
|
||||
&self.indices.unwrap(),
|
||||
self.program.as_ref().unwrap(),
|
||||
&uniforms,
|
||||
&Default::default()
|
||||
).unwrap();
|
||||
|
|
@ -68,18 +63,18 @@ impl GamePlugin for PlaygroundGame {
|
|||
|
||||
fn main() {
|
||||
|
||||
let game = RefCell::new(Game::new());
|
||||
let mut a = game.borrow_mut();
|
||||
a.init();
|
||||
let mut game = Game::new();
|
||||
game.register_plugin(Box::new(PlaygroundGame{ t: 0.0, shape: Default::default(), vertex_buffer: None, indices: None, program: None }));
|
||||
game.init();
|
||||
|
||||
let binding = Some(a.display.clone()).unwrap().unwrap();
|
||||
let binding = Some(game.display.clone()).unwrap();
|
||||
// todo: unwraps on none
|
||||
let display = binding;
|
||||
|
||||
let shape = Vertex::define_shape(
|
||||
Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [ 0.0, 0.5], color: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [ 0.5, -0.25], color: [0.0, 0.0, 1.0] }
|
||||
Vertex { position: [ 0.5, -0.5], color: [0.0, 0.0, 1.0] }
|
||||
);
|
||||
|
||||
|
||||
|
|
@ -124,13 +119,13 @@ fn main() {
|
|||
let pg_game = PlaygroundGame {
|
||||
t: 0.0,
|
||||
shape: shape,
|
||||
vertex_buffer: vertex_buffer,
|
||||
indices: indices,
|
||||
program: program,
|
||||
vertex_buffer: Some(vertex_buffer),
|
||||
indices: Some(indices),
|
||||
program: Some(program),
|
||||
};
|
||||
|
||||
//let mut a = game.borrow_mut();
|
||||
a.register_plugin(Box::new(pg_game));
|
||||
game.register_plugin(Box::new(pg_game));
|
||||
|
||||
a.run();
|
||||
game.run();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue