0

目前,当我在 d-pad(sdl 中的帽子)上按住一个方向时,我的代码只执行一次,这是我的代码:

while (SDL_PollEvent( &event )) 
{
    switch (event.type)
    {
    case SDL_QUIT:
        exitapp = 1;
        break;
    case SDL_KEYDOWN:
        if (event.key.keysym.sym == SDLK_ESCAPE) 
        {
            exitapp = 1;
            // find other keys here: http://sdl.beuc.net/sdl.wiki/SDLKey
        }
        game->KeyDown( event.key.keysym.scancode );
        break;
    case SDL_KEYUP:
        game->KeyUp( event.key.keysym.scancode );
        break;
    case SDL_MOUSEMOTION:
        game->MouseMove( event.motion.x, event.motion.y );
        break;
    case SDL_MOUSEBUTTONUP:
        game->MouseUp( event.button.button );
        break;
    case SDL_MOUSEBUTTONDOWN:
        game->MouseDown( event.button.button );
        break;
    case SDL_JOYHATMOTION:  /* Handle Hat Motion */
        if ( event.jhat.value & SDL_HAT_UP )
        {
            game->HatUp();
        }

        if ( event.jhat.value & SDL_HAT_LEFT )
        {
            game->HatLeft();
        }

        if ( event.jhat.value & SDL_HAT_RIGHT )
        {
            game->HatRight();
        }
        if (event.jhat.value & SDL_HAT_DOWN )
        {
            game->HatDown();
        }
        break;
    default:
        // more info on events in SDL: http://sdl.beuc.net/sdl.wiki/SDL_Event
        break;
    }
}

理想情况下,当我按下 d-pad 时,它会继续调用 game->HatUp()。

我怎样才能让它工作?

4

1 回答 1

0

在事件轮询之间保存帽子状态。

像这样的东西:

map< Uint8, bool > HatStatus; 

while( running )
{
    while (SDL_PollEvent( &event )) 
    {
        switch (event.type)
        {
        ...
        case SDL_JOYHATMOTION:  /* Handle Hat Motion */
            HatStatus[ SDL_HAT_UP    ] = ( true == event.jhat.value & SDL_HAT_UP    );
            HatStatus[ SDL_HAT_LEFT  ] = ( true == event.jhat.value & SDL_HAT_LEFT  );
            HatStatus[ SDL_HAT_RIGHT ] = ( true == event.jhat.value & SDL_HAT_RIGHT );
            HatStatus[ SDL_HAT_DOWN  ] = ( true == event.jhat.value & SDL_HAT_DOWN  );
            break;
        ...
        }
    }

    if( HatStatus[ SDL_HAT_UP    ] )    game->HatUp();
    if( HatStatus[ SDL_HAT_LEFT  ] )    game->HatLeft();
    if( HatStatus[ SDL_HAT_RIGHT ] )    game->HatRight();
    if( HatStatus[ SDL_HAT_DOWN  ] )    game->HatDown();

    ...
}
于 2013-01-07T16:01:14.570 回答