c++ - 如何使用 irrlicht 获取鼠标位置?

标签 c++ irrlicht

我写了一个在按钮上进行鼠标输入的例子。我的意思是当用户点击屏幕时,我想查看鼠标位置 x 和鼠标位置 Y。但是 X 和 Y 总是有垃圾值。这是我的代码:

#include <irrlicht.h>
#include <iostream>
using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


// Declare a structure to hold some context for the event receiver so that it
// has it available inside its OnEvent() method.
struct SAppContext
{
    IrrlichtDevice *device;
};

// Define some values that we'll use to identify individual GUI controls.
enum
{
    GUI_ID_BUTTON = 101,
};


class MyEventReceiver : public IEventReceiver
{
    public:
        MyEventReceiver(SAppContext & context) : Context(context) { }

        virtual bool OnEvent(const SEvent& event)
        {
            if (event.EventType == EET_GUI_EVENT)
            {
                s32 id = event.GUIEvent.Caller->getID();

                switch(event.GUIEvent.EventType)
                {
                    case EGET_BUTTON_CLICKED:
                        switch(id)
                        {
                            case GUI_ID_BUTTON:
                                cout << "X: "<< event.MouseInput.X << endl;
                                cout << "Y: "<< event.MouseInput.Y << endl << endl;
                                return true;

                            default:
                                return false;
                        }
                        break;

                    default:
                        break;
                }
            }
            return false;
        }

    private:
        SAppContext & Context;
};


int main(int argc, char** argv)
{


    // create device and exit if creation failed

    IrrlichtDevice * device = createDevice( video::EDT_SOFTWARE, core::dimension2d<u32>(1280, 720));

    if (device == 0)
        return 1; // could not create selected driver.

    device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
    device->setResizable(true);

    video::IVideoDriver* driver = device->getVideoDriver();
    IGUIEnvironment* env = device->getGUIEnvironment();

    // add button
    env->addButton(rect<s32>(0,0,640,360), 0, GUI_ID_BUTTON,
            L"Button", L"Button");

    // Store the appropriate data in a context structure.
    SAppContext context;
    context.device = device;

    // Then create the event receiver, giving it that context structure.
    MyEventReceiver receiver(context);

    // And tell the device to use our custom event receiver.
    device->setEventReceiver(&receiver);

    /*
    That's all, we only have to draw everything.
    */

    while(device->run() && driver)
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, SColor(0,200,200,200));

        env->drawAll();

        driver->endScene();
    }

    device->drop();

    return 0;
}

和结果图片:

/image/dQEMs.png

我该如何解决这个问题?是否有另一种方法来获取鼠标输入位置?

最佳答案

Irrlicht 文档包含有关如何使用鼠标和游戏杆进行输入的教程。 Read here .

您需要处理 EMIE_MOUSE_MOVED 事件并将鼠标输入的当前位置存储在某处。

if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
   switch(event.MouseInput.Event)
   {
       ...
       case EMIE_MOUSE_MOVED:
           MouseState.Position.X = event.MouseInput.X;
           MouseState.Position.Y = event.MouseInput.Y;
           break;
       ...
   }
} 

关于c++ - 如何使用 irrlicht 获取鼠标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40518088/

相关文章:

c++ - Irrlicht GUI 编辑器导入文件

c++ - 在 Irrlicht 中使用 SDL

c++ - Irrlicht 编译,但在执行时崩溃

c++ - 重新声明类名 class-key

c++ - void 函数允许返回 "nothing"吗?

c++ - 如何知道何时单击组合框的向下箭头?

c++ - 为什么 addDays 不起作用?

c++ - 带有 protobuf 的模糊符号字符串

c++ - Irrlicht:根据四个角坐标在3D空间绘制2D图像

c++ - 什么是 ATOMIC_FLAG_INIT 以及它应该如何使用?