Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use the safer API for window handles #82

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ x11 = ["bytemuck", "nix", "x11rb", "x11-dl"]

[dependencies]
log = "0.4.17"
raw-window-handle = "0.5.0"
raw-window-handle = { version = "0.5.0", features = ["std"] }
thiserror = "1.0.30"

[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
Expand Down Expand Up @@ -77,3 +77,6 @@ rayon = "1.5.1"
members = [
"run-wasm",
]

[patch.crates-io]
winit = { git = "https://github.com/bread-graphics/winit.git", branch = "window-handle", version = "0.28.3" }
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ To run an example with the web backend: `cargo run-wasm --example winit`
Example
==
```rust,no_run
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
let context = softbuffer::Context::new(Rc::clone(&window)).unwrap();
let mut surface = softbuffer::Surface::new(&context, Rc::clone(&window)).unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
7 changes: 4 additions & 3 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

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

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -24,8 +25,8 @@ fn main() {
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);
Expand Down
15 changes: 9 additions & 6 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use image::GenericImageView;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -18,10 +19,12 @@ fn main() {
.collect::<Vec<_>>();

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height()))
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height()))
.build(&event_loop)
.unwrap(),
);

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -37,8 +40,8 @@ fn main() {
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
15 changes: 9 additions & 6 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::rc::Rc;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -21,10 +22,12 @@ fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
fn main() {
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("Press space to show/hide a rectangle")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Press space to show/hide a rectangle")
.build(&event_loop)
.unwrap(),
);

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -40,8 +43,8 @@ fn main() {
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

let mut buffer = Vec::new();
let mut flag = false;
Expand Down
7 changes: 4 additions & 3 deletions examples/winit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

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

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -20,8 +21,8 @@ fn main() {
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
7 changes: 4 additions & 3 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -7,7 +8,7 @@ const BUFFER_HEIGHT: usize = 128;

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

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -23,8 +24,8 @@ fn main() {
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use raw_window_handle::{HandleError, RawDisplayHandle, RawWindowHandle};
use std::error::Error;
use thiserror::Error;

Expand Down Expand Up @@ -27,6 +27,9 @@ pub enum SoftBufferError {
#[error("The provided display handle is null.")]
IncompleteDisplayHandle,

#[error("A handle error occurred: {0}")]
HandleError(#[from] HandleError),

#[error("Platform error")]
PlatformError(Option<String>, Option<Box<dyn Error>>),
}
Expand Down
Loading