This is based on the API that will be used for no-copy presentation. But wraps it in `set_buffer`. This also fixes the Wayland buffer code to set `self.width` and `self.height` on resize, and set the length of the shared memory file when the buffer is created. Co-authored-by: jtnunley <jtnunley01@gmail.com>
64 lines
2 KiB
Rust
64 lines
2 KiB
Rust
use std::num::NonZeroU32;
|
|
use winit::event::{Event, WindowEvent};
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
use winit::window::WindowBuilder;
|
|
|
|
const BUFFER_WIDTH: usize = 256;
|
|
const BUFFER_HEIGHT: usize = 128;
|
|
|
|
fn main() {
|
|
let event_loop = EventLoop::new();
|
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
use winit::platform::web::WindowExtWebSys;
|
|
|
|
web_sys::window()
|
|
.unwrap()
|
|
.document()
|
|
.unwrap()
|
|
.body()
|
|
.unwrap()
|
|
.append_child(&window.canvas())
|
|
.unwrap();
|
|
}
|
|
|
|
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
|
|
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
match event {
|
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
|
surface
|
|
.resize(
|
|
NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(),
|
|
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
|
|
)
|
|
.unwrap();
|
|
|
|
let mut buffer = surface.buffer_mut().unwrap();
|
|
for y in 0..BUFFER_HEIGHT {
|
|
for x in 0..BUFFER_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 * BUFFER_WIDTH + x] = color;
|
|
}
|
|
}
|
|
buffer.present().unwrap();
|
|
}
|
|
Event::WindowEvent {
|
|
event: WindowEvent::CloseRequested,
|
|
window_id,
|
|
} if window_id == window.id() => {
|
|
*control_flow = ControlFlow::Exit;
|
|
}
|
|
_ => {}
|
|
}
|
|
});
|
|
}
|