-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathwinit_wrong_sized_buffer.rs
More file actions
82 lines (70 loc) · 2.74 KB
/
winit_wrong_sized_buffer.rs
File metadata and controls
82 lines (70 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! A window with a surface that is 200 pixels less wide and 400 pixels less tall.
//!
//! This is useful for testing that zero-sized buffers work, as well as testing buffer vs. window
//! size discrepancies in general.
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
mod util;
fn main() {
util::setup();
let event_loop = EventLoop::new().unwrap();
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();
let app = util::WinitAppBuilder::with_init(
|elwt| util::make_window(elwt, |w| w),
move |_elwt, window| {
let size = window.inner_size();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
let width = size.width.saturating_sub(200);
let height = size.height.saturating_sub(400);
tracing::info!("size initially at: {width}/{height}");
surface.resize(width, height).unwrap();
surface
},
)
.with_event_handler(|window, surface, window_id, event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);
if window_id != window.id() {
return;
}
match event {
WindowEvent::Resized(size) => {
let Some(surface) = surface else {
tracing::warn!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let width = size.width.saturating_sub(200);
let height = size.height.saturating_sub(400);
tracing::info!("resized to: {width}/{height}");
surface.resize(width, height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
tracing::warn!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let mut buffer = surface.buffer_mut().unwrap();
for (x, y, pixel) in buffer.pixels_iter() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
*pixel = blue | (green << 8) | (red << 16);
}
buffer.present().unwrap();
}
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
} => {
elwt.exit();
}
_ => {}
}
});
util::run_app(event_loop, app);
}