/******************************************************************************
*	File		-	frame_counter.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/09/03 (y/m/d)
*	Mod Date	-	2021/09/03 (y/m/d)
*	Description	-	Tracks frame statistics
******************************************************************************/

#include "frame_counter.h"
#include <cstring>

namespace lunarium
{
    void FrameCounter::Reset(bool minimalTracking)
    {
        // Zero out the struct
        memset(&mFrameData, 0, sizeof(FrameData));
        mFrameData.LowestFPS = 10000;
        mbMinimalTracking = minimalTracking;
        mSecondTimer = 0.0;
        mTimer.Reset();
    }

    void FrameCounter::NewFrame()
    {
        double now = mTimer.GetElapsedSeconds();
        mSecondTimer += now;
        mFPSCount++;
        mFrameData.NumFrames++;
        mFrameData.TotalRunTime += now;
        mFrameData.LastFrameTime = now;

        if (mSecondTimer >= 1.0)
        {
            mFrameData.CurrentFPS = mFPSCount;
            mFPSCount = 0;
            mSecondTimer = 0.0;
        }

        if (!mbMinimalTracking)
        {
            if (mFrameData.CurrentFPS > mFrameData.HighestFPS)
            {
                mFrameData.HighestFPS = mFrameData.CurrentFPS;
            }

            if (mFrameData.CurrentFPS < mFrameData.LowestFPS)
            {
                mFrameData.LowestFPS = mFrameData.CurrentFPS;
            }

            mFrameData.AverageFrameTime = mFrameData.TotalRunTime / mFrameData.NumFrames;
            mFrameData.AverageFPS = mFrameData.NumFrames / mFrameData.TotalRunTime; // NOTE: Not 100% sure this is correct
        }

        mTimer.Reset();
    }

    const FrameData& FrameCounter::GetFrameData() const
    {
        return mFrameData;
    }
}