veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.18k stars 219 forks source link

Can't build to wasm #438

Open jamesfmackenzie opened 4 years ago

jamesfmackenzie commented 4 years ago

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

veeableful commented 4 years ago

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!

jamesfmackenzie commented 4 years ago

Thanks! Very interested to hear more as this develops

fboerman commented 3 years ago

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/go-sdl2@v0.4.7/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!

veeableful commented 3 years ago

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

steeleduncan commented 2 years ago

Would it be possible to make this work by using https://github.com/tetratelabs/wazero?

veeableful commented 2 years ago

Hi @steeleduncan, thanks for sharing! I will try it out once I find out how to compile SDL2 to WebAssembly.