Sign in to follow this  
fooman_69

How to handle different game "modes"

Recommended Posts

fooman_69    174
hello, I am in the midst of creating my first complete game. However, I have no real clue where to start in terms of creating a start screen, a pause screen, or a pre-set animation sequence. I am using openGL, and often there is a Render function. Right now, my game just has the Render() function which draws the characters and main game screen. Would it be a good idea to create several Render() function such as RenderStartScreen(), RenderGameplay(), RenderPause(), etc?? Also, how would I use the keyboard in terms of a button performing a new action based on what "mode" the game is in (paused, gameplay, startscreen, etc)? Please help. This has been on my mind for awhile now. Any examples and anecdotes are welcome!

Share this post


Link to post
Share on other sites
fyhuang    253
This is where something called a Finite State Machine comes in. A Finite State Machine is, well, a machine that has a finite number of states. In a game these states would be, like for example menu, in-game, etc.

In my games, I allow states to have nested states, so the in-game state could have nested states of active, paused, etc.

A simple state machine in a game might look like this:


int currState;
while ( run == true )
{
// Handle events here

switch( currState )
{
case STATE_MENU: // Defined macro or enum
handleMenu();
renderMenu();
if ( PlayerClickedButton )
currState = STATE_GAME;
break;

case STATE_GAME:
handleGame();
renderGame();
if ( PlayerPressedBackToMenu )
currState = STATE_MENU;
break;
}
}



Of course, there are more advanced methods of doing this - for example, each state would ideally have some sort of 'init' and 'shutdown' method, so that, for example, when switching state to the menu the menu graphics are loaded and game graphics (or whatever state it was previously in) are unloaded.

I personally don't have much knowledge in FSMs and stuff, I did make a simple class to handle this for me and that's what I use. I can email it to you (planning on licensing under GPL but for now just don't distribute and stuff). Others, I'm sure can explain much better than I have.

Hope it helps!

Share this post


Link to post
Share on other sites
MajinMusashi    182
Hi, Fooman!
I think what you are looking for are GAME STATES. In my projects I usually define a base class CGameState and then some inherited CGSIntro, CGSTitleScreen, CGSIngame, each one with its own input handling, object list etc.
And what do you mean with "pre-set animation"? Some guesses:
1) A pre-rendered movie like .AVI or .MOV files
=> Just tell me if you find some easy free way ;) FFMPEG2 seems good to me, but over complicated.
2) A scriptable animation sequence done real-time with your own game engine?
=> Try to tie your game engine with some scripting language like LUA. Honestly, I think you should leave this for now and try, at first, to solve the "game state" issue and make your game playable.

Bye!

Share this post


Link to post
Share on other sites
fooman_69    174
Thanks for the input!!
As for the animation sequence, it was guess #2 MajinMusashi. I was just really curious how developers go about doing this. For example, in 2D rpgs when they are talking and walking around unfolding a part of the story while u basically read and watch.

Anyways, in terms of using basic WIN32 keyboard abailities, how do you create a function such as
LRESULT CALLBACK WindowProc
? I have always just used this one function to capture my basic keyboard needs, and abviously I need to diversify now. Any tips on creating my own handling function?

Share this post


Link to post
Share on other sites

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