glfwSetCursorPosCallback to function in another class

后端 未结 3 1092
太阳男子
太阳男子 2020-12-12 03:56

I\'m really stuck:

I have the mainWindow and in the main game loop I do:

// poll for input
glfwPollEvents();

this->controls->handleInput(windo         


        
相关标签:
3条回答
  • 2020-12-12 04:27

    An alternative solution would be to associate a pointer to controls to the GLFWindow. See glfwSetWindowUserPointer.

    The pointer can be retrieved at an time form the GLFWWindow object by glfwGetWindowUserPointer. Of course the return type is void* and has to be casted to Controls*.

    Instead of the global function or static method a Lambda expressions can be used. e.g:

    glfwSetWindowUserPointer(window, this->controls);
    
    glfwSetCursorPosCallback( window, [](GLFWwindow *window, double x, double y)
    {
        if (Controls *controls = static_cast<Controls*>(glfwGetWindowUserPointer(window)))
            controls->handleMouse(window, x, y);
    } );
    
    0 讨论(0)
  • 2020-12-12 04:35

    You can't pass a class's member function as a function. glfwSetCursorPosCallback it's expecting a function and throwing the error because it gets a member function.

    In other words your expected to provide a global function and pass that to glfwSetCursorPosCallback.

    If you really want the controls object to get the cursor position callback you could store an instance of Controls in a global variable and pass on the callback to that instance. Something like this:

    static Controls* g_controls;
    
    void mousePosWrapper( double x, double y )
    {
        if ( g_controls )
        {
            g_controls->handleMouse( x, y );
        }
    }
    

    Then when you call glfwSetCursorPosCallback you can pass the mousePosWrapper function:

    glfwSetCursorPosCallback( window, mousePosWrapper );
    
    0 讨论(0)
  • 2020-12-12 04:36

    My solution: Do not use the callback-setter. Instead I do the following:

    glfwPollEvents();
    
    this->controls->handleInput(window, mainObj);
    this->controls->handleMouse(window, mainObj);
    

    And within handleMouse I do:

    GLdouble xPos, yPos;
    glfwGetCursorPos(window, &xPos, &yPos);
    
    0 讨论(0)
提交回复
热议问题