Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding global variables while using GLUT

Tags:

c++

scope

glut

GLUT is a great API and it's very easy to use but I am having some difficulty with how it handles scope. When defining callbacks there is no option to pass parameters so it seems to me as though the programmer is forced to rely on global variables, which I find difficult to accept. Right now I have all the GLUT code in it's own module, running on it's own thread, and define a static pointer which I assign at the entry point to the module, like so:

Main module

int main( int argc, char** argv ) {
    int foo;
    boost::thread graphicsThread(glutMain, argc, argv, &foo);

    //...

    graphicsThread.join();
    return 0;
}

GLUT module

static int* FOO_REF;

int glutMain( int argc, char** argv, int* foo ) {
    FOO_REF = foo;
    glutInit(&argc, argv);
    //etc...

Is there a better solution than this?

like image 375
Max DeLiso Avatar asked Nov 13 '22 11:11

Max DeLiso


1 Answers

If you're using freeglut or a derivative and willing to confine yourself to freeglut derivatives only it has a non-standard extension to solve exactly the problem. You can associate a void* with every window. If you make that a struct that contains all the per-window data you want you can avoid the globals entirely.

Synopsis:

#include <GL/glut.h>
#include <GL/freeglut_ext.h>


void * glutGetWindowData();
glutSetWindowData(void *data);
like image 51
Flexo Avatar answered Dec 28 '22 01:12

Flexo