Skip to content

Commit

Permalink
chore(deps): update winit to 0.29
Browse files Browse the repository at this point in the history
  • Loading branch information
tversteeg committed Nov 4, 2023
1 parent faf6ec4 commit 7f2213f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 57 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
39 changes: 27 additions & 12 deletions examples/using_winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ 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);

let game = Game::new();

game_loop(event_loop, window, game, 240, 0.1, |g| {
g.game.your_update_function();
}, |g| {
g.game.your_render_function(&g.window);
}, |g, event| {
if !g.game.your_window_handler(event) { g.exit(); }
});
game_loop(
event_loop,
window,
game,
240,
0.1,
|g| {
g.game.your_update_function();
},
|g| {
g.game.your_render_function(&g.window);
},
|g, event| {
if !g.game.your_window_handler(event) {
g.exit();
}
},
)
.unwrap();
}

#[derive(Default)]
Expand All @@ -42,7 +54,10 @@ impl Game {

pub fn your_render_function(&mut self, window: &Window) {
self.num_renders += 1;
window.set_title(&format!("num_updates: {}, num_renders: {}", self.num_updates, self.num_renders));
window.set_title(&format!(
"num_updates: {}, num_renders: {}",
self.num_updates, self.num_renders
));
}

// A very simple handler that returns false when CloseRequested is detected.
Expand All @@ -51,10 +66,10 @@ impl Game {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => {
return false;
},
_ => {},
}
_ => {}
},
_ => {},
_ => {}
}

true
Expand Down
129 changes: 85 additions & 44 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ use crate::*;

pub use helper::*;

#[cfg(all(not(target_arch = "wasm32"), not(feature="winit"), not(feature="tao")))]
#[cfg(all(
not(target_arch = "wasm32"),
not(feature = "winit"),
not(feature = "tao")
))]
mod helper {
use super::*;

pub fn game_loop<G, U, R>(game: G, updates_per_second: u32, max_frame_time: f64, mut update: U, mut render: R) -> GameLoop<G, Time, ()>
where U: FnMut(&mut GameLoop<G, Time, ()>),
R: FnMut(&mut GameLoop<G, Time, ()>),
pub fn game_loop<G, U, R>(
game: G,
updates_per_second: u32,
max_frame_time: f64,
mut update: U,
mut render: R,
) -> GameLoop<G, Time, ()>
where
U: FnMut(&mut GameLoop<G, Time, ()>),
R: FnMut(&mut GameLoop<G, Time, ()>),
{
let mut game_loop = GameLoop::new(game, updates_per_second, max_frame_time, ());

Expand All @@ -21,24 +32,31 @@ mod helper {
#[cfg(all(target_arch = "wasm32", not(feature = "winit")))]
mod helper {
use super::*;
use web_sys::window;
use wasm_bindgen::JsCast;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::window;

pub fn game_loop<G, U, R>(game: G, updates_per_second: u32, max_frame_time: f64, update: U, render: R)
where G: 'static,
U: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
R: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
pub fn game_loop<G, U, R>(
game: G,
updates_per_second: u32,
max_frame_time: f64,
update: U,
render: R,
) where
G: 'static,
U: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
R: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
{
let game_loop = GameLoop::new(game, updates_per_second, max_frame_time, ());

animation_frame(game_loop, update, render);
}

fn animation_frame<G, U, R>(mut g: GameLoop<G, Time, ()>, mut update: U, mut render: R)
where G: 'static,
U: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
R: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
where
G: 'static,
U: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
R: FnMut(&mut GameLoop<G, Time, ()>) + 'static,
{
if g.next_frame(&mut update, &mut render) {
let next_frame = move || animation_frame(g, update, render);
Expand All @@ -50,44 +68,57 @@ mod helper {
}
}

#[cfg(feature="winit")]
#[cfg(feature = "winit")]
mod helper {
use std::sync::Arc;
use super::*;
use std::sync::Arc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
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) -> !
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,
T: 'static,
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<(), impl std::error::Error>
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,
T: 'static,
{
let mut game_loop = GameLoop::new(game, updates_per_second, max_frame_time, window);
let mut game_loop = GameLoop::new(game, updates_per_second, max_frame_time, window.clone());

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
event_loop.run(move |event, elwt| {
elwt.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::WindowEvent { event, window_id } if window_id == window.id() => {
match event {
WindowEvent::Occluded(occluded) => game_loop.window_occluded = occluded,
WindowEvent::RedrawRequested => {
if !game_loop.next_frame(&mut update, &mut render) {
elwt.exit();
}
}
_ => (),
}
},
Event::MainEventsCleared => {
}
Event::AboutToWait => {
game_loop.window.request_redraw();
},
Event::WindowEvent { event: WindowEvent::Occluded(occluded), .. } => {
game_loop.window_occluded = occluded;
},
_ => {},
}
_ => (),
}
})
}
Expand All @@ -96,19 +127,29 @@ mod helper {
#[cfg(feature = "tao")]
mod helper {
use super::*;
use std::sync::Arc;
use tao::event::Event;
use tao::event_loop::{ControlFlow, EventLoop};
use tao::window::Window;
use std::sync::Arc;

pub use tao;

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) -> !
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,
T: 'static,
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,
) -> !
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,
T: 'static,
{
let mut game_loop = GameLoop::new(game, updates_per_second, max_frame_time, window);

Expand All @@ -123,11 +164,11 @@ mod helper {
if !game_loop.next_frame(&mut update, &mut render) {
*control_flow = ControlFlow::Exit;
}
},
}
Event::MainEventsCleared => {
game_loop.window.request_redraw();
},
_ => {},
}
_ => {}
}
})
}
Expand Down

0 comments on commit 7f2213f

Please sign in to comment.