I have a problem with designing classes for my game which I create.
In my app, there is:
- class
CGamewhich contains all the information about game itself,
e.g. screen width, screen height, etc. In themain()function I
create a pointer toCGameinstance. - class
CGUIObjectwhich includes fields specifying it’s position and
draw()method, which should know how to draw an object according to
screen size. - class
CGUIManagerwhich is a singleton and it includes a list of
CGUIObject‘s. For each object in a list it just callsdraw()
method.
For clarity’s sake, I’ll put some simple code:
class CGame
{
int screenWidth;
int screenHeight;
};
class CGUIObject
{
CPoint position;
void draw(); // this one needs to know what is a screen's width and height
};
class CGUIManager // it's a singleton
{
vector<CGUIObject*> guiObjects;
void drawObjects();
};
And the main.cpp:
CGame* g;
int main()
{
g = new CGame();
while(1)
{
CGUIManager::Instance().drawObjects();
}
return 0;
}
Now the problem is, that each CGUIObject needs to know the screen size which is held by CGame, but I find it very dumb to include pointer to CGame instance in every object.
Could anyone, please, tell me what would be the best approach to achieve this?
Is there a reason that you are needing your screen resolution in your
CGUIObject‘s?They have a position already, so if you have them draw themselves in local space, you can apply a transform to them in your
CGUIManagerin order to lay them out. You abstract your layout from the GUI objects themselves this way, and the objects don’t need to know which container they are living in (the screen, a window, a tab etc).