-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 loc) · 1.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"fmt"
"image/color"
"io"
"os"
"path/filepath"
"runtime"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
fetch "marwan.io/wasm-fetch"
)
type game struct {
ticks int
sampleJSON []byte
}
func (g *game) Update() error {
g.ticks++
return nil
}
func (g *game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0, 64, 64, 255})
s := fmt.Sprintf("Hello, wasmgame!\nTicks = %d\nThe content of asset/sample.json is: %s", g.ticks, string(g.sampleJSON))
x, y := g.ticks%640, g.ticks%360
ebitenutil.DebugPrintAt(screen, s, x, y)
}
func (g *game) Layout(w, h int) (int, int) {
return 640, 360 // Screen resolution (not window size)
}
func main() {
g := &game{}
g.sampleJSON, _ = readFile("asset/sample.json")
ebiten.SetWindowSize(1280, 720) // has no effect on browser
if err := ebiten.RunGame(g); err != nil {
panic(err)
}
}
// open opens a file. In a browser, it downloads the file via HTTP;
// otherwise, it reads the file on disk.
func open(name string) (io.ReadCloser, error) {
name = filepath.Clean(name)
if runtime.GOOS == "js" {
resp, err := fetch.Fetch(name, &fetch.Opts{})
if err != nil {
return nil, err
}
return io.NopCloser(bytes.NewReader(resp.Body)), nil
}
return os.Open(name)
}
func readFile(name string) ([]byte, error) {
f, err := open(name)
if err != nil {
return nil, fmt.Errorf("open %s: %w", name, err)
}
defer f.Close()
return io.ReadAll(f)
}