forked from wmurray8989/go-snakes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (136 loc) · 3.36 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"time"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
"github.com/wmurray8989/go-snakes/assets"
"github.com/wmurray8989/go-snakes/player"
"github.com/wmurray8989/go-snakes/snakes"
"github.com/wmurray8989/go-snakes/tournament"
)
var winTitle = "Go Snakes"
var winWidth, winHeight int32 = 1920, 1080
func run() int {
var window *sdl.Window
var renderer *sdl.Renderer
var err error
if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize SDL: %s\n", err)
return 1
}
defer sdl.Quit()
if window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
return 2
}
defer window.Destroy()
if renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
return 3 // don't use os.Exit(3); otherwise, previous deferred calls will never run
}
defer renderer.Destroy()
if err := ttf.Init(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize TTF: %s\n", err)
return 5
}
defer ttf.Quit()
globalAssets, err := assets.NewAssets()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load assets: %s\n", err)
return 6
}
defer globalAssets.Close()
window.SetResizable(true)
renderer.SetLogicalSize(winWidth, winHeight)
var activeTournament = tournament.NewTournament([32]player.Player{
snakes.SpiralOut,
snakes.Seeker,
snakes.SpiralIn,
snakes.Random,
snakes.Random,
snakes.Random,
snakes.Sleeper,
snakes.Random,
snakes.SpiralIn,
snakes.SpiralOut,
snakes.Random,
snakes.Panicker,
snakes.Seeker,
snakes.Random,
snakes.Sleeper,
snakes.Random,
snakes.SpiralIn,
snakes.SpiralOut,
snakes.Random,
snakes.Panicker,
snakes.Seeker,
snakes.Random,
snakes.Sleeper,
snakes.Random,
snakes.SpiralIn,
snakes.SpiralOut,
snakes.Random,
snakes.Panicker,
snakes.Seeker,
snakes.Random,
snakes.Sleeper,
snakes.Random,
})
fullscreen := true
window.SetFullscreen(sdl.WINDOW_FULLSCREEN_DESKTOP)
running := true
lastTime := time.Time{}
ticksPerSecond := 50
for running {
// Events
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.KeyboardEvent:
switch t.Keysym.Sym {
case sdl.K_q:
running = false
case sdl.K_ESCAPE:
running = false
// case sdl.K_r:
// if t.State == sdl.PRESSED {
// activeMatch = match.NewMatch(snakes.Panic10, snakes.SpiralOut)
// lastTime = time.Time{}
// }
case sdl.K_f:
if t.State == sdl.PRESSED {
if fullscreen {
window.SetFullscreen(0)
} else {
window.SetFullscreen(sdl.WINDOW_FULLSCREEN_DESKTOP)
}
fullscreen = !fullscreen
}
}
}
}
if time.Since(lastTime) > (time.Second / time.Duration(ticksPerSecond)) {
// Logic
activeTournament.Update()
// Render
renderer.SetDrawColor(0, 0, 0, 255)
renderer.Clear()
activeTournament.Render(renderer, globalAssets)
renderer.Present()
lastTime = time.Now().UTC()
}
renderer.SetViewport(&sdl.Rect{
X: 0,
Y: 0,
W: winWidth,
H: winHeight,
})
}
return 0
}
func main() {
os.Exit(run())
}