-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstopwatch.go
219 lines (187 loc) · 4.02 KB
/
stopwatch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"log"
"os"
"runtime"
"time"
"github.com/qeedquan/go-media/image/ttf"
"github.com/qeedquan/go-media/sdl"
"github.com/qeedquan/go-media/sdl/sdlimage/sdlcolor"
"github.com/qeedquan/go-media/sdl/sdlttf"
)
var (
conf struct {
font string
fontsz int
width int
height int
fullscreen bool
tick time.Duration
}
font *sdlttf.Font
window *sdl.Window
renderer *sdl.Renderer
texture *sdl.Texture
canvas *image.RGBA
start, stop time.Time
tick time.Duration
running bool
)
func main() {
runtime.LockOSThread()
log.SetPrefix("stopwatch: ")
log.SetFlags(0)
parseFlags()
initSDL()
reset(false)
for {
event()
update()
blit()
}
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func parseFlags() {
conf.width = 512
conf.height = 180
flag.Usage = usage
flag.IntVar(&conf.width, "w", conf.width, "window width")
flag.IntVar(&conf.height, "h", conf.height, "window height")
flag.BoolVar(&conf.fullscreen, "fs", conf.fullscreen, "fullscreen mode")
flag.StringVar(&conf.font, "f", conf.font, "use font file")
flag.IntVar(&conf.fontsz, "s", conf.fontsz, "font size")
flag.DurationVar(&conf.tick, "d", conf.tick, "start at duration")
flag.Parse()
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [options]")
flag.PrintDefaults()
os.Exit(2)
}
func initSDL() {
err := sdl.Init(sdl.INIT_VIDEO)
ck(err)
err = sdlttf.Init()
ck(err)
w, h := conf.width, conf.height
ptsz := conf.fontsz
if ptsz <= 0 {
switch {
case w <= 100:
ptsz = 8
case w <= 256:
ptsz = 16
case w <= 512:
ptsz = 20
default:
ptsz = 24
}
}
if conf.font == "" {
font, err = sdlttf.OpenFontMem(ttf.VGA437["default"], ptsz)
} else {
font, err = sdlttf.OpenFont(conf.font, ptsz)
}
ck(err)
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "best")
wflag := sdl.WINDOW_RESIZABLE
if conf.fullscreen {
wflag |= sdl.WINDOW_FULLSCREEN_DESKTOP
}
window, renderer, err = sdl.CreateWindowAndRenderer(w, h, wflag)
ck(err)
window.SetTitle("Stopwatch")
renderer.SetLogicalSize(conf.width, conf.height)
texture, err = renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, w, h)
ck(err)
canvas = image.NewRGBA(image.Rect(0, 0, w, h))
}
func reset(resetTick bool) {
start = time.Now()
stop = start
if resetTick {
tick = 0
} else {
tick = conf.tick
}
running = false
}
func event() {
for {
ev := sdl.PollEvent()
if ev == nil {
break
}
switch ev := ev.(type) {
case sdl.QuitEvent:
os.Exit(0)
case sdl.KeyDownEvent:
switch ev.Sym {
case sdl.K_ESCAPE:
os.Exit(0)
case sdl.K_SPACE:
running = !running
if running {
start = time.Now()
stop = start
} else {
tick += stop.Sub(start)
}
case sdl.K_BACKSPACE:
reset(false)
case sdl.K_RETURN:
reset(true)
}
}
}
}
func update() {
if !running {
return
}
stop = time.Now()
}
func blit() {
renderer.SetDrawColor(sdlcolor.Black)
renderer.Clear()
draw.Draw(canvas, canvas.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
blitTime()
texture.Update(nil, canvas.Pix, canvas.Stride)
renderer.Copy(texture, nil, nil)
renderer.Present()
}
func blitTime() {
x, y := conf.width/2, conf.height/10
sr := blitText(x, y, "Running: %v", running)
y += sr.Dy()
tm := start.Format(time.UnixDate)
sr = blitText(x, y, "%-12s: %v", "Last Started", tm)
y += sr.Dy()
tm = stop.Format(time.UnixDate)
blitText(x, y, "%-12s: %v", "Last Stopped", tm)
elapsed := tick
if running {
elapsed += stop.Sub(start)
}
y += sr.Dy() * 3
blitText(x, y, "%s: %-12s", "Elapsed", elapsed.Round(10*time.Millisecond))
}
func blitText(x, y int, format string, args ...interface{}) image.Rectangle {
text := fmt.Sprintf(format, args...)
surface, err := font.RenderUTF8Blended(text, sdlcolor.White)
ck(err)
defer surface.Free()
sr := surface.Bounds()
x -= sr.Dx() / 2
draw.Draw(canvas, image.Rect(x, y, x+sr.Dx(), y+sr.Dy()), surface, image.ZP, draw.Over)
return sr
}