Regulating FPS

In line 11 what is SDL_Getticks();
and in line 25 and 26 what is the purpose of these lines and is SDL_Delay used for slowing down the processing speed , finally what is the purpose of this controlling speed of frame per second i can't see the output other than a blank black screen
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
 int main(int argc, char** argv)
{
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_Surface *screen;
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
//      screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
        bool running = true;
        const int FPS = 30;
        Uint32 start;
        while(running) {
                start = SDL_GetTicks();
                SDL_Event event;
                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_QUIT:
                                        running = false;
                                        break;
                        }
                }
                //logic && render
 
 
                SDL_Flip(screen);
 
                if(1000/FPS > SDL_GetTicks()-start) {
                        SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
        }
        SDL_Quit();
        return 0;
}
Learn to read the documentation to see what the functions do.
http://www.libsdl.org/release/SDL-1.2.15/docs/html/index.html

In many games and simulations you want it to update at a certain speed. You don't want it to run too fast or too slow. You want a consistent speed that as far as possible is the same no matter what computer you use or how much other programs use the CPU. In the program above you don't draw anything so you will not notice the benefits of regulating the frame rate (except maybe that the CPU usage is lower compared to not using SDL_Delay at all).
Yes the link helps alot i will try to read the required functions but still can you give me an idea what SDL_GetTicks(); does and what does the formula in line 24 and 25 does or should i not learn what it does and just copy it as it is when i need it
SDL_GetTicks() returns the number of milliseconds since the SDL library initialization as an unsigned, 32-bit integer. This is made clear in the documentation Peter87 provided. Ctrl+F your query function.

This is it.

http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlgetticks.html

Since 1000 milliseconds = 1 second, and our FPS is supposed to be 30 in this case, we divide 1000 milliseconds by 30 frames in order to to get the number of milliseconds that should pass for each each individual frame. This number (different for each possible frame rate) is then compared with the number of milliseconds since the start of the event loop.

Since SDL_GetTicks() = ms since SDL_Init(SDL_INIT_EVERYTHING)

and

start = the number of milliseconds since SDL_Init as of the assignment at the beginning of the event loop (each time around)

SDL_GetTicks() - start = the number of milliseconds since this time around in the event loop.

If the number of milliseconds this time around in the event loop (this is where we would presumably render a frame of some animated nature, corresponding to some user input) are less than 1000/FPS, then the frame rendered too fast, and we wait until the rest of that period (1000/FPS-(SDL_GetTicks()-start)) before looping around the event loop again and rendering another frame.

But because this is just a demonstration of frame rate capping, we have nothing to render, and like Peter87 said, you will not immediately notice any benefits.
Last edited on
Topic archived. No new replies allowed.