easyGW/
lib.rs

1use winit::{
2    event::{Event, WindowEvent},
3    event_loop::{ControlFlow, EventLoop},
4    window::WindowBuilder,
5};
6use std::num::NonZeroU32;
7use std::rc::Rc;
8
9pub fn new() {
10    let event_loop = EventLoop::new().unwrap();
11    let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
12    let context = softbuffer::Context::new(window.clone()).unwrap();
13    let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
14
15    event_loop.run(move |event, elwt| {
16        elwt.set_control_flow(ControlFlow::Wait);
17
18        match event {
19            Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
20                let (width, height) = {
21                    let size = window.inner_size();
22                    (size.width, size.height)
23                };
24                surface
25                    .resize(
26                        NonZeroU32::new(width).unwrap(),
27                        NonZeroU32::new(height).unwrap(),
28                    )
29                    .unwrap();
30
31                let mut buffer = surface.buffer_mut().unwrap();
32                for index in 0..(width * height) {
33                    buffer[index as usize] = 0;
34                }
35
36                buffer.present().unwrap();
37            }
38            Event::WindowEvent {
39                event: WindowEvent::CloseRequested,
40                window_id,
41            } if window_id == window.id() => {
42                elwt.exit();
43            }
44            _ => {}
45        }
46    }).unwrap();
47}