Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: enable events loop #459

Merged
merged 14 commits into from Jan 4, 2021
Merged

wasm: enable events loop #459

merged 14 commits into from Jan 4, 2021

Conversation

iceiix
Copy link
Owner

@iceiix iceiix commented Jan 1, 2021

Web has a separate render loop and window event loop, will need to handle both

#446 🕸️ Web support

@iceiix iceiix mentioned this pull request Jan 1, 2021
29 tasks
@iceiix
Copy link
Owner Author

iceiix commented Jan 1, 2021

Enabling both fails due to use of moved values, will need to see how to share this state:

error[E0382]: use of moved value: `winit_window`
   --> src/main.rs:417:21
    |
263 |     let (context, shader_version, winit_window, render_loop) = {
    |                                   ------------ move occurs because `winit_window` has type `winit::window::Window`, which does not implement the `Copy` trait
...
403 |     render_loop.run(move |running: &mut bool| {
    |                     ------------------------- value moved into closure here
404 |         tick_all(
405 |             &winit_window,
    |              ------------ variable moved due to use in closure
...
417 |     events_loop.run(move |event, _event_loop, control_flow| {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value used here after move
...
438 |         if !handle_window_event(&winit_window, &mut game, &mut ui_container, event) {
    |                                  ------------ use occurs due to use in closure

error[E0382]: use of moved value: `game`
   --> src/main.rs:417:21
    |
363 |     let mut game = Game {
    |         -------- move occurs because `game` has type `Game`, which does not implement the `Copy` trait
...
403 |     render_loop.run(move |running: &mut bool| {
    |                     ------------------------- value moved into closure here
...
406 |             &mut game,
    |                  ---- variable moved due to use in closure
...
417 |     events_loop.run(move |event, _event_loop, control_flow| {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value used here after move
...
438 |         if !handle_window_event(&winit_window, &mut game, &mut ui_container, event) {
    |                                                     ---- use occurs due to use in closure

error[E0382]: use of moved value: `ui_container`
   --> src/main.rs:417:21
    |
337 |     let mut ui_container = ui::Container::new();
    |         ---------------- move occurs because `ui_container` has type `Container`, which does not implement the `Copy` trait
...
403 |     render_loop.run(move |running: &mut bool| {
    |                     ------------------------- value moved into closure here
...
407 |             &mut ui_container,
    |                  ------------ variable moved due to use in closure
...
417 |     events_loop.run(move |event, _event_loop, control_flow| {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value used here after move
...
438 |         if !handle_window_event(&winit_window, &mut game, &mut ui_container, event) {
    |                                                                ------------ use occurs due to use in closure
  • winit_window
  • game
  • ui_container

@iceiix
Copy link
Owner Author

iceiix commented Jan 2, 2021

Errors with this branch, first comes from winit:

Error importing `index.js`: Error: Using exceptions for control flow, don't mind me. This isn't actually an error!
    __wbindgen_throw webpack:///../pkg/stevenarella_bg.js?:1695
    __wbindgen_throw http://localhost:8080/bootstrap.js:755
    <anonymous> webpack:///./index.js?:5
    js http://localhost:8080/0.bootstrap.js:34
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    promise callback* webpack:///./bootstrap.js?:4
    js http://localhost:8080/bootstrap.js:989
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> webpack:///multi_(webpack)-dev-server/client?:2
    0 http://localhost:8080/bootstrap.js:1376
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> http://localhost:8080/bootstrap.js:977
    <anonymous> http://localhost:8080/bootstrap.js:980
bootstrap.js:5:23
    <anonymous> webpack:///./bootstrap.js?:5
    (Async: promise callback)
    <anonymous> webpack:///./bootstrap.js?:5
    js http://localhost:8080/bootstrap.js:989
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> webpack:///multi_(webpack)-dev-server/client?:2
    0 http://localhost:8080/bootstrap.js:1376
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> http://localhost:8080/bootstrap.js:977
    <anonymous> http://localhost:8080/bootstrap.js:980

https://github.com/rust-windowing/winit/blob/master/src/platform_impl/web/event_loop/mod.rs#L42-L46

        // Throw an exception to break out of Rust exceution and use unreachable to tell the
        // compiler this function won't return, giving it a return type of '!'
        backend::throw(
            "Using exceptions for control flow, don't mind me. This isn't actually an error!",
        );

if not actually an error, why is it logged in the browser console?

The screen renders, but if we click on it, then a panic:

[main.rs:437][INFO] events_loop start
panicked at 'called `Result::unwrap()` on an `Err` value: NotSupported(NotSupportedError)', src/main.rs:657:59

Stack:

__wbg_new_59cb74e423758ede<@webpack:///../pkg/stevenarella_bg.js?:616:15
logError/<@webpack:///../pkg/stevenarella_bg.js?:480:22
__wbg_new_59cb74e423758ede@http://localhost:8080/bootstrap.js:74:77
console_error_panic_hook::Error::new::h749adbfb5a41c4db@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[56268]:0x51c5bb
console_error_panic_hook::hook_impl::h4e610f50bd474f05@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[4675]:0x32126c
console_error_panic_hook::hook::h1342a448bddad9ae@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[56267]:0x51c5b5
core::ops::function::Fn::call::h0983a3994a81de28@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[55821]:0x51b6da
std::panicking::rust_panic_with_hook::h123718ba3bf480af@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[3284]:0x2daf22
std::panicking::begin_panic_handler::{{closure}}::hf393a82e58397bd2@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[5724]:0x34b177
std::sys_common::backtrace::__rust_end_short_backtrace::h2ff2cfc953878925@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[23624]:0x484d86
rust_begin_unwind@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[15706]:0x41d0dd
core::panicking::panic_fmt::h285dc015a87be5a4@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[23626]:0x484deb
core::option::expect_none_failed::hb95d2b1dc5448c17@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[6492]:0x364fda
core::result::Result<T,E>::unwrap::h679f17a6def43069@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[7968]:0x38d487
stevenarella::handle_window_event::h3f4fa933e11518d7@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[465]:0x13e808
stevenarella::main2::{{closure}}::h3bc87607d9e52d26@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[2289]:0x296372
winit::platform_impl::platform::event_loop::EventLoop<T>::run::{{closure}}::ha5512701e6e357bb@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[15733]:0x41d767
<alloc::boxed::Box<F,A> as core::ops::function::FnMut<Args>>::call_mut::h97dd8bb46ad2c2ea@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[13069]:0x3f3542
winit::platform_impl::platform::event_loop::runner::Runner<T>::handle_single_event::h45377ebc1f334d40@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[11462]:0x3d7d4e
winit::platform_impl::platform::event_loop::runner::Shared<T>::handle_event::h4fbde8547cfd50f3@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[1661]:0x25a5a3
winit::platform_impl::platform::event_loop::runner::Shared<T>::run_until_cleared::hf9f22998412e5a48@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[1229]:0x221802
winit::platform_impl::platform::event_loop::runner::Shared<T>::send_events::h5de814012e065709@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[982]:0x1f563b
winit::platform_impl::platform::event_loop::runner::Shared<T>::send_event::heec0a79aa3401608@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[16902]:0x42e7d6
winit::platform_impl::platform::event_loop::window_target::WindowTarget<T>::register::{{closure}}::h303b364ab7072391@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[7899]:0x38b9ce
winit::platform_impl::platform::backend::canvas::pointer_handler::PointerHandler::on_mouse_release::{{closure}}::haf62f63d0cac01a8@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[9233]:0x3aafa7
winit::platform_impl::platform::backend::canvas::Common::add_user_event::{{closure}}::hb24614961c048429@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[4724]:0x323568
winit::platform_impl::platform::backend::canvas::Common::add_event::{{closure}}::h4144a16a994eee89@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[15752]:0x41dbc5
<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hcb9e4ad21dd6fbd4@http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:wasm-function[26842]:0x4a6a03

src/main.rs:657 is window.set_cursor_grab(false).unwrap();, this isn't supported by winit on the web? Would have expected to translate to Pointer Lock API, but only when called from a click.

next error doesn't say much:

Uncaught Error: undefined b2ccb57aa171c84458ca.module.wasm:5428780:1

__rust_start_panic http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:5428780
    rust_panic http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4834776
    h123718ba3bf480af http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2993995
    hf393a82e58397bd2 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3453303
    h2ff2cfc953878925 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4738438
    rust_begin_unwind http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4313309
    h285dc015a87be5a4 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4738539
    hb95d2b1dc5448c17 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3559386
    h679f17a6def43069 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3724423
    h3f4fa933e11518d7 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:1304584
    h3bc87607d9e52d26 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2712434
    ha5512701e6e357bb http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4314983
    <alloc::boxed::Box<F,A> as core::ops::function::FnMut<Args>>::call_mut::h97dd8bb46ad2c2ea http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4142402
    h45377ebc1f334d40 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4029774
    h4fbde8547cfd50f3 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2467235
    hf9f22998412e5a48 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2234370
    h5de814012e065709 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2053691
    heec0a79aa3401608 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4384726
    h303b364ab7072391 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3717582
    haf62f63d0cac01a8 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3846055
    hb24614961c048429 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3290472
    h4144a16a994eee89 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4316101
    <dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hcb9e4ad21dd6fbd4 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4876803
    __wbg_adapter_39 webpack:///../pkg/stevenarella_bg.js?:528
    real webpack:///../pkg/stevenarella_bg.js?:462
    (Async: EventListener.handleEvent)
    __wbg_addEventListener_6a37bc32387cb66d webpack:///../pkg/stevenarella_bg.js?:1108
    handleError webpack:///../pkg/stevenarella_bg.js?:558
    __wbg_addEventListener_6a37bc32387cb66d http://localhost:8080/bootstrap.js:386
    h9c616ab62b0d36fb http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3547500
    h53b3d276cdb8e909 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3576940
    hcf6e0ce669b7387a http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3622698
    hbce29512fe5a0cde http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4010272
    h17982d7f71456822 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:3777362
    hc7927d2c27bb5c8d http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4651373
    hbfabf39b9bdeb249 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2326509
    h7c01d24f30602758 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:2110453
    ha7dd479bc7d628a8 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:4049138
    heddadd9feacdeaa9 http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:1003636
    ha395fada26a8133c http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:5406833
    main http://localhost:8080/b2ccb57aa171c84458ca.module.wasm:5406839
    <anonymous> webpack:///./index.js?:5
    js http://localhost:8080/0.bootstrap.js:34
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    __webpack_require__ self-hosted:1208
    (Async: promise callback)
    <anonymous> webpack:///./bootstrap.js?:4
    js http://localhost:8080/bootstrap.js:989
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> webpack:///multi_(webpack)-dev-server/client?:2
    0 http://localhost:8080/bootstrap.js:1376
    __webpack_require__ http://localhost:8080/bootstrap.js:807
    <anonymous> http://localhost:8080/bootstrap.js:977
    <anonymous> http://localhost:8080/bootstrap.js:980

then this last error, most serious:

[main.rs:414][INFO] render_loop start
panicked at 'already borrowed: BorrowMutError', src/main.rs:415:45

when borrowing:

        render_loop.run(move |running: &mut bool| {
            info!("render_loop start");
            let winit_window = winit_window.borrow_mut();

meaning the events_loop started (then panicked-NotSupportedError), and then render_loop started and borrowed at the same time. Wasn't expecting both loops to be executing simultaneously — maybe they aren't but this simultaneous borrow_mut() is a consequence of events_loop panicking first?

@iceiix
Copy link
Owner Author

iceiix commented Jan 2, 2021

window.set_cursor_grab(false).unwrap() src/main.rs:657

winit has/had support for Pointer Lock API through emscripten: rust-windowing/winit@769d4fe

but it does not yet through web-sys, there is an open issue: rust-windowing/winit#1674

Pointer Lock is definitely needed. But on this line of code, it is actually trying to disable pointer grab. Commenting it out for testing, mouse events do work — including hover and click:

Screen Shot 2021-01-02 at 10 32 51 AM

error[E0716]: temporary value dropped while borrowed
   --> src/main.rs:332:24
    |
332 |     let winit_window = Box::leak(Box::new(glutin_window)).borrow().window();
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------- temporary value is freed at the end of this statement
    |                        |
    |                        creates a temporary which is freed while still in use
    |                        argument requires that borrow lasts for `'static`

error[E0382]: use of moved value: `glutin_window`
   --> src/main.rs:437:21
    |
330 |     let glutin_window = RefCell::new(Box::leak(Box::new(glutin_window)));
    |         ------------- move occurs because `glutin_window` has type `RefCell<&mut ContextWrapper<PossiblyCurrent, winit::window::Window>>`, which does not implement the `Copy` trait
331 |     #[cfg(not(target_arch = "wasm32"))]
332 |     let winit_window = Box::leak(Box::new(glutin_window)).borrow().window();
    |                                           ------------- value moved here
...
437 |     events_loop.run(move |event, _event_loop, control_flow| {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value used here after move
...
458 |             glutin_window.borrow().resize(physical_size);
    |             ------------- use occurs due to use in closure

error: aborting due to 2 previous errors
@iceiix
Copy link
Owner Author

iceiix commented Jan 3, 2021

This works on wasm but breaks native, because of borrowing glutin_window:

error[E0505]: cannot move out of `glutin_window` because it is borrowed
   --> src/main.rs:435:21
    |
330 |     let winit_window = glutin_window.window();
    |                        ----------------------
    |                        |
    |                        borrow of `*glutin_window` occurs here
    |                        argument requires that `*glutin_window` is borrowed for `'static`
...
435 |     events_loop.run(move |event, _event_loop, control_flow| {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move out of `glutin_window` occurs here
...
456 |             glutin_window.resize(physical_size);
    |             ------------- move occurs due to use in closure

@iceiix iceiix merged commit 8208e3b into master Jan 4, 2021
@iceiix iceiix deleted the events_loop branch January 4, 2021 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant