-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
85 lines (64 loc) · 1.42 KB
/
score.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
package main
import (
"fmt"
"strconv"
"sync"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
const (
fontPath = "./res/fonts/Flappy.ttf"
fontSize = 6
)
type score struct {
mu sync.RWMutex
textures []*sdl.Texture
x, y int32
w, h int32
value int64
}
func newScore() (*score, error) {
var textures []*sdl.Texture
return &score{textures: textures, y: 500, x: 10, value: 0}, nil
}
func (s *score) update() {
s.mu.Lock()
defer s.mu.Unlock()
s.value += 1
}
func (s *score) restart() {
s.mu.Lock()
defer s.mu.Unlock()
s.value = 0
}
func (score *score) paint(r *sdl.Renderer) error {
f, err := ttf.OpenFont(fontPath, fontSize)
if err != nil {
return fmt.Errorf("could not load font: %v", err)
}
defer f.Close()
c := sdl.Color{R: 255, G: 100, B: 0, A: 255}
s, err := f.RenderUTF8Solid(strconv.FormatInt(score.value, 10), c)
if err != nil {
return fmt.Errorf("could not render 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()
if err := r.Copy(t, nil, &sdl.Rect{X: 500, Y: 20, W: 50, H: 50}); err != nil {
// if err := r.Copy(t, nil, nil); err != nil {
return fmt.Errorf("could not copy texture: %v", err)
}
r.Present()
return nil
}
func (s *score) destroy() {
s.mu.Lock()
defer s.mu.Unlock()
for _, t := range s.textures {
t.Destroy()
}
}