diff options
Diffstat (limited to 'stockton-render')
-rw-r--r-- | stockton-render/Cargo.toml | 1 | ||||
-rw-r--r-- | stockton-render/src/error.rs | 6 | ||||
-rw-r--r-- | stockton-render/src/lib.rs | 74 | ||||
-rw-r--r-- | stockton-render/src/window.rs | 35 |
4 files changed, 90 insertions, 26 deletions
diff --git a/stockton-render/Cargo.toml b/stockton-render/Cargo.toml index 852d174..5d46e66 100644 --- a/stockton-render/Cargo.toml +++ b/stockton-render/Cargo.toml @@ -13,6 +13,7 @@ nalgebra-glm = "^0.6" shaderc = "0.6.1" log = "0.4.0" image = "0.23.2" +legion = { version = "^0.3" } [features] default = ["vulkan"] diff --git a/stockton-render/src/error.rs b/stockton-render/src/error.rs index cd1aa71..e0de55d 100644 --- a/stockton-render/src/error.rs +++ b/stockton-render/src/error.rs @@ -50,8 +50,4 @@ pub enum CreationError { /// Usually this is out of memory or something happened to the device/surface. /// You'll likely need to exit or create a new context. #[derive(Debug, Clone)] -pub enum FrameError { - AcquireError(hal::window::AcquireError), - SyncObjectError, - PresentError, -} +pub enum FrameError {} diff --git a/stockton-render/src/lib.rs b/stockton-render/src/lib.rs index b7bc8ab..ae902df 100644 --- a/stockton-render/src/lib.rs +++ b/stockton-render/src/lib.rs @@ -32,48 +32,80 @@ extern crate stockton_types; extern crate arrayvec; +#[macro_use] +extern crate legion; + mod culling; pub mod draw; mod error; mod types; - -use stockton_levels::prelude::*; -use stockton_types::World; +pub mod window; use culling::get_visible_faces; use draw::RenderingContext; -use error::{CreationError, FrameError}; +use std::sync::mpsc::{Receiver, Sender}; +use std::sync::Arc; +use std::sync::RwLock; +pub use window::WindowEvent; + +use stockton_levels::prelude::*; +use winit::event_loop::ControlFlow; +use winit::window::Window; + +use std::sync::mpsc::channel; /// Renders a world to a window when you tell it to. -pub struct Renderer<'a, T: MinBSPFeatures<VulkanSystem>> { - world: World<T>, - pub context: RenderingContext<'a>, +/// Also takes ownership of the window and channels window events to be processed outside winit's event loop. +pub struct Renderer<'a> { + /// All the vulkan stuff + context: RenderingContext<'a>, + + /// For getting events from the winit event loop + pub window_events: Receiver<WindowEvent>, + + /// For updating the control flow of the winit event loop + pub update_control_flow: Arc<RwLock<ControlFlow>>, } -impl<'a, T: MinBSPFeatures<VulkanSystem>> Renderer<'a, T> { +impl<'a> Renderer<'a> { /// Create a new Renderer. - /// This initialises all the vulkan context, etc needed. - pub fn new(world: World<T>, window: &winit::window::Window) -> Result<Self, CreationError> { - let context = RenderingContext::new(window, &world.map)?; + pub fn new<T: MinBSPFeatures<VulkanSystem>>( + window: &Window, + file: &T, + ) -> (Self, Sender<WindowEvent>) { + let (tx, rx) = channel(); + let update_control_flow = Arc::new(RwLock::new(ControlFlow::Poll)); - Ok(Renderer { world, context }) + ( + Renderer { + context: RenderingContext::new(window, file).unwrap(), + window_events: rx, + update_control_flow, + }, + tx, + ) } - /// Render a single frame of the world - pub fn render_frame(&mut self) -> Result<(), FrameError> { + /// Render a single frame of the given map. + fn render<T: MinBSPFeatures<VulkanSystem>>(&mut self, map: &T) { // Get visible faces - let faces = get_visible_faces(self.context.camera_pos(), &self.world.map); + let faces = get_visible_faces(self.context.camera_pos(), map); // Then draw them - if self.context.draw_vertices(&self.world.map, &faces).is_err() { + if self.context.draw_vertices(map, &faces).is_err() { unsafe { self.context.handle_surface_change().unwrap() }; // If it fails twice, then error - self.context - .draw_vertices(&self.world.map, &faces) - .map_err(|_| FrameError::PresentError)?; + self.context.draw_vertices(map, &faces).unwrap(); } - - Ok(()) } } + +/// A system that just renders the world. +#[system] +pub fn do_render<T: 'static + MinBSPFeatures<VulkanSystem>>( + #[resource] renderer: &mut Renderer<'static>, + #[resource] map: &T, +) { + renderer.render(map); +} diff --git a/stockton-render/src/window.rs b/stockton-render/src/window.rs new file mode 100644 index 0000000..4520ae8 --- /dev/null +++ b/stockton-render/src/window.rs @@ -0,0 +1,35 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +use Renderer; + +use winit::event::Event as WinitEvent; + +pub struct WindowEvent {} + +impl WindowEvent { + pub fn from(_winit_event: &WinitEvent<()>) -> WindowEvent { + // TODO + WindowEvent {} + } +} + +#[system] +/// A system to process the window events sent to renderer by the winit event loop. +pub fn process_window_events(#[resource] _renderer: &mut Renderer<'static>) { + println!("processing window events..."); +} |