Framerate capping issues (SDL)

Started by
1 comment, last by Rob Loach 18 years, 7 months ago
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.
Advertisement
To cap your frame rate you basically do this:

while(frames are being blitted){ start frame timer; do event handling, calculations, blitting, etc. while(frame timer's time < 1000/FPS) {  wait; }}


I think it's because you forgot to start the timer at the beginning of each frame.

Did you look over tutorial 10?

Learn to make games with my SDL 2 Tutorials

The SDL_gfx has a set of methods to provide framerate capping within SDL applications. If you take a look at the source of SDL_framerate.c, you'll see that it really is quite simple. It goes something like this:

    manager->framecount++;    current_ticks = SDL_GetTicks();    target_ticks = manager->lastticks + (Uint32) ((float) manager->framecount * manager->rateticks);    if (current_ticks <= target_ticks) {	the_delay = target_ticks - current_ticks;	SDL_Delay(the_delay);    } else {	manager->framecount = 0;	manager->lastticks = SDL_GetTicks();    }

This completely depends on what API you're using, but if the API provides a TimeGetTime and a thread delay feature, you'll be set. SDL_gfx is released under the LGPL so its source is quite flexible.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement