-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
294 lines (237 loc) · 5.57 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import "C"
import (
"path/filepath"
"runtime"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/mix"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
const (
winTitle = "Go SDL2"
winWidth = 480
winHeight = 800
)
// Game states.
const (
stateRun = iota
stateFlap
stateDead
)
// States text.
var stateText = map[int]string{
stateRun: "RUN",
stateFlap: "FLAP",
stateDead: "DEAD",
}
// Text represents state text.
type Text struct {
Width int32
Height int32
Texture *sdl.Texture
}
// Engine represents SDL engine.
type Engine struct {
State int
Window *sdl.Window
Renderer *sdl.Renderer
Sprite *sdl.Texture
Font *ttf.Font
Music *mix.Music
Sound *mix.Chunk
StateText map[int]*Text
running bool
}
// NewEngine returns new engine.
func NewEngine() (e *Engine) {
e = &Engine{}
e.running = true
return
}
// Init initializes SDL.
func (e *Engine) Init() (err error) {
err = sdl.Init(sdl.INIT_EVERYTHING)
if err != nil {
return
}
img.Init(img.INIT_PNG)
err = mix.Init(mix.INIT_MP3)
if err != nil {
return
}
err = ttf.Init()
if err != nil {
return
}
err = mix.OpenAudio(mix.DEFAULT_FREQUENCY, mix.DEFAULT_FORMAT, mix.DEFAULT_CHANNELS, 3072)
if err != nil {
return
}
e.Window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
return
}
e.Renderer, err = sdl.CreateRenderer(e.Window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
return
}
return
}
// Destroy destroys SDL and releases the memory.
func (e *Engine) Destroy() {
e.Renderer.Destroy()
e.Window.Destroy()
mix.CloseAudio()
img.Quit()
mix.Quit()
ttf.Quit()
sdl.Quit()
}
// Running checks if loop is running.
func (e *Engine) Running() bool {
return e.running
}
// Quit exits main loop.
func (e *Engine) Quit() {
e.running = false
}
// Load loads resources.
func (e *Engine) Load() {
assetDir := ""
if runtime.GOOS != "android" {
assetDir = filepath.Join("android", "src", "main", "assets")
}
var err error
e.Sprite, err = img.LoadTexture(e.Renderer, filepath.Join(assetDir, "images", "sprite.png"))
if err != nil {
sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadTexture: %s\n", err)
}
e.Font, err = ttf.OpenFont(filepath.Join(assetDir, "fonts", "universalfruitcake.ttf"), 24)
if err != nil {
sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "OpenFont: %s\n", err)
}
e.Music, err = mix.LoadMUS(filepath.Join(assetDir, "music", "frantic-gameplay.mp3"))
if err != nil {
sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadMUS: %s\n", err)
}
e.Sound, err = mix.LoadWAV(filepath.Join(assetDir, "sounds", "click.wav"))
if err != nil {
sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadWAV: %s\n", err)
}
e.StateText = map[int]*Text{}
for k, v := range stateText {
t, _ := e.renderText(v, sdl.Color{0, 0, 0, 0})
_, _, tW, tH, _ := t.Query()
e.StateText[k] = &Text{tW, tH, t}
}
}
// Unload unloads resources.
func (e *Engine) Unload() {
for _, v := range e.StateText {
v.Texture.Destroy()
}
e.Sprite.Destroy()
e.Font.Close()
e.Music.Free()
e.Sound.Free()
}
// renderText renders texture from ttf font.
func (e *Engine) renderText(text string, color sdl.Color) (texture *sdl.Texture, err error) {
surface, err := e.Font.RenderUTF8Blended(text, color)
if err != nil {
return
}
defer surface.Free()
texture, err = e.Renderer.CreateTextureFromSurface(surface)
return
}
//export SDL_main
func SDL_main() {
runtime.LockOSThread()
e := NewEngine()
err := e.Init()
if err != nil {
sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "Init: %s\n", err)
}
defer e.Destroy()
e.Load()
defer e.Unload()
// Sprite size
const n = 128
// Sprite rects
var rects []*sdl.Rect
for x := 0; x < 6; x++ {
rect := &sdl.Rect{int32(n * x), 0, n, n}
rects = append(rects, rect)
}
e.Music.Play(-1)
var frame int = 0
var alpha uint8 = 255
var showText bool = true
var text *Text = e.StateText[stateRun]
for e.Running() {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case sdl.QuitEvent:
e.Quit()
case sdl.MouseButtonEvent:
e.Sound.Play(2, 0)
if t.Type == sdl.MOUSEBUTTONDOWN && t.Button == sdl.BUTTON_LEFT {
alpha = 255
showText = true
if e.State == stateRun {
text = e.StateText[stateFlap]
e.State = stateFlap
} else if e.State == stateFlap {
text = e.StateText[stateDead]
e.State = stateDead
} else if e.State == stateDead {
text = e.StateText[stateRun]
e.State = stateRun
}
}
case sdl.KeyboardEvent:
if t.Keysym.Scancode == sdl.SCANCODE_ESCAPE || t.Keysym.Scancode == sdl.SCANCODE_AC_BACK {
e.Quit()
}
}
}
e.Renderer.Clear()
var clips []*sdl.Rect
w, h := e.Window.GetSize()
x, y := int32(w/2), int32(h/2)
switch e.State {
case stateRun:
e.Renderer.SetDrawColor(168, 235, 254, 255)
clips = rects[0:2]
case stateFlap:
e.Renderer.SetDrawColor(251, 231, 240, 255)
clips = rects[2:4]
case stateDead:
e.Renderer.SetDrawColor(255, 250, 205, 255)
clips = rects[4:6]
}
clip := clips[frame/2]
e.Renderer.FillRect(nil)
e.Renderer.Copy(e.Sprite, clip, &sdl.Rect{x - (n / 2), y - (n / 2), n, n})
if showText {
text.Texture.SetAlphaMod(alpha)
e.Renderer.Copy(text.Texture, nil, &sdl.Rect{x - (text.Width / 2), y - n*1.5, text.Width, text.Height})
}
e.Renderer.Present()
sdl.Delay(50)
frame += 1
if frame/2 >= 2 {
frame = 0
}
alpha -= 10
if alpha <= 10 {
alpha = 255
showText = false
}
}
}
func main() {
SDL_main()
}