Give render context ownership of window

This commit is contained in:
Jarrod Doyle 2023-10-14 10:00:56 +01:00
parent 1a92b52630
commit 0dd7bbd37e
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 5 additions and 3 deletions

View File

@ -1,6 +1,7 @@
use winit::{dpi::PhysicalSize, window::Window}; use winit::{dpi::PhysicalSize, window::Window};
pub struct Context { pub struct Context {
pub window: Window,
pub instance: wgpu::Instance, pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>, pub size: PhysicalSize<u32>,
pub surface: wgpu::Surface, pub surface: wgpu::Surface,
@ -11,7 +12,7 @@ pub struct Context {
} }
impl Context { impl Context {
pub async fn new(window: &Window, limits: wgpu::Limits) -> Self { pub async fn new(window: Window, limits: wgpu::Limits) -> 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,
@ -57,6 +58,7 @@ impl Context {
surface.configure(&device, &surface_config); surface.configure(&device, &surface_config);
Self { Self {
window,
instance, instance,
size, size,
surface, surface,

View File

@ -15,13 +15,13 @@ pub async fn run() {
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
let mut render_ctx = gfx::Context::new(&window, wgpu::Limits::default()).await; let mut render_ctx = gfx::Context::new(window, wgpu::Limits::default()).await;
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent { Event::WindowEvent {
ref event, ref event,
window_id, window_id,
} if window_id == window.id() => match event { } if window_id == render_ctx.window.id() => match event {
WindowEvent::CloseRequested WindowEvent::CloseRequested
| WindowEvent::KeyboardInput { | WindowEvent::KeyboardInput {
input: input: