1

I am developing a 2D game in C++ using SFML API, but I am having an issue with class design. I think that I am disobeying the SRP (Single Responsibility Principle) because I have one main class called "Game" that should handle the window. It contains a method containing main game loop. Now the problem; it contains object (composition) of more than 5 classes, such as Player, Menu, SplashScreen. Do you think that it is a good idea to create such "God" class? Do you know any open source C++ 2D games from which I could learn? Thank you in advance.

6
  • 1
    Are you having problems with the design, or do you just want to think it through? If you are not sure, sometimes it's not a bad idea to just continue coding with what you have and find out what's good and bad about it. You will learn all sorts of things along the way.
    – paddy
    Jan 30, 2013 at 13:18
  • I am just not sure. I have searched throughout stack overflow and found several posts regarding the "God" class (unfortunately they were not about game development). I, myself, do not think it is bad, but I have seen a few people saying that it is not a good habit to create such classes.
    – Brukmoon
    Jan 30, 2013 at 13:20
  • I agree with @paddy. If you've never done this kind of thing before, you will indeed learn a whole lot, and probably have several "misfires" before you feel comfortable with what you've written. As to your last question, try here: en.wikipedia.org/wiki/List_of_open_source_video_games
    – kenrogers
    Jan 30, 2013 at 13:22
  • If you avoid useful paradigms because of academic stigma, you will spend all your time refactoring code and never finish your project. Just let it get a bit grungey and have fun making your game. The next game you make will have better structure. And the next....
    – paddy
    Jan 30, 2013 at 13:27
  • Thank you for your answers! I just do not want to learn bad habits, and that is why I have asked. I will do what you have said; and when I finish the development, I will post here an article regarding my experience.
    – Brukmoon
    Jan 30, 2013 at 13:27

1 Answer 1

1

See this thread on the Gamedev site about God classes (yes, they are a bad idea because tight coupling makes code less robust in the face of change).

Don't know about 2D, but there is a whole GitHub repo with id-Software 3D game engines. Apart from the graphics, this should provide a treasure trove for you. See also the Doom source code review for ideas on good coding style for writing games.

2
  • And what exactly should I consider as a god class? A class that has several responsibilities? If so, should a class that contains whole game loop be considered as a god class? It also contains objects such as Player, SplashScreen etc. that have methods such as 'move', but they are all called inside the gameLoop method.
    – Brukmoon
    Jan 30, 2013 at 13:37
  • @Brukmoon Yes, many unrelated responsiblities that should be refactored into separate clases. see this article for a small coding example. Jan 30, 2013 at 13:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.