Signed-off-by: John Nunley <dev@notgull.net> Co-Authored-By: dAxpeDDa <daxpedda@gmail.com>
67 lines
2.3 KiB
Rust
67 lines
2.3 KiB
Rust
use std::num::NonZeroU32;
|
|
use std::rc::Rc;
|
|
use winit::event::{Event, WindowEvent};
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
use winit::window::WindowBuilder;
|
|
|
|
fn main() {
|
|
let event_loop = EventLoop::new().unwrap();
|
|
let window = Rc::new(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())
|
|
.unwrap();
|
|
}
|
|
|
|
let context = softbuffer::Context::new(window.clone()).unwrap();
|
|
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
|
|
|
|
event_loop
|
|
.run(move |event, elwt| {
|
|
elwt.set_control_flow(ControlFlow::Wait);
|
|
|
|
match event {
|
|
Event::WindowEvent {
|
|
window_id,
|
|
event: WindowEvent::RedrawRequested,
|
|
} if window_id == window.id() => {
|
|
if let (Some(width), Some(height)) = {
|
|
let size = window.inner_size();
|
|
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
|
|
} {
|
|
surface.resize(width, height).unwrap();
|
|
|
|
let mut buffer = surface.buffer_mut().unwrap();
|
|
for y in 0..height.get() {
|
|
for x in 0..width.get() {
|
|
let red = x % 255;
|
|
let green = y % 255;
|
|
let blue = (x * y) % 255;
|
|
let index = y as usize * width.get() as usize + x as usize;
|
|
buffer[index] = blue | (green << 8) | (red << 16);
|
|
}
|
|
}
|
|
|
|
buffer.present().unwrap();
|
|
}
|
|
}
|
|
Event::WindowEvent {
|
|
event: WindowEvent::CloseRequested,
|
|
window_id,
|
|
} if window_id == window.id() => {
|
|
elwt.exit();
|
|
}
|
|
_ => {}
|
|
}
|
|
})
|
|
.unwrap();
|
|
}
|