veloren/voxygen/src/window.rs

86 lines
2.1 KiB
Rust
Raw Normal View History

2019-01-07 21:10:31 +00:00
// Library
2019-01-11 17:30:13 +00:00
use glutin;
use gfx_window_glutin;
2019-01-02 22:08:13 +00:00
// Crate
2019-01-07 21:10:31 +00:00
use crate::{
Error,
2019-01-11 17:30:13 +00:00
render::{
Renderer,
TgtColorFmt,
TgtDepthFmt,
},
2019-01-07 21:10:31 +00:00
};
pub struct Window {
2019-01-11 17:30:13 +00:00
events_loop: glutin::EventsLoop,
window: glutin::GlWindow,
2019-01-07 21:10:31 +00:00
renderer: Renderer,
}
impl Window {
pub fn new() -> Result<Window, Error> {
2019-01-11 17:30:13 +00:00
let events_loop = glutin::EventsLoop::new();
2019-01-11 17:30:13 +00:00
let win_builder = glutin::WindowBuilder::new()
2019-01-07 21:10:31 +00:00
.with_title("Veloren (Voxygen)")
2019-01-11 17:30:13 +00:00
.with_dimensions(glutin::dpi::LogicalSize::new(800.0, 500.0))
.with_maximized(false);
2019-01-11 17:30:13 +00:00
let ctx_builder = glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 2)))
.with_vsync(true);
let (
window,
device,
factory,
tgt_color_view,
tgt_depth_view,
) = gfx_window_glutin::init::<TgtColorFmt, TgtDepthFmt>(
win_builder,
ctx_builder,
&events_loop,
).map_err(|err| Error::BackendError(Box::new(err)))?;
2019-01-07 21:10:31 +00:00
let tmp = Ok(Self {
events_loop,
2019-01-11 17:30:13 +00:00
window,
renderer: Renderer::new(
device,
factory,
tgt_color_view,
tgt_depth_view,
)?,
2019-01-07 21:10:31 +00:00
});
tmp
}
2019-01-07 21:10:31 +00:00
pub fn renderer(&self) -> &Renderer { &self.renderer }
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
2019-01-02 22:08:13 +00:00
2019-01-07 21:10:31 +00:00
pub fn fetch_events(&mut self) -> Vec<Event> {
let mut events = vec![];
self.events_loop.poll_events(|event| match event {
2019-01-11 17:30:13 +00:00
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)),
_ => {},
},
_ => {},
});
2019-01-07 21:10:31 +00:00
events
}
pub fn display(&self) -> Result<(), Error> {
2019-01-11 17:30:13 +00:00
self.window.swap_buffers()
.map_err(|err| Error::BackendError(Box::new(err)))
}
}
pub enum Event {
Close,
Char(char),
}