2022-10-01 23:58:09 +03:00
|
|
|
use super::state::State;
|
|
|
|
use winit::{
|
|
|
|
event::*,
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub async fn run() {
|
|
|
|
let event_loop = EventLoop::new();
|
2023-01-24 16:54:26 +02:00
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_fullscreen(Some(winit::window::Fullscreen::Borderless(None)))
|
|
|
|
.with_maximized(true)
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
2022-10-01 23:58:09 +03:00
|
|
|
|
2023-01-27 23:16:57 +02:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
|
|
|
// Winit prevents sizing with CSS, so we have to set
|
|
|
|
// the size manually when on web.
|
2023-01-28 14:03:40 +02:00
|
|
|
// https://github.com/rust-windowing/winit/pull/2074
|
2023-01-27 23:16:57 +02:00
|
|
|
use winit::dpi::PhysicalSize;
|
|
|
|
window.set_inner_size(PhysicalSize::new(1920, 1080));
|
|
|
|
|
|
|
|
use winit::platform::web::WindowExtWebSys;
|
|
|
|
web_sys::window()
|
|
|
|
.and_then(|win| win.document())
|
|
|
|
.and_then(|doc| doc.body())
|
|
|
|
.and_then(|body| {
|
|
|
|
let canvas = web_sys::Element::from(window.canvas());
|
|
|
|
body.append_child(&canvas).ok()
|
|
|
|
})
|
|
|
|
.expect("Couldn't append canvas to document body.");
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:23:43 +02:00
|
|
|
let mut state = State::new(&window).await;
|
|
|
|
let mut last_render = instant::Instant::now();
|
|
|
|
let mut is_focused = true;
|
2023-01-24 16:54:26 +02:00
|
|
|
|
2022-10-01 23:58:09 +03:00
|
|
|
// Event loop
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
match event {
|
2022-10-02 18:59:20 +03:00
|
|
|
Event::DeviceEvent { ref event, .. } => {
|
|
|
|
state.input(None, Some(event));
|
|
|
|
}
|
2022-10-01 23:58:09 +03:00
|
|
|
Event::WindowEvent {
|
|
|
|
ref event,
|
|
|
|
window_id,
|
|
|
|
} if window_id == window.id() => {
|
2022-10-02 18:59:20 +03:00
|
|
|
if !state.input(Some(event), None) {
|
2022-10-01 23:58:09 +03:00
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested
|
|
|
|
| WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
KeyboardInput {
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
2023-01-28 12:23:43 +02:00
|
|
|
} => {
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
{
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 23:58:09 +03:00
|
|
|
WindowEvent::Resized(physical_size) => {
|
|
|
|
state.resize(*physical_size);
|
|
|
|
}
|
|
|
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
state.resize(**new_inner_size);
|
|
|
|
}
|
2023-01-23 00:24:05 +02:00
|
|
|
WindowEvent::Focused(focused) => {
|
2023-01-28 12:23:43 +02:00
|
|
|
lock_cursor(&window, *focused);
|
|
|
|
is_focused = *focused;
|
2023-01-23 00:24:05 +02:00
|
|
|
}
|
2022-10-01 23:58:09 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
2023-01-28 12:23:43 +02:00
|
|
|
let now = instant::Instant::now();
|
2022-10-02 18:59:20 +03:00
|
|
|
let dt = now - last_render;
|
|
|
|
last_render = now;
|
2023-01-28 12:23:43 +02:00
|
|
|
if is_focused {
|
|
|
|
state.update(dt);
|
|
|
|
match state.render() {
|
|
|
|
Ok(_) => {}
|
|
|
|
// Reconfigure the surface if lost
|
|
|
|
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
|
|
|
// The system is out of memory, we should probably quit
|
|
|
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
|
|
|
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
|
|
|
Err(e) => eprintln!("{:?}", e),
|
|
|
|
}
|
2022-10-01 23:58:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
// RedrawRequested will only trigger once, unless we manually
|
|
|
|
// request it.
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-01-28 12:23:43 +02:00
|
|
|
|
|
|
|
fn lock_cursor(window: &winit::window::Window, lock: bool) {
|
|
|
|
if lock {
|
|
|
|
window
|
|
|
|
.set_cursor_grab(if cfg!(target_arch = "wasm32") {
|
|
|
|
winit::window::CursorGrabMode::Locked
|
|
|
|
} else {
|
|
|
|
winit::window::CursorGrabMode::Confined
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
window.set_cursor_visible(false);
|
|
|
|
} else {
|
|
|
|
window
|
|
|
|
.set_cursor_grab(winit::window::CursorGrabMode::None)
|
|
|
|
.unwrap();
|
|
|
|
window.set_cursor_visible(true);
|
|
|
|
}
|
|
|
|
}
|