diff --git a/client/Cargo.toml b/client/Cargo.toml index d9abb76b..8d46e253 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -14,9 +14,9 @@ server = { path = "../server" } tracing = "0.1.10" ash = { version = "0.37.1", features = ["loaded"] } lahar = { git = "https://github.com/Ralith/lahar", rev = "88abd75e41d04c3a4199d95f581cb135f5962844" } -winit = "0.28.1" +winit = { version = "0.29.10", features = ["rwh_05"] } ash-window = "0.12.0" -raw-window-handle = "0.5.0" +raw-window-handle = { version = "0.5.0", features = ["std"] } directories = "5.0.1" vk-shader-macros = "0.2.5" nalgebra = { workspace = true } diff --git a/client/src/graphics/window.rs b/client/src/graphics/window.rs index f34ba4be..adcf2298 100644 --- a/client/src/graphics/window.rs +++ b/client/src/graphics/window.rs @@ -6,12 +6,12 @@ use ash::{extensions::khr, vk}; use lahar::DedicatedImage; use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; use tracing::{error, info}; +use winit::event::KeyEvent; +use winit::keyboard::{KeyCode, PhysicalKey}; use winit::{ dpi::PhysicalSize, - event::{ - DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent, - }, - event_loop::{ControlFlow, EventLoop}, + event::{DeviceEvent, ElementState, Event, MouseButton, WindowEvent}, + event_loop::EventLoop, window::{CursorGrabMode, Window as WinitWindow, WindowBuilder}, }; @@ -27,7 +27,7 @@ pub struct EarlyWindow { impl EarlyWindow { pub fn new() -> Self { - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("hypermine") .build(&event_loop) @@ -105,7 +105,7 @@ impl Window { } /// Run the event loop until process exit - pub fn run(mut self, gfx: Arc) -> ! { + pub fn run(mut self, gfx: Arc) { // Allocate the presentable images we'll be rendering to self.swapchain = Some(SwapchainMgr::new( &self, @@ -128,34 +128,9 @@ impl Window { self.event_loop .take() .unwrap() - .run(move |event, _, control_flow| match event { - Event::MainEventsCleared => { - while let Ok(msg) = self.net.incoming.try_recv() { - self.handle_net(msg); - } - - if let Some(sim) = self.sim.as_mut() { - let this_frame = Instant::now(); - let dt = this_frame - last_frame; - sim.set_movement_input(na::Vector3::new( - right as u8 as f32 - left as u8 as f32, - up as u8 as f32 - down as u8 as f32, - back as u8 as f32 - forward as u8 as f32, - )); - sim.set_jump_held(jump); - - sim.look( - 0.0, - 0.0, - 2.0 * (anticlockwise as u8 as f32 - clockwise as u8 as f32) - * dt.as_secs_f32(), - ); - - sim.step(dt, &mut self.net); - last_frame = this_frame; - } - - self.draw(); + .run(move |event, window_target| match event { + Event::AboutToWait => { + self.window.request_redraw(); } Event::DeviceEvent { event, .. } => match event { DeviceEvent::MouseMotion { delta } if mouse_captured => { @@ -171,6 +146,34 @@ impl Window { _ => {} }, Event::WindowEvent { event, .. } => match event { + WindowEvent::RedrawRequested => { + while let Ok(msg) = self.net.incoming.try_recv() { + self.handle_net(msg); + } + + if let Some(sim) = self.sim.as_mut() { + let this_frame = Instant::now(); + let dt = this_frame - last_frame; + sim.set_movement_input(na::Vector3::new( + right as u8 as f32 - left as u8 as f32, + up as u8 as f32 - down as u8 as f32, + back as u8 as f32 - forward as u8 as f32, + )); + sim.set_jump_held(jump); + + sim.look( + 0.0, + 0.0, + 2.0 * (anticlockwise as u8 as f32 - clockwise as u8 as f32) + * dt.as_secs_f32(), + ); + + sim.step(dt, &mut self.net); + last_frame = this_frame; + } + + self.draw(); + } WindowEvent::Resized(_) => { // Some environments may not emit the vulkan signals that recommend or // require surface reconstruction, so we need to check for messages from the @@ -180,7 +183,7 @@ impl Window { } WindowEvent::CloseRequested => { info!("exiting due to closed window"); - *control_flow = ControlFlow::Exit; + window_target.exit(); } WindowEvent::MouseInput { button: MouseButton::Left, @@ -211,39 +214,39 @@ impl Window { } } WindowEvent::KeyboardInput { - input: - KeyboardInput { + event: + KeyEvent { state, - virtual_keycode: Some(key), + physical_key: PhysicalKey::Code(key), .. }, .. } => match key { - VirtualKeyCode::W => { + KeyCode::KeyW => { forward = state == ElementState::Pressed; } - VirtualKeyCode::A => { + KeyCode::KeyA => { left = state == ElementState::Pressed; } - VirtualKeyCode::S => { + KeyCode::KeyS => { back = state == ElementState::Pressed; } - VirtualKeyCode::D => { + KeyCode::KeyD => { right = state == ElementState::Pressed; } - VirtualKeyCode::Q => { + KeyCode::KeyQ => { anticlockwise = state == ElementState::Pressed; } - VirtualKeyCode::E => { + KeyCode::KeyE => { clockwise = state == ElementState::Pressed; } - VirtualKeyCode::R => { + KeyCode::KeyR => { up = state == ElementState::Pressed; } - VirtualKeyCode::F => { + KeyCode::KeyF => { down = state == ElementState::Pressed; } - VirtualKeyCode::Space => { + KeyCode::Space => { if let Some(sim) = self.sim.as_mut() { if !jump && state == ElementState::Pressed { sim.set_jump_pressed_true(); @@ -251,12 +254,12 @@ impl Window { jump = state == ElementState::Pressed; } } - VirtualKeyCode::V if state == ElementState::Pressed => { + KeyCode::KeyV if state == ElementState::Pressed => { if let Some(sim) = self.sim.as_mut() { sim.toggle_no_clip(); } } - VirtualKeyCode::Escape => { + KeyCode::Escape => { let _ = self.window.set_cursor_grab(CursorGrabMode::None); self.window.set_cursor_visible(true); mouse_captured = false; @@ -280,11 +283,12 @@ impl Window { } _ => {} }, - Event::LoopDestroyed => { + Event::LoopExiting => { self.metrics.report(); } _ => {} - }); + }) + .unwrap(); } fn handle_net(&mut self, msg: net::Message) { @@ -364,18 +368,18 @@ impl Window { } } -fn number_key_to_index(key: VirtualKeyCode) -> Option { +fn number_key_to_index(key: KeyCode) -> Option { match key { - VirtualKeyCode::Key1 => Some(0), - VirtualKeyCode::Key2 => Some(1), - VirtualKeyCode::Key3 => Some(2), - VirtualKeyCode::Key4 => Some(3), - VirtualKeyCode::Key5 => Some(4), - VirtualKeyCode::Key6 => Some(5), - VirtualKeyCode::Key7 => Some(6), - VirtualKeyCode::Key8 => Some(7), - VirtualKeyCode::Key9 => Some(8), - VirtualKeyCode::Key0 => Some(9), + KeyCode::Digit1 => Some(0), + KeyCode::Digit2 => Some(1), + KeyCode::Digit3 => Some(2), + KeyCode::Digit4 => Some(3), + KeyCode::Digit5 => Some(4), + KeyCode::Digit6 => Some(5), + KeyCode::Digit7 => Some(6), + KeyCode::Digit8 => Some(7), + KeyCode::Digit9 => Some(8), + KeyCode::Digit0 => Some(9), _ => None, } }