Advertisement Jump to content
Sign in to follow this  

Framerate capping issues (SDL)

This topic is 4876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[])



	Font Main;



		// All the game code will go in here


			char curFPS[64];

			sprintf(curFPS, "Current FPS:%f",(float)framecount/(fps.ReturnTicks()/1000.f));





			while(fps.ReturnTicks()<1000/FRAMERATE) // FRAMERATE is 85FPS





	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



	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; };


	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



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.

Share this post

Link to post
Share on other sites
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)

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

Did you look over tutorial 10?

Share this post

Link to post
Share on other sites
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:

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;
} 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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!