Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing control of a drawing window "deeper" into a program?

Tags:

c++

graphics

sfml

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.

like image 209
Kvothe Avatar asked May 13 '15 15:05

Kvothe


1 Answers

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.

like image 143
πάντα ῥεῖ Avatar answered Sep 21 '22 19:09

πάντα ῥεῖ