Jump to content
  • Advertisement
Sign in to follow this  
win32mfc

Writing a game, and the game loop..

This topic is 4975 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

Hiya, I've recently decided to get back into working with DirectX, using the latest Managed DirectX 9. I have a question that's more general in nature, though. Most game books I've seen show the "main" game loop something like this: main() { init() while(!done) { handle_user_input() update_world() render_world() } free_resources() } A while back, I wrote a 2D game of pong that started by going into full-screen and giving you a simple menu, with a rotating arrow graphic next to the selected item (using arrow keys to move up/down and enter to select.) The way I did it was to use lots of switch statements like this: switch(m_state) { case TITLE_SCREEN: handle_title_screen(); break; case SHOW_CREDITS: handle_show_credits(); break; case START_GAME: handle_start_game(); break; case PLAY_GAME: handle_play_game(); break; case END_LEVEL: handle_end_level(); break; case NEW_LEVEL: handle_new_level(); break; case END_GAME: handle_end_game(); break; } My question is how do people normally go about putting in all the extra stuff a game usually has, like title screen, demo mode, etc? Am I going about it in a "sane" manner? Thanks for any input! // CHRIS EDIT: fixed typo

Share this post


Link to post
Share on other sites
Advertisement
Well the way I handle this is by using a state stack. I have an abstract class which all game states, such as the different menus and the game itself, inherrit from. I then have a stack of these states and I simply run whichever one is on top each frame.

Share this post


Link to post
Share on other sites
The "How to Program RPG" book does it the same way. Basically it defines a State object as follows:

struct StateObject {
void (*stateHandler)(void);
};

Then it creates a stack of StateObjects. Every loop through the game, it simply calls the stateHandler method. This method can then print title screens, display menus, run the main game, etc.

Drawbacks: stateHandler is a pointer to a funtion so only functions and not class methods can be used; unless the class method is static. =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yup, similar thing, although I just use a plain pointer instead of a stack:

Same topic in beginners forum

Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!