Created
December 8, 2017 18:50
-
-
Save izzyaxel/158e8a8d6c8143202cd39531f88940a6 to your computer and use it in GitHub Desktop.
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
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; | |
} |
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
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