not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.37k stars 324 forks source link

Third Party WASM Library Support #212

Open Pebaz opened 3 years ago

Pebaz commented 3 years ago

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

not-fl3 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

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 commented 2 years ago

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

nicolas-sabbatini commented 9 months ago

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

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