Skip to content

Commit 0e6706c

Browse files
authored
ex: Bump winit to v0.30
This commit bumps winit, which is used in some examples, to version v0.30. This new version comes with many new changes; mainly the move away from closures towards applications. In order to work with this, I create a simple "winit_app" module that allows the examples to simply and easily create winit applications. It takes two closures: one that initializes some state on resume, and another that handles events. I've found this approach reduces diff noise by a lot. Benchmarks still use deprecated functions for now. Signed-off-by: John Nunley <[email protected]>
1 parent 28ebe6e commit 0e6706c

File tree

13 files changed

+445
-433
lines changed

13 files changed

+445
-433
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,13 @@ jobs:
2525
- name: Run Typos
2626
run: typos
2727

28-
miri-tests:
29-
runs-on: ubuntu-latest
30-
needs: fmt
31-
steps:
32-
- uses: actions/checkout@v4
33-
- uses: hecrj/setup-rust-action@v2
34-
with:
35-
rust-version: nightly
36-
components: miri
37-
- name: Run tests with miri
38-
run: cargo +nightly miri test
39-
4028
tests:
4129
name: Tests
4230
needs: fmt
4331
strategy:
4432
fail-fast: false
4533
matrix:
46-
rust_version: ['1.65.0', stable, nightly]
34+
rust_version: ['1.70.0', stable, nightly]
4735
platform:
4836
- { target: x86_64-pc-windows-msvc, os: windows-latest, }
4937
- { target: i686-pc-windows-msvc, os: windows-latest, }
@@ -98,7 +86,7 @@ jobs:
9886
run: sudo apt-get install gcc-multilib
9987

10088
- name: Pin deps that break MSRV
101-
if: matrix.rust_version == '1.65.0'
89+
if: matrix.rust_version == '1.70.0'
10290
run: |
10391
cargo update -p exr --precise 1.71.0
10492
cargo update -p ahash --precise 0.8.7

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Bump MSRV to 1.70.0.
4+
35
# 0.4.2
46

57
- Add the ability to get the underlying window handle. (#193)

Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repository = "https://github.com/rust-windowing/softbuffer"
1010
keywords = ["framebuffer", "windowing"]
1111
categories = ["game-development", "graphics", "gui", "multimedia", "rendering"]
1212
exclude = ["examples"]
13-
rust-version = "1.65.0"
13+
rust-version = "1.70.0"
1414

1515
[[bench]]
1616
name = "buffer_mut"
@@ -80,9 +80,8 @@ cfg_aliases = "0.2.0"
8080
[dev-dependencies]
8181
colorous = "1.0.12"
8282
criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] }
83-
instant = "0.1.12"
84-
winit = "0.29.2"
85-
winit-test = "0.1.0"
83+
web-time = "1.0.0"
84+
winit = "0.30.0"
8685

8786
[dev-dependencies.image]
8887
version = "0.24.6"
@@ -106,11 +105,6 @@ members = [
106105
"run-wasm",
107106
]
108107

109-
[[test]]
110-
name = "present_and_fetch"
111-
path = "tests/present_and_fetch.rs"
112-
harness = false
113-
114108
[package.metadata.docs.rs]
115109
all-features = true
116110
rustdoc-args = ["--cfg", "docsrs"]

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,24 @@ use std::num::NonZeroU32;
6262
use std::rc::Rc;
6363
use winit::event::{Event, WindowEvent};
6464
use winit::event_loop::{ControlFlow, EventLoop};
65-
use winit::window::WindowBuilder;
65+
use winit::window::Window;
66+
67+
include!("../examples/utils/winit_app.rs");
6668
6769
fn main() {
6870
let event_loop = EventLoop::new().unwrap();
69-
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
70-
let context = softbuffer::Context::new(window.clone()).unwrap();
71-
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
7271
73-
event_loop.run(move |event, elwt| {
72+
let mut app = winit_app::WinitAppBuilder::with_init(|elwt| {
73+
let window = {
74+
let window = elwt.create_window(Window::default_attributes());
75+
Rc::new(window.unwrap())
76+
};
77+
let context = softbuffer::Context::new(window.clone()).unwrap();
78+
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
79+
80+
(window, surface)
81+
}).with_event_handler(|state, event, elwt| {
82+
let (window, surface) = state;
7483
elwt.set_control_flow(ControlFlow::Wait);
7584
7685
match event {
@@ -107,7 +116,9 @@ fn main() {
107116
}
108117
_ => {}
109118
}
110-
}).unwrap();
119+
});
120+
121+
event_loop.run_app(&mut app).unwrap();
111122
}
112123
```
113124

benches/buffer_mut.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(deprecated)] // TODO
2+
13
use criterion::{criterion_group, criterion_main, Criterion};
24

35
fn buffer_mut(c: &mut Criterion) {
@@ -16,9 +18,8 @@ fn buffer_mut(c: &mut Criterion) {
1618
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
1719

1820
let mut evl = winit::event_loop::EventLoop::new().unwrap();
19-
let window = winit::window::WindowBuilder::new()
20-
.with_visible(false)
21-
.build(&evl)
21+
let window = evl
22+
.create_window(winit::window::Window::default_attributes().with_visible(false))
2223
.unwrap();
2324

2425
evl.run_on_demand(move |ev, elwt| {

build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
fn main() {
2+
println!("cargo:rustc-check-cfg=cfg(free_unix)");
3+
println!("cargo:rustc-check-cfg=cfg(kms_platform)");
4+
println!("cargo:rustc-check-cfg=cfg(x11_platform)");
5+
println!("cargo:rustc-check-cfg=cfg(wayland_platform)");
6+
27
cfg_aliases::cfg_aliases! {
38
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))) },
49
kms_platform: { all(feature = "kms", free_unix, not(target_arch = "wasm32")) },

examples/animation.rs

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,81 @@
1-
use instant::Instant;
21
#[cfg(not(target_arch = "wasm32"))]
32
use rayon::prelude::*;
43
use std::f64::consts::PI;
54
use std::num::NonZeroU32;
6-
use std::rc::Rc;
5+
use web_time::Instant;
76
use winit::event::{Event, KeyEvent, WindowEvent};
87
use winit::event_loop::{ControlFlow, EventLoop};
98
use winit::keyboard::{Key, NamedKey};
10-
use winit::window::WindowBuilder;
9+
10+
include!("./utils/winit_app.rs");
1111

1212
fn main() {
1313
let event_loop = EventLoop::new().unwrap();
14-
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
14+
let start = Instant::now();
1515

16-
#[cfg(target_arch = "wasm32")]
17-
{
18-
use winit::platform::web::WindowExtWebSys;
16+
let app = winit_app::WinitAppBuilder::with_init(|event_loop| {
17+
let window = winit_app::make_window(event_loop, |w| w);
1918

20-
web_sys::window()
21-
.unwrap()
22-
.document()
23-
.unwrap()
24-
.body()
25-
.unwrap()
26-
.append_child(&window.canvas().unwrap())
27-
.unwrap();
28-
}
19+
let context = softbuffer::Context::new(window.clone()).unwrap();
20+
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
2921

30-
let context = softbuffer::Context::new(window.clone()).unwrap();
31-
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
22+
let old_size = (0, 0);
23+
let frames = pre_render_frames(0, 0);
3224

33-
let mut old_size = (0, 0);
34-
let mut frames = pre_render_frames(0, 0);
25+
(window, surface, old_size, frames)
26+
})
27+
.with_event_handler(move |state, event, elwt| {
28+
let (window, surface, old_size, frames) = state;
3529

36-
let start = Instant::now();
37-
event_loop
38-
.run(move |event, elwt| {
39-
elwt.set_control_flow(ControlFlow::Poll);
40-
41-
match event {
42-
Event::WindowEvent {
43-
window_id,
44-
event: WindowEvent::RedrawRequested,
45-
} if window_id == window.id() => {
46-
if let (Some(width), Some(height)) = {
47-
let size = window.inner_size();
48-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
49-
} {
50-
let elapsed = start.elapsed().as_secs_f64() % 1.0;
51-
52-
if (width.get(), height.get()) != old_size {
53-
old_size = (width.get(), height.get());
54-
frames = pre_render_frames(width.get() as usize, height.get() as usize);
55-
};
56-
57-
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
58-
59-
surface.resize(width, height).unwrap();
60-
let mut buffer = surface.buffer_mut().unwrap();
61-
buffer.copy_from_slice(frame);
62-
buffer.present().unwrap();
63-
}
64-
}
65-
Event::AboutToWait => {
66-
window.request_redraw();
67-
}
68-
Event::WindowEvent {
69-
event:
70-
WindowEvent::CloseRequested
71-
| WindowEvent::KeyboardInput {
72-
event:
73-
KeyEvent {
74-
logical_key: Key::Named(NamedKey::Escape),
75-
..
76-
},
77-
..
78-
},
79-
window_id,
80-
} if window_id == window.id() => {
81-
elwt.exit();
30+
elwt.set_control_flow(ControlFlow::Poll);
31+
32+
match event {
33+
Event::WindowEvent {
34+
window_id,
35+
event: WindowEvent::RedrawRequested,
36+
} if window_id == window.id() => {
37+
if let (Some(width), Some(height)) = {
38+
let size = window.inner_size();
39+
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
40+
} {
41+
let elapsed = start.elapsed().as_secs_f64() % 1.0;
42+
43+
if (width.get(), height.get()) != *old_size {
44+
*old_size = (width.get(), height.get());
45+
*frames = pre_render_frames(width.get() as usize, height.get() as usize);
46+
};
47+
48+
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
49+
50+
surface.resize(width, height).unwrap();
51+
let mut buffer = surface.buffer_mut().unwrap();
52+
buffer.copy_from_slice(frame);
53+
buffer.present().unwrap();
8254
}
83-
_ => {}
8455
}
85-
})
86-
.unwrap();
56+
Event::AboutToWait => {
57+
window.request_redraw();
58+
}
59+
Event::WindowEvent {
60+
event:
61+
WindowEvent::CloseRequested
62+
| WindowEvent::KeyboardInput {
63+
event:
64+
KeyEvent {
65+
logical_key: Key::Named(NamedKey::Escape),
66+
..
67+
},
68+
..
69+
},
70+
window_id,
71+
} if window_id == window.id() => {
72+
elwt.exit();
73+
}
74+
_ => {}
75+
}
76+
});
77+
78+
winit_app::run_app(event_loop, app);
8779
}
8880

8981
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {

0 commit comments

Comments
 (0)