繁体   English   中英

通过成员 Function 到 glfwSet*Callback

[英]Pass Member Function To glfwSet*Callback

我正在尝试设置一个“事件接收器”class 以将 GLFW 回调包装到 class 用户可以继承但在将基类成员 ZC1C425268E68385D1AB5074C17A94F1 传递到 GLFWW17A94F1 函数时遇到问题。

#include <GLFW/glfw3.h>

//
//
//  Event Receiver Framework

enum EventTypes {
    Keyboard = 1,
    Mouse = 2
};

struct Event {
    EventTypes Action;
    //  Other Event Related Values
};

class EventReceiver {
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
        Event NewEvent;
        NewEvent.Action = EventTypes::Keyboard;
        OnEvent(NewEvent);
    }

public:
    virtual void OnEvent(Event& NewEvent) = 0;
};

//
//
//  Application Framework

class MyApplication {
    GLFWwindow* window;
public:
    void SetEventReceiver(EventReceiver* EventHandler) {
        glfwSetKeyCallback(window, &EventHandler->key_callback);
    }

};

//
//  
//  User-Defined Event Receiver

class MyEventReceiver : public EventReceiver {
public:
    void OnEvent(Event& NewEvent) {
        if (NewEvent.Action == EventTypes::Keyboard) {
            //  Custom Event Actions
        }
        else if (NewEvent.Action == EventTypes::Mouse) {
            //  Custom Event Actions
        }
    }
};

//
//
//  Main

int main() {
    MyEventReceiver _Events;
    MyApplication _App;
    _App.SetEventReceiver(&_Events);
    //
    //  Do logic
    //
    return 0;
}

这实质上提供了一个带有纯虚函数的基类,供用户继承和覆盖,以便为事件发生时提供自己的逻辑。 用户定义的子类旨在传递到更大的“应用程序框架”class 中,它会进行必要的调用以设置 GLFW 回调函数。

Of course you cannot pass a member function as a function argument expecting a c-style-function because you must provide access to the class instance, or in other words, you have to provide the class instance so the member function being called knows what object用作“this 指针”。

对于 GLFW 中的 'glfwSet*Callback' 函数,我不确定如何做到这一点。

只有 static 成员函数可以在 GLFW 中像这样使用,相反,您可以使用 glfwGetWindowUserPointer 和 glfwSetWindowUserPointer 来提供对底层 class ZA8CFDE6331BD59EB2AC966F8911ZC4 的访问。

#include <GLFW/glfw3.h>

//
//
//  Event Receiver Framework

enum EventTypes {
    Keyboard = 1,
    Mouse = 2
};

struct Event {
    EventTypes Action;
    //  Other Event Related Values
};


class EventReceiver {
    static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
        Event NewEvent;
        NewEvent.Action = EventTypes::Keyboard;
        static_cast<EventReceiver*>(glfwGetWindowUserPointer(window))->OnEvent(NewEvent);
    }

public:
    virtual void OnEvent(Event& NewEvent) = 0;
    friend class MyApplication;
};

//
//
//  Application Framework

class MyApplication {
    GLFWwindow* window;
public:
    void SetEventReceiver(EventReceiver* EventHandler) {
        glfwSetWindowUserPointer(window, EventHandler);
        glfwSetKeyCallback(window, &EventReceiver::key_callback);
    }

};

//
//  
//  User-Defined Event Receiver

class MyEventReceiver : public EventReceiver {
public:
    void OnEvent(Event& NewEvent) {
        if (NewEvent.Action == EventTypes::Keyboard) {
            //  Custom Event Actions
        }
        else if (NewEvent.Action == EventTypes::Mouse) {
            //  Custom Event Actions
        }
    }
};

//
//
//  Main

int main() {
    MyEventReceiver _Events;
    MyApplication _App;
    _App.SetEventReceiver(&_Events);
    //
    //  Do logic
    //
    return 0;
}

暂无
暂无

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

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