-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
79 lines (66 loc) · 1.8 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
72
73
74
75
76
77
78
79
package main
import (
"flag"
"runtime"
"time"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
const (
width = 500.0
height = 500.0
)
var (
columns = 20
rows = 20
seed = time.Now().UnixNano()
threshold = 0.2
fps = 20
)
func init() {
flag.IntVar(&columns, "columns", columns, "Sets the number of columns.")
flag.IntVar(&rows, "rows", rows, "Sets the number of columns.")
flag.Int64Var(&seed, "seed", seed, "Sets the starting seed of the game, used to randomize the initial state.")
flag.Float64Var(&threshold, "threshold", threshold, "A percentage between 0 and 1 used in conjunction with the -seed to determine if a cell starts alive. For example, 0.15 means each cell has a 15% chance of starting alive.")
flag.IntVar(&fps, "fps", fps, "Sets the frames-per-second, used set the speed of the simulation.")
flag.Parse()
}
func main() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
window := initGlfw()
defer glfw.Terminate()
prog := initOpenGL()
// Make the cells and start the loop
cells := makeCells(seed, threshold)
t := time.Now()
for !window.ShouldClose() {
tick(cells)
if err := draw(prog, window, cells); err != nil {
panic(err)
}
time.Sleep(time.Second/time.Duration(fps) - time.Since(t))
t = time.Now()
}
}
// tick updates the state of each cell in the game board.
func tick(cells [][]*cell) {
for x := range cells {
for _, c := range cells[x] {
c.checkState(cells)
}
}
}
// draw redraws the game board and the cells within.
func draw(prog uint32, window *glfw.Window, cells [][]*cell) error {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.UseProgram(prog)
for x := range cells {
for _, c := range cells[x] {
c.draw()
}
}
glfw.PollEvents()
window.SwapBuffers()
return nil
}