【问题标题】:SDL doesn't detect Arrow KeysSDL 未检测到箭头键
【发布时间】:2011-02-03 04:48:14
【问题描述】:

我正在通过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;
    }
}

在正常工作的地方,达到默认情况,除了按箭头键之外的所有内容。我想知道是否有人能发现我做错了什么。

【问题讨论】:

    标签: c++ sdl keypress


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2011-05-30
      相关资源
      最近更新 更多