Skip to content

Instantly share code, notes, and snippets.

@StarJade-Park
Created January 6, 2020 11:01
Show Gist options
  • Save StarJade-Park/17e3e30dcd1964ab342565ebf3bd53cb to your computer and use it in GitHub Desktop.
Save StarJade-Park/17e3e30dcd1964ab342565ebf3bd53cb to your computer and use it in GitHub Desktop.
#include "DeltaTime.hpp"
Timer::Time Timer::s_MainTimer = Timer::Time();
// DeltaTime.h
#pragma once
#ifndef __DELTA_TIME_H__
#define __DELTA_TIME_H__
#include "profileapi.h"
// static class
class Timer final
{
private:
class Time
{
// friend로 Timer에게만 공개합니다.
friend Timer;
private:
Time()
{
reset();
}
~Time() = default;
void reset( )
{
QueryPerformanceFrequency(&m_CountTime);
QueryPerformanceCounter(&m_CurTime);
QueryPerformanceCounter(&m_PrevTime);
}
float update( )
{
QueryPerformanceCounter(&m_CurTime);
// delta time = current time - previous time
m_dDeltaTime = ( static_cast<double>(m_CurTime.QuadPart) - static_cast<double>(m_PrevTime.QuadPart) ) / static_cast<double>(m_CountTime.QuadPart);
m_fDeltaTime = static_cast<float>(m_dDeltaTime);
m_PrevTime = m_CurTime;
return m_fDeltaTime;
}
private:
LARGE_INTEGER m_CountTime;
LARGE_INTEGER m_CurTime;
LARGE_INTEGER m_PrevTime;
double m_dDeltaTime;
float m_fDeltaTime;
};
private:
static Time s_MainTimer;
public:
Timer() = delete;
~Timer() = delete;
static float DeltaTime()
{
return s_MainTimer.m_fDeltaTime;
}
static void Update()
{
s_MainTimer.update();
}
static void Reset()
{
s_MainTimer.reset();
}
};
#endif // !__DELTA_TIME_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment