SDL鼠标点击 [英] SDL Mouse Click

查看:243
本文介绍了SDL鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前正在为我的游戏开发一个选项菜单,我有一个按钮,当按下它时,它将文本更改为阵列中的下一个分辨率,因此基本上用户按下此按钮将其分辨率更改为



我的问题是获取点击事件。



现在,当用户按下按钮,当鼠标向下时,它返回true,而不是按鼠标。我只想在鼠标按下时只返回true。



我已经环顾四周了,我发现的一切似乎都与我的已经完成了,或者正如我所说,在鼠标停止时返回true,而不是初始点击。



我的事件在EventManager单例中处理,这里是我看到的功能是必要的:



我的更新功能,这是事件被轮询的地方,值得注意的是我正在使用名为e的私人SDL_Event

  void EventManager :: update(){
while(SDL_PollEvent(& e)){
SDL_GetMouseState(& mouseX,& mouseY);
switch(e.type){
case SDL_QUIT:
running = false;

}
}
}

我的mousePress函数,我想要一个鼠标按钮返回。

  int EventManager :: mousePress(){
if(e。 type == SDL_MOUSEBUTTONDOWN){
return e.button.button;
}
return 0;
}


解决方案

$ c> SDL_GetMouseState(),它可以让你看到鼠标的实际状态(这可能是它的名字来自的地方);使用你正在轮询的事件。 SDL应该给你一个 SDL_MouseButtonEvent ,其中包含您需要的信息,只能排队一次。



请参阅 https://wiki.libsdl.org/SDL_MouseButtonEvent



编辑以澄清我的意思:



你会使用这样的东西:

  void EventManager :: update(){
SDL_Event e;
while(SDL_PollEvent(& e)){
switch(e.type){
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN:
//按一下鼠标按钮后,做任何你想做的事,
//例如:
mousePress(e.button);
break;

}
}
}

mousePress-Function你可以测试,哪个鼠标按钮被按下:

  void EventManager :: mousePress(SDL_MouseButtonEvent& b ){
if(b.button == SDL_BUTTON_LEFT){
//处理左键单击
}
}

这样做是因为SDL_PollEvent每个事件只能返回一次。如果没有新的事件,它将返回一个空事件。所以1点击= 1次SDL_PollEvent(),e的类型为SDL_MOUSEBUTTONDOWN,之后为SDL_MOUSEBUTTONUP的1倍SDL_PollEvent()。如果您之间或之后调用SDL_PollEvent(),它将返回0并将e作为空事件,而不是调用开关。如果您回复MOUSEBUTTONDOWN或MOUSEBUTTONUP或者两者都取决于您...



我还将SDL_Event声明为本地变量,以 update )。为什么?事件背后的想法是,当某个事件发生时,它是一个事件对象。然后你对事件做出反应,忘记它。所以theres不需要有一个全局变量。如果你想防止不断的构造/破坏,你也可以声明它是静态的。但这只是一些提示,与您的原始问题无关。


So, I'm currently working on an options menu for my game, I have a button that when pressed it changes it's text to the next resolution in an array, so basically the user presses this button to change their resolution to the next string in the array.

My problem is getting the click event.

Right now, when the user presses the button, it returns true while the mouse is down, instead of when the mouse is pressed. I want to only return true in the mouse event when the mouse is pressed.

I've looked around, and everything I've found seems to be similar to what I've done or, as I said, returning true while the mouse is down, instead of the initial click.

My events are handled in a EventManager singleton, and here are the functions that I see as necessary:

My update function, this is where the event is polled, it is worth noting I'm using a private SDL_Event named "e".

void EventManager::update(){
    while(SDL_PollEvent(&e)){
        SDL_GetMouseState(&mouseX, &mouseY);
        switch(e.type){
            case SDL_QUIT:
                running = false;

        }
    }
}

My mousePress function, where I want a mouse press returned.

int EventManager::mousePress(){
    if(e.type == SDL_MOUSEBUTTONDOWN){
        return e.button.button;
    }
    return 0;
}

解决方案

Instead of using SDL_GetMouseState(), which gets you the actual state of the mouse (thats probably where its name comes from ;) ), use the event you are polling. SDL should give you a SDL_MouseButtonEvent which contains the informations you need and should only be queued once.

See https://wiki.libsdl.org/SDL_MouseButtonEvent

Edit to clarify what i mean:

You would use something like this:

void EventManager::update(){
    SDL_Event e;
    while(SDL_PollEvent(&e)){
        switch(e.type){
            case SDL_QUIT:
                running = false;
                break;
            case SDL_MOUSEBUTTONDOWN:
                //do whatever you want to do after a mouse button was pressed,
                // e.g.:
                mousePress(e.button);
                break;

        }
    }
}

Inside your mousePress-Function you can then test, which of the mouse buttons has been pressed:

void EventManager::mousePress(SDL_MouseButtonEvent& b){
  if(b.button == SDL_BUTTON_LEFT){
    //handle a left-click
  }
}

This works, because SDL_PollEvent will only return exactly once for every Event. If theres no new Event, it will return an empty Event. So 1 click = 1 times SDL_PollEvent() with e being of type SDL_MOUSEBUTTONDOWN afterwards and 1 times SDL_PollEvent() with e being of type SDL_MOUSEBUTTONUP afterwards. If you call SDL_PollEvent() in between or afterwards, it will return 0 and leave e as an Empty Event, not calling the switch at all. If you respond to MOUSEBUTTONDOWN or MOUSEBUTTONUP or both is up to you...

I've also declared the SDL_Event a local variable to update(). Why? The Idea behind an Event is, that theres an Event-Object whenever some event has occured. Then you react to the event and forget about it. So theres no need to have a global variable. If you want to prevent constant construction/destruction, you can also declare it to be static. But thats just some hint, not related to your original question.

这篇关于SDL鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆