tags:

views:

36

answers:

1

I am working through the SDL tutorials over at http://lazyfoo.net/SDL_tutorials/index.php and I'm stuck on tutorial 8 where I'm working with key presses.

I'm using the following code:

//Our Main application loop
while(!quit){
    if(SDL_PollEvent(&curEvents)){
        if(curEvents.type == SDL_QUIT){
            quit = true;
        }

        //If a key was pressed
        if( curEvents.type == SDL_KEYDOWN )
        {
            //Set the proper message surface
            switch( curEvents.key.keysym.sym )
            {
                case SDLK_UP: 
                    message = upMessage; 
                    break;
                case SDLK_DOWN: 
                    message = downMessage; 
                    break;
                case SDLK_LEFT: 
                    message = leftMessage; 
                    break;
                case SDLK_RIGHT: 
                    message = rightMessage; break;
                default:
                    message = TTF_RenderText_Solid(font, "Unknown Key", textColor); 
                    break; 
            }
        }
    }

    if( message != NULL )
    {
        //Apply the background to the screen
        applySurface( 0, 0, background, screen );

        //Apply the message centered on the screen
        applySurface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );

        //Null the surface pointer
        message = NULL;
    }

    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

Where works fine, the default case is reached, for everything BUT pressing the arrow keys. I was wondering if someone could spot what I'm doing wrong.

+1  A: 

I discovered the error which was not in the code posted above. The error was that for the arrow keypress messages, I used RenderText before the font was opened. By the time the posted code block was reached, font had already been opened and is the reason why that message shows.

Scott