简体   繁体   English

如何正确使用SDL_MOUSEBUTTONDOWN?

[英]How to use SDL_MOUSEBUTTONDOWN correctly?

I'm trying to use x and y coordinates from mouse for a game, but i can not get the coordinates. 我正在尝试使用鼠标的x和y坐标进行游戏,但是我无法获取坐标。

This is the event i've been reading: 这是我一直在阅读的事件:

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

I need something like: 我需要类似的东西:

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

I think the sintax is very different because i saw some examples using SDL_MOUSEBUTTONDOWN. 我认为sintax是非常不同的,因为我看到了一些使用SDL_MOUSEBUTTONDOWN的示例。

EDIT 编辑

I apologize, know this question might not be clear, but Peter's answer was just what I needed, thanks for your time. 我很抱歉,知道这个问题可能不清楚,但是彼得的答案正是我所需要的,谢谢您的时间。

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

If you want to intercept a click in an specific area first you need to be polling the events and check for a SDL_MouseButtonEvent. 如果要先拦截特定区域中的单击,则需要轮询事件并检查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    
                }
        }
    }
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM