-
Notifications
You must be signed in to change notification settings - Fork 0
/
universe.go
203 lines (160 loc) · 4.08 KB
/
universe.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
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
"syscall"
"time"
)
type mvObject interface {
GetLocation() F3
GetRotation() F3
GetMotion() (F3, F3)
}
type mvSolid interface {
GetLocation() F3
GetRotation() F3
GetMotion() (F3, F3)
GetComposition() F3
}
type View interface {
GetName() string
GetLocation() F2
GetSize() F2
HandleEvent(event sdl.Event)
Configure()
Update()
Draw(g *Graphics)
}
type fpsWidget struct {
location F2
size F2
updateLastTick time.Time
updateDelta time.Duration
updateHistory []float64
renderLastTick time.Time
renderDelta time.Duration
renderHistory []float64
rssPrev float64
rssHistory []float64
}
func (f *fpsWidget) GetName() string {
return "Performance"
}
func (f *fpsWidget) HandleEvent(event sdl.Event) {
}
func (f *fpsWidget) GetLocation() F2 {
return f.location
}
func (f *fpsWidget) GetSize() F2 {
return f.size
}
func (f *fpsWidget) Configure() {
f.updateLastTick = time.Now()
f.renderLastTick = time.Now()
}
func MapF2(value float64, input F2, output F2) float64 {
return output.X + (output.Y-output.X)*((value-input.X)/(input.Y-input.X))
}
func (f *fpsWidget) Update() {
f.updateDelta = time.Duration(time.Since(f.updateLastTick).Nanoseconds())
f.updateHistory = append(f.updateHistory, f.updateDelta.Seconds())
if len(f.updateHistory) > 240*2 {
f.updateHistory = f.updateHistory[1:]
}
f.updateLastTick = time.Now()
}
func (f *fpsWidget) Draw(g *Graphics) {
f.renderDelta = time.Duration(time.Since(f.renderLastTick).Nanoseconds())
f.renderHistory = append(f.renderHistory, f.renderDelta.Seconds())
if len(f.renderHistory) > 120*2 {
f.renderHistory = f.renderHistory[1:]
}
f.renderLastTick = time.Now()
list := NewList("Performance", f.location, f.size)
renAvg := 0.0
for _, it := range f.renderHistory {
renAvg += it * 1000
}
renAvg /= float64(len(f.renderHistory))
item := NewItem("Render", fmt.Sprintf("%.2f FPS", 1000.0/renAvg))
list.AddItem(item)
upsAvg := 0.0
for _, it := range f.updateHistory {
upsAvg += it * 1000
}
upsAvg /= float64(len(f.updateHistory))
item2 := NewItem("Update", fmt.Sprintf("%.2f UPS", 1000.0/upsAvg))
list.AddItem(item2)
rusage := new(syscall.Rusage)
syscall.Getrusage(0, rusage)
f.rssHistory = append(f.rssHistory, float64(rusage.Maxrss)/1024/1024)
if len(f.rssHistory) > 360 {
f.rssHistory = f.rssHistory[1:]
}
f.rssPrev = float64(rusage.Maxrss) / 1024 / 1024
item3 := NewItem("MaxRSS", fmt.Sprintf("%.2f MB ", float64(rusage.Maxrss)/1024/1024))
list.AddItem(item3)
list.AddItem(NewItem("Frame Duration", fmt.Sprintf("%.2f MS ", renAvg)))
list.AddItem(NewItem("Update Duration", fmt.Sprintf("%.2f MS ", upsAvg)))
list.Draw(g)
}
func (f *fpsWidget) GetBounds() (F2, F2) {
return f.location, f.size
}
type Clock interface {
GetTicks() float64
GetDelta() float64
GetRate() float64
Tick()
}
type PerformanceClock struct {
ticks float64
delta float64
rate float64
previous float64
}
func (p *PerformanceClock) Tick() {
p.delta = float64(sdl.GetTicks()) - p.previous
p.previous = float64(sdl.GetTicks())
p.rate = 1000 / p.delta
p.ticks++
}
func (p *PerformanceClock) GetTicks() float64 {
return p.ticks
}
func (p *PerformanceClock) GetDelta() float64 {
return p.delta
}
func (p *PerformanceClock) GetRate() float64 {
return p.rate
}
type Performance struct {
location, size F2
clocks []Clock
}
func (f *Performance) GetName() string {
return "Performance"
}
func (f *Performance) Configure() {
f.clocks = append(f.clocks, &PerformanceClock{}, &PerformanceClock{})
}
func (f *Performance) HandleEvent(event sdl.Event) {
}
func (f *Performance) GetLocation() F2 {
return f.location
}
func (f *Performance) GetSize() F2 {
return f.size
}
func (f *Performance) Update() {
f.clocks[0].Tick()
}
func (f *Performance) Draw(g *Graphics) {
f.clocks[1].Tick()
list := NewList("Performance", f.location, f.size)
render := NewItem("Render", fmt.Sprintf("%.2f FPS", f.clocks[1].GetRate()))
list.AddItem(render)
update := NewItem("Update", fmt.Sprintf("%.2f UPS", 1000/f.clocks[0].GetRate()))
list.AddItem(update)
list.Draw(g)
}