SDL 未检测到箭头键

发布于 2024-08-27 11:34:06 字数 1645 浏览 10 评论 0原文

我正在学习 http://lazyfoo.net/SDL_tutorials/index.php 我被困在教程 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;
    }
}

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-09-03 11:34:07

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文