2
0

fpsTracker.cpp 3.4 KB

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