Click here to Skip to main content
15,607,524 members
Articles / Desktop Programming / MFC
Posted 13 Sep 2001


85 bookmarked

State Pattern in C++ Applications

Rate me:
Please Sign up or sign in to vote.
4.68/5 (31 votes)
13 Sep 20014 min read
Small game framework based on the state pattern


The state pattern is very useful when you need an object to alter its behaviour when this object’s state changes. The purpose of this article is to give you an example of the State pattern in action. In that case, the State pattern has been applied to a small game framework.

During the last year, I have written some small DirectX games. One of the problems I’ve encountered was the transition between game states. For example, when the game starts, I was usually displaying an Introduction page on which you could see a menu showing my game options. Then, according to the user choice, I had to start the game, show the high scores or show something else. I’ve solved my problem by considering all those choices as states.

Below is a diagram showing my game logic built on the State pattern.

Class diagram

In the application class (CStatePattern_GameApp), we have a function called Run(), which provides a default message loop. Basically, this function updates the game and then processes all other messages. The Update function simply asks the view to render the current frame.

To avoid overhead, the DoFrame function will check if it needs to redraw something, according to the frame rate, or not. I won’t give too much detail on the frame rate stuff since this is not the purpose of my article. But if you are interested, all the code related to the frame rate stuff is located in the CStatePattern_GameView class.

Let’s get back to the State pattern. In the next section, I will explain what happens when the DoFrame function is called. There are two main parts in this function. The first thing that gets done is the game update. Usually, this is where you would update all game objects in the current state (ship position, collision detection, etc.):


The second part is the game rendering:

m_GameManager.Draw(pDC /* device context */ );

The game manager holds a pointer to a CGameState object. Therefore, when we call the Update function of the CGameManager object, this will simply redirect the call to the current state object:


This call represents a big part of the State pattern. As you can see, we are calling the Update function of the current state object, but we are also giving a pointer to ourselves as a parameter. The reason is that after performing state-specific work, the Update function of the current state object will be able to change the current state of the game.

An important thing to understand is that the CGameManager object doesn’t know a thing about the game. It is the CGameState subclasses that will define the game logic and each state transition as the game goes by.

The declaration above is really important. For example, let’s say that in our game, when the user hits the space bar, the ship fires. Basically, the CStatePattern_GameView will receive a key event. The view will simply redirect the event to the game manager. The game manager does not know anything about the game logic, so it redirects this event to the current state object. If PlayState is the current state, the KeyEvent function of that state will check which key was struck. In this case, it’s going to fire. If the current state would have been IntroductionState, this event could have had a totally different effect.


State transitions are another important part of the pattern.

For example, the Update function of the CPlayState could check if the player has any ships left. If this is not the case, this function would call the ChangeState function of the CGameManager like so:

pGameManager->ChangeState(CGameOverState::Instance() /* singleton pattern */);
The states are not changed by the game manager, but by the state itself.

The declaration above might look strange, but when you think about it, it’s just logic. Let’s say that the player needs to press F1 to start a game while he is in the introduction page. The introduction page knows what to do when the user presses F1. In that case, it will tell the game manager to change the current state to PlayState.

Demo Project

In my demo project, I simply demonstrate some state transitions. When the game starts, we are in Introduction state. This state will wait for the user to press a key. According to the key pressed, the Introduction state will change the CGameManager’s state. F1 will switch to the CPlayState, and F2 will switch to the CHighScoreState.

In my demo project, I have made some TRACE calls to show you the details of each transition.


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionBackdoor Pin
morten1128-Jul-13 22:55
morten1128-Jul-13 22:55 
GeneralA few questions Pin
Benang26-Dec-06 19:27
Benang26-Dec-06 19:27 
GeneralBoard Game Problem Pin
viethuy21-May-06 17:48
viethuy21-May-06 17:48 
GeneralA State Pattern UML Tool in Standard C Pin
Jerome_D15-Apr-06 4:08
Jerome_D15-Apr-06 4:08 
QuestionWhich UML Tool used ? Pin
anand_study24-Jan-05 20:14
anand_study24-Jan-05 20:14 
AnswerRe: Which UML Tool used ? Pin
Daniel Larocque25-Jan-05 1:37
Daniel Larocque25-Jan-05 1:37 
GeneralSome special cases (the good one) Pin
Cedric Moonen29-Nov-04 1:51
Cedric Moonen29-Nov-04 1:51 
GeneralRe: Some special cases (the good one) Pin
Cedric Moonen29-Nov-04 9:26
Cedric Moonen29-Nov-04 9:26 
General!Some special cases Pin
Cedric Moonen29-Nov-04 1:44
Cedric Moonen29-Nov-04 1:44 
GeneralState Pattern in C# Applications [modified] Pin
Axel Rietschin19-Oct-04 22:12
professionalAxel Rietschin19-Oct-04 22:12 
GeneralBusy-wait Pin
richard mcgrath25-Jul-03 14:02
richard mcgrath25-Jul-03 14:02 
GeneralState Pattern Pin
Rajasekaran R3-Sep-02 3:19
Rajasekaran R3-Sep-02 3:19 
GeneralRe: State Pattern Pin
Binarygb21-Sep-03 14:06
Binarygb21-Sep-03 14:06 
GeneralA suggestion Pin
Anonymous18-Jul-02 20:43
Anonymous18-Jul-02 20:43 
GeneralGood Pin
NormDroid17-Sep-01 22:38
professionalNormDroid17-Sep-01 22:38 
Generalthanx Pin
16-Sep-01 19:16
suss16-Sep-01 19:16 
GeneralRe: thanx Pin
17-Sep-01 17:54
suss17-Sep-01 17:54 
GeneralKeep patterning... Pin
Paul Selormey15-Sep-01 20:34
Paul Selormey15-Sep-01 20:34 
GeneralRe: Keep patterning... Pin
NormDroid17-Sep-01 23:50
professionalNormDroid17-Sep-01 23:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.