Added RenderCtx

This commit is contained in:
Joshua Barretto 2019-01-02 22:08:13 +00:00
parent 7a29286053
commit 6f358fc762
5 changed files with 76 additions and 23 deletions

View File

@ -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"

View File

@ -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,
}
}
}

View File

@ -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();
}

45
voxygen/src/render_ctx.rs Normal file
View File

@ -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<Resources, gfx::format::Srgba8>;
type TgtDepthView = DepthStencilView<Resources, gfx::format::DepthStencil>;
pub struct RenderCtx {
device: Device,
encoder: Encoder<Resources, CommandBuffer>,
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<f32>) {
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();
}
}

View File

@ -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<Resources, gfx::format::Srgba8>;
type TgtDepthView = DepthStencilView<Resources, gfx::format::DepthStencil>;
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<F: FnMut(Event)>(&mut self, mut f: F) {
self.events_loop.poll_events(|event| match event {
glutin::Event::WindowEvent { event, .. } => match event {