Sign in to follow this  

Writing a game, and the game loop..

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

This topic is 4761 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this