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::{backend::glutin::SimpleWindowBuilder, glutin::surface::WindowSurface, winit::{event_loop::EventLoop, window::Window}, Surface};
|
||||||
|
|
||||||
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, ENGINE_NAME};
|
use crate::{game_plugin::GamePlugin, ENGINE_NAME};
|
||||||
|
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
pub running: bool,
|
pub running: bool,
|
||||||
pub game_plugin: Option<Box<dyn GamePlugin>>,
|
pub game_plugin: Option<Box<dyn GamePlugin>>,
|
||||||
pub window: Option<Window>,
|
pub window: Window,
|
||||||
pub display: Option<glium::Display<WindowSurface>>,
|
pub display: glium::Display<WindowSurface>,
|
||||||
pub event_loop: Option<EventLoop<()>>
|
pub event_loop: EventLoop<()>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn new() -> Self {
|
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,
|
running: true,
|
||||||
game_plugin: None,
|
game_plugin: None,
|
||||||
window: None,
|
window: _window.0,
|
||||||
display: None,
|
display: _window.1,
|
||||||
event_loop: None,
|
event_loop: _event_loop,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,42 +32,29 @@ impl Game {
|
||||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||||
game_plugin.init();
|
game_plugin.init();
|
||||||
} else {
|
} 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) {
|
pub fn update(&mut self) {
|
||||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||||
game_plugin.update();
|
game_plugin.update();
|
||||||
} else {
|
|
||||||
//todo!("Default Impl update")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) {
|
pub fn render(&mut self) {
|
||||||
if let Some(ref display) = self.display{
|
let mut target = self.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 {
|
||||||
game_plugin.render(&mut target);
|
game_plugin.render(&mut target);
|
||||||
} else {
|
|
||||||
//todo!("Default Impl render")
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = &target.finish().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target.finish().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cleanup(&mut self) {
|
pub fn cleanup(&mut self) {
|
||||||
if let Some(ref mut game_plugin) = self.game_plugin {
|
if let Some(ref mut game_plugin) = self.game_plugin {
|
||||||
game_plugin.cleanup();
|
game_plugin.cleanup();
|
||||||
} else {
|
|
||||||
//todo!("Default Impl cleanup")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,18 +68,18 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_display(&self) -> &glium::Display<WindowSurface>{
|
pub fn get_display(&self) -> &glium::Display<WindowSurface>{
|
||||||
self.display.as_ref().unwrap()
|
&self.display
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_window(self) -> Option<Window> {
|
pub fn get_window(self) -> Window {
|
||||||
return self.window;
|
self.window
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ApplicationHandler for Game {
|
/* impl ApplicationHandler for Game {
|
||||||
fn resumed(&mut self, event_loop: &event_loop::ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &event_loop::ActiveEventLoop) {
|
||||||
self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(
|
fn window_event(
|
||||||
|
|
@ -102,30 +90,34 @@ impl ApplicationHandler for Game {
|
||||||
) {
|
) {
|
||||||
let _ = window_id;
|
let _ = window_id;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
event_loop.exit();
|
event_loop.exit();
|
||||||
},
|
},
|
||||||
WindowEvent::Resized(window_size) => {
|
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 => {
|
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 vertex;
|
||||||
pub mod game;
|
pub mod game;
|
||||||
pub mod game_plugin;
|
pub mod game_plugin;
|
||||||
|
pub mod window;
|
||||||
use glium::{glutin::surface::WindowSurface, uniform, uniforms, winit::{event::{self, StartCause}, event_loop::{self, EventLoop, EventLoopBuilder}, window::{self, Window}}, Display, Surface};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const ENGINE_NAME: &str = "Moonhare Engine";
|
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::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::{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};
|
use moonhare_engine::{game::Game, game_plugin::GamePlugin, vertex::Vertex};
|
||||||
|
|
||||||
struct PlaygroundGame {
|
struct PlaygroundGame {
|
||||||
t: f32,
|
t: f32,
|
||||||
shape: Vec<Vertex>,
|
shape: Vec<Vertex>,
|
||||||
vertex_buffer: VertexBuffer<Vertex>,
|
vertex_buffer: Option<VertexBuffer<Vertex>>,
|
||||||
indices: NoIndices,
|
indices: Option<NoIndices>,
|
||||||
program: Program,
|
program: Option<Program>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GamePlugin for PlaygroundGame {
|
impl GamePlugin for PlaygroundGame {
|
||||||
|
|
@ -52,9 +47,9 @@ impl GamePlugin for PlaygroundGame {
|
||||||
};
|
};
|
||||||
|
|
||||||
target.draw(
|
target.draw(
|
||||||
&self.vertex_buffer,
|
self.vertex_buffer.as_ref().unwrap(),
|
||||||
&self.indices,
|
&self.indices.unwrap(),
|
||||||
&self.program,
|
self.program.as_ref().unwrap(),
|
||||||
&uniforms,
|
&uniforms,
|
||||||
&Default::default()
|
&Default::default()
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
@ -68,18 +63,18 @@ impl GamePlugin for PlaygroundGame {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let game = RefCell::new(Game::new());
|
let mut game = Game::new();
|
||||||
let mut a = game.borrow_mut();
|
game.register_plugin(Box::new(PlaygroundGame{ t: 0.0, shape: Default::default(), vertex_buffer: None, indices: None, program: None }));
|
||||||
a.init();
|
game.init();
|
||||||
|
|
||||||
let binding = Some(a.display.clone()).unwrap().unwrap();
|
let binding = Some(game.display.clone()).unwrap();
|
||||||
// todo: unwraps on none
|
// todo: unwraps on none
|
||||||
let display = 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] },
|
||||||
Vertex { position: [ 0.0, 0.5], color: [0.0, 1.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 {
|
let pg_game = PlaygroundGame {
|
||||||
t: 0.0,
|
t: 0.0,
|
||||||
shape: shape,
|
shape: shape,
|
||||||
vertex_buffer: vertex_buffer,
|
vertex_buffer: Some(vertex_buffer),
|
||||||
indices: indices,
|
indices: Some(indices),
|
||||||
program: program,
|
program: Some(program),
|
||||||
};
|
};
|
||||||
|
|
||||||
//let mut a = game.borrow_mut();
|
//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