forked from neomede/flappy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
55 lines (44 loc) · 1.39 KB
/
text.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
package main
import (
_ "embed"
"fmt"
"github.com/veandco/go-sdl2/sdl"
ttf "github.com/veandco/go-sdl2/ttf"
)
//go:embed res/fonts/Flappy.ttf
var embedTtf []byte
func drawTitle(r *sdl.Renderer) error {
r.Clear()
rectTitle := &sdl.Rect{X: 10, Y: windowHeight / 4, W: windowWidth - 20, H: windowHeight / 2}
rectPress := &sdl.Rect{X: windowWidth / 4, Y: windowHeight - (windowHeight / 4), W: windowWidth / 2, H: windowHeight / 6}
color := sdl.Color{R: 255, G: 100, B: 0, A: 255}
err := drawText(r, "Flappy Gopher", rectTitle, color)
if err != nil {
return fmt.Errorf("could not copy texture: %v", err)
}
err = drawText(r, "press any button to start", rectPress, color)
if err != nil {
return fmt.Errorf("could not copy texture: %v", err)
}
r.Present()
return nil
}
func drawText(renderer *sdl.Renderer, text string, rect *sdl.Rect, color sdl.Color) error {
//path := "res/fonts/Flappy.ttf"
mem, err := sdl.RWFromMem(embedTtf)
//font, err := ttf.OpenFont(path, 30)
font, err := ttf.OpenFontRW(mem, 0, 30)
if err != nil {
return fmt.Errorf("could not load font: %v", err)
}
surface, err := font.RenderUTF8Solid(text, color)
if err != nil {
return fmt.Errorf("could not render text: %v", err)
}
defer surface.Free()
tex, err := renderer.CreateTextureFromSurface(surface)
if err != nil {
return fmt.Errorf("could not create texture: %v", err)
}
return renderer.Copy(tex, nil, rect)
}