Skip to content

Instantly share code, notes, and snippets.

@jdmunro
Created February 11, 2009 18:49
Show Gist options
  • Save jdmunro/62171 to your computer and use it in GitHub Desktop.
Save jdmunro/62171 to your computer and use it in GitHub Desktop.
--- teeworlds-0.5.1-src/src/game/client/components/hud.cpp
+++ teeworlds/teeworlds-0.5.1-src/src/game/client/components/hud.cpp
@@ -18,7 +18,12 @@
HUD::HUD()
{
-
+ // OR: memset(&fps_samples,0.0f,MAX_FPS_SAMPLES);
+ for(int i = 0; i < MAX_FPS_SAMPLES; i++)
+ fps_samples[i] = 0.0f;
+
+ fps_sum = 0.0f;
+ fps_index = 0;
}
void HUD::on_reset()
@@ -151,8 +156,18 @@
{
if(config.cl_showfps)
{
+ // calculate avg. fps,
+ fps_sum -= fps_samples[fps_index];
+ fps_sum += 1.0f / client_frametime();
+ fps_samples[fps_index] = 1.0f / client_frametime();
+
+ // reset counter when max samples reached
+ fps_index++;
+ if(fps_index == MAX_FPS_SAMPLES-1)
+ fps_index = 0;
+
char buf[512];
- str_format(buf, sizeof(buf), "%d", (int)(1.0f/client_frametime()));
+ str_format(buf, sizeof(buf), "%d", (int)(fps_sum / MAX_FPS_SAMPLES));
gfx_text(0, width-10-gfx_text_width(0,12,buf,-1), 5, 12, buf, -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment