PDA

View Full Version : Confused with game objects initialization in OO



robert_s
04-23-2002, 04:07 PM
Hi!
I am a bit confused with object initialization in my engine.
Imagine you have in your engine Main.cpp and 2 classes called : CMenu.cpp and CButton.
Each class contain one member function : (CButton) initButtonTexture() and (CMenu) initMenu().
Now in your Main.cpp you create 1 object called menu by using CMenu class : Cmenu menu;
This object will basically construct and display menu in window.
eg. menu.display()

The CButton object is used inside the CMenu class to create buttons. So Menu will contain buttons.
The CButton is #included in my CMenu header file but it is not included in Main.cpp. In Main.cpp I have only CMenu object constructed.


Now. lets say I want every button in my menu to be textured with different texture.

My question is.. what is the correct way of initializing textures for these 2 objects. menu and button.
eg. for the button shoud I always pass from main.cpp in init() a string (path) eg. "img/button.tga" by using menu object eg. menu.initMenuTextures("img/button.tga") and pass hte path to menu and then from menu pass the path further to the button class eg. in menu I have an object button so I call button.initiButtonTexture( char* path) which passes the path to button class which will receive the path all the way from main and then it can load the texture. (thats how I've done it)

Is it correct way or you think that I should not be passing from main the path via menu eg menu.initMenu("img/button.tga") to button. ie. from main to menu and from menu to button but instead I should just specify the path (img/button.tga) inside the button class so I dont have to mess around with passing the path from main to menu and from menu to button. This way would save a bit more time but I think another problem would arise when for eg I would like to create a button with different texture. This way I wont be able to specify a different path from main init() when loadng textures. I would have go to my CButton class and edit the path in textureloader in order to laod different texture. unless I create two member functions in Button called
initButtonTex1()
{ loadtex( img/button1.tga); }
and initButtonTex2()
{ loadtex(img/button2.tga);}

and then call them from menu without specifying the path (parameterless) eg. menuInit(){ button1.initButtonTex1() ; button1.initButtonTex2()}
}
which doesn't make sense to me.

Which way do you think guys is the correct one. Or how would you do?
thanks!


[This message has been edited by robert_s (edited 04-23-2002).]

ChrisBond
04-24-2002, 03:26 AM
OT Warning - Not actually OpenGL, but IMHO anyone that cares enough about their code to look for more "correct" ways to initialize their system should be rewarded... http://www.opengl.org/discussion_boards/ubb/smile.gif

I'm not going to claim to have a "correct" method, per se. But I do have a preffered means of initialization that I find a most "elegant" solution. (You often can't prove correctness, but you can wax eloquent about elegance and no one asks for verification).

Try using ".ini files", or their equivalents. Rather than passing the names of the textures you need around, just pass a handle to the .ini file. We actually use XML, but it could be as simple a format as

Number = NULL-ended string

where the Number is the button's ID. Then at startup the button looks in the list for it's ID and reads it's associated filename.

As a bonus, this gives your more advanced users the ability to "skin" your app by changing the file around.

Hope this helps...

-Chris Bond

nigels
04-25-2002, 12:34 PM
My impression is that you want to centralise
your management of textures so that your
application doesn't need to load the same
texture for every button. You could use the
OpenGL texture binding feature, and tell each
button which texture ID to use.