fpsTracker.cpp

Engine/source/util/fpsTracker.cpp

More...

Public Variables

Public Functions

DefineEngineFunction(resetFPSTracker , void , () , "()" "@brief Reset FPS stats (fps::)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "@ingroup Game" )

Detailed Description

Public Variables

FPSTracker gFPS 

Public Functions

DefineEngineFunction(resetFPSTracker , void , () , "()" "@brief Reset FPS stats (fps::)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "@ingroup Game" )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "util/fpsTracker.h"
 25#include "console/console.h"
 26#include "console/engineAPI.h"
 27
 28FPSTracker gFPS;
 29
 30FPSTracker::FPSTracker()
 31{
 32   mUpdateInterval = 0.25f;
 33   reset();
 34}
 35
 36void FPSTracker::reset()
 37{
 38   fpsNext         = (F32)Platform::getRealMilliseconds()/1000.0f + mUpdateInterval;
 39
 40   fpsRealLast       = 0.0f;
 41   fpsReal           = 0.0f;
 42   fpsRealMin        = 0.000001f; // Avoid division by zero.
 43   fpsRealMax        = 1.0f;
 44   fpsVirtualLast    = 0.0f;
 45   fpsVirtual        = 0.0f;
 46   fpsFrames         = 0;
 47}
 48
 49void FPSTracker::update()
 50{
 51   const F32 alpha  = 0.07f;
 52   F32 realSeconds    = (F32)Platform::getRealMilliseconds()/1000.0f;
 53   F32 virtualSeconds = (F32)Platform::getVirtualMilliseconds()/1000.0f;
 54
 55   fpsFrames++;
 56   if (fpsFrames > 1)
 57   {
 58      fpsReal    = fpsReal*(1.0-alpha) + (realSeconds-fpsRealLast)*alpha;
 59      fpsVirtual = fpsVirtual*(1.0-alpha) + (virtualSeconds-fpsVirtualLast)*alpha;
 60
 61      if( fpsFrames > 10 ) // Wait a few frames before updating these.
 62      {
 63         // Update min/max.  This is a bit counter-intuitive, as the comparisons are
 64         // inversed because these are all one-over-x values.
 65
 66         if( fpsReal > fpsRealMin )
 67            fpsRealMin = fpsReal;
 68         if( fpsReal < fpsRealMax )
 69            fpsRealMax = fpsReal;
 70      }
 71   }
 72
 73   fpsRealLast    = realSeconds;
 74   fpsVirtualLast = virtualSeconds;
 75
 76   // update variables every few frames
 77   F32 update = fpsRealLast - fpsNext;
 78   if (update > 0.5f)
 79   {
 80      F32 delta = realSeconds - fpsNext;
 81      Con::setVariable( "fps::frameDelta",avar("%g", delta));
 82      Con::setVariable( "fps::real",      avar( "%4.1f", 1.0f / fpsReal ) );
 83      Con::setVariable( "fps::realMin",   avar( "%4.1f", 1.0f / fpsRealMin ) );
 84      Con::setVariable( "fps::realMax",   avar( "%4.1f", 1.0f / fpsRealMax ) );
 85      Con::setVariable( "fps::virtual",   avar( "%4.1f", 1.0f / fpsVirtual ) );
 86
 87      if (update > mUpdateInterval)
 88         fpsNext  = fpsRealLast + mUpdateInterval;
 89      else
 90         fpsNext += mUpdateInterval;
 91   }
 92}
 93
 94DefineEngineFunction( resetFPSTracker, void, (), , "()"
 95   "@brief Reset FPS stats (fps::)\n\n"
 96   "@ingroup Game")
 97{
 98   gFPS.reset();
 99}
100