LoginSignup
2
2

More than 1 year has passed since last update.

ebitenのwasm表示ではまった

Last updated at Posted at 2021-09-24

 sakura.ne.jpでapplication/wasmを設定しても実行できなかったのでメモを残しておきます。ebitenでのwasmを扱うマニュアルは、 https://ebiten.org/documents/webassembly.html にあります。

まず、コンパイルはWindowsでは、&&を足して

set GOOS=js&& set GOARCH=wasm&& go build -o ../build/game.wasm

でコンパイル出来ます。

 sakura.ne.jpでサーバー設定が効かなかったので、PHPのスプリクトにて、

wasm.php
<?php
    header('Content-Type: application/wasm');
    echo(file_get_contents("game.wasm"));
    exit;

PHPのheaderでファイルタイプを送信してから、wasmファイルを読み込んでブラウザへ送信してます。

 wasmを表示するHTMLファイルの該当部分の

WebAssembly.instantiateStreaming(fetch("wasm.php"), go.importObject).then(result => {

ファイル名をwasm.phpへ変更して、ftpなどでファイルを転送。ブラウザで表示すると、wasmで実行できるはずです。

 画像ファイルなどもサーバーへ転送してないとエラーが出ます。(実は、そこで少し嵌まった… 私、wasm知らなすぎ…)

 以上の方法で、ebitenで作ったゲームのwasmファイルを実行出来ます。:grinning: 今、作っているゲームのウェブは、まだ見せれませんが。:neutral_face:

 あと、オマケで、いつも使っているBATファイルも置いておきます。

make.bat
@ECHO off

if "%1" == "compile" (
cd src
go build -o ../build/game.exe
cd ..
)

if "%1" == "wasm" (
cd src
set GOOS=js&& set GOARCH=wasm&& go build -o ../build/game.wasm
cd ..
)

if "%1" == "exec" (
cd build
game.exe
cd ..
)

if "%1" == "vet" (
cd src
go vet .
cd ..
)

if "%1" == "fmt" (
cd src
gofmt -s -w .
rem go vet .
cd ..
)

REM モジュールの整合性を整え直す
if "%1" == "tidy" (
cd src
go mod tidy
cd ..
)

echo end.
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2