Hi all,
To be able to support multiple platforms in the future, I've abstracted my Game Timer class.
Below is the result (just the headers/ interfaces).
When I had just one Timer class (win32 specific), it was a 'normal' (stack) member of my application class.
Meaning it is constructed automatically etc.
Now it is a std::unique_ptr, to give me control over creation for the requested platform.
I.e. if win32 mTimer -> std::make_unique (or reset) <Win32GameTimer> etc.
This all works fine, except for one point.
The windows message WM_ACTIVATE is called earlier then my Init, which sets up the Game timer, in this case win32.
Meaning the application will crash/ nullptr (mTimer).
I found a quick fix by simply adding a nullptr check and calling Start after the initialization.
case WM_ACTIVATE:
if(LOWORD(wParam) == WA_INACTIVE)
{
mAppPaused = true;
mTimer->Stop();
}
else
{
mAppPaused = false;
if(mTimer) mTimer->Start();
}
/** Setup and start GameTimer **/
if(pPlatform == USE_WIN32) mTimer = std::make_unique<Crealysm::COMMON::Win32GameTimer>();
mTimer->Start();
This works, but I'm just curious if there are better/ more elegant ways to solve this.
Any input is appreciated.
class IGameTimer
{
public:
IGameTimer();
virtual ~IGameTimer();
virtual float GetTotalTime() const = 0;
virtual float GetDeltaTime() const = 0;
virtual void Reset() = 0; // Call before message loop.
virtual void Start() = 0; // Call when unpaused.
virtual void Stop() = 0; // Call when paused.
virtual void Tick() = 0; // Call every frame.
protected:
double mSecondsPerCount;
double mDeltaTime;
bool mStopped;
// time tracking vars etc. are in platform specific/ inherited classes
};
class Win32GameTimer : public IGameTimer
{
public:
Win32GameTimer();
~Win32GameTimer();
// Abstracted: Win32 implementation
float GetTotalTime() const;
float GetDeltaTime() const;
void Reset(); // Call before message loop.
void Start(); // Call when unpaused.
void Stop(); // Call when paused.
void Tick(); // Call every frame.
private:
// Win32 specific
__int64 mBaseTime;
__int64 mPausedTime;
__int64 mStopTime;
__int64 mPrevTime;
__int64 mCurrTime;
};