Ikemen-GO icon indicating copy to clipboard operation
Ikemen-GO copied to clipboard

Porting to wasm/js/webgl using Gopherjs

Open samhocevar opened this issue 2 years ago • 5 comments

I would like to build a web version of an Ikemen GO game, and from my understanding of the codebase this will not be trivial and I’d like to discuss a few changes I am willing to contribute. Here is my proposed roadmap:

  1. identify external modules that are not Gopherjs-compatible
  2. when sensible, isolate code that uses these modules in specific files
  3. find acceptable replacements for the incompatible modules
  4. when not possible, just disable these modules

1. Modules that are not Gopherjs-compatible

  • github.com/faiface/beep ✔️ (pure Go implementation)
  • github.com/flopp/go-findfont ❌
  • github.com/go-gl/gl ❌ (alternative available, see below)
  • github.com/go-gl/glfw/v3.3/glfw ❌ (alternative available, see below)
  • github.com/ikemen-engine/glfont ❌
  • github.com/ikemen-engine/go-openal ❌
  • github.com/sqweek/dialog ❌
  • github.com/yuin/gopher-lua ✔️ (pure Go implementation)

2. Isolate incompatible code

Here is an example of how code could be isolated: this commit moves all the code from font.go that uses glfont and go-findfont into a separate font_ttf.go file. When built with go build -tags nottf, this file is ignored and another file, font_nottf.go, is built instead. Right now the implementation is empty and just panics, but there could be a smarter implementation or at least more forgiving error handling.

3. Propose acceptable replacements

OpenGL code is far too entangled in the codebase for a refactor like above. However, the following two libraries appear to work with GopherJS and may work as replacements, with minimal changes:

  • https://github.com/goxjs/glfw
  • https://github.com/goxjs/gl

OpenAL can probably be replaced with Oto, which is already pulled as a dependency by the Beep module :

  • https://github.com/hajimehoshi/oto

Note that @hajimehoshi is a game developer and has provided the Go community with great tools for porting Go games to Wasm (as well as other platforms thanks to some impressive work, see https://ebiten.org/blog/native_compiling_for_nintendo_switch.html)

4. Make the rest optional

It’ll probably be tricky to replace glfont in the short term, but I think it’s an acceptable loss. Also the dialog module has no replacement, but a Wasm program can directly call a JavaScript function such as alert() so I don’t see a real problem here either.

samhocevar avatar Apr 05 '22 19:04 samhocevar

I have some updates on this. The GopherJS port went pretty well, and I have now switched to WASM instead because GopherJS does not really support unsafe.Pointer. Here is a summary of the changes:

Changes to Ikemen GO

  • use https://github.com/fyne-io/gl-js instead of https://github.com/go-gl/gl
  • use https://github.com/fyne-io/glfw-js instead of https://github.com/go-gl/glfw/v3.3/glfw
  • disable writing to files (cfgPath, stats.json, Ikemen.log)
  • external/scrip/motif.lua: do not use Open_Sans.def (use arcade.def instead)
  • use https://github.com/jvilk/BrowserFS for filesystem access

Changes to Elecbyte Screenpack

  • data/system.def: do not use Open_Sans.def (use arcade.def instead)

Changes to gl-gs

Pull requests sent here: https://github.com/fyne-io/gl-js/pulls?q=is%3Apr+author%3Asamhocevar

Changes to glfw-js

All pull requests have been accepted by the fyne-io authors: https://github.com/fyne-io/glfw-js/pulls?q=is%3Apr+author%3Asamhocevar

Changes to BrowserFS

  • make sure atimeMs, mtimeMs, ctimeMs are properly initialised in Stats()

Changes to Go

  • in c:/Program Files/Go/src/syscall/fs_js.go, replace !jsErr.IsNull() with !jsErr.IsUndefined() && !jsErr.IsNull()
  • no pull request yet
  • the same fix was sent to the GopherJS project https://github.com/gopherjs/gopherjs/pull/1118

samhocevar avatar May 15 '22 16:05 samhocevar

It is really pretty excited to see this port !!!!

hallucino avatar May 24 '22 08:05 hallucino

Here’s how it looks like so far: http://sam.hocevar.net/projects/ikemen/

samhocevar avatar May 24 '22 08:05 samhocevar

Yes I found it !!! Pretty awesome :)

hallucino avatar May 24 '22 09:05 hallucino

Great work :) was thinking about this myself - we really need a web version of this engine.

hiddentao avatar Jun 18 '22 09:06 hiddentao