forked from n64decomp/sm64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps.inc.c
59 lines (49 loc) · 1.45 KB
/
fps.inc.c
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
/*
* Framerate counter
*
* Calculates the game's current framerate by using osGetTime() and
* prints it out on the lower left side of the screen.
*
* HOW TO USE:
*
* Add this include statement to the top of game.c
*
* #include "../../enhancements/fps.inc.c"
*
* Then, at the end of the while(1) loop in the function thread5_game_loop()
* add the function call render_fps()
*
* That's it! Build the rom file and press L when the game boots up
* to toggle the FPS counter.
*/
// SDK states that 1 cycle takes about 21.33 nanoseconds
#define SECONDS_PER_CYCLE 0.00000002133f
#define FPS_COUNTER_X_POS 24
#define FPS_COUNTER_Y_POS 190
OSTime gLastOSTime = 0;
float gFrameTime = 0.0f;
u16 gFrames = 0;
u16 gFPS = 0;
u8 gRenderFPS = FALSE;
void calculate_frameTime_from_OSTime(OSTime diff) {
gFrameTime += diff * SECONDS_PER_CYCLE;
gFrames++;
}
void render_fps(void) {
// Toggle rendering framerate with the L button.
if (gPlayer1Controller->buttonPressed & L_TRIG) {
gRenderFPS ^= 1;
}
if (gRenderFPS) {
OSTime newTime = osGetTime();
calculate_frameTime_from_OSTime(newTime - gLastOSTime);
// If frame time is longer or equal to a second, update FPS counter.
if (gFrameTime >= 1.0f) {
gFPS = gFrames;
gFrames = 0;
gFrameTime -= 1.0f;
}
print_text_fmt_int(FPS_COUNTER_X_POS, FPS_COUNTER_Y_POS, "FPS %d", gFPS);
gLastOSTime = newTime;
}
}