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

Third Party WASM Library Support #212

Open
Pebaz opened this issue Apr 24, 2021 · 6 comments
Open

Third Party WASM Library Support #212

Pebaz opened this issue Apr 24, 2021 · 6 comments

Comments

@Pebaz
Copy link
Contributor

Pebaz commented Apr 24, 2021

I am not skilled with webassembly Rust builds but as noted here:

https://gitlab.com/gilrs-project/gilrs/-/issues/99

Macroquad has a custom WASM build process. The only downside to this I see is that all other WASM compatible libraries do not work with Macroquad.

It's possible that a custom or manual step is needed and I just don't know what it is, but perhaps integrating into a standard like wasm-bindgen or stdweb would enable other WASM compatible libraries to be seamlessly used with Macroquad.

If there is an easy manual step like adding a script tag to the HTML page and I just don't know how that is another matter but it'd be awesome to just build and run :)

@Pebaz Pebaz changed the title WASM Library Support Third Party WASM Library Support Apr 24, 2021
@not-fl3
Copy link
Owner

not-fl3 commented Apr 24, 2021

It is certainly possible, we do not use either wasm-bindgen nor stdweb, therefore it may be possible to load macroquad's wasm with those tools somehow.

But personally, I am not really interested in this - I like the pipeline without wasm-bindgen/stdweb and do not want to integrate either of them into any of my projects.

So I would not work on this in foreseen future, but people want it sometimes and if someone will do some guide on using macroquad will wasm-bindgen - I would appreciate it and will add a link to that guide to readme :)

@tgolsson
Copy link

tgolsson commented May 8, 2021

Approaching it from the other end, maybe there should be a guide for doing this using the standard method for macroquad. I.e., how would a user go about binding a new dependency without those tools?

@not-fl3
Copy link
Owner

not-fl3 commented May 8, 2021

Approaching it from the other end, maybe there should be a guide for doing this using the standard method for macroquad. I.e., how would a user go about binding a new dependency without those tools?

Sure, we actually have such a guide!

https://github.com/not-fl3/miniquad/wiki/JavaScript-interop

@tgolsson
Copy link

tgolsson commented May 8, 2021

I took a stab at getting wasm-bindgen to work for macroquad; and a working flow is as follows:

  • build everything through wasm-bindgen
  • patch the my-project.js from wasm-bindgen as follows:
    • remove all env references (__wbg_star0, etc)
    • add a setter for the wasm variable
    • add an early-out from init which returns the wbg part of the imports
  • register a plugin for macroquad which:
    • adds the wbg stubs from the wasmm-bindgen code to the objectImports in on_register
    • sets the wbg wasm object to wasm_exports in on_init

With these changes, the index.html below seems to work. Note that this needs to use the wasm file generated by wasm-bindgen, as it'll otherwise contain some unlinkable methods in undefined modules.

        <script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
        <script type="module">
            import init, { set_wasm } from "./wbindgen/my-project.js";

            async function run() {
                let wbg = await init();

                miniquad_add_plugin({
                    register_plugin: (a) => (a.wbg = wbg),
                    on_init: () => set_wasm(wasm_exports),
                    version: "0.0.1",
                    name: "wbg",
                });

                load("./wbindgen/my-project_bg.wasm");
            }

            run();
        </script>

I packaged this as a build-script here: https://gist.github.com/tgolsson/d78f7887a8542f3fd6f125070e5e22d6

@gsanta
Copy link

gsanta commented Jun 17, 2022

The script is not working for me. Could you explain what kind of patch is necessary to make it work with wasm-bindgen?

sanchopanca added a commit to sanchopanca/tic-tac-toe-rust that referenced this issue May 17, 2023
Otherwise wasm builds don't work

Ok, this is an interesting one. Apparently Macroquad has
a custom WASM build process[1], and the moment another WASM compatible
library is added the the project, it no longer works in browser.
rand crate, or rather its dependency getrandom, is one of those.

There is a way to make it potentially work together[2], but I decided
not going into this madness, I feel it's a huge time sink.

Using quad_rand is not without its problems, it still needs a source
of randomness to intialize the RNG; the workaround that I found is
using time from the launch of the app till the player chooses side.

Then, the shuffle method is Fisher-Yates algorithm that I got
from wikipedia[3]. Admittedly, it took more than one attempt to
weed out all the off-by-one errors that I made.

Overall I feel like this whole situation is a big turn-off for me
from using macroquad for anything that needs to run in a web-browser.
Being locked out from using many dependencies is quite a limitation.
As I mentioned above, it's potentially solvable, but this not something
you want to spend your time while developing a game.

[1] not-fl3/macroquad#212
[2] not-fl3/macroquad#212 (comment)
[3] https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
@nicolas-sabbatini
Copy link

the last version of the script to build macroquad whit wasm bindgen:

https://gist.github.com/nicolas-sabbatini/8af10dddc96be76d2bf24fc671131add

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

No branches or pull requests

5 participants