Skip to content

Instantly share code, notes, and snippets.

@izzyaxel
Created December 8, 2017 18:50
Show Gist options
  • Save izzyaxel/158e8a8d6c8143202cd39531f88940a6 to your computer and use it in GitHub Desktop.
Save izzyaxel/158e8a8d6c8143202cd39531f88940a6 to your computer and use it in GitHub Desktop.
void Timer::init(uint32_t targetRate)
{
if(targetRate <= 0) this->frameTime = 0;
else this->frameTime = MILLI / targetRate;
this->prevTime = SDL_GetTicks();
this->curTime = SDL_GetTicks();
}
void Timer::mark()
{
this->prevTime = this->curTime;
this->curTime = SDL_GetTicks();
this->deltaTime = this->curTime - this->prevTime;
}
bool Timer::sleep()
{
if(this->frameTime <= 0) return true;
uint32_t sleepTime = SDL_GetTicks() - this->curTime;
if(sleepTime < this->frameTime)
{
SDL_Delay(this->frameTime - sleepTime);
return true;
}
else return false;
}
struct Timer
{
void init(uint32_t targetRate);
void mark();
bool sleep();
inline void setFrameTime(uint32_t targetRate){this->frameTime = MILLI / targetRate;}
float deltaTime = 0.0f;
private:
uint32_t prevTime = 0, curTime = 0, frameTime = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment