glutTimerFunc issues

后端 未结 2 1658
一整个雨季
一整个雨季 2021-01-25 15:10

I am using a timer function to animate, but I am having issues when I place it in the Renderer class.

void Renderer::animate(int value)
{
 glutTimerFunc(TIMERMSE         


        
相关标签:
2条回答
  • 2021-01-25 15:53

    glutTimerFunc() expects a pointer to a function of type void (*func)(int value), not a member function of type void (Renderer::*func)(int value).

    Make Render::animate static or use a global function.

    0 讨论(0)
  • 2021-01-25 15:56

    The issue is that Renderer::animate is a class member function and so has a hidden this parameter. glutTimerFunc doesn't know what value of this to pass, so if you somehow tried to sneak past the compiler with a nasty cast, it would crash at runtime.

    The solution is to change Renderer::animate into a static method or an ordinary global function. You then need to store the pointer to your Renderer instance in a global variable, e.g.:

    class Renderer
    {
        static void staticAnimate(int value);
        void animate(int value);
        ...
    };
    Renderer *gRenderer = ...;
    
    void Renderer::staticAnimate(int value)
    {
        gRenderer->animate(value);
    }
    
    ...
    
    glutTimerFunc(TIMERMSECS, &Renderer::staticAnimate, 0);
    

    If sizeof(void*) <= sizeof(int) (true on 32-bit systems but not true on 64-bit systems), then you could also pass the instance via the value parameter; however, since this is not portable and you won't ever need to call glutTimerFunc on multiple different instances simultaneously, you shouldn't have to worry about using a global variable for this.

    0 讨论(0)
提交回复
热议问题