Skip to content

Commit b97e10e

Browse files
committed
Simplify creation of Context in examples
1 parent 92dba66 commit b97e10e

File tree

8 files changed

+41
-64
lines changed

8 files changed

+41
-64
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,16 @@ mod winit_app;
7979
8080
fn main() {
8181
let event_loop = EventLoop::new().unwrap();
82+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
8283
8384
let mut app = winit_app::WinitAppBuilder::with_init(
8485
|elwt| {
85-
let window = {
86-
let window = elwt.create_window(Window::default_attributes());
87-
Rc::new(window.unwrap())
88-
};
89-
let context = softbuffer::Context::new(window.clone()).unwrap();
90-
91-
(window, context)
86+
let window = elwt.create_window(Window::default_attributes());
87+
Rc::new(window.unwrap())
9288
},
93-
|_elwt, (window, context)| softbuffer::Surface::new(context, window.clone()).unwrap(),
89+
|_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(),
9490
)
95-
.with_event_handler(|(window, _context), surface, event, elwt| {
91+
.with_event_handler(|window, surface, event, elwt| {
9692
elwt.set_control_flow(ControlFlow::Wait);
9793
9894
match event {

benches/buffer_mut.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn buffer_mut(c: &mut Criterion) {
1818
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
1919

2020
let mut evl = winit::event_loop::EventLoop::new().unwrap();
21+
let context = Context::new(evl.owned_display_handle()).unwrap();
2122
let window = evl
2223
.create_window(winit::window::Window::default_attributes().with_visible(false))
2324
.unwrap();
@@ -28,10 +29,7 @@ fn buffer_mut(c: &mut Criterion) {
2829
if let winit::event::Event::AboutToWait = ev {
2930
elwt.exit();
3031

31-
let mut surface = {
32-
let context = Context::new(elwt).unwrap();
33-
Surface::new(&context, &window).unwrap()
34-
};
32+
let mut surface = { Surface::new(&context, &window).unwrap() };
3533

3634
let size = window.inner_size();
3735
surface

examples/animation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ fn main() {
1414
let event_loop = EventLoop::new().unwrap();
1515
let start = Instant::now();
1616

17+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
18+
1719
let app = winit_app::WinitAppBuilder::with_init(
1820
|event_loop| {
1921
let window = winit_app::make_window(event_loop, |w| w);
2022

21-
let context = softbuffer::Context::new(window.clone()).unwrap();
22-
2323
let old_size = (0, 0);
2424
let frames = pre_render_frames(0, 0);
2525

26-
(window, context, old_size, frames)
26+
(window, old_size, frames)
2727
},
28-
|_elwft, (window, context, _old_size, _frames)| {
29-
softbuffer::Surface::new(context, window.clone()).unwrap()
28+
move |_elwft, (window, _old_size, _frames)| {
29+
softbuffer::Surface::new(&context, window.clone()).unwrap()
3030
},
3131
)
3232
.with_event_handler(move |state, surface, event, elwt| {
33-
let (window, _context, old_size, frames) = state;
33+
let (window, old_size, frames) = state;
3434

3535
elwt.set_control_flow(ControlFlow::Poll);
3636

examples/fruit.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@ fn main() {
1313
let (width, height) = (fruit.width(), fruit.height());
1414

1515
let event_loop = EventLoop::new().unwrap();
16+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
1617

1718
let app = winit_app::WinitAppBuilder::with_init(
1819
move |elwt| {
19-
let window = winit_app::make_window(elwt, |w| {
20+
winit_app::make_window(elwt, |w| {
2021
w.with_inner_size(winit::dpi::PhysicalSize::new(width, height))
21-
});
22-
23-
let context = softbuffer::Context::new(window.clone()).unwrap();
24-
25-
(window, context)
22+
})
2623
},
27-
move |_elwt, (window, context)| {
28-
let mut surface = softbuffer::Surface::new(context, window.clone()).unwrap();
24+
move |_elwt, window| {
25+
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
2926
// Intentionally only set the size of the surface once, at creation.
3027
// This is needed if the window chooses to ignore the size we passed in above, and for the
3128
// platforms softbuffer supports that don't yet extract the size from the window.
@@ -38,8 +35,7 @@ fn main() {
3835
surface
3936
},
4037
)
41-
.with_event_handler(move |state, surface, event, elwt| {
42-
let (window, _context) = state;
38+
.with_event_handler(move |window, surface, event, elwt| {
4339
elwt.set_control_flow(ControlFlow::Wait);
4440

4541
match event {

examples/rectangle.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@ fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
2424

2525
fn main() {
2626
let event_loop = EventLoop::new().unwrap();
27+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
2728

2829
let app = winit_app::WinitAppBuilder::with_init(
2930
|elwt| {
3031
let window = winit_app::make_window(elwt, |w| {
3132
w.with_title("Press space to show/hide a rectangle")
3233
});
3334

34-
let context = softbuffer::Context::new(window.clone()).unwrap();
35-
3635
let flag = false;
3736

38-
(window, context, flag)
39-
},
40-
|_elwt, (window, context, _flag)| {
41-
softbuffer::Surface::new(context, window.clone()).unwrap()
37+
(window, flag)
4238
},
39+
move |_elwt, (window, _flag)| softbuffer::Surface::new(&context, window.clone()).unwrap(),
4340
)
4441
.with_event_handler(|state, surface, event, elwt| {
45-
let (window, _context, flag) = state;
42+
let (window, flag) = state;
4643

4744
elwt.set_control_flow(ControlFlow::Wait);
4845

examples/winit.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ fn main() {
1212
}
1313

1414
pub(crate) fn entry(event_loop: EventLoop<()>) {
15-
let app = winit_app::WinitAppBuilder::with_init(
16-
|elwt| {
17-
let window = winit_app::make_window(elwt, |w| w);
18-
19-
let context = softbuffer::Context::new(window.clone()).unwrap();
15+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
2016

21-
(window, context)
22-
},
23-
|_elwt, (window, context)| softbuffer::Surface::new(context, window.clone()).unwrap(),
17+
let app = winit_app::WinitAppBuilder::with_init(
18+
|elwt| winit_app::make_window(elwt, |w| w),
19+
move |_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(),
2420
)
25-
.with_event_handler(|(window, _context), surface, event, elwt| {
21+
.with_event_handler(|window, surface, event, elwt| {
2622
elwt.set_control_flow(ControlFlow::Wait);
2723

2824
match event {

examples/winit_multithread.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ pub mod ex {
99
use std::num::NonZeroU32;
1010
use std::sync::{mpsc, Arc, Mutex};
1111
use winit::event::{Event, KeyEvent, WindowEvent};
12-
use winit::event_loop::{ControlFlow, EventLoop};
12+
use winit::event_loop::{ControlFlow, EventLoop, OwnedDisplayHandle};
1313
use winit::keyboard::{Key, NamedKey};
1414
use winit::window::Window;
1515

1616
use super::winit_app;
1717

18-
type Surface = softbuffer::Surface<Arc<Window>, Arc<Window>>;
18+
type Surface = softbuffer::Surface<OwnedDisplayHandle, Arc<Window>>;
1919

2020
fn render_thread(
2121
window: Arc<Window>,
@@ -60,6 +60,8 @@ pub mod ex {
6060
}
6161

6262
pub fn entry(event_loop: EventLoop<()>) {
63+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
64+
6365
let app = winit_app::WinitAppBuilder::with_init(
6466
|elwt| {
6567
let attributes = Window::default_attributes();
@@ -68,8 +70,6 @@ pub mod ex {
6870
winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true);
6971
let window = Arc::new(elwt.create_window(attributes).unwrap());
7072

71-
let context = softbuffer::Context::new(window.clone()).unwrap();
72-
7373
// Spawn a thread to handle rendering for this specific surface. The channels will
7474
// be closed and the thread will be stopped whenever this surface (the returned
7575
// context below) is dropped, so that it can all be recreated again (on Android)
@@ -82,17 +82,17 @@ pub mod ex {
8282
move || render_thread(window, do_render, render_done)
8383
});
8484

85-
(window, context, start_render, finish_render)
85+
(window, start_render, finish_render)
8686
},
87-
|_elwt, (window, context, _start_render, _finish_render)| {
87+
move |_elwt, (window, _start_render, _finish_render)| {
8888
println!("making surface...");
8989
Arc::new(Mutex::new(
90-
softbuffer::Surface::new(context, window.clone()).unwrap(),
90+
softbuffer::Surface::new(&context, window.clone()).unwrap(),
9191
))
9292
},
9393
)
9494
.with_event_handler(|state, surface, event, elwt| {
95-
let (window, _context, start_render, finish_render) = state;
95+
let (window, start_render, finish_render) = state;
9696
elwt.set_control_flow(ControlFlow::Wait);
9797

9898
match event {

examples/winit_wrong_sized_buffer.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ const BUFFER_HEIGHT: usize = 128;
1111

1212
fn main() {
1313
let event_loop = EventLoop::new().unwrap();
14+
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
1415

1516
let app = winit_app::WinitAppBuilder::with_init(
16-
|elwt| {
17-
let window = winit_app::make_window(elwt, |w| w);
18-
19-
let context = softbuffer::Context::new(window.clone()).unwrap();
20-
21-
(window, context)
22-
},
23-
|_elwt, (window, context)| {
24-
let mut surface = softbuffer::Surface::new(context, window.clone()).unwrap();
17+
|elwt| winit_app::make_window(elwt, |w| w),
18+
move |_elwt, window| {
19+
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
2520
// Intentionally set the size of the surface to something different than the size of the window.
2621
surface
2722
.resize(
@@ -32,8 +27,7 @@ fn main() {
3227
surface
3328
},
3429
)
35-
.with_event_handler(|state, surface, event, elwt| {
36-
let (window, _context) = state;
30+
.with_event_handler(|window, surface, event, elwt| {
3731
elwt.set_control_flow(ControlFlow::Wait);
3832

3933
match event {

0 commit comments

Comments
 (0)