From ab76a63256cd399a65a434976a8bdc79c20e0604 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 25 Apr 2024 18:07:54 +0100 Subject: [PATCH] Add context builder --- src/context.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 2 +- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/context.rs b/src/context.rs index a05560f..76aeaa3 100644 --- a/src/context.rs +++ b/src/context.rs @@ -2,7 +2,11 @@ use std::sync::Arc; use thiserror::Error; use winit::{ - dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window, + dpi::PhysicalSize, + error::{EventLoopError, OsError}, + event::WindowEvent, + event_loop::{EventLoop, EventLoopWindowTarget}, + window::{Window, WindowBuilder}, }; #[derive(Error, Debug)] @@ -15,6 +19,10 @@ pub enum ContextError { Adapter, #[error("Device request failed: {0}")] Device(#[from] wgpu::RequestDeviceError), + #[error("Event loop creation failed: {0}")] + EventLoop(#[from] EventLoopError), + #[error("Window creation failed: {0}")] + Os(#[from] OsError), } pub struct Context<'window> { @@ -131,3 +139,75 @@ impl<'window> Context<'window> { handled } } + +pub struct ContextBuilder { + title: String, + vsync: bool, + resolution: PhysicalSize, + backends: wgpu::Backends, + features: wgpu::Features, + limits: wgpu::Limits, +} + +impl Default for ContextBuilder { + fn default() -> Self { + Self { + title: "Crawl".to_string(), + vsync: true, + resolution: PhysicalSize::new(1280, 720), + backends: Default::default(), + features: Default::default(), + limits: Default::default(), + } + } +} + +impl ContextBuilder { + pub fn new() -> Self { + Self::default() + } + + pub fn with_title(mut self, title: &str) -> Self { + self.title = title.to_string(); + self + } + + pub fn with_vsync(mut self, vsync: bool) -> Self { + self.vsync = vsync; + self + } + + pub fn with_resolution(mut self, width: u32, height: u32) -> Self { + self.resolution = PhysicalSize::new(width, height); + self + } + + pub fn with_backends(mut self, backends: wgpu::Backends) -> Self { + self.backends = backends; + self + } + + pub fn with_features(mut self, features: wgpu::Features) -> Self { + self.features = features; + self + } + + pub fn with_limits(mut self, limits: wgpu::Limits) -> Self { + self.limits = limits; + self + } + + pub async fn build(self) -> Result<(Context<'static>, EventLoop<()>), ContextError> { + let event_loop = EventLoop::new()?; + let window = Arc::new( + WindowBuilder::new() + .with_title(self.title) + .with_inner_size(self.resolution) + .build(&event_loop)?, + ); + + let context = Context::new(window, self.backends, self.features, self.limits).await?; + + Ok((context, event_loop)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 5a9020a..27bd34d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ mod texture; pub use self::{ bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, buffer::{BufferExt, BulkBufferBuilder}, - context::Context, + context::{Context, ContextBuilder}, texture::{Texture, TextureBuilder}, };