The 2 parts that I am splitting my game into are what I call the "engine" and the main "module". The potential third one is some helper code that is shared between the two to make development easier.
The engine contains the game loop, loads everything that needs to be loaded, and other stuff you expect from an engine.
The module part is a .wasm file that adds actual content to the game. So as an example: the engine will define a basic entity struct and its the job of the module to extend it to create a "merfolk" struct.
The module being a wasm file greatly limits how the two parts are able to communicate, and right now I plan to use the basic scheme provided in
this tutorial.
Simply put, in order for the engine to execute a function in the module that requires the passing of "complicated" data the following steps will take place:
- Zero out the first 4 bytes of the memory from the module
- convert the data into a shared binary format
- get the length of this binary data and write it down somewhere in the memory of the module
- call the function, passing the location of this data together with the length
- The module is able to convert it back and does it magic
- Once the module is done, it converts the new data to the binary format
- write the binary data somewhere in memory
- get the length and store it in the first 4 bytes
- return the position of the new data
- The engine, reads the first 4 bytes to get the length
- It then uses the returned position and the length to read the newly returned data
- Convert the data back into something it can use
As you can see, that are quite a few steps and also not exactly the nicest ones. This is where the glue code comes into play. It contains a serialize and deserialize function and a macro for the module side that automatically converts normal functions into ones that use this scheme.
The main problem that I am actually running into however is that the wasm library that I use doesn't itself compile to WASM. As a result, I need to write some javascript that will do the same thing.
And yes, this means that getting WASM to work inside the browser turns out to be more work then to get it to work on native builds.