This commit is contained in:
Lauri Räsänen 2023-01-27 23:16:57 +02:00
parent a7ea7ee65b
commit dc3bd2a433
6 changed files with 119 additions and 8 deletions

View file

@ -17,9 +17,33 @@ pub async fn run() {
let mut state = State::new(&window).await;
let mut last_render = Instant::now();
window
#[cfg(target_arch = "wasm32")]
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init().expect("could not initialize logger");
// Winit prevents sizing with CSS, so we have to set
// the size manually when on web.
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.");
}
#[cfg(not(target_arch = "wasm"))]
{
window
.set_cursor_grab(winit::window::CursorGrabMode::Confined)
.unwrap();
}
window.set_cursor_visible(false);
// Event loop

22
src/lib.rs Normal file
View file

@ -0,0 +1,22 @@
#![allow(clippy::needless_return)]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod core;
mod shaders;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn run() {
#[cfg(target_arch = "wasm32")]
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(log::Level::Warn).expect("Couldn't initialize logger");
wasm_bindgen_futures::spawn_local(core::window::run());
}
#[cfg(not(target_arch = "wasm32"))]
{
env_logger::init();
pollster::block_on(core::window::run());
}
}

View file

@ -1,9 +1,5 @@
#![allow(clippy::needless_return)]
mod core;
mod shaders;
use wgpu_renderer::run;
fn main() {
env_logger::init();
pollster::block_on(core::window::run());
run();
}