-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
95 lines (82 loc) · 1.17 KB
/
util.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
package main
import (
"os"
"os/user"
"path/filepath"
"time"
"github.com/qeedquan/go-media/sdl"
)
func dpath(path ...string) string {
return filepath.Join(*assets, filepath.Join(path...))
}
func spath(path ...string) string {
return filepath.Join(*pref, filepath.Join(path...))
}
func quitEvents() {
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)
}
}
}
}
func delay(ms time.Duration) {
ticker := time.NewTicker(ms * time.Millisecond)
defer ticker.Stop()
loop:
for {
select {
case <-ticker.C:
break loop
default:
quitEvents()
drawScreen()
}
}
}
func clamp(x, a, b int) int {
if x < a {
return a
}
if x > b {
return b
}
return x
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func getInitials() string {
user, err := user.Current()
if err != nil {
return "TUX"
}
s := ""
l := 0
for _, ch := range user.Name {
s += string(ch)
if l++; l == 3 {
break
}
}
for ; l < 3; l++ {
s += " "
}
return s
}
func exit(status int) {
saveOptions()
os.Exit(status)
}