problem with beginning sequence for game

Started by
10 comments, last by BungoMan85 20 years, 11 months ago
Bungo, this will appear in article 5/6

The virtual function approach is the one I''ve found to be the best. You have a base class IAppState:


  class IAppState{ public: virtual bool Enter()=0; //tell the state to start up virtual void Draw()=0; virtual void Update(float timeStep)=0; virtual void Leave()=0; //tell the state to shut down};  


You make the functions ''pure virtual'' (virtual with =0 on the end) to force derived classes to implement them, so that way you don''t forget to provide a Leave() function or whatever.

Then you just derive from it, and have a singleton AppStateManager somewhere, which contains an IAppState pointer to the current state, and has a SwitchState() function which calls Enter() and Leave() on the appropriate states.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
What I've started doing recently is move the message pump to it's own function. It doesn't matter where the message pump funcs are called, as long as they are called in the same thread that created the window. so my Msg func looks like this: (There is actually more going on in the func, llike my game clock, etc.)


if(PeekMessage(&msg, NULL,0,0,PM_NOREMOVE))
{
if (msg.message == WM_QUIT)
Quit = 2;
GetMessage (&msg, NULL, 0, 0);
TranslateMessage (&msg);
DispatchMessage (&msg);
}



Then in my main func, I call all the substates just like I would in a good old fashoined linear dos game. The contents of these substate funcs look basically like this:


while (!Quit)
{
// Always process messages first
DoSys ();

// Do rest of stuff
}



The Quit flag is a private member of my CGame class. When the WndFunc reieves a WM_CLOSE it sets quit to 1. Also, anytime I want the app to end I just set quit to 1. Quit being non-zero causes all my substate funcs to quit out, and not run if they have yet to be invoked. The reason DoMsgs sets quit to 2 is that the main func needs to wait till all messages have been processed and the WM_QUIT message has been recieved. Once this happens the program ends.

-Zims

[edited by - Zimans on May 2, 2003 1:08:00 PM]

This topic is closed to new replies.

Advertisement