ref: 9b056ef99bd476810c8d70289bf82b1ebd05d8e3
parent: dd68ed48e0c94dba29278fa43731f2babadc59e4
author: Iliyas Jorio <iliyas@jor.io>
date: Wed Jul 29 15:54:23 EDT 2020
Compute frames per second
--- a/src/SDLU.cpp
+++ b/src/SDLU.cpp
@@ -47,6 +47,10 @@
static MBoolean s_interestedInTyping = false;
static std::deque<BufferedKey> s_keyBuffer;
+// To compute frames per second
+static int s_fpsAccumulator = 0;
+static int s_fpsSampleStart = 0;
+const int k_fpsSampleInterval = 500;
int SDLUi_EventFilter(void*, SDL_Event *event)
{
@@ -431,4 +435,13 @@
SDL_RenderClear(g_renderer);
SDL_RenderCopy(g_renderer, g_windowTexture, NULL, NULL);
SDL_RenderPresent(g_renderer);
+ s_fpsAccumulator++;
+ int now = SDL_GetTicks();
+ int elapsed = now - s_fpsSampleStart;
+ if (elapsed > k_fpsSampleInterval) {
+ float fps = s_fpsAccumulator / (elapsed / 1000.0f);
+ printf("FPS: %.1f\n", fps);
+ s_fpsAccumulator = 0;
+ s_fpsSampleStart = now;
+ }
}