-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
55 lines (48 loc) · 991 Bytes
/
display.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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct {
Width int
Height int
Title string
World World
Pixels []byte
}
func NewGame(world World) Game {
return Game{
Width: world.Width,
Height: world.Height,
Title: "Game of Life",
World: world,
Pixels: make([]byte, world.Width*world.Height*4),
}
}
func (g *Game) Update() error {
g.World.Update()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
for i, v := range g.World.Cells {
if v {
g.Pixels[4*i] = 0xff
g.Pixels[4*i+1] = 0xff
g.Pixels[4*i+2] = 0xff
g.Pixels[4*i+3] = 0xff
} else {
g.Pixels[4*i] = 0
g.Pixels[4*i+1] = 0
g.Pixels[4*i+2] = 0
g.Pixels[4*i+3] = 0
}
}
screen.WritePixels(g.Pixels)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return g.Width, g.Height
}
func (g *Game) Run() error {
ebiten.SetWindowSize(g.Width, g.Height)
ebiten.SetWindowTitle(g.Title)
return ebiten.RunGame(g)
}