go-sdl2
go-sdl2 copied to clipboard
Can't build to wasm
When I try to build my Go app to wasm, I get many SDL failures:
$ GOOS=js GOARCH=wasm go build -o myapp.wasm .\sdl.go:9:20: undefined: sdl.Surface .\sdl.go:9:43: undefined: sdl.Renderer .\sdl.go:22:46: undefined: sdl.MapRGB .\sdl.go:34:37: undefined: sdl.Rect .\sdl.go:39:12: undefined: sdl.Init .\sdl.go:39:21: undefined: sdl.INIT_EVERYTHING .\sdl.go:42:8: undefined: sdl.Quit .\sdl.go:44:27: undefined: sdl.CreateWindowAndRenderer .\sdl.go:52:18: undefined: sdl.CreateRGBSurface .\sdl.go:64:16: undefined: sdl.PollEvent .\sdl.go:64:16: too many errors
Is this expected to work? I've built many C/C++ SDL games to WebAssembly - but new to Go
Hi @jamesfmackenzie, unfortunately it isn't supported yet. From what I read, it seems like the Go WebAssembly back-end doesn't handle linking with C library. It's certainly something that I will keep an eye on though as I'm creating a static site for it where WebAssembly would be useful to show examples!
Thanks! Very interested to hear more as this develops
Hi @jamesfmackenzie and @veeableful ! I think there was an update to the go webassembly back-end since I now get a very different error. Im a beginner in golang so not sure how to fix this but maybe a starting point for the experts here.
➜ microworlds git:(master) GOOS=js GOARCH=wasm go build -o myapp.wasm
go build github.com/veandco/go-sdl2/gfx: build constraints exclude all Go files in /home/frank/go/pkg/mod/github.com/veandco/[email protected]/gfx
The full source code for the project you can find here. Its an experiment with microworld simulations from me and uses sdl2 and sdl2-gfx. I will probably update it in the future, this try was based on commit f945471
Am happy to help with further tries or information if needed but am now kinda stuck, would be very cool if this worked!
Hi @fboerman, we don't yet support WASM output as Go itself doesn't support cgo with WASM. I think Go might not ever support it, or at least not anytime soon 😢.
If you need to create the web version, I believe it would probably be best to write it in JavaScript. From the look of the project's source code, I think the SDL2 usage is minimal enough that it can be translated to use HTML5 Canvas or maybe even SVG relatively easily. With pure JS, the code size would be really small, perhaps several KBs compared to Go's WASM output (min. 1.2MB for a hello world).
Would it be possible to make this work by using https://github.com/tetratelabs/wazero?
Hi @steeleduncan, thanks for sharing! I will try it out once I find out how to compile SDL2 to WebAssembly.