Skip to content

Instantly share code, notes, and snippets.

@rakin406
Created December 4, 2023 14:58
Show Gist options
  • Select an option

  • Save rakin406/6ae4cfe166d2ac8593eb6a01b5a5e431 to your computer and use it in GitHub Desktop.

Select an option

Save rakin406/6ae4cfe166d2ac8593eb6a01b5a5e431 to your computer and use it in GitHub Desktop.
C++ clock and delta time
#include "clock.h"
#include <chrono>
Clock::Clock() : lastTick { std::chrono::steady_clock::now() }, deltaTime { 0 }
{
}
void Clock::tick()
{
using namespace std::chrono;
auto now { steady_clock::now() };
duration<float> elapsed { lastTick - now };
deltaTime = elapsed.count();
lastTick = now;
}
float Clock::getDeltaTime() const { return deltaTime; }
#ifndef CLOCK_H
#define CLOCK_H
#include <chrono>
class Clock
{
public:
Clock();
/**
* @brief Updates clock one tick.
*/
void tick();
/**
* @brief Gets delta time.
* @return delta time.
*/
float getDeltaTime() const;
private:
std::chrono::steady_clock::time_point lastTick {};
float deltaTime {};
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment