mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-07 11:58:13 +00:00
Improved GetFPS() calculation for average
This commit is contained in:
23
src/core.c
23
src/core.c
@@ -1611,9 +1611,30 @@ void SetTargetFPS(int fps)
|
||||
}
|
||||
|
||||
// Returns current FPS
|
||||
// NOTE: We calculate an average framerate
|
||||
int GetFPS(void)
|
||||
{
|
||||
return (int)roundf(1.0f/GetFrameTime());
|
||||
#define FPS_CAPTURE_FRAMES_COUNT 30 // 30 captures
|
||||
#define FPS_AVERAGE_TIME_SECONDS 0.5f // 500 millisecondes
|
||||
#define FPS_STEP (FPS_AVERAGE_TIME_SECONDS/FPS_CAPTURE_FRAMES_COUNT)
|
||||
|
||||
static int index = 0;
|
||||
static float history[FPS_CAPTURE_FRAMES_COUNT] = { 0 };
|
||||
static float average = 0, last = 0;
|
||||
float fpsFrame = GetFrameTime();
|
||||
|
||||
if (fpsFrame == 0) return 0;
|
||||
|
||||
if ((GetTime() - last) > FPS_STEP)
|
||||
{
|
||||
last = GetTime();
|
||||
index = (index + 1)%FPS_CAPTURE_FRAMES_COUNT;
|
||||
average -= history[index];
|
||||
history[index] = fpsFrame/FPS_CAPTURE_FRAMES_COUNT;
|
||||
average += history[index];
|
||||
}
|
||||
|
||||
return (int)roundf(1.0f/average);
|
||||
}
|
||||
|
||||
// Returns time in seconds for last frame drawn
|
||||
|
19
src/text.c
19
src/text.c
@@ -768,24 +768,7 @@ void UnloadFont(Font font)
|
||||
// NOTE: Uses default font
|
||||
void DrawFPS(int posX, int posY)
|
||||
{
|
||||
// NOTE: We are rendering fps every certain time for better viewing on high framerates
|
||||
|
||||
static int fps = 0;
|
||||
static int counter = 0;
|
||||
static int refreshRate = 20;
|
||||
|
||||
if (counter < refreshRate) counter++;
|
||||
else
|
||||
{
|
||||
fps = GetFPS();
|
||||
refreshRate = fps;
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
// TODO: Find a better way to calculate FPS, maybe calculate the mean of multiple measures?
|
||||
|
||||
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
||||
DrawText(TextFormat("%2i FPS", fps), posX, posY, 20, LIME);
|
||||
DrawText(TextFormat("%2i FPS", GetFPS()), posX, posY, 20, LIME);
|
||||
}
|
||||
|
||||
// Draw text (using default font)
|
||||
|
Reference in New Issue
Block a user