Calculating Frame Rate

I am having trouble calculating frameRate. Can someone please help me with my code. :/
I am getting 118,521 FPS....does this sound right or am I doing something Wrong? Please help
i am trying to construct a game loop and would like to limit the fps to 60 or 30.. I am trying to make a pong game as my first SDL game. Thanks for your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    double currentTime = 0;
    double lasttime    = 0;
    
    int frameCount = 0;
    int frameRate = 0;
    
    while (event.type != SDL_QUIT) {
        frameCount++;
        
        SDL_PollEvent(&event);
        
        currentTime = SDL_GetTicks();

        
        if (currentTime > lasttime + 1000) {

            lasttime = currentTime;
            frameRate = frameCount;
            frameCount = 0;
           
        }
    }
Last edited on
The framerate is Frames Per Second, which is the number of times you redraw your screen every second.

The simplest way to do this is

1
2
3
4
5
while (...)
{
  draw();
  double FPS = 1000.0 / SDL_GetTicks();
}

You will typically want to average the last N values you get to get a more useful FPS value.

Good luck!

Topic archived. No new replies allowed.