Closed gen2brain closed 6 years ago
Now there is a different problem, _emscripten_set_main_loop issue is solved with this https://github.com/gen2brain/raylib-go/blob/master/raylib/external/scripts/emcc-generate-js.sh#L15 .
Function that is set with SetMainLoop(somefunc, 0, true) is running in loop as it should, but doesn't render anything on screen, first frame is visible and then just gray background.
I have a clue now what is wrong. The only working functions in raylib_js.go for now are the one that doesn't return or accept structs, like InitWindow() for example, the others should be called like this, via ccall http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-javascript-from-native and http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#ccall.
Something like this in GopherJS:
// LoadWaveEx is used, not _LoadWaveEx name that is exported
js.Global.Get("Module").Call("ccall", "LoadWaveEx", "array",
[]string{"array", "number", "number", "number", "number"},
[]interface{}{data, sampleCount, sampleRate, sampleSize, channels})
But then, there is again problem for all functions that work with structs passed by value. Structs can be converted to []byte
with unsafe
or encodings/binary
, so GopherJS pass it as TypedArray
, argument type from emscripten side is then array
, but return type can not be array
, only number
or string
, emscripten complains then, because it can not know size.
I will look more what can be done with http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/WebIDL-Binder.html#webidl-binder and http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#embind , results can be complete bindings to raylib in javascript, example is box2d here https://github.com/kripken/box2d.js , that can than be easily used from GopherJS .
Edit: embind example here https://github.com/kripken/emscripten/issues/3083#issuecomment-67284586
Closing for now, don't have time for this, I tried to use embind but it is not so easy.
Hi, I saw a video by someone using raylib-go and it got me wondering about the web export. That aspect seems to be abandoned for 5 years?
I recently did a game jam in Odin with Raylib. I used Caedo's template: https://github.com/Caedo/raylib_wasm_odin
It works by having a C/Emscripten raylib game, which calls out to external code (i.e. the actual game code) in the Odin wasm module. Perhaps something similar could be done for raylib-go?
@avelican Well no, it is not possible in Go to compile WASM obj (.o) like in your example. Odin relies on LLVM like many other newer languages while Go is using pure Go. What is possible in theory is to compile raylib with emscripten, not for the web, but as a standalone WASI, then use wazero
(the web assembly runtime that is completely cgo-free) to call raylib functions (wazero also has an extension for emscripten).
I may try just to see how it works (and if it works) when I find the time, with some basic example, but it would require new bindings, and that would be a third binding beside cgo and purego bindings, so it would be hard to maintain.
Support for web platform is partially implemented. There are still problems with _emscripten_set_main_loop().
go generate -tags js github.com/gen2brain/raylib-go/raylib
will generateraylib.js
file in github.com/gen2brain/raylib-go/raylib (emcc
from emscripten must be in PATH).GopherJS bindings to
raylib.js
are here https://github.com/gen2brain/raylib-go/blob/master/raylib/raylib_js.go .gopherjs build
is then used to build project example or game to js. After that, https://github.com/gen2brain/raylib-go/blob/master/raylib/external/web/index.html should be edited to include that js file.If I open that index.html in browser, I can see that InitWindow() works, OpenGL ES is active, etc. but per emscripten docs https://kripken.github.io/emscripten-site/docs/api_reference/emscripten.h.html#c.emscripten_set_main_loop, I need to set update()/mainloop() function there and use that in app, raylib also does that, but in C code (and includes emscripten.h).
https://github.com/gen2brain/raylib-go/blob/master/raylib/external/web/mainloop.js is added at the end of raylib.js (with --post-js option), and then SetMainLoop() https://github.com/gen2brain/raylib-go/blob/master/raylib/platform_web.go#L22 should be called in app.
Currently this gives error:
Edit: demo is here http://81.4.106.254/raylib-go/ , .js files are not optimized/compressed/obfuscated .