Use an Arc for window

This commit is contained in:
Jarrod Doyle 2024-03-08 16:27:08 +00:00
parent b884af0380
commit b0589c4b9e
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 12 additions and 7 deletions

View File

@ -1,21 +1,24 @@
use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use winit::{ use winit::{
dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window, dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window,
}; };
pub struct Context<'w> { pub struct Context<'window> {
pub window: &'w Window, pub window: Arc<Window>,
pub instance: wgpu::Instance, pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>, pub size: PhysicalSize<u32>,
pub surface: wgpu::Surface<'w>, pub surface: wgpu::Surface<'window>,
pub surface_config: wgpu::SurfaceConfiguration, pub surface_config: wgpu::SurfaceConfiguration,
pub adapter: wgpu::Adapter, pub adapter: wgpu::Adapter,
pub device: wgpu::Device, pub device: wgpu::Device,
pub queue: wgpu::Queue, pub queue: wgpu::Queue,
} }
impl<'w> Context<'w> { impl<'window> Context<'window> {
pub async fn new(window: &'w Window, limits: wgpu::Limits) -> Result<Self> { pub async fn new(window: Arc<Window>, limits: wgpu::Limits) -> Result<Self> {
log::info!("Initialising WGPU context..."); log::info!("Initialising WGPU context...");
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::VULKAN, backends: wgpu::Backends::VULKAN,
@ -28,7 +31,7 @@ impl<'w> Context<'w> {
// - A GPU device to draw to the surface // - A GPU device to draw to the surface
// - A draw command queue // - A draw command queue
log::info!("Initialising window surface..."); log::info!("Initialising window surface...");
let surface = instance.create_surface(window)?; let surface = instance.create_surface(window.clone())?;
log::info!("Requesting GPU adapter..."); log::info!("Requesting GPU adapter...");
let adapter = instance let adapter = instance

View File

@ -2,6 +2,8 @@ mod gfx;
mod input; mod input;
mod scripting; mod scripting;
use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use gfx::Context; use gfx::Context;
use input::Input; use input::Input;
@ -18,7 +20,7 @@ pub fn main() -> Result<()> {
env_logger::init(); env_logger::init();
let (event_loop, window) = make_window()?; let (event_loop, window) = make_window()?;
let context = pollster::block_on(Context::new(&window, Limits::default()))?; let context = pollster::block_on(Context::new(Arc::new(window), Limits::default()))?;
run(event_loop, context)?; run(event_loop, context)?;
Ok(()) Ok(())