简体   繁体   中英

Pass Member Function To glfwSet*Callback

I'm trying to setup an 'event receiver' class to wrap the GLFW callbacks into a class users can inherit from but having an issue passing the base-class member function into the GLFW 'set callback' functions.

#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;
}

This essentially provides a base-class with pure virtual functions for users to inherit from and override to provide their own logic for when events happen. The user-defined sub-class is meant to be passed into a larger 'application-framework' class which makes the necessary calls to setup the GLFW callback functions.

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 to use as the 'this pointer'.

I am not sure how to do this exactly in regards to the 'glfwSet*Callback' functions inside GLFW.

Only static member functions can be used like this in GLFW, instead, you can use glfwGetWindowUserPointer and glfwSetWindowUserPointer to provide access to the underlying class object in these situations if you require it.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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