Skip to content

Commit 125ad07

Browse files
authored
Merge pull request #64 from rust-windowing/display-window-split
Split display/window wrappers
2 parents 3b33bbb + 1290699 commit 125ad07

File tree

11 files changed

+297
-157
lines changed

11 files changed

+297
-157
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ For now, the priority for new platforms is:
5353
Example
5454
==
5555
```rust,no_run
56-
use softbuffer::GraphicsContext;
5756
use winit::event::{Event, WindowEvent};
5857
use winit::event_loop::{ControlFlow, EventLoop};
5958
use winit::window::WindowBuilder;
6059
6160
fn main() {
6261
let event_loop = EventLoop::new();
6362
let window = WindowBuilder::new().build(&event_loop).unwrap();
64-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
63+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
64+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
6565
6666
event_loop.run(move |event, _, control_flow| {
6767
*control_flow = ControlFlow::Wait;
@@ -86,7 +86,7 @@ fn main() {
8686
})
8787
.collect::<Vec<_>>();
8888
89-
graphics_context.set_buffer(&buffer, width as u16, height as u16);
89+
surface.set_buffer(&buffer, width as u16, height as u16);
9090
}
9191
Event::WindowEvent {
9292
event: WindowEvent::CloseRequested,

examples/animation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use instant::Instant;
22
#[cfg(not(target_arch = "wasm32"))]
33
use rayon::prelude::*;
4-
use softbuffer::GraphicsContext;
54
use std::f64::consts::PI;
65
use winit::event::{Event, WindowEvent};
76
use winit::event_loop::{ControlFlow, EventLoop};
@@ -25,7 +24,8 @@ fn main() {
2524
.unwrap();
2625
}
2726

28-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
27+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
28+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
2929

3030
let mut old_size = (0, 0);
3131
let mut frames = pre_render_frames(0, 0);
@@ -48,7 +48,7 @@ fn main() {
4848
};
4949

5050
let buffer = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
51-
graphics_context.set_buffer(buffer.as_slice(), width as u16, height as u16);
51+
surface.set_buffer(buffer.as_slice(), width as u16, height as u16);
5252
}
5353
Event::MainEventsCleared => {
5454
window.request_redraw();

examples/fruit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use image::GenericImageView;
2-
use softbuffer::GraphicsContext;
32
use winit::event::{Event, WindowEvent};
43
use winit::event_loop::{ControlFlow, EventLoop};
54
use winit::window::WindowBuilder;
@@ -38,14 +37,15 @@ fn main() {
3837
.unwrap();
3938
}
4039

41-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
40+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
41+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
4242

4343
event_loop.run(move |event, _, control_flow| {
4444
*control_flow = ControlFlow::Wait;
4545

4646
match event {
4747
Event::RedrawRequested(window_id) if window_id == window.id() => {
48-
graphics_context.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
48+
surface.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
4949
}
5050
Event::WindowEvent {
5151
event: WindowEvent::CloseRequested,

examples/libxcb.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
44
mod example {
55
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
6-
use softbuffer::GraphicsContext;
76
use x11rb::{
87
connection::Connection,
98
protocol::{
@@ -54,13 +53,12 @@ mod example {
5453

5554
// Create a new softbuffer context.
5655
// SAFETY: The display and window handles outlive the context.
57-
let mut context = unsafe {
58-
GraphicsContext::from_raw(
59-
RawWindowHandle::Xcb(window_handle),
60-
RawDisplayHandle::Xcb(display_handle),
61-
)
62-
}
63-
.unwrap();
56+
let context =
57+
unsafe { softbuffer::Context::from_raw(RawDisplayHandle::Xcb(display_handle)) }
58+
.unwrap();
59+
let mut surface =
60+
unsafe { softbuffer::Surface::from_raw(&context, RawWindowHandle::Xcb(window_handle)) }
61+
.unwrap();
6462

6563
// Register an atom for closing the window.
6664
let wm_protocols_atom = conn
@@ -104,7 +102,7 @@ mod example {
104102
.collect::<Vec<_>>();
105103

106104
// Draw the buffer.
107-
context.set_buffer(&source, width, height);
105+
surface.set_buffer(&source, width, height);
108106
}
109107
Event::ConfigureNotify(configure_notify) => {
110108
width = configure_notify.width;

examples/rectangle.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use softbuffer::GraphicsContext;
21
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
32
use winit::event_loop::{ControlFlow, EventLoop};
43
use winit::window::WindowBuilder;
@@ -41,7 +40,8 @@ fn main() {
4140
.unwrap();
4241
}
4342

44-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
43+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
44+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
4545

4646
let mut buffer = Vec::new();
4747
let mut flag = false;
@@ -66,7 +66,7 @@ fn main() {
6666
redraw(&mut buffer, width, height, flag);
6767

6868
// Blit the offscreen buffer to the window's client area
69-
graphics_context.set_buffer(&buffer, width as u16, height as u16);
69+
surface.set_buffer(&buffer, width as u16, height as u16);
7070
}
7171

7272
Event::WindowEvent {

examples/winit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use softbuffer::GraphicsContext;
21
use winit::event::{Event, WindowEvent};
32
use winit::event_loop::{ControlFlow, EventLoop};
43
use winit::window::WindowBuilder;
@@ -21,7 +20,8 @@ fn main() {
2120
.unwrap();
2221
}
2322

24-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
23+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
24+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
2525

2626
event_loop.run(move |event, _, control_flow| {
2727
*control_flow = ControlFlow::Wait;
@@ -46,7 +46,7 @@ fn main() {
4646
})
4747
.collect::<Vec<_>>();
4848

49-
graphics_context.set_buffer(&buffer, width as u16, height as u16);
49+
surface.set_buffer(&buffer, width as u16, height as u16);
5050
}
5151
Event::WindowEvent {
5252
event: WindowEvent::CloseRequested,

examples/winit_wrong_sized_buffer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use softbuffer::GraphicsContext;
21
use winit::event::{Event, WindowEvent};
32
use winit::event_loop::{ControlFlow, EventLoop};
43
use winit::window::WindowBuilder;
@@ -24,7 +23,8 @@ fn main() {
2423
.unwrap();
2524
}
2625

27-
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
26+
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
27+
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
2828

2929
event_loop.run(move |event, _, control_flow| {
3030
*control_flow = ControlFlow::Wait;
@@ -45,7 +45,7 @@ fn main() {
4545
})
4646
.collect::<Vec<_>>();
4747

48-
graphics_context.set_buffer(&buffer, BUFFER_WIDTH as u16, BUFFER_HEIGHT as u16);
48+
surface.set_buffer(&buffer, BUFFER_WIDTH as u16, BUFFER_HEIGHT as u16);
4949
}
5050
Event::WindowEvent {
5151
event: WindowEvent::CloseRequested,

src/error.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ use thiserror::Error;
55
#[derive(Error, Debug)]
66
#[non_exhaustive]
77
pub enum SoftBufferError {
8+
#[error(
9+
"The provided display returned an unsupported platform: {human_readable_display_platform_name}."
10+
)]
11+
UnsupportedDisplayPlatform {
12+
human_readable_display_platform_name: &'static str,
13+
display_handle: RawDisplayHandle,
14+
},
815
#[error(
916
"The provided window returned an unsupported platform: {human_readable_window_platform_name}, {human_readable_display_platform_name}."
1017
)]
11-
UnsupportedPlatform {
18+
UnsupportedWindowPlatform {
1219
human_readable_window_platform_name: &'static str,
1320
human_readable_display_platform_name: &'static str,
1421
window_handle: RawWindowHandle,
15-
display_handle: RawDisplayHandle,
1622
},
1723

1824
#[error("The provided window handle is null.")]

0 commit comments

Comments
 (0)