diff --git a/examples/winit.rs b/examples/winit.rs index 3ecb33c8..be8e4343 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -32,21 +32,21 @@ fn main() { let size = window.inner_size(); (size.width, size.height) }; - let buffer = (0..((width * height) as usize)) - .map(|index| { - let y = index / (width as usize); - let x = index % (width as usize); - let red = x % 255; - let green = y % 255; - let blue = (x * y) % 255; - let color = blue | (green << 8) | (red << 16); + surface.resize(width, height); - color as u32 - }) - .collect::>(); + let buffer = surface.buffer_mut(); + for index in 0..(width * height) { + let y = index as u32 / width; + let x = index as u32 % width; + let red = x % 255; + let green = y % 255; + let blue = (x * y) % 255; - surface.set_buffer(&buffer, width as u16, height as u16); + buffer[index as usize] = blue | (green << 8) | (red << 16); + } + + surface.present(); } Event::WindowEvent { event: WindowEvent::CloseRequested, diff --git a/src/lib.rs b/src/lib.rs index 384aee4f..ba1c04c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,33 @@ macro_rules! make_dispatch { } impl SurfaceDispatch { + pub fn resize(&mut self, width: u32, height: u32) { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.resize(width, height), + )* + } + } + + pub fn buffer_mut(&mut self) -> &mut [u32] { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.buffer_mut(), + )* + } + } + + pub fn present(&mut self) { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.present(), + )* + } + } + unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) { match self { $( @@ -235,6 +262,18 @@ impl Surface { }) } + pub fn resize(&mut self, width: u32, height: u32) { + self.surface_impl.resize(width, height); + } + + pub fn buffer_mut(&mut self) -> &mut [u32] { + self.surface_impl.buffer_mut() + } + + pub fn present(&mut self) { + self.surface_impl.present(); + } + /// Shows the given buffer with the given width and height on the window corresponding to this /// graphics context. Panics if buffer.len() ≠ width*height. If the size of the buffer does /// not match the size of the window, the buffer is drawn in the upper-left corner of the window. diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index bbe92311..28462032 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -87,12 +87,12 @@ impl WaylandImpl { )); } - fn resize(&mut self, width: u32, height: u32) { + pub fn resize(&mut self, width: u32, height: u32) { self.width = width as i32; self.height = height as i32; } - fn buffer_mut(&mut self) -> &mut [u32] { + pub fn buffer_mut(&mut self) -> &mut [u32] { if let Some((_front, back)) = &mut self.buffers { // Block if back buffer not released yet if !back.released() { @@ -111,7 +111,7 @@ impl WaylandImpl { unsafe { self.buffers.as_mut().unwrap().1.mapped_mut() } } - fn present(&mut self) { + pub fn present(&mut self) { let _ = self .display .event_queue