SDL:如何在鼠标左键按下时使用 case 语句获取鼠标运动

SDL: how to get the mouse motion while the left mouse button is down using case statement

我目前正在使用 SDL2,对它还很陌生。我正在尝试使用案例声明仅在按下鼠标左键时获取鼠标运动坐标。

最后,我需要能够单击一个对象并找出鼠标从该选定对象拖动了多少。

到目前为止,我已经能够让鼠标按下和鼠标移动分开工作,但不能同时工作。

这是我的鼠标按下事件代码:

void SDL::OnEvent(SDL_Event *_event)
{
Mallet mallet;

switch (_event->type)
{
case SDL_QUIT:
    m_running = false;
    break;

default:
    break;


case SDL_KEYUP:
    switch (_event->key.keysym.sym)
    {
    case SDLK_SPACE:
        if(m_playerTurn == 1)
            m_playerTurn = 2;
        else
            m_playerTurn = 1;

        std::cout<<"player turn = "<<m_playerTurn<<std::endl;
        break;

    }


case SDL_MOUSEBUTTONDOWN:

    switch(_event->button.button)
    {
    case SDL_BUTTON_LEFT:
        int x = _event->button.x;
        int y = _event->button.y;

        if(m_playerTurn == 1)
        {
            bool collision = checkCollision(x, y, m_player1->getTeamMallets(), mallet);
            if(collision)
                std::cout<<"collision with P1"<<std::endl;
        }

        if(m_playerTurn == 2)
        {
            bool collision = checkCollision(x, y, m_player2->getTeamMallets(), mallet);
            if(collision)
                std::cout<<"collision with P2"<<std::endl;
        }

        break;
    }
}

}

谁能帮忙。

非常感谢。

SDL_MOUSEBUTTONDOWN 上设置变量 click = true ans 保存 x,y 坐标, 在 SDL_MOUSEMOTION 检查点击是否 == true 并更新 x,y 坐标,
SDL_MOUSEBUTTONUP 上设置 click = false 并计算距离。

http://lazyfoo.net/tutorials/SDL/17_mouse_events/index.php