문제

How can I access the class instance inside a GLFW3 input callback function, for example this one.

I want my instance do something when a specific event happens. Each instance might do something different for a specific event.

Specifically, my class has a std::map< int, std::function< void()>>, where a key is mapped to a function.

EDIT: I tried the following, but this gives me an error that it doesn't match the glfwSetKeyCallback function call.

glfwSetKeyCallback(window, [this](GLFWwindow * window, int key, int scancode, int action, int mods){
    addCommand(m_events.at(key));
});
도움이 되었습니까?

해결책

Taken from here.

You need something like this:

glfwSetWindowUserPointer(window, this);
glfwSetKeyCallback(window, [](GLFWwindow * window, int key, int scancode, int action, int mods){

    Window * win = static_cast<Window *>(glfwGetWindowUserPointer(window));
    win->addCommand(win->m_events.at(key));

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top