-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
56 lines (45 loc) · 1.07 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
package main
import (
"fmt"
"strconv"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
var scoreDst sdl.Rect = sdl.Rect{X: width - 60, Y: 10, W: 50, H: 72}
type score struct {
points int
font *ttf.Font
}
func newScore() (*score, error) {
font, err := ttf.OpenFont("res/font/Anton-Regular.ttf", 56)
if err != nil {
return nil, fmt.Errorf("could not open font, %v", err)
}
return &score{
points: 0,
font: font,
}, nil
}
func (sc *score) paint(r *sdl.Renderer) error {
surface, err := sc.font.RenderUTF8Solid(strconv.Itoa(sc.points), sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil {
return fmt.Errorf("could not render points, %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, &scoreDst)
if err != nil {
return fmt.Errorf("could not copy texture, %v", err)
}
return nil
}
func (sc *score) destroy() {
sc.font.Close()
}
func (sc *score) addPoint() {
sc.points++
}