Registering a GLUT Callback in C++

Discussion in 'C++' started by Michael Kipper, Jan 4, 2006.

  1. Hi,

    I'm having a problem moving from C to C++.
    In C, I have a function:

    void display(void) {}

    that I can register with OpenGL as a callback using:

    GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )

    called like so:

    glutDisplayFunc( display );

    My problem is when I move to C++ objects, I have a class with a member
    function 'void display(void)', which I want to register like:

    glutDisplayFunc( this->display );

    But I get the error (in MSVC++):

    error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
    'void (void)' to 'void (__cdecl *)(void)'

    What is the correct way to do this?

    Thanks,
    Michael
    Michael Kipper, Jan 4, 2006
    #1
    1. Advertising

  2. Michael Kipper

    Guest

    Michael Kipper wrote:
    > Hi,
    >
    > I'm having a problem moving from C to C++.
    > In C, I have a function:
    >
    > void display(void) {}
    >
    > that I can register with OpenGL as a callback using:
    >
    > GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )
    >
    > called like so:
    >
    > glutDisplayFunc( display );
    >
    > My problem is when I move to C++ objects, I have a class with a member
    > function 'void display(void)', which I want to register like:
    >
    > glutDisplayFunc( this->display );
    >
    > But I get the error (in MSVC++):
    >
    > error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
    > 'void (void)' to 'void (__cdecl *)(void)'
    >
    > What is the correct way to do this?


    "correct" is subjective.

    http://www.google.com/search?hl=en&lr=&q=c callbacks in c++&btnG=Search

    You can't actually do what you are trying to do. You'll need to use
    some hack. See above for some ideas.
    , Jan 4, 2006
    #2
    1. Advertising

  3. Michael Kipper wrote:
    > I'm having a problem moving from C to C++.
    > In C, I have a function:
    >
    > void display(void) {}
    >
    > that I can register with OpenGL as a callback using:
    >
    > GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )
    >
    > called like so:
    >
    > glutDisplayFunc( display );
    >
    > My problem is when I move to C++ objects, I have a class with a member
    > function 'void display(void)', which I want to register like:
    >
    > glutDisplayFunc( this->display );
    >
    > But I get the error (in MSVC++):
    >
    > error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
    > 'void (void)' to 'void (__cdecl *)(void)'
    >
    > What is the correct way to do this?


    There is none.

    Read the FAQ, it explains why non-static members cannot be used as
    callbacks.

    V
    Victor Bazarov, Jan 4, 2006
    #3
  4. Michael Kipper

    James Juno Guest

    Michael Kipper wrote:
    > Hi,
    >
    > I'm having a problem moving from C to C++.
    > In C, I have a function:
    >
    > void display(void) {}
    >
    > that I can register with OpenGL as a callback using:
    >
    > GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )
    >
    > called like so:
    >
    > glutDisplayFunc( display );
    >
    > My problem is when I move to C++ objects, I have a class with a member
    > function 'void display(void)', which I want to register like:
    >
    > glutDisplayFunc( this->display );
    >
    > But I get the error (in MSVC++):
    >
    > error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
    > 'void (void)' to 'void (__cdecl *)(void)'
    >
    > What is the correct way to do this?
    >
    > Thanks,
    > Michael


    Maybe try something like GLUT for C++

    http://www.cs.unc.edu/~rademach/glui/

    -JJ
    James Juno, Jan 4, 2006
    #4
  5. Michael Kipper

    BobR Guest

    Michael Kipper wrote in message ...
    >Hi,
    >I'm having a problem moving from C to C++.
    >In C, I have a function:
    > void display(void) {}
    >that I can register with OpenGL as a callback using:
    > GLUTAPI void glutDisplayFunc( void (GLUTCALLBACK *func)(void) )
    >called like so:
    > glutDisplayFunc( display );
    >My problem is when I move to C++ objects, I have a class with a member
    >function 'void display(void)', which I want to register like:
    > glutDisplayFunc( this->display );
    >But I get the error (in MSVC++):
    > error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from
    > 'void (void)' to 'void (__cdecl *)(void)'
    >What is the correct way to do this?
    >Thanks,
    >Michael



    You need to use 'static'. Much removed from the example below, but, should be
    enough for you to get the idea.

    // ---------------------------------------------------------
    // GLutBase.cpp - Minimal GLut window
    // made with (C) GLUTwriter by BobR <07dec01>
    // ---------------------------------------------------------
    #include <windows.h> // Standard Header For MSWindows Applications
    #include "glut.h" // GL Utility Toolkit (GLUT 3.7) Header. LOCAL dir.

    class GLRock{
    public: //--------------------------------------- public
    GLRock(){ } //GLRock() Ctor
    ~GLRock(){} //GLRock() Dtor
    //--------------------------------------------------------------
    bool InitGL(); // All setup for OpenGL goes here
    // ======= InitCallBacks =======
    static void InitCallBacks(); // Match functions to their counterparts
    static void DrawGLScene(); // Drawing done here
    // ---------------------------------------------
    protected: //--------------------------------------- protected
    private: //--------------------------------------- private
    // ---------------------------------------------
    }; //GLRock
    // ---------------------------------------------

    // ======= InitGL =======
    bool GLRock::InitGL(){
    // --- All setup for OpenGL goes here ---
    return TRUE;
    } //InitGL()
    // ---------------------------------------------

    // ======= DrawGLScene =======
    void GLRock::DrawGLScene(){
    // --- Drawing done here ---
    } //DrawGLScene()
    // ---------------------------------------------

    // ======= InitCallBacks =======
    void GLRock::InitCallBacks(){ // Match functions to their counterparts
    glutDisplayFunc( DrawGLScene ); // Register Display Function
    // --- etc. ---
    } //InitCallBacks()
    // ---------------------------------------------

    // ======= MAIN =======
    int main(int argc, char** argv) {
    glutInit(&argc, argv); // GLUT Initializtion
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA );
    if( g_gamemode ){
    glutGameModeString("640x480:24"); // Select The 640x480 in 24bpp
    Mode
    if(glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
    glutEnterGameMode(); // Enter Full Screen
    else g_gamemode = false; // No Game Mode, switch to Windowed
    }
    if( not g_gamemode){
    glutInitWindowSize(640, 480); // Window size if Windowed Mode
    glutCreateWindow("GLut OpenGL Rock Demo"); // Window Title
    }

    GLRock GLR;
    if( not GLR.InitGL() ){ // GL Initialization
    /* "InitGL FAILED */
    return EXIT_FAILURE;
    }

    GLR.InitCallBacks();

    glutMainLoop(); // Go To GLUT Main Loop
    // --- Never reaches this line ---
    return 0;
    } //main()
    // =========================================

    --
    Bob R
    POVrookie
    BobR, Jan 4, 2006
    #5
  6. Michael Kipper

    Manuel Guest

    Michael Kipper wrote:

    > What is the correct way to do this?


    I've solved wrapping the callbacks.
    Example (I use a reshape method from a custom obj called MainWindow):

    ----------------------------------------------------------
    //Wrapping the glut reshape callback
    void reshape(int w, int h){MainWindow.reshape (w,h);}

    int main ( int argc, char** argv )
    {

    glutInit ( &argc, argv );
    glutReshapeFunc ( reshape );
    glutDisplayFunc ( display );
    glutMotionFunc(motion);
    glutMainLoop ( );
    system("PAUSE");
    return 0;
    }
    ----------------------------------------------------------
    Manuel, Jan 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson

    GLUT callback registration (not quite OT)

    Martin Magnusson, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    502
    Ivan Vecerina
    Sep 6, 2003
  2. Bushido Hacks
    Replies:
    0
    Views:
    2,392
    Bushido Hacks
    Feb 20, 2005
  3. Adam Hartshorne

    GLUT & QT Together

    Adam Hartshorne, Apr 24, 2005, in forum: C++
    Replies:
    7
    Views:
    6,358
    Alvin Beach
    Apr 24, 2005
  4. Juan T. Llibre
    Replies:
    2
    Views:
    3,886
    =?Utf-8?B?UmljaA==?=
    Dec 16, 2006
  5. Tim Spens
    Replies:
    1
    Views:
    1,072
    Matimus
    Jun 27, 2008
Loading...

Share This Page