-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathwinit_wrong_sized_buffer.rs
More file actions
77 lines (68 loc) · 2.58 KB
/
winit_wrong_sized_buffer.rs
File metadata and controls
77 lines (68 loc) · 2.58 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
use std::num::NonZeroU32;
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
#[path = "utils/winit_app.rs"]
mod winit_app;
fn main() {
let event_loop = EventLoop::new().unwrap();
let app = winit_app::WinitAppBuilder::with_init(
|elwt| {
let window = winit_app::make_window(elwt, |w| w);
let context = softbuffer::Context::new(window.clone()).unwrap();
(window, context)
},
|_elwt, (window, context)| {
let mut surface = softbuffer::Surface::new(context, window.clone()).unwrap();
// Intentionally set the size of the surface to something different than the size of the window.
surface
.resize(NonZeroU32::new(256).unwrap(), NonZeroU32::new(128).unwrap())
.unwrap();
surface
},
)
.with_event_handler(|state, surface, event, elwt| {
let (window, _context) = state;
elwt.set_control_flow(ControlFlow::Wait);
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let mut buffer = surface.buffer_mut().unwrap();
let width = buffer.width();
for y in 0..buffer.height() {
for x in 0..width {
let red = x as u32 % 255;
let green = y as u32 % 255;
let blue = (x as u32 * y as u32) % 255;
let color = blue | (green << 8) | (red << 16);
buffer[y * width + x] = color;
}
}
buffer.present().unwrap();
}
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);
}