Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
OpenGL Game Development By Example

You're reading from   OpenGL Game Development By Example Design and code your own 2D and 3D games efficiently using OpenGL and C++

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781783288199
Length 340 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Madsen Madsen
Author Profile Icon Madsen
Madsen
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

State management


Think about it. If we want our game to pause, then we have to set some kind of flag that tells the game that we want it to take a break. We could set up a Boolean:

bool m_isPaused;

We would set m_isPaused to true if the game is paused, and set it to false if the game is running.

The problem with this approach is that there are a lot of special cases that we may run into in a real game. At any time the game might be:

  • Starting

  • Ending

  • Running

  • Paused

These are just some example of game states. A game state is a particular mode that requires special handling. As there can be so many states, we usually create a state manager to keep track of the state we are currently in.

Creating a state manager

The simplest version of a state manager begins with an enum that defines all of the game states. Open RoboRacer.cpp and add the following code just under the include statements:

enum GameState
{
  GS_Running,
  GS_Paused
};

Then go to the variable declarations block and add the following line:

GameState...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images