Hello,
I'm trying to get a framerate regulation system working on my game.
At the moment, I've just got an empty window, but I'm putting the framerate into the window caption. Here's the code I've got in main.cpp:
Timer fps,update;
int main(int argc, char *argv[])
{
BoxRacer.Initialise();
BoxRacer.SetWindow(WINDOW_W,WINDOW_H,32,"BoxRacer","");
fps.CapFPS();
update.StartTimer();
fps.StartTimer();
Font Main;
Main.LoadFont("data/arial.ttf",12,255,255,0);
while(BoxRacer.IsRunning())
{
returnedtime=fps.ReturnTicks();
SDL_Flip(BoxRacer.MainWindow);
// All the game code will go in here
framecount++;
if(update.ReturnTicks()>1000)
{
char curFPS[64];
sprintf(curFPS, "Current FPS:%f",(float)framecount/(fps.ReturnTicks()/1000.f));
SDL_WM_SetCaption(curFPS,NULL);
update.StartTimer();
}
if(fps.IsCapped())
{
while(fps.ReturnTicks()<1000/FRAMERATE) // FRAMERATE is 85FPS
{
//wait
}
}
}
return 0;
}
And here's my Timer class:
// TimerClass.h
// A timer class for regulating frame rate
// Thanks to Lazy Foo' Productions
#ifndef _TIMER_H
#define _TIMER_H
class Timer
{
public:
Timer();
void StartTimer();
void StopTimer();
void PauseTimer();
void UnpauseTimer();
int ReturnTicks();
bool IsStarted() { return TimerStarted; };
bool IsPaused() { return TimerPaused; };
void CapFPS() { Capped=true; };
bool IsCapped() { return Capped; };
private:
bool Capped;
bool TimerPaused;
bool TimerStarted;
int TicksAtStart; // The number of ticks to which the timer has been initialised
int TicksAtPause; // The number of ticks at the point where the timer was paused
};
#endif
As the comments say, thanks to Lazy Foo' Productions for the timer code.
The problem I'm having is that the framerate won't cap. The number in the window caption changes every second, but it's erratic.
It'll often start at something like 350.2738495, then the next second it'll go to 353.4587392, then 358.1384759 and so on and so forth.
The framerate should cap at 85FPS.
What am I doing wrong? Is it because I've just got an empty window just now, but that shouldn't make a difference?
What I'm wanting is to have a capped framerate, and always be able to display the current framerate in the window title bar.
Thanks in advance,
ukdeveloper.