diff --git a/examples/winit_wrong_sized_buffer.rs b/examples/winit_wrong_sized_buffer.rs new file mode 100644 index 0000000..38aadce --- /dev/null +++ b/examples/winit_wrong_sized_buffer.rs @@ -0,0 +1,42 @@ +use winit::event::{Event, WindowEvent}; +use winit::event_loop::{ControlFlow, EventLoop}; +use winit::window::WindowBuilder; +use softbuffer::GraphicsContext; + +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(); + let mut graphics_context = unsafe { GraphicsContext::new(window) }; + + event_loop.run(move |event, _, control_flow| { + *control_flow = ControlFlow::Wait; + + match event { + Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => { + let buffer = (0..((BUFFER_WIDTH*BUFFER_HEIGHT) as usize)).map(|index|{ + let y = index / (BUFFER_WIDTH as usize); + let x = index % (BUFFER_WIDTH as usize); + let red = x % 255; + let green = y % 255; + let blue = (x*y) % 255; + + let color = blue | (green << 8) | (red << 16); + + color as u32 + }).collect::>(); + + graphics_context.set_buffer(&buffer, BUFFER_WIDTH as u16, BUFFER_HEIGHT as u16); + } + Event::WindowEvent { + event: WindowEvent::CloseRequested, + window_id + } if window_id == graphics_context.window().id() => { + *control_flow = ControlFlow::Exit; + }, + _ => {} + } + }); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 37451a7..8f4b279 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,10 @@ impl GraphicsContext { #[inline] pub fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16){ + if (width as usize)*(height as usize) != buffer.len(){ + panic!("The size of the passed buffer is not the correct size. Its length must be exactly width*height."); + } + unsafe { self.graphics_context_impl.set_buffer(buffer, width, height); }