-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (76 loc) · 1.89 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
package main
import (
"fmt"
"runtime"
"time"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
const (
width = 800
height = 600
)
var titleDst sdl.Rect = sdl.Rect{X: width/2 - 300, Y: 150, W: 600, H: 300}
func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fmt.Printf("could not initialize SDL, %v\n", err)
}
defer sdl.Quit()
if err := ttf.Init(); err != nil {
fmt.Printf("could not initialize ttf, %v\n", err)
}
defer ttf.Quit()
window, renderer, err := sdl.CreateWindowAndRenderer(width, height, sdl.WINDOW_SHOWN)
if err != nil {
fmt.Printf("could not create window, %v\n", err)
}
defer window.Destroy()
scene, err := newScene(renderer)
if err != nil {
fmt.Printf("could not create new sceen, %v", err)
}
defer scene.destroy()
sdl.PumpEvents()
err = drawTitle(renderer, "Flappy Bird")
if err != nil {
fmt.Printf("could not print title, %v\n", err)
}
time.Sleep(2 * time.Second)
events := make(chan sdl.Event)
sceenErrc := scene.run(events, renderer)
runtime.LockOSThread()
for {
select {
case events <- sdl.WaitEvent():
case err = <-sceenErrc:
if err != nil {
fmt.Printf("runtime error, %v\n", err)
}
return
}
}
}
func drawTitle(r *sdl.Renderer, text string) error {
r.Clear()
font, err := ttf.OpenFont("res/font/Lobster-Regular.ttf", 148)
if err != nil {
return fmt.Errorf("could not open font, %v", err)
}
defer font.Close()
surface, err := font.RenderUTF8Solid(text, sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil {
return fmt.Errorf("could not render title, %v", err)
}
defer surface.Free()
texture, err := r.CreateTextureFromSurface(surface)
if err != nil {
return fmt.Errorf("could not create texture, %v", err)
}
defer texture.Destroy()
err = r.Copy(texture, nil, &titleDst)
if err != nil {
return fmt.Errorf("could not copy texture, %v", err)
}
r.Present()
return nil
}