From f728bb6627a87d70e206869bd23485470e5c4963 Mon Sep 17 00:00:00 2001 From: Chris Patuzzo Date: Thu, 15 Feb 2024 14:58:56 +0000 Subject: [PATCH] Update winit to 0.29.x I decided not to apply the cargo fmt for now to keep the change clearer in the commit history. Thanks @tversteeg for guiding me through the update with your pull request: https://github.com/tuzz/game-loop/pull/21 --- Cargo.toml | 2 +- examples/using_winit.rs | 4 ++-- src/helper.rs | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf4ca02..a521b39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ features = [ ] [dependencies] -winit = { version = "0.28", optional = true } +winit = { version = "0.29", optional = true } tao = { version = "0.21", optional = true } [[example]] diff --git a/examples/using_winit.rs b/examples/using_winit.rs index b3156d2..a1a60b2 100644 --- a/examples/using_winit.rs +++ b/examples/using_winit.rs @@ -9,7 +9,7 @@ use game_loop::winit::window::{Window, WindowBuilder}; use std::sync::Arc; fn main() { - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); let window = Arc::new(window); @@ -22,7 +22,7 @@ fn main() { g.game.your_render_function(&g.window); }, |g, event| { if !g.game.your_window_handler(event) { g.exit(); } - }); + }).unwrap(); } #[derive(Default)] diff --git a/src/helper.rs b/src/helper.rs index 921a0ab..78dd1a6 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -56,37 +56,38 @@ mod helper { use super::*; use winit::event::{Event, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; + use winit::error::EventLoopError; use winit::window::Window; pub use winit; - pub fn game_loop(event_loop: EventLoop, window: Arc, game: G, updates_per_second: u32, max_frame_time: f64, mut update: U, mut render: R, mut handler: H) -> ! + pub fn game_loop(event_loop: EventLoop, window: Arc, game: G, updates_per_second: u32, max_frame_time: f64, mut update: U, mut render: R, mut handler: H) -> Result<(), EventLoopError> where G: 'static, U: FnMut(&mut GameLoop>) + 'static, R: FnMut(&mut GameLoop>) + 'static, - H: FnMut(&mut GameLoop>, &Event<'_, T>) + 'static, + H: FnMut(&mut GameLoop>, &Event) + 'static, T: 'static, { let mut game_loop = GameLoop::new(game, updates_per_second, max_frame_time, window); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, window_target| { + window_target.set_control_flow(ControlFlow::Poll); // Forward events to existing handlers. handler(&mut game_loop, &event); match event { - Event::RedrawRequested(_) => { - if !game_loop.next_frame(&mut update, &mut render) { - *control_flow = ControlFlow::Exit; - } - }, - Event::MainEventsCleared => { + Event::AboutToWait => { game_loop.window.request_redraw(); }, Event::WindowEvent { event: WindowEvent::Occluded(occluded), .. } => { game_loop.window_occluded = occluded; }, + Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => { + if !game_loop.next_frame(&mut update, &mut render) { + window_target.exit(); + } + }, _ => {}, } })