From 6f358fc762ec8a5f139c67670d791aa5f9de0948 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 2 Jan 2019 22:08:13 +0000 Subject: [PATCH] Added RenderCtx --- voxygen/Cargo.toml | 3 +++ voxygen/src/main.rs | 20 +++++++++-------- voxygen/src/menu/title.rs | 10 +++++++++ voxygen/src/render_ctx.rs | 45 +++++++++++++++++++++++++++++++++++++++ voxygen/src/window.rs | 21 ++++++------------ 5 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 voxygen/src/render_ctx.rs diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index feb943fe65..4744287815 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -12,3 +12,6 @@ gfx = "0.17.1" gfx_device_gl = "0.15.0" gfx_window_glutin = "0.25.0" glutin = "0.17.0" + +# Mathematics +vek = "0.9" diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index eca06ce4d1..8e2cbab9a4 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -1,4 +1,5 @@ mod menu; +mod render_ctx; mod window; // Standard @@ -34,17 +35,18 @@ fn main() { window: Window::new(), }; - loop { + while let Some(state_result) = states.last_mut().map(|last| last.play(&mut global_state)){ // Implement state transfer logic - match states.last_mut().map(|last| last.play(&mut global_state)) { - Some(StateResult::Close) => { states.pop(); }, - Some(StateResult::Push(new_state)) => { states.push(new_state); }, - Some(StateResult::Switch(mut new_state)) => if let Some(old_state) = states.last_mut() { - mem::swap(old_state, &mut new_state); - } else { - break; + match state_result { + StateResult::Close => { + states.pop(); + }, + StateResult::Push(new_state) => { + states.push(new_state); + }, + StateResult::Switch(mut new_state) => { + states.last_mut().map(|old_state| mem::swap(old_state, &mut new_state)); }, - None => break, } } } diff --git a/voxygen/src/menu/title.rs b/voxygen/src/menu/title.rs index ccb2354ec1..bab4b3b183 100644 --- a/voxygen/src/menu/title.rs +++ b/voxygen/src/menu/title.rs @@ -1,3 +1,6 @@ +// Library +use vek::*; + // Crate use crate::{ PlayState, @@ -22,6 +25,13 @@ impl PlayState for TitleState { Event::Close => running = false, }); + global_state.window.render_ctx_mut().clear(Rgba::new( + 0.0, + 0.3, + 1.0, + 1.0, + )); + global_state.window.render_ctx_mut().flush_and_cleanup(); global_state.window.swap_buffers(); } diff --git a/voxygen/src/render_ctx.rs b/voxygen/src/render_ctx.rs new file mode 100644 index 0000000000..b4a1efe459 --- /dev/null +++ b/voxygen/src/render_ctx.rs @@ -0,0 +1,45 @@ +// Library +use gfx_device_gl::{Device, Resources, Factory, CommandBuffer}; +use gfx::{ + handle::{RenderTargetView, DepthStencilView}, + Device as DeviceTrait, + Encoder, +}; +use vek::*; + +type TgtColorView = RenderTargetView; +type TgtDepthView = DepthStencilView; + +pub struct RenderCtx { + device: Device, + encoder: Encoder, + factory: Factory, + tgt_color_view: TgtColorView, + tgt_depth_view: TgtDepthView, +} + +impl RenderCtx { + pub fn new( + device: Device, + mut factory: Factory, + tgt_color_view: TgtColorView, + tgt_depth_view: TgtDepthView, + ) -> Self { + Self { + device, + encoder: Encoder::from(factory.create_command_buffer()), + factory, + tgt_color_view, + tgt_depth_view, + } + } + + pub fn clear(&mut self, col: Rgba) { + self.encoder.clear(&self.tgt_color_view, col.into_array()); + } + + pub fn flush_and_cleanup(&mut self) { + self.encoder.flush(&mut self.device); + self.device.cleanup(); + } +} diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 17ba361bb7..365960c409 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -1,9 +1,6 @@ // External -use gfx::handle::{RenderTargetView, DepthStencilView}; -use gfx_device_gl::{Device, Resources, Factory}; use gfx_window_glutin; use glutin::{ - self, Api::OpenGl, dpi::LogicalSize, ContextBuilder, @@ -14,15 +11,8 @@ use glutin::{ WindowBuilder, }; -type TgtColorView = RenderTargetView; -type TgtDepthView = DepthStencilView; - -pub struct RenderCtx { - device: Device, - factory: Factory, - tgt_color_view: TgtColorView, - tgt_depth_view: TgtDepthView, -} +// Crate +use crate::render_ctx::RenderCtx; pub struct Window { events_loop: EventsLoop, @@ -56,15 +46,18 @@ impl Window { Self { events_loop, gl_window, - render_ctx: RenderCtx { + render_ctx: RenderCtx::new( device, factory, tgt_color_view, tgt_depth_view, - }, + ), } } + pub fn render_ctx(&self) -> &RenderCtx { &self.render_ctx } + pub fn render_ctx_mut(&mut self) -> &mut RenderCtx { &mut self.render_ctx } + pub fn poll_events(&mut self, mut f: F) { self.events_loop.poll_events(|event| match event { glutin::Event::WindowEvent { event, .. } => match event {