Browse Source

Adding current fps to engine subsystem

Josh Engebretson 9 years ago
parent
commit
b6191dfb69
2 changed files with 30 additions and 4 deletions
  1. 18 3
      Source/Atomic/Engine/Engine.cpp
  2. 12 1
      Source/Atomic/Engine/Engine.h

+ 18 - 3
Source/Atomic/Engine/Engine.cpp

@@ -72,6 +72,9 @@
 
 
 #include "../DebugNew.h"
 #include "../DebugNew.h"
 
 
+// ATOMIC BEGIN
+static const float ENGINE_FPS_UPDATE_INTERVAL = 0.5f;
+// ATOMIC END
 
 
 #if defined(_MSC_VER) && defined(_DEBUG)
 #if defined(_MSC_VER) && defined(_DEBUG)
 // From dbgint.h
 // From dbgint.h
@@ -119,7 +122,10 @@ Engine::Engine(Context* context) :
     audioPaused_(false),
     audioPaused_(false),
     // ATOMIC BEGIN    
     // ATOMIC BEGIN    
     paused_(false),
     paused_(false),
-    runNextPausedFrame_(false)
+    runNextPausedFrame_(false),
+    fpsTimeSinceUpdate_(ENGINE_FPS_UPDATE_INTERVAL),
+    fpsFramesSinceUpdate_(0),
+    fps_(0)
     // ATOMIC END
     // ATOMIC END
 {
 {
     // Register self as a subsystem
     // Register self as a subsystem
@@ -164,7 +170,7 @@ Engine::Engine(Context* context) :
 #endif
 #endif
 
 
     SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(Engine, HandleExitRequested));
     SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(Engine, HandleExitRequested));
-    // ATOMIC BEGIN    
+    // ATOMIC BEGIN        
     SubscribeToEvent(E_PAUSERESUMEREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseResumeRequested));
     SubscribeToEvent(E_PAUSERESUMEREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseResumeRequested));
     SubscribeToEvent(E_PAUSESTEPREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseStepRequested));
     SubscribeToEvent(E_PAUSESTEPREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseStepRequested));
     // ATOMIC END
     // ATOMIC END
@@ -513,8 +519,17 @@ void Engine::RunFrame()
         Update();
         Update();
 
 
     }
     }
-// ATOMIC END
 
 
+    fpsTimeSinceUpdate_ += timeStep_;
+    ++fpsFramesSinceUpdate_;
+    if (fpsTimeSinceUpdate_ > ENGINE_FPS_UPDATE_INTERVAL)
+    {
+        fps_ = (int)(fpsFramesSinceUpdate_ / fpsTimeSinceUpdate_);
+        fpsFramesSinceUpdate_ = 0;
+        fpsTimeSinceUpdate_ = 0;
+    }
+
+// ATOMIC END
 
 
     Render();
     Render();
     ApplyFrameLimit();
     ApplyFrameLimit();

+ 12 - 1
Source/Atomic/Engine/Engine.h

@@ -128,9 +128,12 @@ public:
     bool IsPaused() const { return paused_; }
     bool IsPaused() const { return paused_; }
     /// Return whether to run the next frame even if paused (for stepping frame by frame)
     /// Return whether to run the next frame even if paused (for stepping frame by frame)
     bool GetRunNextPausedFrame() const { return runNextPausedFrame_; }
     bool GetRunNextPausedFrame() const { return runNextPausedFrame_; }
-   
+
+    /// Return the engine's current framerate (updated at 1/2 second intervals)
+    unsigned GetFps() const { return fps_; }
     
     
     bool GetDebugBuild() const;
     bool GetDebugBuild() const;
+
     // ATOMIC END
     // ATOMIC END
 
 
 private:
 private:
@@ -182,6 +185,14 @@ private:
     bool paused_;
     bool paused_;
     /// Whether to run the next frame even if paused (for stepping frame by frame)
     /// Whether to run the next frame even if paused (for stepping frame by frame)
     bool runNextPausedFrame_;
     bool runNextPausedFrame_;
+
+    /// Time since last fps display update
+    float fpsTimeSinceUpdate_;
+    /// Frames since last fps display update
+    float fpsFramesSinceUpdate_;
+    /// Calculated fps
+    unsigned fps_;
+
     // ATOMIC END
     // ATOMIC END
    
    
 };
 };