C++ GLFW3 input handling

2019-08-06 18:29发布

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?

2条回答
Viruses.
2楼-- · 2019-08-06 19:04

just put the name of the callback function:

//callbacks header file
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);

//GLFW init function
glfwSetCursorPosCallback(window, cursor_position_callback);
查看更多
Lonely孤独者°
3楼-- · 2019-08-06 19:14

Use glfwSetWindowUserPointer() to set the Engine instance a given window should call callbacks on.

Then in the (capture-less) lambdas you can call glfwGetWindowUserPointer() with window, cast the void* to Engine*, and call the appropriate member function.

Example:

#include <GLFW/glfw3.h>
#include <cstdlib>

class Engine
{
public:
    void onCursorPos( double x, double y )
    {
        mX = x;
        mY = y;
    }

    double mX, mY;
};

int main()
{
    if( !glfwInit() )
        exit( EXIT_FAILURE );

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 2 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 );
    GLFWwindow* window = glfwCreateWindow( 640, 480, "Simple example", NULL, NULL );

    Engine engine;
    glfwSetWindowUserPointer( window, &engine );

    glfwSetCursorPosCallback( window, []( GLFWwindow* window, double x, double y )
    {
        Engine* engine = static_cast<Engine*>( glfwGetWindowUserPointer( window ) );
        engine->onCursorPos( x, y );
    } );

    glfwMakeContextCurrent( window );
    glfwSwapInterval( 1 );

    while( !glfwWindowShouldClose( window ) )
    {
        glClearColor( engine.mX / 640.0, engine.mY / 480.0, 1.0, 1.0 );
        glClear( GL_COLOR_BUFFER_BIT );

        glfwSwapBuffers( window );
        glfwPollEvents();
    }
    glfwDestroyWindow( window );
    glfwTerminate();
    exit( EXIT_SUCCESS );
}
查看更多
登录 后发表回答