-
Notifications
You must be signed in to change notification settings - Fork 1
/
guifps.cpp
212 lines (177 loc) · 4.65 KB
/
guifps.cpp
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
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include <set>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include "guifps.h"
#include <GL/gl.h>
namespace QG
{
static float sortedTimes[GUIFPS_NUM_SAMPLES];
FPSGraph::FPSGraph()
{
for (currentSample = 0; currentSample < GUIFPS_NUM_SAMPLES; ++currentSample)
{
times[currentSample] = 0.0f;
}
time = 0.0f;
frames = 0;
width = 100;
height = 100;
padding = 0;
border = 8;
margin = 0;
fill = QG::NONE;
heightScale = 1.0f;
tpfLast = 0.0f;
fpsLast = 0.0f;
tpfAverage = 0.0f;
tpfMedian = 0.0f;
tpfMaximum = 0.0f;
tpfMinimum = 0.0f;
print = false;
gotNewSample = false;
}
FPSGraph::~FPSGraph()
{
if (frames > 0)
{
tpfLast = time / frames;
fpsLast = frames / time;
if (print)
printf("rem %if: %.2ffps %.2fms\n", (int)frames, fpsLast, 1000.0*tpfLast);
}
}
void FPSGraph::updateStats()
{
memcpy(sortedTimes, times, GUIFPS_NUM_SAMPLES * sizeof(float));
std::sort(sortedTimes, sortedTimes + GUIFPS_NUM_SAMPLES);
tpfAverage = 0.0f;
tpfMaximum = sortedTimes[0];
tpfMinimum = sortedTimes[0];
for (int i = 0; i < GUIFPS_NUM_SAMPLES; ++i)
{
tpfAverage += sortedTimes[i];
if (tpfMaximum < sortedTimes[i]) tpfMaximum = sortedTimes[i];
if (tpfMinimum > sortedTimes[i]) tpfMinimum = sortedTimes[i];
}
tpfAverage /= GUIFPS_NUM_SAMPLES;
tpfMedian = sortedTimes[GUIFPS_NUM_SAMPLES/2];
}
void FPSGraph::update(float dt)
{
time += dt;
++frames;
if (time > GUIFPS_MIN_TIME)
{
tpfLast = time / frames;
fpsLast = frames / time;
time = 0.0f;
frames = 0;
currentSample = (currentSample + 1) % GUIFPS_NUM_SAMPLES;
times[currentSample] = tpfLast;
updateStats();
if (print)
printf("%.2ffps %.2fms\n", fpsLast, 1000.0*tpfLast);
gotNewSample = true;
}
else
gotNewSample = false;
if (tpfMaximum > 0)
{
float step = dt * 10000.0;
float targetHeight = 0.6666 / tpfMaximum;
float to = targetHeight - heightScale;
if (fabs(to) > step)
heightScale += step * to / fabs(to);
else
heightScale = targetHeight;
}
}
void FPSGraph::update()
{
if (nineboxImage < 0)
{
nineboxImage = nineboxPool.create(NINEBOX_BACKGROUND);
}
Widget::update();
}
void FPSGraph::drawContent(mat44 mat)
{
mygl.projection = mat * mat44::translate(0, 0, 0.1f);
const float fps30 = 1.0/30.0;
const float fps60 = 1.0/60.0;
glEnable(GL_BLEND);
float scalew = width / (float)(GUIFPS_NUM_SAMPLES-1);
float offsetx = - time / GUIFPS_MIN_TIME;
//graph grid
mygl.colour(0, 1, 0, 0.3);
mygl.begin(Immediate::LINES);
const int vbarFreq = GUIFPS_NUM_SAMPLES / GUIFPS_VERTICAL_BARS;
for (int i = vbarFreq - currentSample % vbarFreq; i < GUIFPS_NUM_SAMPLES; i += vbarFreq)
{
float x = i + offsetx;
if (x < 0.0f) x += 1.0f;
mygl.vertex(x*scalew, 0);
mygl.vertex(x*scalew, height);
}
mygl.end(false);
//horizontal bars at 30 and 60 fps
mygl.colour(0, 1, 0, 0.5);
mygl.begin(Immediate::LINES);
if (fps30 * heightScale <= 1.0)
{
mygl.vertex(0, height - fps30 * heightScale * height);
mygl.vertex(width, height - fps30 * heightScale * height);
}
if (fps60 * heightScale <= 1.0)
{
mygl.vertex(0, height - fps60 * heightScale * height);
mygl.vertex(width, height - fps60 * heightScale * height);
}
mygl.end(false);
//graph line
mygl.colour(0, 1, 0, 0.5);
mygl.begin(Immediate::LINE_STRIP);
for (int i = 0; i < GUIFPS_NUM_SAMPLES; ++i)
{
float t = times[(currentSample + 1 + i) % GUIFPS_NUM_SAMPLES];
float x = i + offsetx;
if (x < 0.0f) x = 0.0f;
mygl.vertex(x*scalew, height - t * heightScale * height);
}
mygl.end(false);
//plot median and average
mygl.begin(Immediate::LINES);
if (tpfAverage * heightScale <= 1.0)
{
mygl.colour(0, 0.5, 1, 0.5);
mygl.vertex(0, height - tpfAverage * heightScale * height);
mygl.vertex(width, height - tpfAverage * heightScale * height);
}
if (tpfMedian * heightScale <= 1.0)
{
mygl.colour(1, 0.5, 0.5, 0.5);
mygl.vertex(0, height - tpfMedian * heightScale * height);
mygl.vertex(width, height - tpfMedian * heightScale * height);
}
mygl.end(false);
//plot points
mygl.colour(0, 1, 0, 1);
mygl.begin(Immediate::POINTS);
for (int i = 0; i < GUIFPS_NUM_SAMPLES; ++i)
{
float t = times[(currentSample + 1 + i) % GUIFPS_NUM_SAMPLES];
if (t > fps30)
mygl.colour(1, 0, 0, 1);
else
mygl.colour(0, 1, 0, 1);
float x = i + offsetx;
if (x < 0.0f) x = 0.0f;
mygl.vertex(x*scalew, height - t * heightScale * height);
}
mygl.end(false);
mygl.flush();
}
}