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: 225

Filed under:
|
|
|

I have a problem with designing classes for my game which I create.

In my app, there is:

  • class CGame which contains all the information about game itself, e.g. screen width, screen height, etc. In the main() function I create a pointer to CGame instance.
  • class CGUIObject which includes fields specifying it's position and draw() method, which should know how to draw an object according to screen size.
  • class CGUIManager which is a singleton and it includes a list of CGUIObject's. For each object in a list it just calls draw() 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

Related posts about c++

Related posts about game-design