<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stephen McIntyre &#187; C++</title>
	<atom:link href="http://stephenmcintyre.net/category/blog/c-plus-plus/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Fri, 25 Nov 2011 00:10:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Handling Game State Management in C++</title>
		<link>http://stephenmcintyre.net/blog/c-plus-plus-state-management/</link>
		<comments>http://stephenmcintyre.net/blog/c-plus-plus-state-management/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 11:54:15 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=405</guid>
		<description><![CDATA[One thing that is initially overlooked in game design is handling menus and levels within the user interface. However, we know that it&#8217;s always going to be, and is, a crucial part to any successful game, as it helps provide the user with an extra level of control and eases general flow. It&#8217;s just a [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that is initially overlooked in game design is handling menus and levels within the user interface. However, we know that it&#8217;s always going to be, and is, a crucial part to any successful game, as it helps provide the user with an extra level of control and eases general flow. It&#8217;s just a case then of finding the code to do it.</p>
<p>This tutorial will cover a simple technique for handling <a href="http://wikipedia.org/wiki/State_management">state management</a> with enums or &#8220;<a href="http://wikipedia.org/wiki/Enumerated_type">enumerated types</a>&#8220;. If you have used state management or even just enums in C# before, then this will all feel very familiar to you. There are however some very subtle differences between the markup of the two languages.<span id="more-405"></span></p>
<p>First off, I&#8217;ll assume that you already have a program that runs inside of a window, ready for holding graphics and text. You should also have a main game loop that you use for any updates.</p>
<h3>Setting the Enum Type</h3>
<p>We start by setting our enum. It&#8217;s set like a structure, in the way that we can create an object with this enum as its type. It doesn&#8217;t hold any values itself though &#8211; the key is that it can only be set to certain values that you give it. I&#8217;ll cover this a bit better below.</p>
<p>This should go before your game loop (so that it doesn&#8217;t keep trying to set itself and cause errors).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> eGameState <span style="color: #009900;">&#123;</span>GSM_MENU<span style="color: #339933;">,</span> GSM_LEVEL<span style="color: #339933;">,</span> GSM_END<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Looks a lot like a structure, or even an array.</p>
<p>The first part should be okay. It&#8217;s just setting our new enum type and giving it the name of &#8220;eGameState&#8221; so we can use it later.</p>
<p>The stuff inside the braces are the values that you want it to hold. These technically don&#8217;t mean anything, as they&#8217;re just given IDs from 0 upwards, i.e. GSM_MENU = 0, GSM_LEVEL = 1&#8230;</p>
<p>Basically, each item in that list is turned into a constant (which is why I like to use capitals). It is similar to declaring them separately with <a href="http://msdn.microsoft.com/en-us/library/teas0593%28VS.80%29.aspx">#define</a> or <a href="http://msdn.microsoft.com/en-us/library/07x6b05d%28VS.80%29.aspx">const</a>.</p>
<p>The constants shouldn&#8217;t be defined before they go into the enum, otherwise you&#8217;ll get an error. Creating the type will do it for you.</p>
<h3>Declaring the Variable</h3>
<p>We then create an instance as we would for a class or struct:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">eGameState gameState <span style="color: #339933;">=</span> GSM_MENU<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can set this to be whatever option you like from the list that you put into your game state type, inside the braces. Remember, ONLY the options from the list can be used (the ones in capitals).</p>
<h3>The Switch-Case</h3>
<p>To check our <code>gameState</code> var, we set up a <a href="http://wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_php.2C_ActionScript">switch statement</a>. It means we can check one variable against lots of values, which is what we want. Plus it saves us dealing with loads of really annoying &#8220;else if&#8221; statements.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>gameState<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> GSM_MENU <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Menu state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> GSM_LEVEL <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Level state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> GSM_END <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// End state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This goes inside our main game loop.</p>
<p>We then write whatever we like into each part and treat it as a new state. For example, if you check for a button press to go from the menu into your level area, you just change the value of <code>gameState</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>buttonPressed<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  gameState <span style="color: #339933;">=</span> GSM_LEVEL<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the game loop updates again, the switch will then find that the <code>gameState</code> has changed and go to the new screen.</p>
<p>If you already have a game set up without state management, you can replace the code inside your main loop with this, and insert your original code into the &#8220;Level state&#8221; section.</p>
<p>That should cover it. Any questions, leave some comments below or contact me on Twitter <a href="http://twitter.com/_bigSteve">@_bigSteve</a>.</p>
<p>Have fun <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/c-plus-plus-state-management/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

