poliet
August 7th, 2008, 09:40 AM
Hi all!
I am a beginner and currently working on a little hobby Opengl game project and I am having several .h and .cpp files.
In my main.cpp file I have my glutDisplayFunc(renderScene). I would like to use a "ship" class that is declared in ship.h and the impemetation of the functions in consequently in ship.cpp. One of the constructors in ship.cpp Ship::Ship(string textures){...} takes a string in order to attach a certain texture to the object.
Since I wanted to have it all neat and nice, I use in main.cpp a func "initObjects" to initialize a ship object via Ship A("usethistexture"). This function initObjects is called directly in main.cpp from main{... initObjects(void);... }
I would like to use now this object A in my renderscene in main.cpp because in renderscene I use something something like
glBindTexture ( GL_TEXTURE_2D, A.itsTextures[0] );
The problem is, that since Ship A is initialized in initObjects it would get out of scope once it leaves initObjects and renderScene doesn't know Ship A therefore.
My first idea was to make the object external by trying something like in main.h and ship.h: extern Ship A;
but then I get a compiler error undefined reference to _A.
Anyway this seemed also strange to me to have all Objects being global.
Thus I tried to find an alternative and I found a solution which works but is pretty ugly.
I initialize the object Ship A in renderscene the first time the function is being called, since renderscene is an endlessloop. It works but the code looks ugly and i didn't manage to keep my promise in intitializing the object in initObjects().
Another thought then was I may pass A by reference to renderscene but this would be also pretty cumbersome to pass everytime an object to renderscene which is created in initObjects.
My last thought was then to make a new class in main.cpp which holds pointers to all objects. But this seems to me to be a lot of work and kind of exotic and if would declare this new class in newclass.h, I would face exactly the same problem as now.
I would be grateful for any suggestion how to have Ship A in the scope of renderscene in the cleanest possible way. Thanks.
Jack
I am a beginner and currently working on a little hobby Opengl game project and I am having several .h and .cpp files.
In my main.cpp file I have my glutDisplayFunc(renderScene). I would like to use a "ship" class that is declared in ship.h and the impemetation of the functions in consequently in ship.cpp. One of the constructors in ship.cpp Ship::Ship(string textures){...} takes a string in order to attach a certain texture to the object.
Since I wanted to have it all neat and nice, I use in main.cpp a func "initObjects" to initialize a ship object via Ship A("usethistexture"). This function initObjects is called directly in main.cpp from main{... initObjects(void);... }
I would like to use now this object A in my renderscene in main.cpp because in renderscene I use something something like
glBindTexture ( GL_TEXTURE_2D, A.itsTextures[0] );
The problem is, that since Ship A is initialized in initObjects it would get out of scope once it leaves initObjects and renderScene doesn't know Ship A therefore.
My first idea was to make the object external by trying something like in main.h and ship.h: extern Ship A;
but then I get a compiler error undefined reference to _A.
Anyway this seemed also strange to me to have all Objects being global.
Thus I tried to find an alternative and I found a solution which works but is pretty ugly.
I initialize the object Ship A in renderscene the first time the function is being called, since renderscene is an endlessloop. It works but the code looks ugly and i didn't manage to keep my promise in intitializing the object in initObjects().
Another thought then was I may pass A by reference to renderscene but this would be also pretty cumbersome to pass everytime an object to renderscene which is created in initObjects.
My last thought was then to make a new class in main.cpp which holds pointers to all objects. But this seems to me to be a lot of work and kind of exotic and if would declare this new class in newclass.h, I would face exactly the same problem as now.
I would be grateful for any suggestion how to have Ship A in the scope of renderscene in the cleanest possible way. Thanks.
Jack