Skip to content

Commit

Permalink
Fix FPS counter (newlib port required)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickgaiser committed May 7, 2020
1 parent ff170c6 commit 619c0e8
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ static void guiShow();

#ifdef __DEBUG

#include <timer.h>

#define CLOCKS_PER_MILISEC 147456

// debug version displays an FPS meter
static u32 curtime = 0;
static u32 time_since_last = 0;
static u32 time_render = 0;
static clock_t prevtime = 0;
static clock_t curtime = 0;
static float fps = 0.0f;

extern GSGLOBAL *gsGlobal;
Expand Down Expand Up @@ -181,25 +176,19 @@ void guiUnlock(void)

void guiStartFrame(void)
{
#ifdef __DEBUG
u32 newtime = cpu_ticks() / CLOCKS_PER_MILISEC;
time_since_last = newtime - curtime;
curtime = newtime;
#endif

guiLock();
rmStartFrame();
guiFrameId++;
}

void guiEndFrame(void)
{
rmEndFrame();
#ifdef __DEBUG
u32 newtime = cpu_ticks() / CLOCKS_PER_MILISEC;
time_render = newtime - curtime;
// Measure time directly after vsync
prevtime = curtime;
curtime = clock();
#endif

rmEndFrame();
guiUnlock();
}

Expand Down Expand Up @@ -1314,8 +1303,15 @@ static void guiDrawOverlays()
y += yadd;
y += yadd; // Empty line

if (time_since_last != 0) {
fps = fps * 0.99 + 10.0f / (float)time_since_last;
if (prevtime != 0) {
clock_t diff = curtime - prevtime;
// Raw FPS value with 2 decimal places
float rawfps = ((100 * CLOCKS_PER_SEC) / diff) / 100.0f;

if (fps == 0.0f)
fps = rawfps;
else
fps = fps * 0.9f + rawfps / 10.0f; // Smooth FPS value

snprintf(text, sizeof(text), "%.1f FPS", fps);
fntRenderString(gTheme->fonts[0], x, y, ALIGN_LEFT, 0, 0, text, GS_SETREG_RGBA(0x60, 0x60, 0x60, 0x80));
Expand Down

0 comments on commit 619c0e8

Please sign in to comment.