Search code examples
c++openglstructureglutpong

Structuring Pong


I am making Pong using C++ and OpenGL using Visual Express 2010. It is one of the first games I have made, and I am wondering how to best structure it. The main part that is stumping me is the game menu. Should I put the different game modes in different functions, and have a switch in my main function? For example, in Main.cpp, I would include the line

glutDisplayFunction(maincode)

In another file, I would define maincode as something like such (again, psuedocode):

maincode():
    switch m:
       case 1:
           singleplayer = true
           multiplayer = false
           menu = false
       case 2:
           singleplayer = false
           multiplayer = true
           menu = false
       case 3:
           singleplayer = false
           multiplayer = false
           menu = true

I would then, in each file, check to see the values of singleplayer, multiplayer, and menu to determine which mode I am in, then display code accordingly.

However, I get the feeling that this method would get much more complicated as the game gets more complicated, so I don't think this is the proper way to do it.

How should I structure the game and menu?

P.S. I am a C++ programmer for a living, but I just don't have experience programming games.


Solution

  • It's just unnecessary complexity to have 3 distinct bools (totaling to 8 different states, 3 of which would be valid) when the only information you need is which mode you are currently in.

    Better use an enumeration, i.e.

    enum GameMode {
    
      GM_SinglePlayer, GM_Multiplayer, GM_Menu
    
    };
    

    and a single variable of type GameMode:

    GameMode mode;
    mode = GM_Menu;
    

    Having different, mutually exclusive (that's the point that you missed here!) states to switch in between is an important concept in programming, so you are definitely on the right track. Keep working.