Skip to content

Commit

Permalink
ex: Bump winit to v0.30
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
notgull authored May 7, 2024
1 parent 28ebe6e commit 0e6706c
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 433 deletions.
16 changes: 2 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ jobs:
- name: Run Typos
run: typos

miri-tests:
runs-on: ubuntu-latest
needs: fmt
steps:
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
with:
rust-version: nightly
components: miri
- name: Run tests with miri
run: cargo +nightly miri test

tests:
name: Tests
needs: fmt
strategy:
fail-fast: false
matrix:
rust_version: ['1.65.0', stable, nightly]
rust_version: ['1.70.0', stable, nightly]
platform:
- { target: x86_64-pc-windows-msvc, os: windows-latest, }
- { target: i686-pc-windows-msvc, os: windows-latest, }
Expand Down Expand Up @@ -98,7 +86,7 @@ jobs:
run: sudo apt-get install gcc-multilib

- name: Pin deps that break MSRV
if: matrix.rust_version == '1.65.0'
if: matrix.rust_version == '1.70.0'
run: |
cargo update -p exr --precise 1.71.0
cargo update -p ahash --precise 0.8.7
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Bump MSRV to 1.70.0.

# 0.4.2

- Add the ability to get the underlying window handle. (#193)
Expand Down
12 changes: 3 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/rust-windowing/softbuffer"
keywords = ["framebuffer", "windowing"]
categories = ["game-development", "graphics", "gui", "multimedia", "rendering"]
exclude = ["examples"]
rust-version = "1.65.0"
rust-version = "1.70.0"

[[bench]]
name = "buffer_mut"
Expand Down Expand Up @@ -80,9 +80,8 @@ cfg_aliases = "0.2.0"
[dev-dependencies]
colorous = "1.0.12"
criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] }
instant = "0.1.12"
winit = "0.29.2"
winit-test = "0.1.0"
web-time = "1.0.0"
winit = "0.30.0"

[dev-dependencies.image]
version = "0.24.6"
Expand All @@ -106,11 +105,6 @@ members = [
"run-wasm",
]

[[test]]
name = "present_and_fetch"
path = "tests/present_and_fetch.rs"
harness = false

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use winit::window::Window;
include!("../examples/utils/winit_app.rs");
fn main() {
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
event_loop.run(move |event, elwt| {
let mut app = winit_app::WinitAppBuilder::with_init(|elwt| {
let window = {
let window = elwt.create_window(Window::default_attributes());
Rc::new(window.unwrap())
};
let context = softbuffer::Context::new(window.clone()).unwrap();
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
(window, surface)
}).with_event_handler(|state, event, elwt| {
let (window, surface) = state;
elwt.set_control_flow(ControlFlow::Wait);
match event {
Expand Down Expand Up @@ -107,7 +116,9 @@ fn main() {
}
_ => {}
}
}).unwrap();
});
event_loop.run_app(&mut app).unwrap();
}
```

Expand Down
7 changes: 4 additions & 3 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)] // TODO

use criterion::{criterion_group, criterion_main, Criterion};

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

let mut evl = winit::event_loop::EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new()
.with_visible(false)
.build(&evl)
let window = evl
.create_window(winit::window::Window::default_attributes().with_visible(false))
.unwrap();

evl.run_on_demand(move |ev, elwt| {
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
fn main() {
println!("cargo:rustc-check-cfg=cfg(free_unix)");
println!("cargo:rustc-check-cfg=cfg(kms_platform)");
println!("cargo:rustc-check-cfg=cfg(x11_platform)");
println!("cargo:rustc-check-cfg=cfg(wayland_platform)");

cfg_aliases::cfg_aliases! {
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))) },
kms_platform: { all(feature = "kms", free_unix, not(target_arch = "wasm32")) },
Expand Down
130 changes: 61 additions & 69 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,81 @@
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use std::rc::Rc;
use web_time::Instant;
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;

include!("./utils/winit_app.rs");

fn main() {
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
let start = Instant::now();

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

web_sys::window()
.unwrap()
.document()
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas().unwrap())
.unwrap();
}
let context = softbuffer::Context::new(window.clone()).unwrap();
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
let old_size = (0, 0);
let frames = pre_render_frames(0, 0);

let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);
(window, surface, old_size, frames)
})
.with_event_handler(move |state, event, elwt| {
let (window, surface, old_size, frames) = state;

let start = Instant::now();
event_loop
.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Poll);

match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
if let (Some(width), Some(height)) = {
let size = window.inner_size();
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
let elapsed = start.elapsed().as_secs_f64() % 1.0;

if (width.get(), height.get()) != old_size {
old_size = (width.get(), height.get());
frames = pre_render_frames(width.get() as usize, height.get() as usize);
};

let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];

surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
}
Event::AboutToWait => {
window.request_redraw();
}
Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();
elwt.set_control_flow(ControlFlow::Poll);

match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
if let (Some(width), Some(height)) = {
let size = window.inner_size();
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
let elapsed = start.elapsed().as_secs_f64() % 1.0;

if (width.get(), height.get()) != *old_size {
*old_size = (width.get(), height.get());
*frames = pre_render_frames(width.get() as usize, height.get() as usize);
};

let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];

surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
_ => {}
}
})
.unwrap();
Event::AboutToWait => {
window.request_redraw();
}
Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
});

winit_app::run_app(event_loop, app);
}

fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {
Expand Down
Loading

0 comments on commit 0e6706c

Please sign in to comment.