Repository navigation
Expand file tree
/
Copy pathprof_draw.c
More file actions
301 lines (252 loc) · 7.54 KB
/
Copy pathprof_draw.c
File metadata and controls
301 lines (252 loc) · 7.54 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "prof.h"
#include "prof_internal.h"
#pragma warning(disable:4305; disable:4244)
// use factor to compute a glow amount
static int get_colors(float factor,
float text_color_ret[3],
float glow_color_ret[3],
float *glow_alpha_ret)
{
const float GLOW_RANGE = 0.5f;
const float GLOW_ALPHA_MAX = 0.5f;
float glow_alpha;
int i;
float hot[3] = {1, 1.0, 0.9};
float cold[3] = {0.15, 0.9, 0.15};
float glow_cold[3] = {0.5f, 0.5f, 0};
float glow_hot[3] = {1.0f, 1.0f, 0};
if (factor < 0) factor = 0;
if (factor > 1) factor = 1;
for (i=0; i < 3; ++i)
text_color_ret[i] = cold[i] + (hot[i] - cold[i]) * factor;
// Figure out whether to start up the glow as well.
glow_alpha = (factor - GLOW_RANGE) / (1 - GLOW_RANGE);
if (glow_alpha < 0) {
*glow_alpha_ret = 0;
return 0;
}
for (i=0; i < 3; ++i)
glow_color_ret[i] = glow_cold[i] + (glow_hot[i] - glow_cold[i]) * factor;
*glow_alpha_ret = glow_alpha * GLOW_ALPHA_MAX;
return 1;
}
Prof_extern_C void Prof_draw_graph(float sx, float sy, float x_spacing, float y_spacing, void (*draw_line)(float x0, float y0, float x1, float y1, float r, float g, float b, float a))
{
#ifdef Prof_ENABLED
Prof_Begin(iprof_draw_graph)
GraphLocation loc = { sx, sy, x_spacing, y_spacing * 1000 };
Prof_graph(128, draw_line, &loc);
Prof_End
#endif
}
// float to string conversion with sprintf() was
// taking up 10-20% of the Prof_draw time, so I
// wrote a faster float-to-string converter
static char int_to_string[100][4];
static char int_to_string_decimal[100][4];
static char int_to_string_mid_decimal[100][4];
static void int_to_string_init(void)
{
int i;
for (i=0; i < 100; ++i) {
sprintf(int_to_string[i], "%d", i);
sprintf(int_to_string_decimal[i], ".%02d", i);
sprintf(int_to_string_mid_decimal[i], "%d.%d", i/10, i % 10);
}
}
static char *formats[5] =
{
"%.0f",
"%.1f",
"%.2f",
"%.3f",
"%.4f",
};
static void float_to_string(char *buf, float num, int precision)
{
int x,y;
switch(precision) {
case 2:
if (num < 0 || num >= 100)
break;
x = num;
y = (num - x) * 100;
strcpy(buf, int_to_string[x]);
strcat(buf, int_to_string_decimal[y]);
return;
case 3:
if (num < 0 || num >= 10)
break;
num *= 10;
x = num;
y = (num - x) * 100;
strcpy(buf, int_to_string_mid_decimal[x]);
strcat(buf, int_to_string_decimal[y]+1);
return;
case 4:
if (num < 0 || num >= 1)
break;
num *= 100;
x = num;
y = (num - x) * 100;
buf[0] = '0';
strcpy(buf+1, int_to_string_decimal[x]);
strcat(buf, int_to_string_decimal[y]+1);
return;
}
sprintf(buf, formats[precision], num);
}
Prof_extern_C void Prof_draw(float sx, float sy,
float full_width, float height,
float line_spacing, int precision,
void (*printText)(float x, float y, char *str, float r, float g, float b, float a),
float (*textWidth)(char *str),
void (*draw_rectangle)(float x0, float y0, float x1, float y1, float r, float g, float b, float a))
{
#ifdef Prof_ENABLED
Prof_Begin(iprof_draw)
int i,j,n,o;
float backup_sy;
float field_width = textWidth("5555.55");
float name_width = full_width - field_width * 3;
float plus_width = textWidth("+");
int max_records;
Prof_Report *pob;
if (!int_to_string[0][0]) int_to_string_init();
if (precision < 1) precision = 1;
if (precision > 4) precision = 4;
pob = Prof_create_report();
float rf, gf, bf, af;
for (i=0; i < NUM_TITLE; ++i) {
if (pob->title[i]) {
float header_x0 = sx;
float header_x1 = header_x0 + full_width;
if (i == 0) {
rf = 0.1f;
gf = 0.3f;
bf = 0;
af = 0.85;
} else {
rf = 0.2f;
gf = 0.1f;
bf = 0.1f;
af = 0.85;
}
draw_rectangle(header_x0, sy-2, header_x1, sy-line_spacing+2, rf, gf, bf, af);
if (i == 0) {
rf = 0.6;
gf = 0.4;
bf = 0;
af = 0;
} else {
rf = 0.8f;
gf = 0.1f;
bf = 0.1f;
af = 0;
}
printText(sx+2, sy, pob->title[i], rf, gf, bf, af);
sy += 1.5*line_spacing;
height -= abs(line_spacing)*1.5;
}
}
max_records = height / abs(line_spacing);
o = 0;
n = pob->num_record;
if (n > max_records) n = max_records;
if (pob->hilight >= o + n) {
o = pob->hilight - n + 1;
}
backup_sy = sy;
rf = 0;
gf = 0;
bf = 0;
af = 0.85;
draw_rectangle(sx, sy, sx + full_width, sy - line_spacing, rf, gf, bf, af);
sy += line_spacing;
for (i = 0; i < n; i++) {
float y0, y1;
if (i & 1) {
rf = 0.1;
gf = 0.1f;
bf = 0.2;
af = 0.85;
} else {
rf = 0.1f;
gf = 0.1f;
bf = 0.3;
af = 0.85;
}
if (i+o == pob->hilight) {
rf = 0.3f;
gf = 0.3f;
bf = 0.1f;
af = 0.85;
}
y0 = sy;
y1 = sy - line_spacing;
draw_rectangle(sx, y0, sx + full_width, y1, rf, gf, bf, af);
sy += line_spacing;
}
sy = backup_sy;
rf = 0.7;
gf = 0.7;
bf = 0.7;
af = 0;
if (pob->header[0])
printText(sx+8, sy, pob->header[0], rf, gf, bf, af);
for (j=1; j < NUM_HEADER; ++j)
if (pob->header[j])
printText(sx + name_width + field_width * (j-1) +
field_width/2 - textWidth(pob->header[j])/2, sy, pob->header[j],
rf, gf, bf, af);
sy += line_spacing;
for (i = 0; i < n; i++) {
char buf[256], *b = buf;
Prof_Report_Record *r = &pob->record[i+o];
float text_color[3], glow_color[3];
float glow_alpha;
float x = sx + textWidth(" ") * r->indent + plus_width/2;
if (r->prefix) {
buf[0] = r->prefix;
++b;
} else {
x += plus_width;
}
if (r->number)
sprintf(b, "%s (%d)", r->name, r->number);
else
sprintf(b, "%s", r->name);
if (get_colors(r->heat, text_color, glow_color, &glow_alpha)) {
rf = glow_color[0];
gf = glow_color[1];
bf = glow_color[2];
af = glow_alpha;
printText(x+2, sy-1, buf, rf, gf, bf, af);
}
rf = text_color[0];
gf = text_color[1];
bf = text_color[2];
af = 1.0f;
printText(x + 1, sy, buf, rf, gf, bf, af);
for (j=0; j < NUM_VALUES; ++j) {
if (r->value_flag & (1 << j)) {
int pad;
float_to_string(buf, r->values[j], j == 2 ? 2 : precision);
pad = field_width- plus_width - textWidth(buf);
if (r->indent) pad += plus_width;
printText(sx + pad + name_width + field_width * j, sy, buf, rf, gf, bf, af);
}
}
sy += line_spacing;
}
Prof_free_report(pob);
Prof_End
#endif
}