So I've been working on a game engine using OpenGL + GLFW. Originally I had been using the LWJGL wrappers and using Java. I've decided to port my codebase to C++. I had been making use of the "glfwSetCursorPosCallback" function via a lambda like so:
//Java:
glfwSetCursorPosCallback(win, (window, xpos, ypos) -> {
engine.onCursorMoved(xpos, ypos);
});
Doing this allowed me to tie together all of the different "events" in the Engine class and keep the GLFW setup code + the raw update loop separate from the rest of the engine code. Leaving the Engine class nice and clean.
I would like to do the same thing using C++ however the following is invalid:
glfwSetCursorPosCallback(window, [engine](GLFWwindow* window, double x, double y) {
engine.onCursorMoved(x, y);
});
With C++ lambdas you cannot pass anything in the "[]" block if the lambda is being used as a function parameter.
So I was looking into it a bit more a on-top of that initial problem I also read that the performance was worse using lambdas.
So I tried passing the member function as the parameter instead:
// I changed "onCursorMoved" use the proper parameter signature
// "onCursorMoved(GLFWwindow* window, double x, double y)"
glfwSetCursorPosCallback(window, engine.onCursorMoved);
Trying this also failed as you cannot pass an instanced classes member function as a parameter to glfwSetCursorPosCallback.
So I ask, what approach should I take? Is there ways to get around the lambda/member function limitations, or some totally different approach to this that I'm completely missing?
P.S. - I'm quite out of practice with C++ so please forgive me if the answer is blatantly obvious.
Edit: To help illustrate/clarify what I'm trying to achieve, this is the Engine.h based on my previous Java version.
class Engine {
private:
//member variables for scene graph, etc
public:
Engine();
~Engine();
public:
void onWindowResized(int width, int height);
void onCursorMoved(double x, double y);
void onUpdate(float timeStep);
void onRender();
};
Basically the different functions prefixed with "on" are fired off by the GLFW callbacks/loop in main, among potentially other things. Is this approach doable, somehow, or is there a better way of doing this in C++, coming from Java where everything is in Objects, is this mindset flawed for this situation?