Created
December 4, 2023 14:58
-
-
Save rakin406/6ae4cfe166d2ac8593eb6a01b5a5e431 to your computer and use it in GitHub Desktop.
C++ clock and delta time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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