I am using SFML 2.3
to control the graphics in my program. I need a class to be able to access the window, to draw to it even if that class was not the one that originally created the window (does not have ownership). E.g. the Battle
class needs to be able to draw the battle scene and manipulate characters there but the World
class would need the window before and after Battle
to show the player's movements in the main game.
How should the window object be handled? My initial thought is to have a GameMaster
class that has the window as a static member. But rather than blunder along with this method I thought it better to check what the usual consensus was.
I realise I could always pass a reference to the window to each class that needs it but this would make the constructors for all the classes that need to manipulate the window quite bloated.
I'm afraid you have to decide to do a choice between using a Singleton (GameMaster
) that allows access to the main window, or pass a reference to it with your dependent class objects.
I personally would prefer the latter (I won't consider adding another constructor parameter to a couple of classes as bloat), because the overall flexibility of the class design would be better than using a Singleton.
You may consider, to outsource your World
class assembly code to a separate Factory
class. This one could hold the necessary main window object as a member and pass it to all of the created World
, Battle
, etc. instances necessary to assemble the World
, initiate the game mission respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With