-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
151 lines (121 loc) · 2.85 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
// main.go
package main
import (
"fmt"
"os"
"runtime"
"time"
"github.com/veandco/go-sdl2/mix"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
func main() {
if err := run(); err != nil {
// fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
}
func playClickSound() {
clickSound, err := mix.LoadWAV("./res/music/keyEvent.mp3") // Replace with the path to your click sound file
if err != nil {
fmt.Printf("Failed to load sound file: %v\n", err)
return
}
defer clickSound.Free()
clickSound.Play(-1, 1)
sdl.Delay(250)
}
func playDeathSound() {
clickSound, err := mix.LoadWAV("./res/music/death.mp3") // Replace with the path to your click sound file
if err != nil {
fmt.Printf("Failed to load sound file: %v\n", err)
return
}
defer clickSound.Free()
clickSound.Play(-1, 1)
sdl.Delay(500)
}
func playGameOverSound() {
clickSound, err := mix.LoadWAV("./res/music/gameOver.mp3") // Replace with the path to your click sound file
if err != nil {
fmt.Printf("Failed to load sound file: %v\n", err)
return
}
defer clickSound.Free()
clickSound.Play(-1, 1)
sdl.Delay(4000)
}
func run() error {
err := sdl.Init(sdl.INIT_EVERYTHING)
if err != nil {
return fmt.Errorf("could not initialize SDL: %v", err)
}
defer sdl.Quit()
err = ttf.Init()
if err != nil {
return fmt.Errorf("could not init TTF: %v", err)
}
err = mix.OpenAudio(48000, sdl.AUDIO_S16, 2, 4096)
if err != nil {
return fmt.Errorf("failed to open audio: %v", err)
}
defer mix.CloseAudio()
err = mix.Init(int(mix.MP3))
if err != nil {
return fmt.Errorf("failed to initialize SDL mixer: %v", err)
}
defer mix.Quit()
w, r, err := sdl.CreateWindowAndRenderer(1920, 1080, sdl.WINDOWEVENT_SHOWN)
if err != nil {
return fmt.Errorf("could not create window: %v", err)
}
defer w.Destroy()
_ = r
if err = drawTitle(r, "goflap"); err != nil {
return fmt.Errorf("could not draw title: %v", err)
}
s, err := NewScene(r)
if err != nil {
return fmt.Errorf("could not create scene: %v", err)
}
defer s.destroy()
events := make(chan sdl.Event)
runtime.LockOSThread()
for {
select {
case err := <-s.run(r, events):
return err
case events <- sdl.WaitEvent():
}
}
}
func drawTitle(r *sdl.Renderer, title string) error {
r.Clear()
f, err := ttf.OpenFont("./res/fonts/GoFlap.ttf", 20)
if err != nil {
return fmt.Errorf("could not init font: %v", err)
}
defer f.Close()
s, err := f.RenderUTF8Solid(title, sdl.Color{
R: 255,
G: 100,
B: 0,
A: 255,
})
if err != nil {
return fmt.Errorf("could not write title: %v", err)
}
defer s.Free()
t, err := r.CreateTextureFromSurface(s)
if err != nil {
return fmt.Errorf("could not create texture : %v", err)
}
defer t.Destroy()
err = r.Copy(t, nil, nil)
if err != nil {
return fmt.Errorf("could not copy texture: %v", err)
}
r.Present()
time.Sleep(time.Second * 2)
return nil
}