-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfonts.go
155 lines (132 loc) · 3.49 KB
/
fonts.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
152
153
154
155
// fonts demonstrates how to render text using TTF fonts.
package main
import (
"fmt"
"image"
"image/color"
"log"
"path"
"runtime"
"time"
"github.com/mewkiz/pkg/goutil"
"github.com/mewspring/sfml/font"
"github.com/mewspring/sfml/texture"
"github.com/mewspring/sfml/window"
"github.com/mewspring/we"
)
// dataDir is the absolute path to the example source directory.
var dataDir string
func init() {
// Locate the absolute path to the example source directory.
var err error
dataDir, err = goutil.SrcDir("github.com/mewspring/sfml/examples/data")
if err != nil {
log.Fatalln(err)
}
}
func main() {
err := fonts()
if err != nil {
log.Fatalln(err)
}
}
// fonts demonstrates how to render text using TTF fonts.
func fonts() (err error) {
// Some operating systems require that the main thread is used for both
// window creation and event handling. Therefore we lock the goroutine to an
// OS thread.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Open a window with the specified dimensions.
win, err := window.Open(640, 480)
if err != nil {
return err
}
defer win.Close()
// Load background texture.
bg, err := texture.Load(path.Join(dataDir, "bg2.png"))
if err != nil {
return err
}
defer bg.Free()
// Load the text TTF font.
textFont, err := font.Load(path.Join(dataDir, "Exocet.ttf"))
if err != nil {
return err
}
defer textFont.Free()
// Create a new graphical text entry based on the Exocet TTF font and
// initialize its text to "TTF fonts", its font size to 32 (the default is
// 12) and its color to white (the default is black).
text, err := font.NewText(textFont, "TTF fonts", 32, color.White)
if err != nil {
return err
}
defer text.Free()
// Load the fps TTF font.
fpsFont, err := font.Load(path.Join(dataDir, "DejaVuSansMono.ttf"))
if err != nil {
return err
}
defer fpsFont.Free()
// Create a graphical FPS text entry. The text of this graphical text entry
// will be updated repeatedly using SetText.
fps, err := font.NewText(fpsFont, 14, color.White)
if err != nil {
return err
}
defer fps.Free()
// start and frames will be used to calculate the average FPS of the
// application.
start := time.Now()
frames := 0.0
// 60 FPS
ticker := time.NewTicker(time.Second / 60)
// Drawing and event loop.
for {
// Cap the FPS.
<-ticker.C
// Fill the window with white color.
win.Fill(color.White)
// Draw the entire background texture onto the window.
err = win.Draw(image.ZP, bg)
if err != nil {
return err
}
// Draw the entire text onto the window starting the destination point
// (420, 12).
dp := image.Pt(420, 12)
err = win.Draw(dp, text)
if err != nil {
return err
}
// Update the text of the FPS text entry.
fps.SetText(getFPS(start, frames))
// Draw the entire FPS text entry onto the screen starting at the
// destination point (8, 4).
dp = image.Pt(8, 4)
err = win.Draw(dp, fps)
if err != nil {
return err
}
// Display what has been rendered so far to the window.
win.Display()
frames++
// Poll events until the event queue is empty.
for e := win.PollEvent(); e != nil; e = win.PollEvent() {
fmt.Printf("%T: %v\n", e, e)
switch e.(type) {
case we.Close:
// Close the window.
return nil
}
}
}
}
// getFPS returns the average FPS as a string, based on the provided start time
// and frame count.
func getFPS(start time.Time, frames float64) (text string) {
// Average FPS.
fps := frames / time.Since(start).Seconds()
return fmt.Sprintf("FPS: %.2f", fps)
}