1

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.

2
  • 2
    The scope of this question is vast. I suggest reading a book on game programming. Jun 12, 2011 at 17:50
  • one cannot have both single player and multiplayer true, so they are mutually exclusive. keep one single multiplayer and if it is true the game mode multiplater, if it is false it is singleplayer.
    – phoxis
    Jun 12, 2011 at 17:50

3 Answers 3

4

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.

2
  • I understand what you are saying (enum is a much better method of doing it, I can't believe I had never thought of that!), but I am wondering if this sort of method is sound in the first place. Jun 12, 2011 at 17:58
  • It's common practice (especially for small games such as Pong) to have your game states structured like this. Usually there's a main loop that dispatches the work to the menu or the game logic depending on the current state. So you're doing well. Jun 12, 2011 at 17:59
0

The best way to organize a GUI application is using the MVC design pattern. Either that, or it's small modification presenter first.

Knowing that, answers to such questions as your are simple to answer (Alexander Gessler's answer is quite ok)

0

glutDisplayFunc() is a quick way to display stuff but it quickly becomes more trouble than it's worth when writing simple projects.

Unless you're multithreading and keeping a constant copy of your game-state for glutDisplayFunc() to read, I'd say avoid using it - and even then, probably, too.

For game loops, I believe it's better to drive OpenGL rather than let it drive you.

enum EMode { ...etc };

EMode menu();
void single();
void multi();

int main()
  {
    init(...);
    bool loop = true;
    while(loop)
      {
        switch(menu())
          {
            case EMode.single:
              game_loop(1);
              break;
            case EMode.multi:
              game_loop(2);
              break;
            case quit:
              loop = false;
            ...etc
          }
      }
    finish(...);
    return 0;
  }

Simple menu loop. (Forgive my code style it's different than usual).
It's not a great layout, but it would work for a simple game.

  • init() initializes openGL.
  • menu(), single() and multi() each have their own loops, which render and swap the buffers manually.
  • menu() returns a value specifying which menu item was chosen.


Game loop that runs at constant speed and doesn't waste CPU.

void game_loop(int players)
  {
    CGame game = new Game(players);
    bool loop = true;
    time_type next_frame = get_time() - 1;
    while(loop)
      {
        time_type time = get_time();
        if(time > next_frame)
          {
            do_input(game);
            while(time > next_frame)
              {
                game.tick();
                next_frame += frame_period;
              }
            do_render(game); // draw stuff here, and swap buffers.
          }
        else
          {
            // Don't waste CPU (optional).
            wait_till(next_frame);
          }
      }
  }

Again, not wonderful, but function enough. (I use something like this).

  • CGame is the game class (if you want to be OO).

  • get_time(), wait_till(), time_type etc are all made up. Use whatever function/type suits.

Quick and off the top of my head - probably even has some bugs - but it could give you an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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