Skip to content

Instantly share code, notes, and snippets.

@Rydgel
Created January 5, 2017 15:27
Show Gist options
  • Save Rydgel/55a8e59b773b820baa680e5aff2ef7cd to your computer and use it in GitHub Desktop.
Save Rydgel/55a8e59b773b820baa680e5aff2ef7cd to your computer and use it in GitHub Desktop.
FPS Timer
#include "Timer.hpp"
void Timer::init()
{
m_lastLoopTime = getTime();
m_timeCount = 0.0;
m_fps = 0;
m_fpsCount = 0;
m_ups = 0;
m_upsCount = 0;
}
const double Timer::getTime()
{
auto duration = Clock::now().time_since_epoch();
auto getMs = std::chrono::duration_cast<MilliSeconds>(duration).count();
return getMs / 1000.0;
}
float Timer::getDelta()
{
double time = getTime();
float delta = (float) (time - m_lastLoopTime);
m_lastLoopTime = time;
m_timeCount += delta;
return delta;
}
void Timer::updateFPS()
{
m_fpsCount ++;
}
void Timer::updateUPS()
{
m_upsCount ++;
}
void Timer::update()
{
if (m_timeCount > 1.0) {
m_fps = m_fpsCount;
m_fpsCount = 0;
m_ups = m_upsCount;
m_upsCount = 0;
m_timeCount -= 1.0;
}
}
int Timer::getFPS()
{
return m_fps > 0 ? m_fps : m_fpsCount;
}
int Timer::getUPS()
{
return m_ups > 0 ? m_ups : m_upsCount;
}
#ifndef VOXELS_TIMER_HPP
#define VOXELS_TIMER_HPP
#include <iostream>
using Clock = std::chrono::high_resolution_clock;
using MilliSeconds = std::chrono::milliseconds;
class Timer
{
private:
double m_lastLoopTime;
float m_timeCount;
int m_fps;
int m_fpsCount;
int m_ups;
int m_upsCount;
public:
Timer() = default;
~Timer() = default;
void init();
const double getTime();
float getDelta();
void updateFPS();
void updateUPS();
void update();
int getFPS();
int getUPS();
};
#endif //VOXELS_TIMER_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment