Fix FPS counter accuracy for high frame rates

This commit is contained in:
Alexander Overvoorde 2016-07-23 17:09:42 +02:00
parent d8ce023170
commit 90565fe2be
1 changed files with 13 additions and 15 deletions

View File

@ -301,21 +301,20 @@ void rct2_draw(rct_drawpixelinfo *dpi)
gCurrentDrawCount++; gCurrentDrawCount++;
} }
static uint32 _lastFPSUpdateTicks; static time_t _lastSecond;
static uint32 _lastFPSTicks; static int _currentFPS;
static float _currentFPS; static int _frames;
static float rct2_measure_fps() static void rct2_measure_fps()
{ {
uint32 currentTicks = SDL_GetTicks(); _frames++;
if (currentTicks - _lastFPSUpdateTicks > 500) {
_lastFPSUpdateTicks = currentTicks;
uint32 frameDelta = currentTicks - _lastFPSTicks; if (time(NULL) != _lastSecond) {
_currentFPS = 1000.0f / frameDelta; _currentFPS = _frames;
} _frames = 0;
_lastFPSTicks = currentTicks; }
return _currentFPS;
_lastSecond = time(NULL);
} }
static void rct2_draw_fps(rct_drawpixelinfo *dpi) static void rct2_draw_fps(rct_drawpixelinfo *dpi)
@ -332,9 +331,8 @@ static void rct2_draw_fps(rct_drawpixelinfo *dpi)
ch = utf8_write_codepoint(ch, FORMAT_MEDIUMFONT); ch = utf8_write_codepoint(ch, FORMAT_MEDIUMFONT);
ch = utf8_write_codepoint(ch, FORMAT_OUTLINE); ch = utf8_write_codepoint(ch, FORMAT_OUTLINE);
ch = utf8_write_codepoint(ch, FORMAT_WHITE); ch = utf8_write_codepoint(ch, FORMAT_WHITE);
const char *formatString = (_currentFPS >= 100.0f ? "%.0f" : "%.1f"); sprintf(ch, "%d", _currentFPS);
sprintf(ch, formatString, _currentFPS);
// Draw Text // Draw Text
int stringWidth = gfx_get_string_width(buffer); int stringWidth = gfx_get_string_width(buffer);