diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | 0353181306702c40ad0fe482b5c2b159b46794a4 (patch) | |
tree | 33acc6a9e8ea4705884cf93b78cf869008f71832 /stockton-skeleton/src/lib.rs | |
parent | 664f0b0777ba96298b29f0c753d52a81cbb233f1 (diff) |
refactor(all): rename some crates
Diffstat (limited to 'stockton-skeleton/src/lib.rs')
-rw-r--r-- | stockton-skeleton/src/lib.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/stockton-skeleton/src/lib.rs b/stockton-skeleton/src/lib.rs new file mode 100644 index 0000000..03f6d53 --- /dev/null +++ b/stockton-skeleton/src/lib.rs @@ -0,0 +1,92 @@ +#[cfg(feature = "vulkan")] +extern crate gfx_backend_vulkan as back; +extern crate gfx_hal as hal; +extern crate nalgebra_glm as na; + +#[macro_use] +extern crate derive_builder; + +pub mod buffers; +pub mod builders; +pub mod context; +pub mod draw_passes; +pub mod error; +pub mod queue_negotiator; +mod target; +pub mod texture; +pub mod types; +pub mod utils; + +use context::RenderingContext; +use draw_passes::{DrawPass, IntoDrawPass}; + +use anyhow::{Context, Result}; + +use stockton_types::Session; +use winit::window::Window; + +/// Renders a world to a window when you tell it to. +/// Also takes ownership of the window and channels window events to be processed outside winit's event loop. +pub struct Renderer<DP> { + /// All the vulkan stuff + context: RenderingContext, + + /// The draw pass we're using + draw_pass: DP, +} + +impl<DP: DrawPass> Renderer<DP> { + /// Create a new Renderer. + pub fn new<IDP: IntoDrawPass<DP>>( + window: &Window, + session: &mut Session, + idp: IDP, + ) -> Result<Self> { + let mut context = RenderingContext::new::<IDP, DP>(window)?; + + // Draw pass + let draw_pass = idp + .init(session, &mut context) + .context("Error initialising draw pass")?; + + Ok(Renderer { context, draw_pass }) + } + + /// Render a single frame of the given session. + pub fn render(&mut self, session: &Session) -> Result<()> { + // Try to draw + if self + .context + .draw_next_frame(session, &mut self.draw_pass) + .is_err() + { + // Probably the surface changed + self.handle_surface_change(session)?; + + // If it fails twice, then error + self.context.draw_next_frame(session, &mut self.draw_pass)?; + } + + Ok(()) + } + + pub fn get_aspect_ratio(&self) -> f32 { + let e = self.context.target_chain().properties().extent; + e.width as f32 / e.height as f32 + } + + pub fn handle_surface_change(&mut self, session: &Session) -> Result<()> { + unsafe { + self.context.handle_surface_change()?; + self.draw_pass + .handle_surface_change(session, &mut self.context)?; + } + + Ok(()) + } + + /// Get a reference to the renderer's context. + pub fn context(&self) -> &RenderingContext { + &self.context + } +} |