소스 검색

Merge pull request #1133 from AtomicGameEngine/JME-ATOMIC-FRAMERATE

Adding current fps to engine subsystem
JoshEngebretson 9 년 전
부모
커밋
cd6bd0af29
2개의 변경된 파일30개의 추가작업 그리고 4개의 파일을 삭제
  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"
 
+// ATOMIC BEGIN
+static const float ENGINE_FPS_UPDATE_INTERVAL = 0.5f;
+// ATOMIC END
 
 #if defined(_MSC_VER) && defined(_DEBUG)
 // From dbgint.h
@@ -119,7 +122,10 @@ Engine::Engine(Context* context) :
     audioPaused_(false),
     // ATOMIC BEGIN    
     paused_(false),
-    runNextPausedFrame_(false)
+    runNextPausedFrame_(false),
+    fpsTimeSinceUpdate_(ENGINE_FPS_UPDATE_INTERVAL),
+    fpsFramesSinceUpdate_(0),
+    fps_(0)
     // ATOMIC END
 {
     // Register self as a subsystem
@@ -164,7 +170,7 @@ Engine::Engine(Context* context) :
 #endif
 
     SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(Engine, HandleExitRequested));
-    // ATOMIC BEGIN    
+    // ATOMIC BEGIN        
     SubscribeToEvent(E_PAUSERESUMEREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseResumeRequested));
     SubscribeToEvent(E_PAUSESTEPREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseStepRequested));
     // ATOMIC END
@@ -513,8 +519,17 @@ void Engine::RunFrame()
         Update();
 
     }
-// ATOMIC END
 
+    fpsTimeSinceUpdate_ += timeStep_;
+    ++fpsFramesSinceUpdate_;
+    if (fpsTimeSinceUpdate_ > ENGINE_FPS_UPDATE_INTERVAL)
+    {
+        fps_ = (int)(fpsFramesSinceUpdate_ / fpsTimeSinceUpdate_);
+        fpsFramesSinceUpdate_ = 0;
+        fpsTimeSinceUpdate_ = 0;
+    }
+
+// ATOMIC END
 
     Render();
     ApplyFrameLimit();

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

@@ -128,9 +128,12 @@ public:
     bool IsPaused() const { return paused_; }
     /// Return whether to run the next frame even if paused (for stepping frame by frame)
     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;
+
     // ATOMIC END
 
 private:
@@ -182,6 +185,14 @@ private:
     bool paused_;
     /// Whether to run the next frame even if paused (for stepping frame by frame)
     bool runNextPausedFrame_;
+
+    /// Time since last fps display update
+    float fpsTimeSinceUpdate_;
+    /// Frames since last fps display update
+    float fpsFramesSinceUpdate_;
+    /// Calculated fps
+    unsigned fps_;
+
     // ATOMIC END
    
 };