Add context builder

This commit is contained in:
Jarrod Doyle 2024-04-25 18:07:54 +01:00
parent 20e4ff829c
commit ab76a63256
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 82 additions and 2 deletions

View File

@ -2,7 +2,11 @@ use std::sync::Arc;
use thiserror::Error; use thiserror::Error;
use winit::{ 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)] #[derive(Error, Debug)]
@ -15,6 +19,10 @@ pub enum ContextError {
Adapter, Adapter,
#[error("Device request failed: {0}")] #[error("Device request failed: {0}")]
Device(#[from] wgpu::RequestDeviceError), 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> { pub struct Context<'window> {
@ -131,3 +139,75 @@ impl<'window> Context<'window> {
handled handled
} }
} }
pub struct ContextBuilder {
title: String,
vsync: bool,
resolution: PhysicalSize<u32>,
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))
}
}

View File

@ -6,7 +6,7 @@ mod texture;
pub use self::{ pub use self::{
bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, bind_group::{BindGroupBuilder, BindGroupLayoutBuilder},
buffer::{BufferExt, BulkBufferBuilder}, buffer::{BufferExt, BulkBufferBuilder},
context::Context, context::{Context, ContextBuilder},
texture::{Texture, TextureBuilder}, texture::{Texture, TextureBuilder},
}; };