Skip to content

Commit

Permalink
Update winit to 0.29.x
Browse files Browse the repository at this point in the history
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:

#21
  • Loading branch information
tuzz committed Feb 15, 2024
1 parent 240293e commit f728bb6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
4 changes: 2 additions & 2 deletions examples/using_winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)]
Expand Down
21 changes: 11 additions & 10 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<G, U, R, H, T>(event_loop: EventLoop<T>, window: Arc<Window>, game: G, updates_per_second: u32, max_frame_time: f64, mut update: U, mut render: R, mut handler: H) -> !
pub fn game_loop<G, U, R, H, T>(event_loop: EventLoop<T>, window: Arc<Window>, 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<G, Time, Arc<Window>>) + 'static,
R: FnMut(&mut GameLoop<G, Time, Arc<Window>>) + 'static,
H: FnMut(&mut GameLoop<G, Time, Arc<Window>>, &Event<'_, T>) + 'static,
H: FnMut(&mut GameLoop<G, Time, Arc<Window>>, &Event<T>) + '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();
}
},
_ => {},
}
})
Expand Down

0 comments on commit f728bb6

Please sign in to comment.