window.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use winit::{event::{ElementState, Event, KeyEvent, WindowEvent}, event_loop::EventLoop, keyboard::{KeyCode, PhysicalKey}};
  2. use crate::surface;
  3. pub async fn run(){
  4. env_logger::init();
  5. let event_loop = EventLoop::new().unwrap();
  6. let window = winit::window::WindowBuilder::new().with_title("Hello, World!").build(&event_loop).unwrap();
  7. let mut state = surface::State::new(&window).await;
  8. event_loop
  9. .run(move |event, control_flow| {
  10. match event {
  11. Event::WindowEvent {
  12. ref event,
  13. window_id,
  14. } if window_id == state.window().id() => {
  15. if !state.input(event) {
  16. // UPDATED!
  17. match event {
  18. WindowEvent::CloseRequested
  19. | WindowEvent::KeyboardInput {
  20. event:
  21. KeyEvent {
  22. state: ElementState::Pressed,
  23. physical_key: PhysicalKey::Code(KeyCode::Escape),
  24. ..
  25. },
  26. ..
  27. } => control_flow.exit(),
  28. WindowEvent::Resized(physical_size) => {
  29. log::info!("physical_size: {physical_size:?}");
  30. state.resize(*physical_size);
  31. }
  32. WindowEvent::RedrawRequested => {
  33. // This tells winit that we want another frame after this one
  34. state.window().request_redraw();
  35. state.update();
  36. match state.render() {
  37. Ok(_) => {}
  38. // Reconfigure the surface if it's lost or outdated
  39. Err(
  40. wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated,
  41. ) => state.resize(state.size),
  42. // The system is out of memory, we should probably quit
  43. Err(wgpu::SurfaceError::OutOfMemory) => {
  44. log::error!("OutOfMemory");
  45. control_flow.exit();
  46. }
  47. // This happens when the a frame takes too long to present
  48. Err(wgpu::SurfaceError::Timeout) => {
  49. log::warn!("Surface timeout")
  50. }
  51. }
  52. }
  53. _ => {}
  54. }
  55. }
  56. }
  57. _ => {}
  58. }
  59. })
  60. .unwrap();
  61. }