|
| 1 | +use image::GenericImageView; |
| 2 | +use softbuffer::GraphicsContext; |
| 3 | +use winit::event::{Event, WindowEvent}; |
| 4 | +use winit::event_loop::{ControlFlow, EventLoop}; |
| 5 | +use winit::window::WindowBuilder; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + //see fruit.jpg.license for the license of fruit.jpg |
| 9 | + let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap(); |
| 10 | + let buffer = fruit.pixels().map(|(_x, _y, pixel)|{ |
| 11 | + let red = pixel.0[0] as u32; |
| 12 | + let green = pixel.0[1] as u32; |
| 13 | + let blue = pixel.0[2] as u32; |
| 14 | + |
| 15 | + blue | (green << 8) | (red << 16) |
| 16 | + }).collect::<Vec<_>>(); |
| 17 | + |
| 18 | + let event_loop = EventLoop::new(); |
| 19 | + let window = WindowBuilder::new().build(&event_loop).unwrap(); |
| 20 | + let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap(); |
| 21 | + |
| 22 | + event_loop.run(move |event, _, control_flow| { |
| 23 | + *control_flow = ControlFlow::Wait; |
| 24 | + |
| 25 | + match event { |
| 26 | + Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => { |
| 27 | + graphics_context.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16); |
| 28 | + } |
| 29 | + Event::WindowEvent { |
| 30 | + event: WindowEvent::CloseRequested, |
| 31 | + window_id, |
| 32 | + } if window_id == graphics_context.window().id() => { |
| 33 | + *control_flow = ControlFlow::Exit; |
| 34 | + } |
| 35 | + _ => {} |
| 36 | + } |
| 37 | + }); |
| 38 | +} |
0 commit comments