]> git.bts.cx Git - benzene.git/blob - src/bz/debug/perfgraph.c
Initial version
[benzene.git] / src / bz / debug / perfgraph.c
1 #include <bz/debug/perfgraph_internal.h>
2
3 #include <bz/types/common.h>
4 #include <bz/gfx/gfx.h>
5
6 extern void resetSystemTimer(void);
7 extern float getSystemTimer(void);
8
9 struct BZPerfTimer {
10 const char *identifier;
11 float startTime;
12 float elapsedTime;
13 };
14 typedef struct BZPerfTimer BZPerfTimer;
15
16 BZPerfTimer perfTimers[32];
17
18 void bzPerfReset() {
19 resetSystemTimer();
20
21 for (size_t i = 0; i < 32; ++i) { // FIXME
22 perfTimers[i].identifier = NULL;
23 perfTimers[i].elapsedTime = 0.0f;
24 }
25 }
26
27 static BZPerfTimer *bzPerfTimerFind(const char *identifier) {
28 BZPerfTimer *timer = NULL;
29
30 for (size_t i = 0; i < 32; ++i) {
31 if (perfTimers[i].identifier == NULL && timer == NULL) {
32 timer = &perfTimers[i];
33 } else if (perfTimers[i].identifier == identifier) {
34 timer = &perfTimers[i];
35 break;
36 }
37 }
38
39 bzAssert(timer != NULL);
40 return timer;
41 }
42
43 void _bzPerfTimerStart(const char *identifier) {
44 BZPerfTimer *timer = bzPerfTimerFind(identifier);
45 timer->identifier = identifier;
46 timer->startTime = getSystemTimer();
47 }
48
49 void _bzPerfTimerStop(const char *identifier) {
50 float endTime = getSystemTimer();
51 BZPerfTimer *timer = bzPerfTimerFind(identifier);
52 timer->elapsedTime += endTime - timer->startTime;
53 }
54
55 void _bzPerfTimerOutput() {
56 for (size_t i = 0; i < 32; ++i) {
57 if (perfTimers[i].identifier != NULL) {
58 bzLog("Timer '%s': %fs", perfTimers[i].identifier, perfTimers[i].elapsedTime);
59 }
60 }
61 }
62
63 void _bzPerfTimerDraw() {
64 int y = 10;
65 int width = 50;
66 for (size_t i = 0; i < 32; ++i) {
67 if (perfTimers[i].identifier != NULL) {
68 bzRectFill(10, y, 10 + width, y+3, 5);
69 float t = perfTimers[i].elapsedTime / (1 / 30.0f);
70 bzRectFill(10, y, 10 + width * t, y + 3, 8);
71 bzPrint(10 + width, y, 7, perfTimers[i].identifier);
72 y += 5;
73 }
74 }
75 }