How to initialize excess with fake parameters?

I use opengl, using the GLUT and GLEW libraries to create a plugin for a specific application.

This plugin does not start with a simple int main (argc, argv). Therefore, I cannot pass these values ​​to glutInit ().

I tried something like this:

glutInit(0, NULL); <--- Crash GLenum err = glewInit(); 

But I crashed when I tried to call the glutInit () function. Can I restore these settings in some way so that it does not crash and can still use the Glut library .. ??

+4
source share
4 answers

You can do it as follows:

 #include <GL/freeglut.h> int main() { char fakeParam[] = "fake"; char *fakeargv[] = { fakeParam, NULL }; int fakeargc = 1; glutInit( &fakeargc, fakeargv ); //... } 

but note that this is an ugly hack.

+6
source

You may need to call glutInit with a valid argv parameter, even if you don't have one:

 char *my_argv[] = { "myprogram", NULL }; int my_argc = 1; glutInit(&my_argc, my_argv); 

Edit

It may also be that the first parameter is a pointer to an int , and it cannot be NULL? Then it may be enough just to pass a valid argc parameter:

 int my_argc = 0; glutInit(&my_argc, NULL); 
+4
source

Pay attention to the following code from the source ( freeglut_init.c:677 ):

 void FGAPIENTRY glutInit( int* pargc, char** argv ) { char* displayName = NULL; char* geometry = NULL; int i, j, argc = *pargc; ... 

(Note the dereferencing.)

glutInit() seems to require a minimum process name, although this is not clarified on the manual page.

+3
source

I propose this as a de facto standard for initializing glut applications.

 static inline void glutInstall() { char *glut_argv[] = { "", (char *)0 }; int glut_argc = 0; glutInit(&my_argc, my_argv); } 

This function can be changed for each application to provide a saturation with the arguments that it needs (if any), while constantly solving the problem of everyone asking why you pass command line arguments to a third-party library.

0
source

Source: https://habr.com/ru/post/1392036/


All Articles