SDLgamer Menu‎ > ‎

Lesson 5. Use your keyboard to move the image in a window

From Lesson 4 , we already be able to use computer doing some interesting job , such as
free body simulation(with gravity) or make an object move alone a track described by a position
function.    For example , assume an object(represented by an image) locates at (x,y) point.   
When apply the gravity to it , then its height y should be :
v = v + 1
y = y + v
So , the gravity and time constant are all 1 in here.    Since the PC screen up side down , falling down is +1.    When you leave this two lines in a loop , the y value of the object will go larger and larger by time.

Back to the lesson.
For a gamer , just like those driver they like to use steering wheel and manual gear to control a
vehicle , we will like to use a keyboard , mouse , or gamepad to control something , anything.   
That makes life interesting !

This time , we add some codes so the window can be controlled by keyboard.

You can copy the program code in your project , or download this zip file.
ATest.zip 184k

        
 #include "SDL.h"

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

SDL_Event event;
bool bRun = 1;
SDL_Surface *screen , *logo;
SDL_Rect logoRect;
logoRect.x = 100 ;
logoRect.y = 120 ;

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");


while(bRun) {
    SDL_FillRect(screen , NULL , 0x221122);
    SDL_BlitSurface( logo , NULL , screen , &logoRect );
    SDL_Flip(screen);
    SDL_Delay( 20 );
    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;
}


Let's see what these program codes described.

bool bRun = 1;
Declare a bool variable here , later we can use it for running / stop a while loop.

while(bRun) { }
This while loop will not end until bRun = 0.

SDL_FillRect(screen , NULL , 0x221122);
SDL_BlitSurface( logo , NULL , screen , &logoRect );

Fill the window with color "0x221122" by SDL_FillRect before blit surface.    So
there's no image residue like Lesson 4.

while( SDL_PollEvent( &event ) ){ }
Check SDL document :
Polls for currently pending events, and returns 1 if there are any pending events, or 0 if there are none available.

So , it'll enter this while loop if there're any pending events.

Try these two syntax  to  replace  while( ) loop  :
    1. if (
SDL_PollEvent( &event ) ) { }
    2.
SDL_PollEvent( &event ) ;


Observe what's the difference between them.

switch( event.type ){
    case SDL_KEYDOWN:
        break;
    case SDL_QUIT:
        bRun = 0 ;
        break;
    default:
        break;
    }
There's a table about event in the document.   In here , we got :
 SDL_KEYDOWN
 event.type indicate to this case as the key been pressed down.
 SDL_SDL_QUIT event.type indicate to this case as click on close windows(X)
Watch out the "break;" command , if you missed it , it won't stop until next break.

switch( event.key.keysym.sym ){
                    case SDLK_UP:
                        logoRect.y = logoRect.y - 10;
                        break;
                    case SDLK_DOWN:
                    case SDLK_LEFT:
                        break;
                    case SDLK_RIGHT:
                        logoRect.x = logoRect.x + 10;
                        break;
                    case SDLK_ESCAPE:
                        bRun = 0 ;
                        break;
                    default:
                         break;
    }
When a key been pressed , the number of the key will show in event.key.keysym.sym.    Just like other
program , SDL use a name to present that number (see c/c++ enum).
Here , SDLK_UP indicate the arrow key : UP.
Since the up-left corner been defined as origin (0,0) , so UP key should decrease the y value of logo , while
RIGHT key increase the x value of logo.(see Lesson 4)
When ESC key been pressed , bRun = 0 , means GameOVER !

Lesson 5 END

This is a very good start about controlling an object !
Next Lesson , will show you how to make the animation !

Before that , let's turn off the console window when run the program.
Select Project -> Project Options (Alt + P) -> Click on "Win32 GUI" -> OK
Select Execute -> Rebuild All (Ctrl + F11)



bdragon
Copyright © 2009 bdragon All rights reserved.
ċ
ATest.zip
(184k)
Bdragon Ho,
Aug 5, 2009, 5:44 PM
Comments