如何正确使用SDL_MOUSEBUTTONDOWN?

How to use SDL_MOUSEBUTTONDOWN correctly?

我正在尝试使用鼠标的 x 和 y 坐标进行游戏,但我无法获取坐标。

这是我一直在阅读的活动:

typedef struct{
    Uint8 type;
    Uint8 button;
    Uint8 state;
    Uint16 x, y;
} SDL_MouseButtonEvent;

我需要这样的东西:

if(SDL_MouseButtonEvent.x >= 100)
{
//do something
}

我认为语法非常不同,因为我看到一些示例使用 SDL_MOUSEBUTTONDOWN。

编辑

抱歉,我知道这个问题可能不太清楚,但彼得的回答正是我所需要的,感谢您的宝贵时间。

如果您想首先拦截特定区域的点击,您需要轮询事件并检查 SDL_MouseButtonEvent。

while(running)
{
    while(SDL_PollEvent(&event)) // check to see if an event has happened
    {
        switch(event.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_MOUSEBUTTONDOWN: // if the event is mouse click
                if(event.mouse.x >= 100)  // check if it is in the desired area
                {
                    //do something    
                }
        }
    }
}

希望对您有所帮助。

快速精度:event.mouse.x 已被 event.motion.xevent.motion.xrel 替换,分别用于绝对和相对鼠标移动,int