SDLgamer Menu‎ > ‎

Lesson 6. The animation in a window


The previous Lesson no. 5 , we put a delay function in while loop , like this :
while(bRun) {
    SDL_Delay(20) ;
    }
This tells the computer : Come back to this program after 20ms.
Since we didn't put in too many heavy codes in this while loop  , so roughly speaking ,
it almost runs 50 times for 1 sec (1000ms / 20ms = 50).    That means , the screen been
updated 50 times per sec , the keyboard event been updated 50 times per sec.
So the Frame Per Sec is 50.

Ok , let's make the animation in a window.
At first , we need a series of pictures.   This example uses a little girl that comes from
a game "DATA EAST : Magical Drop 3" as demonstration.    Or you can use your
home made pictures.

 
 
 
 
 
 

Screen shot & program download. ATest_L6.zip 199k

Here's the source code.    Copy from Lesson 5 and just add a few codes.
 #include "SDL.h"

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

SDL_Event event;
bool bRun = 1;
int pidx = 0;
SDL_Surface *screen , *logo;
enum walk { justice1 , justice2 , justice3 , justice4
    , justice5 , justice6 , justice_No  };

SDL_Surface *anim[justice_No];
SDL_Rect logoRect , animRect;
logoRect.x = 100 ;
logoRect.y = 120 ;
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);

logo = SDL_LoadBMP("./sdllogo.bmp");
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");


while(bRun) {
    SDL_FillRect(screen , NULL , 0x221122);
    SDL_BlitSurface( logo , NULL , screen , &logoRect );
    SDL_BlitSurface( anim[pidx] , NULL , screen , &animRect );
    SDL_Flip(screen);
    SDL_Delay( 200 );
    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:
                        logoRect.y = logoRect.y - 10;
                        break;
                    case SDLK_DOWN:
                        logoRect.y = logoRect.y + 10;
                        break;
                    case SDLK_LEFT:
                        logoRect.x = logoRect.x - 10;
                        break;
                    case SDLK_RIGHT:
                        logoRect.x = logoRect.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;
}


enum walk { justice1 , justice2 , justice3 , justice4 , justice5 , justice6 , justice_No  };

This is one of the usage for enum.    See the table list :
 Namejustice1
justice2
justice3
justice4
justice5
justice6
justice_No
 Value 01
2
3
4
 56

By any chance , if insert a "justice7" between "justice6" and "justice_No" , the value of "justice7" will be 6 , and "justice_No" change to 7 automatically.     So , when you write the program code of a for loop , say :
for( int i = 0 ; i < justice_No ; i++ ) Load_Image( i );

Even you new add an image , you don't need to modify the code in this part.

http://www.daniweb.com/forums/thread1767.html#

 An enumeration type is a user defined type that enables the user to define the range of values for the type. Named constants are used to represent the values of an enumeration, for example:

enum weekday {monday, tuesday, wednesday, thursday, friday, saturday, sunday};
weekday currentDay = wednesday;
if(currentDay==tuesday){
// do something
}



SDL_Surface *anim[justice_No];
In here , if we fill in 6 instead of "justice_No" , then we have to modify it every time when
we add new picture for it.

SDL_Rect logoRect , animRect;
animRect uses for coordinate of anim[ ] .

anim[5] = SDL_LoadBMP("./anim/justice6.bmp");
Loading the picture into anim[ ].

SDL_BlitSurface( anim[pidx] , NULL , screen , &animRect );
Blit a picture on location animRect.   pidx indicates which one in anim[ ].

pidx++;
if(pidx >= justice_No) pidx = 0;

The value of pidx should between 0 and 5.    Set this condition to make it happen.

Lesson 6 END

Looks like the program code become bigger & bigger since Lesson 1.
Maybe we should simplify it a little bit , how do you think?

Then , take the SDLlogo out , and use keyboard to control the anim[ ] !


bdragon
Copyright © 2009 bdragon All rights reserved.
ċ
Bdragon Ho,
Aug 7, 2009, 9:34 PM
Comments