SDL Gamer

Search this site

SDLgamer Menu‎ > ‎

Lesson 7. Frame Per Second - Simplify Ver.

The intermediate courses may be too complicate for a beginner , so I rewrite some of
the lessons and focus on using SDL as simple as possible.    Then the program code
will be easier to read and modify by your own purpose.

For Lesson 7 , let's try how to control frame per second (FPS) for a windows program.
So , we can set in a while loop , say run 50 times in a second , or 50 FPS.
Which means , update the screen , process user events , and do the calculation for
50 times in a second.

Translate it to code :
int FPS = 50 ;

In other word , the interval of a frame is 1 sec / 50 FPS = 0.02 sec = 20 ms
Write this into program code :
int interval = 1 * 1000 / FPS ; // The unit of interval is ms.

There's a SDL internal tick running since we initialize SDL , and we can use a SDL function
 & an integer to get & store it :
int NextTick;
NextTick = SDL_GetTicks( ) + interval ;

Here's the function that used to set the program to assigned FPS :

 void FPS_Fn(void)
{
if ( NextTick > SDL_GetTicks( ) ) SDL_Delay( NextTick - SDL_GetTicks( ) );
    NextTick = SDL_GetTicks( ) + interval ;
return;
}

Put this function into a loop can set the program to a assigned FPS.

Here's the simplified program code.   And also the example file for downloading :
ATest_L7b.zip 201k
 #include "SDL.h"

int FPS = 50;    // Assign a FPS
int NextTick , interval ;

// Initialize FPS_Fn( )
void FPS_Initial(void) {
NextTick = 0 ;
interval = 1 * 1000 / FPS ;
return;
}


// Frame Per Second Function  , put this in a loop
void FPS_Fn(void) {
if ( NextTick > SDL_GetTicks( ) ) SDL_Delay( NextTick - SDL_GetTicks( ) );
NextTick = SDL_GetTicks( ) + interval ;
return;
}


int main(int argc, char *argv[])
{

SDL_Event event;
bool bRun = 1;
int pidx = 0 , aTick = 0;
SDL_Surface *screen ;
enum walk { justice1 , justice2 , justice3 , justice4
    , justice5 , justice6 , justice_No  };
SDL_Surface *anim[justice_No];
SDL_Rect animRect;
animRect.x = 300 ;
animRect.y = 220 ;

atexit(SDL_Quit);

if( SDL_Init(SDL_INIT_VIDEO) < 0 ) exit(1);

SDL_WM_SetCaption("SDL Window", NULL);

screen = SDL_SetVideoMode( 640 , 480 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);

anim[0] = SDL_LoadBMP("./anim/justice1.bmp");
anim[1] = SDL_LoadBMP("./anim/justice2.bmp");
anim[2] = SDL_LoadBMP("./anim/justice3.bmp");
anim[3] = SDL_LoadBMP("./anim/justice4.bmp");
anim[4] = SDL_LoadBMP("./anim/justice5.bmp");
anim[5] = SDL_LoadBMP("./anim/justice6.bmp");

FPS_Initial( );

while(bRun) {
    aTick++;
    SDL_FillRect(screen , NULL , 0x221122);
    SDL_BlitSurface( anim[pidx] , NULL , screen , &animRect );
    SDL_Flip(screen);
    FPS_Fn( );
    if( aTick%10 == 0 ) pidx++;
    if(pidx >= justice_No) pidx = 0;
    while( SDL_PollEvent( &event ) ){
        switch( event.type ){
            case SDL_KEYDOWN:
                switch( event.key.keysym.sym ){
                    case SDLK_UP:
                        animRect.y = animRect.y - 10;
                        break;
                    case SDLK_DOWN:
                        animRect.y = animRect.y + 10;
                        break;
                    case SDLK_LEFT:
                        animRect.x = animRect.x - 10;
                        break;
                    case SDLK_RIGHT:
                        animRect.x = animRect.x + 10;
                        break;
                    case SDLK_ESCAPE:
                        bRun = 0 ;
                        break;
                    default:
                        break;
                    } // switch( event.key.keysym.sym ){ END
                break; // case SDL_KEYDOWN: END
            case SDL_QUIT:
                bRun = 0;
                break;
            default:
                break;
            } // switch( event.type ){ END
        } // while( SDLK_PollEvent( &event ) ){ END
    }; // while(bRun) { END
    

return 0;
}



It's much easier , isn't it?
Everything are in one page !  Hahaha !

With the SDL_Delay function , you can save the CPU resources.
And the parameter FPS can control frame per second for your program.


bdragon
Copyright © 2009 bdragon All rights reserved.
Č
ċ
ď
Bdragon Ho,
Aug 12, 2009, 5:28 PM
Comments