SDL不检测箭头键

时间:2010-04-01 01:43:24

标签: c++ sdl keypress

我正在http://lazyfoo.net/SDL_tutorials/index.php处理SDL教程,而且我正在使用教程8,我正在使用按键操作。

我正在使用以下代码:

//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;
    }
}

如果工作正常,则达到默认情况,但是按下箭头键即可。我想知道是否有人能发现我做错了什么。

1 个答案:

答案 0 :(得分:1)

我发现了上面发布的代码中没有的错误。错误是对于箭头按键消息,我在打开字体之前使用了RenderText。到达发布的代码块时,字体已经打开,这就是该消息显示的原因。

相关问题