Detect framerate at runtime

Started by Radiant, Tue 03/12/2013 22:07:47

Previous topic - Next topic

Radiant

Is it possible to detect the game's actual framerate at runtime? I'm referring to the value shown on-screen with a Debug(4,1) call, except that I want to store it in a variable; I'm not referring to the target framerate found by GetGameSpeed().

My goal here is to automatically suspend a CPU-heavy routine if and when the game detects itself slowing down.

Khris

I can't find anything other than using the seconds of DateTime, that will give you a pretty low resolution, but should suffice to roughly detect the framerate.

monkey0506

As Khris states, AGS doesn't have a more precise timing mechanic than DateTime.RawTime, but you could track the exact FPS of the previous 1 second by simply updating a variable in rep_ex_always:

Code: ags
int fpsCounter = 0;
int lastFPS = 0;
DateTime *lastTime; // = null

function repeatedly_execute_always()
{
  DateTime *now = DateTime.Now;
  if (lastTime == null) lastTime = now;
  if (lastTime.RawTime == now.RawTime) fpsCounter++;
  else
  {
    lastFPS = fpsCounter;
    lastTime = now;
    fpsCounter = 1;
  }
}


You could cache the results from this in a small array to get a blended average which may more accurately reflect minor changes, but the average itself would still only update once per second (as that's the only way to tell in-game that real-world time has actually passed).

SMF spam blocked by CleanTalk