<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>KEY_ datatype</title>
		<link>http://www.allegro.cc/forums/view/604238</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 05 Jun 2010 01:55:05 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi</p><p>Basically I don&#39;t understand enumerations at all, and I want to make an array of key press enums.</p><p>Firstly, how would I declare this array?</p><p>Secondly, any recommended reading for enumerations for newbies?</p><p>Thanks
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Cameron)</author>
		<pubDate>Fri, 04 Jun 2010 03:03:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I havent got to learning those yet but I was given this link and it looks strait forward. I just dont need them atm to learn it.</p><p><a href="http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/">http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Fri, 04 Jun 2010 03:38:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868275#target">Chris Cameron</a> said:</div><div class="quote"><p> Basically I don&#39;t understand enumerations at all, and I want to make an array of key press enums.</p></div></div><p>Can you describe what you want them for? Making a set of enumeration instead of just using Allegro&#39;s predefined KEY_ macros my not be necessary.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 03:54:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I want to add a layer between the keyboard presses and the key[KEY_BLAH] stuff so that I can very quickly change controls in 1 header rather than mess around with various classes.</p><p>This is what I&#39;m attempting:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">int</span> IN_UP    <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  2</span><span class="k1">int</span> IN_DOWN    <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
<span class="number">  3</span><span class="k1">int</span> IN_LEFT    <span class="k3">=</span> <span class="n">2</span><span class="k2">;</span>
<span class="number">  4</span><span class="k1">int</span> IN_RIGHT  <span class="k3">=</span> <span class="n">3</span><span class="k2">;</span>
<span class="number">  5</span>
<span class="number">  6</span><span class="k1">int</span> in_keys<span class="k2">[</span>total_keys<span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>KEY_W, KEY_S KEY_A, KEY_D<span class="k2">}</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span><span class="k1">bool</span> inKey<span class="k2">(</span><span class="k1">int</span> <a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">)</span><span class="k2">{</span>
<span class="number">  9</span>  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>in_keys<span class="k2">[</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">]</span><span class="k2">]</span><span class="k2">)</span> <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 10</span>  <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 11</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Cameron)</author>
		<pubDate>Fri, 04 Jun 2010 04:22:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well in that case, you don&#39;t need an array:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">typedef</span> <span class="k1">enum</span> <span class="k2">{</span> IN_UP <span class="k3">=</span> KEY_W, IN_DOWN <span class="k3">=</span> KEY_S, IN_LEFT <span class="k3">=</span> KEY_D, IN_RIGHT <span class="k3">=</span> KEY_D <span class="k2">}</span> KeyDefs<span class="k2">;</span>

<span class="k1">bool</span> inKey<span class="k2">(</span>KeyDefs keyPressed<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>keyPressed<span class="k2">]</span><span class="k2">)</span> <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
  <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 04:38:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You do want the array if you want to change the mapping at runtime though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 04 Jun 2010 05:15:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As Tom said, I want to be able to change it at runtime.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Cameron)</author>
		<pubDate>Fri, 04 Jun 2010 05:18:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868300#target">Chris Cameron</a> said:</div><div class="quote"><p> As Tom said, I want to be able to change it at runtime.</p></div></div><p>That contradicts what you said earlier. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Doing it at runtime, I wouldn&#39;t use an enumeration at all.  Just read in the value the user wants to use for each, and store that in the applicable array offset.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 05:21:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s what I&#39;m trying to do. The only reason I mentioned enums at all is because I looked at keyboard.h and found the KEY_ stuff stored as enums.</p><p>My problem is that </p><p>int inkeys[x] = {KEY_W, KEY_A,. . .};</p><p>Is not correct as the keys are not integers. How should I declare this array?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Cameron)</author>
		<pubDate>Fri, 04 Jun 2010 05:37:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use the scancode value.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 05:38:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868308#target">Chris Cameron</a> said:</div><div class="quote"><p>Is not correct as the keys are not integers. How should I declare this array?</p></div></div><p>They are integers though. It&#39;ll be fine. Just use an int array.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 04 Jun 2010 06:21:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868331#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> They are integers though. It&#39;ll be fine. Just use an int array.</p></div></div><p>I thought C++ didn&#39;t allow you to use them interchangeably, or is it just that you&#39;re not allowed to use ints where it&#39;s expecting an enum?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 06:26:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can&#39;t ever remember the exact rules. Most of the time I never actually use the enum type itself, and just use ints where I&#39;m going to use the enum values.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 04 Jun 2010 06:27:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I looked it up<span class="ref"><sup>[<a href="#">1</a>]</sup></span>.  You can use the enum value in place of an int, but not the other way around.  So in this case, an int array with enum values will be fine.
</p><div class="ref-block"><h2>References</h2><ol><li><a href="http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/">http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/</a></li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 04 Jun 2010 06:29:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868302#target">LennyLen</a> said:</div><div class="quote"><p>
I wouldn&#39;t use an enumeration at all.  Just read in the value the user wants to use for each, and store that in the applicable array offset.
</p></div></div><p>
The array offset is what you would enumerate. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">enum</span> GAME_KEYS
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    KEY_FORWARD,
<span class="number">  4</span>    KEY_BACKWARD,
<span class="number">  5</span>    KEY_LEFT,
<span class="number">  6</span>    KEY_RIGHT,
<span class="number">  7</span>    KEY_JUMP,
<span class="number">  8</span>    KEY_FIRE
<span class="number">  9</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span><span class="k1">int</span> gamekeys<span class="k2">[</span><span class="n">6</span><span class="k2">]</span> <span class="k3">=</span>
<span class="number"> 12</span><span class="k2">{</span>
<span class="number"> 13</span>    KEY_W,
<span class="number"> 14</span>    KEY_S,
<span class="number"> 15</span>    KEY_A,
<span class="number"> 16</span>    KEY_D,
<span class="number"> 17</span>    KEY_SPACE,
<span class="number"> 18</span>    KEY_ENTER
<span class="number"> 19</span><span class="k2">}</span><span class="k2">;</span>
</div></div><p>
<i>(Note: the above is for demonstration purposes only. I would discourage a global.)</i></p><p>And for the record, an <span class="source-code"><span class="k1">int</span></span> can be cast to an <span class="source-code"><span class="k1">enum</span></span> type. Then again, you might be just as well declaring the game keys as separate <span class="source-code"><span class="k1">int</span></span>s since there doesn&#39;t seem to be any advantage to the array... At least none that springs to mind right now. I would certainly store them in a <span class="source-code"><span class="k1">struct</span></span> or <span class="source-code"><span class="k1">class</span></span> though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 04 Jun 2010 07:31:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604238/868333#target">LennyLen</a> said:</div><div class="quote"><p>
I thought C++ didn&#39;t allow you to use them interchangeably, or is it just that you&#39;re not allowed to use ints where it&#39;s expecting an enum?
</p></div></div><p>
C++ distinguishes 3 kinds of casts:<br />- <b>widening casts</b>, where a value is cast to a more general type (e.g. a char to an int, or an object to one of its superclasses, like std::cin to a std::istream). If the new type can represent every value that the old type can represent, then it&#39;s a widening cast. Widening casts may be performed implicitly.<br />- <b>narrowing casts</b>, the opposite of a widening cast. Because there is a possible loss of precision, or an incorrect pointer may be generated, C++ mandates that narrowing casts be explicit.<br />- <b>invalid casts</b>, where you try to cast to a completely incompatible type</p><p>Casting from an enum to int is widening, and thus allowed to be done implicitly. Casting from int to enum is narrowing, and must be done explicitly (keeping in mind that the cast may fail).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Sat, 05 Jun 2010 01:55:05 +0000</pubDate>
	</item>
</rss>
