macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Third Party WASM Library Support

Open Pebaz opened this issue 3 years ago • 6 comments

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 avatar Apr 24 '21 13:04 Pebaz

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 :)

not-fl3 avatar Apr 24 '21 14:04 not-fl3

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?

tgolsson avatar May 08 '21 00:05 tgolsson

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

not-fl3 avatar May 08 '21 00:05 not-fl3

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

tgolsson avatar May 08 '21 10:05 tgolsson

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

gsanta avatar Jun 17 '22 22:06 gsanta

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

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

nicolas-sabbatini avatar Feb 14 '24 22:02 nicolas-sabbatini