breaking: Use raw-window-handle version 0.6

Signed-off-by: John Nunley <dev@notgull.net>
Co-Authored-By: dAxpeDDa <daxpedda@gmail.com>
This commit is contained in:
John Nunley 2023-10-26 19:15:51 -07:00 committed by GitHub
parent 18c944736e
commit 0bcd2e22a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 830 additions and 628 deletions

View file

@ -3,13 +3,14 @@ use instant::Instant;
use rayon::prelude::*;
use std::f64::consts::PI;
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();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
#[cfg(target_arch = "wasm32")]
{
@ -21,57 +22,58 @@ fn main() {
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas())
.append_child(&window.canvas().unwrap())
.unwrap();
}
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);
let start = Instant::now();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
event_loop
.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Poll);
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let elapsed = start.elapsed().as_secs_f64() % 1.0;
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
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))
} {
let elapsed = start.elapsed().as_secs_f64() % 1.0;
if (width, height) != old_size {
old_size = (width, height);
frames = pre_render_frames(width as usize, height as usize);
};
if (width.get(), height.get()) != old_size {
old_size = (width.get(), height.get());
frames = pre_render_frames(width.get() as usize, height.get() as usize);
};
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
}
Event::AboutToWait => {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
Event::MainEventsCleared => {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
})
.unwrap();
}
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {