I’ve begun the rewrite of my already popular project. Fun fact it’s featured on the OFFICIAL raylib-go repo with 2.2k stars.
I implemented a low level javascript runtime glue thingy that allows Go to call raylib functions more efficiently. Here’s how:
In the previous version Go would have to first copy memory over to Javascript, then Javascript would copy the memory to Raylib. (Go -> JS -> Raylib)
In V2, we control the Go runtime itself.. This allows us to implement functions that directly interact with Go memory. And allow us to copy Go memory directly into Raylib’s memory (Go <-> Raylib)
Using this, I basically created my own easy to use foreign function interface but for webassembly.
Here is a code snippet
// SetWindowTitle - Set title for window (only PLATFORM_DESKTOP)
func SetWindowTitle(title string) {
ptr := cStringFromGoString(title)
defer free(ptr)
setWindowTitle(ptr)
}
cStringFromGoString and free() are functions implemented in JS, they literally just copy the Go string into raylib’s memory using malloc. Controlling the Go runtime allows us to mess directly with the Go stack. We are basically writing assembly glue but instead of assembly, it’s javascript.
I apologize if my explanation doesn’t make sense, I plan on making a full detailed blog post later.