Informing GUI objects about screen size - Designing
        Posted  
        
            by 
                Mosquito
            
        on Game Development
        
        See other posts from Game Development
        
            or by Mosquito
        
        
        
        Published on 2012-11-21T13:55:01Z
        Indexed on 
            2012/11/21
            17:16 UTC
        
        
        Read the original article
        Hit count: 277
        
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 anddraw()method, which should know how to draw an object according to screen size.
- class CGUIManagerwhich is a singleton and it includes a list ofCGUIObject'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?
© Game Development or respective owner