Frame Per Second Too High

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <sstream>
#include <SDL.h>
#include <SDL_ttf.h>
#include "KD_Surface.h"
#include "KD_Message.h"
#include "KD_Sound.h"
#include "KD_Timer.h"

void event_handler();

KD_TIMER fps;
KD_TIMER update;

int main(int argc,char *argv[])
{
	if( initialization() == false )
	{
		return 1;
	}

	//Frame Calculation
	int frame = 0;

	//Start Update Timer
	update.start();
	//Start FPS Timer
	fps.start();

	//Background Of Game
	SDL_Surface *background = NULL;
	background = load_image("background.jpg");
	
	apply_surface(0,0,background,screen);

	//Update The Screen
	if(SDL_Flip(screen) == -1)
	{
		return false;
	}

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				//Quit the program
				quit = true;
			}
		}

		//Increment Frame
		frame++;

		//If a second has passed since the caption was last updated
		if( update.get_ticks() > 1000 )
		{
			//The frame rate as a string
			std::stringstream caption;

			//Calculate the frames per second and create the string
			caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000 );

			//Reset the caption
			SDL_WM_SetCaption( caption.str().c_str(), NULL );

			//Restart the update timer
			update.start();    

		}
	}

	clean_up();

	return 0;
}


//The Ultimate Event Handler !
void event_handler()
{

}



Plz check the code,It shows about 1 million fps in the caption,which make me worried.Does the algorithm seem right ?
The loop doesn't do much so the fps should be pretty high. Try putting a SDL_Delay(100); on line 51 and if you get about 10 fps it is correct.
Nice,thanks, but why did it work ?
closed account (zwA4jE8b)
because your computer is iterating through that loop as fast as it can, and that is very very fast
Your original program is running as fast as the computer will let it, which is very very fast.

By adding SDL_Delay(100), you are telling the computer to wait for 100 milliseconds before continuing, thus slowing the framerate.
If the frame++ means increasing frame;will it eventually get out of the limitation of an int ?
closed account (zwA4jE8b)
make it an unsigned int. eventually a limit will be reached but not anytime soon
or a Uint64
Last edited on
Plz take a look at this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
int main(int argc,char *argv[])
{
	if( initialization() == false )
	{
		return 1;
	}

	//Background Of Game
	SDL_Surface *background = NULL;
	background = load_image("background.jpg");	



	//Apply Background
	apply_surface(0,0,background,screen);

	//Apply Message

	//Update The Screen
	if(SDL_Flip(screen) == -1)
	{
		return false;
	}

	//Start Update Timer
	fps_upd.start();
	//Start FPS_Calc
	fps_calc.start();
	//Start FPS_Reg
	fps_reg.start();

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				//Quit the program
				quit = true;
			}
		}

		//Increment Frame
		frame++;

		//Regulate Frame Rate
		if(fps_reg.get_ticks() < ( 1000 / FRAMES_PER_SECOND ) )
		{
			//Sleep The Remaining Frame Time
			SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps_reg.get_ticks() );
		}

		//If a second has passed since the caption was last updated
		if( fps_upd.get_ticks() > 1000 )
		{
			//The frame rate as a string
			std::stringstream caption;

			//Calculate the frames per second and create the string
			caption << "Average Frames Per Second: " << frame / ( fps_calc.get_ticks() / 1000 );

			//Reset the caption
			SDL_WM_SetCaption( caption.str().c_str(), NULL );

			//Restart the update timer
			fps_upd.start();    

		}

		fps_reg.start();
	}

	clean_up();

	return 0;
}


I've deleted the SDL_Delay above,and now I regulated the frame rate.
I set const int FRAMES_PER_SECOND = 20; and now it's runnning at 20fps (seems fine).
Plz check at the code and check for any error.If everything's fine,I'd go to the next step.
Last edited on
I would recommend using the hi-res timer for your platform, or if you're going for multi-platform, making a wrapper around platform specific hi-res timer functionality.

Windows has QueryPerformanceFrequency and QueryPerformanceCounter you can use to calculate FPS and the time it takes to render a scene.

Linux has clock_getres and clock_gettime.

From either, you should be able to calculate the performance of your app without worrying about rollover on an integer counter.
Topic archived. No new replies allowed.