Archived

This topic is now archived and is closed to further replies.

Seperate game instances?

This topic is 5576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Say I have a menu class and a game class. They are both seperate instances which are not mean to be simultaneously running. My question is: how do I set and determine which instance is running? Every article I read on game programming didn''t suggest a thing as to how to best do this. Over the past week I''ve spent my time trying to find out what would work. Here are the ideas I came up with: #1:
  
int WinMain(...) {
  Init();
  state = MENU;

  while(1) {
    check_messages();

    switch (state) {
      case MENU:
        Menu.Frame();
        break;
      case GAME:
        Game.Frame();
        break;
    }
  }
  return(0);
}
  
#2:
  
void Proc_Menu(void);
void Proc_Game(void);

int WinMain(...) {
  Init();
  void (*Main_Process)(void);
  Main_Process = Proc_Menu;

  while(1) {
    check_messages();

    Main_Process();
  }
}
  
#3
  
class GINST { virtual void Frame(void); };
class MENU: public GINST { void Frame(void); };
class GAME: public GINST { void Frame(void); };

int WinMain(...) {
  Init();
  MENU Menu;
  GAME Game;
  GINST *Game_Instance;
  Game_Instance = Menu;

  while(1) {
    check_messages();

    Game_Instance.Frame();
  }
}
  
And this applies to credits and intro too. Am I going at it all wrong? Should I try to learn multithreading and make it take care of the window messages while I run uninterupted loops through the classes instead of the main loop? If anyone would point me to useful source or give me tips on how to go about it, I would be very greatful. Thanks in advance, -Shambles

Share this post


Link to post
Share on other sites
enum Gamestate{ MENU , GAME , CLIP };

Gamestate MyGameState = MENU ;

....
while(1){
...
if( MyGameState == MENU )
// Do Menu Stuff
else if( MyGameState == GAME )
// Do Ingame Stuff
else if( MyGameState == CLIP )
// Play avi
...
}


Check the Hands-on forum!

Share this post


Link to post
Share on other sites
Option #3 is what I use. I call my base class GameTask, then subclass it with each specific task I want to run. The taskmanager provides methods to set the current task. Each task has an Init and Cleanup method. TaskManager::SetTask first calls m_pCurrentTask_>Cleanup, then Inits the new task, and sets the pointer to point at the new one. Eache frame (or tick), m_pCurrentTask->Run is called.

The thing I like about this system is that it forces you to view the game structure much diffrently than using a switch statement or a series of if... else. Also, the system can be extended to handle multiple tasks that can run each frame (or tick). Threads can be implemented to run background tasks, and built into the task manager or each individual task. Lot''s of design ideas to play with here. I believe I first saw this type of system in Auran Jet.

Share this post


Link to post
Share on other sites