Skip to content

Commit

Permalink
Port to winit 0.29
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed Nov 3, 2024
1 parent 1d9da0b commit 9f1ba83
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 541 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ glow = { version = "0.15.0", default-features = false }
log = "0.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
glutin = { version = "0.30.3", optional = true, default-features = false }
glutin = { version = "0.31", optional = true, default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web_sys = { version = "0.3", package = "web-sys", features = [
Expand All @@ -49,7 +49,7 @@ image-loading = ["image"]
debug_inspector = []

[dev-dependencies]
winit = { version = "0.28.1", default-features = false }
winit = { version = "0.29", default-features = false }
euclid = "0.22.3"
rand = "0.8"
svg = "0.14.0"
Expand All @@ -65,8 +65,8 @@ swash = "=0.1.17" # keep this in sync with cosmic-text
lazy_static = "1.4.0"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
glutin = "0.30.0"
glutin-winit = "0.3.0"
glutin = "0.31.0"
glutin-winit = "0.4.0"
raw-window-handle = "0.5.0"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion book/src/1_getting_started/1_setting_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use glutin::{
};

fn main() {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let _context = create_window(&event_loop);
}

Expand Down
2 changes: 1 addition & 1 deletion book/src/1_getting_started/2_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use glutin::{
};

fn main() {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let (context, gl_display, window, surface) = create_window(&event_loop);

let renderer = unsafe { OpenGl::new_from_function_cstr(|s| gl_display.get_proc_address(s).cast()) }
Expand Down
32 changes: 17 additions & 15 deletions book/src/1_getting_started/3_event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use glutin_winit::DisplayBuilder;
use raw_window_handle::HasRawWindowHandle;
use winit::dpi::PhysicalPosition;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::event_loop::EventLoop;
use winit::window::WindowBuilder;
use winit::{dpi::PhysicalSize, window::Window};

Expand All @@ -21,7 +21,7 @@ use glutin::{
};

fn main() {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let (context, gl_display, window, surface) = create_window(&event_loop);

let renderer = unsafe { OpenGl::new_from_function_cstr(|s| gl_display.get_proc_address(s).cast()) }
Expand All @@ -32,20 +32,22 @@ fn main() {

let mut mouse_position = PhysicalPosition::new(0., 0.);

event_loop.run(move |event, _target, control_flow| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CursorMoved { position, .. } => {
mouse_position = position;
window.request_redraw();
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
event_loop
.run(move |event, target| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CursorMoved { position, .. } => {
mouse_position = position;
window.request_redraw();
}
WindowEvent::CloseRequested => target.exit(),
WindowEvent::RedrawRequested { .. } => {
render(&context, &surface, &window, &mut canvas, mouse_position);
}
_ => {}
},
_ => {}
},
Event::RedrawRequested(_) => {
render(&context, &surface, &window, &mut canvas, mouse_position);
}
_ => {}
})
})
.unwrap();
}

fn create_window(event_loop: &EventLoop<()>) -> (PossiblyCurrentContext, Display, Window, Surface<WindowSurface>) {
Expand Down
65 changes: 33 additions & 32 deletions examples/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rand::{
};
use resource::resource;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event::{DeviceEvent, ElementState, Event, MouseButton, WindowEvent},
event_loop::{EventLoop, EventLoopWindowTarget},
window::Window,
};

Expand Down Expand Up @@ -166,7 +166,7 @@ impl Game {
}
}

fn handle_events(&mut self, window: &Window, event: &Event<()>, control_flow: &mut ControlFlow) {
fn handle_events(&mut self, window: &Window, event: &Event<()>, event_loop_target: &EventLoopWindowTarget<()>) {
if self.state == State::InGame {
let _ = window.set_cursor_grab(winit::window::CursorGrabMode::Confined);
window.set_cursor_visible(false);
Expand All @@ -178,15 +178,15 @@ impl Game {
match event {
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
event:
winit::event::KeyEvent {
physical_key: winit::keyboard::PhysicalKey::Code(winit::keyboard::KeyCode::Escape),
state: ElementState::Pressed,
..
},
..
} => match self.state {
State::TitleScreen => *control_flow = ControlFlow::Exit,
State::TitleScreen => event_loop_target.exit(),
State::InGame => self.state = State::Paused,
State::Paused => self.state = State::TitleScreen,
State::Win { .. } | State::GameOver { .. } => self.state = State::TitleScreen,
Expand Down Expand Up @@ -1269,13 +1269,12 @@ fn run(
let start = Instant::now();
let mut prevt = start;

el.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;

game.handle_events(&window, &event, control_flow);
el.run(move |event, event_loop_window_target| {
game.handle_events(&window, &event, event_loop_window_target);
event_loop_window_target.set_control_flow(winit::event_loop::ControlFlow::Poll);

match event {
Event::LoopDestroyed => *control_flow = ControlFlow::Exit,
Event::LoopExiting => event_loop_window_target.exit(),
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(physical_size) => {
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -1286,30 +1285,32 @@ fn run(
);
game.size = Size::new(physical_size.width as f32, physical_size.height as f32);
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => event_loop_window_target.exit(),
WindowEvent::RedrawRequested { .. } => {
let dpi_factor = window.scale_factor();
let size = window.inner_size();
canvas.set_size(size.width, size.height, dpi_factor as f32);
canvas.clear_rect(0, 0, size.width, size.height, Color::rgbf(0.15, 0.15, 0.12));

let now = Instant::now();
let dt = (now - prevt).as_secs_f32();
prevt = now;

game.update(dt);
game.draw(&mut canvas);

canvas.flush();
#[cfg(not(target_arch = "wasm32"))]
surface.swap_buffers(&context).unwrap();
}
_ => (),
},
Event::RedrawRequested(_) => {
let dpi_factor = window.scale_factor();
let size = window.inner_size();
canvas.set_size(size.width, size.height, dpi_factor as f32);
canvas.clear_rect(0, 0, size.width, size.height, Color::rgbf(0.15, 0.15, 0.12));

let now = Instant::now();
let dt = (now - prevt).as_secs_f32();
prevt = now;

game.update(dt);
game.draw(&mut canvas);

canvas.flush();
#[cfg(not(target_arch = "wasm32"))]
surface.swap_buffers(&context).unwrap();
}
Event::MainEventsCleared => window.request_redraw(),

Event::AboutToWait => window.request_redraw(),
_ => (),
}
});
})
.unwrap();
}

fn vector_direction(target: Vector) -> Direction {
Expand Down
Loading

0 comments on commit 9f1ba83

Please sign in to comment.