2007-07-15 16:36:04 +03:00
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
/*
|
|
|
|
* SDL_framerate.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
2007-07-15 16:36:04 +03:00
|
|
|
*/
|
2011-04-05 20:38:24 +03:00
|
|
|
|
2009-05-20 13:08:56 +03:00
|
|
|
#include "../stdafx.h"
|
2007-07-15 16:36:04 +03:00
|
|
|
#include "SDL_framerate.h"
|
2011-04-05 20:38:24 +03:00
|
|
|
#include <SDL.h>
|
2007-07-15 16:36:04 +03:00
|
|
|
|
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
FPSManager::FPSManager(int rate)
|
2007-07-15 16:36:04 +03:00
|
|
|
{
|
2011-04-05 20:38:24 +03:00
|
|
|
this->rate = rate;
|
|
|
|
this->rateticks = (1000.0 / (double) rate);
|
|
|
|
this->fps = 0;
|
2007-07-15 16:36:04 +03:00
|
|
|
}
|
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
void FPSManager::init()
|
2007-07-15 16:36:04 +03:00
|
|
|
{
|
2011-04-05 20:38:24 +03:00
|
|
|
this->lastticks = SDL_GetTicks();
|
2007-07-15 16:36:04 +03:00
|
|
|
}
|
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
void FPSManager::framerateDelay()
|
2007-07-15 16:36:04 +03:00
|
|
|
{
|
2011-04-05 20:38:24 +03:00
|
|
|
Uint32 currentTicks = SDL_GetTicks();
|
|
|
|
double diff = currentTicks - this->lastticks;
|
2007-07-15 16:36:04 +03:00
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
if (diff < this->rateticks) // FPS is higher than it should be, then wait some time
|
|
|
|
{
|
|
|
|
SDL_Delay(ceil(this->rateticks) - diff);
|
|
|
|
}
|
2007-07-15 16:36:04 +03:00
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
this->fps = ceil(1000. / (SDL_GetTicks() - this->lastticks));
|
|
|
|
this->lastticks = SDL_GetTicks();
|
2007-07-15 16:36:04 +03:00
|
|
|
}
|