如何正确使用SDL_MOUSEBUTTONDOWN

How to use SDL_MOUSEBUTTONDOWN correctly?

本文关键字:MOUSEBUTTONDOWN SDL 何正确      更新时间:2023-10-16

我试图使用x和y坐标从鼠标的游戏,但我不能得到坐标。

这是我一直在阅读的事件:

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

我需要这样的东西:

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

我认为sintax是非常不同的,因为我看到了一些使用SDL_MOUSEBUTTONDOWN的例子。

编辑

我很抱歉,我知道这个问题可能不是很清楚,但是Peter的回答正是我需要的,谢谢你的时间。

快速精确:鼠标的绝对移动和相对移动分别用event.motion.xevent.motion.xrel代替event.mouse.x,均为int

如果你想拦截一个点击在一个特定的区域首先你需要轮询事件和检查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    
                }
        }
    }
}