Last active
October 25, 2015 21:16
-
-
Save shakesoda/11238165 to your computer and use it in GitHub Desktop.
simple high resolution timer w/SDL2
This file contains 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
/* USAGE: | |
* Timer foo; // Start the timer | |
* const TimeData &time = foo.touch(); // Update and return TimeData (delta being the time since last touch) | |
* const TimeData &time_peek = foo.peek(); // returns current TimeData only updating the current time (does not reset!) | |
* SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION | |
* | |
* I haven't actually tested peek, but it looks right... */ | |
#pragma once | |
#include <SDL.h> | |
struct TimeData { | |
Uint64 then; | |
Uint64 now; | |
Uint64 frequency; | |
double delta; | |
}; | |
class Timer { | |
public: | |
Timer() { | |
m_time.now = SDL_GetPerformanceCounter(); | |
m_time.then = m_time.now; | |
m_time.frequency = SDL_GetPerformanceFrequency(); | |
} | |
// Note: delta is since last touch OR peek | |
const TimeData &touch() { | |
m_time.then = m_time.now; | |
m_time.now = SDL_GetPerformanceCounter(); | |
m_time.delta = double(m_time.now - m_time.then) / double(m_time.frequency); | |
return m_time; | |
} | |
// Note: delta is since last touch | |
const TimeData &peek() { | |
m_time.now = SDL_GetPerformanceCounter(); | |
m_time.delta = double(m_time.now - m_time.then) / double(m_time.frequency); | |
return m_time; | |
} | |
protected: | |
TimeData m_time; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment