Skip to main content
GameDev.net gamedev.net
🔒 Locked

Design pattern

Started by Mithoric Jan 9, 2007 at 11:01 PM 5 replies 1k views
Original Post
Mithoric
Mithoric
I've written a basic engine and api to build a game. But now I find myself wondering how to do it. Somehow tracking a game state and using a giant switch statement doesn't feel like the best way to do it, it wouldn't be really flexible. So, how does everyone else do it? I don't know how everyone else's engine's work, I use what I like to call a base class which you inherit from and it has functions called from the engine at whatever time...init() destroy() cycle() event(). So getting started is as simple as deriving a class and passing an instance of it to the engine object.

class DEMO:public CQBase{
    public:
        void init();
        void cycle();
        void event(CQEvent);
        void destroy();
};

...

DEMO d;
CQEngine engine(&d);
engine.window.init(1024,768,16,"CQDemo",false);
engine.run()

Hopefully this makes sense. I was thinking, perhaps I could modify it slightly so that I could easily swap bases. This would require wrapping the engine and however many base classes in another class though and routing calls between the wrapper and the selected base class to manage the game state. I'm not sure if this is really, flexible, enough though. How do you guys do it?
GamerYZ
GamerYZ
You could use a finite state machine.
Mithoric
Mithoric
Well, I know that much already. My problem lies in how to manage that state in a more flexible way than having a switch control structure in both
cycle()
and
event()
... I think I will implement an array of base classes or something and allowing switching between them, seems easiest for my current design.
Bob Janova
Bob Janova
I do this with a state stack. Certain actions (within a state, you're always in a state) push a new state, replace the current state or pop the top state(s).

For example, at startup the state stack contains one state, the main menu state. Selecting menu options either replaces the state with a different menu state or pushes a new menu state on top, whichever makes more sense to the particular game. When you start a game, the menu state is replaced by a loading state, which itself is replaced by an in-game state. If you press Escape during a game, a menu state is pushed onto the stack. Etc, etc.

Each state has an Update(long time) method, or similar, which is called every frame (even if it's not at the top of the stack). It also has a ProcessInput method which allows it to respond to keyboard or mouse activity; input is passed to the top state, and if that ignores it to the one below, and so on.

For reference, the relevant parts from my ten hour game; not polished but it should give you the idea:
// A game state :)public interface GameState { void Init(long ticks); void Update(long ticks); void Render(Renderer r); bool ProcessKeyInput(KeyEvent e); bool ProcessMouseInput(MouseEvent e);}// In the main classclass Game { Stack gameStates = new Stack();  Update(){  Renderer.Clear();  long ticks = DateTime.Now.Ticks - zeroticks;  foreach(GameState state in gameStates){   state.Update(ticks);   state.Render(Renderer);  }  renderer.graphics = e.Graphics;  Renderer.Render(); } void ProcessKeyEvent(KeyEvent ke){  foreach(GameState state in gameStates)   if(state.ProcessKeyInput(ke)) break; }}
Mithoric
Mithoric
Well that's pretty much what I've done now, so I can't be too far wrong if someone else is thinking the same way! Thanks guys. Just had to think of a state in a more broad manner.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.