<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] Simple Keypress?</title>
		<link>http://www.allegro.cc/forums/view/610536</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 29 Jun 2012 04:43:28 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is there a simple way with Allegro 5 to just wait for a keypress to continue?</p><p>I have a title screen.  In A4 I used to use keypress() to wait for the user to press a key to continue.</p><p>Please tell me I don&#39;t have to go through all the trouble to set up key events and such JUST to wait for a simple keypress to continue?</p><p>If Allegro doesn&#39;t have a simple method, what is a good way under C++?  I am about ready to switch back to simple C programming.</p><p>I googled it and get all sorts of stuff about cin.ignore(), cin.clear() etc... etc... and none of them work.  I can&#39;t see WHY Allegro 5 can&#39;t have some basic functions for simple things like waiting for any key to continue. &lt;sigh&gt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 28 Jun 2012 11:45:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Completely not tested, but here you go.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> wait_for_keypress<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a> <span class="k3">*</span>event_queue<span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> event<span class="k2">;</span>
  
  <a href="http://www.allegro.cc/manual/al_install_keyboard"><span class="a">al_install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  event_queue <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_event_queue"><span class="a">al_create_event_queue</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>event_queue, <a href="http://www.allegro.cc/manual/al_get_keyboard_event_source"><span class="a">al_get_keyboard_event_source</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>

  <span class="k1">do</span>
    <a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>event_queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">while</span> <span class="k2">(</span>event.type <span class="k3">!</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_DOWN<span class="k2">)</span><span class="k2">;</span>
  
  <a href="http://www.allegro.cc/manual/al_destroy_event_queue"><span class="a">al_destroy_event_queue</span></a><span class="k2">(</span>event_queue<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Usually you would have the event loop already, because you need it for other stuff.</p><p>By the way, the C and C++ standard libraries don&#39;t have any GUI-related functions, just console.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Thu, 28 Jun 2012 13:21:48 +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/610536/958884#target">Neil Roy</a> said:</div><div class="quote"><p>I googled it and get all sorts of stuff about cin.ignore(), cin.clear() etc... etc... and none of them work. I can&#39;t see WHY Allegro 5 can&#39;t have some basic functions for simple things like waiting for any key to continue. &lt;sigh&gt;</p></div></div><p>for any reasonable program using Allegro 5, it will surely have an event loop already set up. Then all you have to do is sit and wait for a key event.</p><p>You don&#39;t do things in Allegro 5 the way you did in Allegro 4. Trying to use the same &quot;paradigms&quot; will just cause you headaches.</p><p>It&#39;s a different api, and a different way of thinking.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 28 Jun 2012 13:41:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, it&#39;s difficult to re-organize my thinking.  I&#39;m used to C and Allegro 4, now trying to use C++ and Allegro 5.  I had a hard time sleeping last night, just had programming on my brain. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=";D" border="0" />, I realized what my mistake was, and that is I need to create an input class of some sort.</p><p>I had set up what was needed for getting input the normal Allegro 5 way, in a loop as discussed in other parts of the forum, but that&#39;s no good in a game that has separate functions.</p><p>After looking at the demos I noticed A5teroids has an input class, that helped me see how to do it properly I think, I haven&#39;t created it yet but I am enthusiastic again. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />  I am SO not used to programming in C++, I was ready to switch back to C but that would make no difference because as mentioned, I noticed that all the input examples online have to do with CONSOLE input, which I find strange in 2012.  </p><p>One thing I am learning, all the tutorials online in the world can&#39;t replace just experience, trial and error.  Most of the tutorials give bad examples and teach bad habits that don&#39;t work well in actual games.</p><p>&quot;The easier your solution is, the harder it will be on you later on&quot; seems to be pretty accurate. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />]</p><p>Edit 1: How would you handle keyboard input in C?  In C++ you have classes.  I assume there would be global variables for events?  It would be handy to know as I am still not so certain I want to program in C++. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Edit 2: Okay, I got it working without classes for now, although I DO want to try my hand at creating an input class.</p><p>I initialize <span class="source-code">queue</span> and <span class="source-code">timer</span> etc.. in an <span class="source-code">initialize<span class="k2">(</span><span class="k2">)</span></span> function, <span class="source-code">timer</span> and <span class="source-code">queue</span> are global variables (I&#39;m in trouble now)...</p><p>Oh, <span class="source-code">display</span> is also a global, I originally done this so I can have a <span class="source-code">shutdown<span class="k2">(</span><span class="k2">)</span></span> function that can be called from anywhere in the program which destroys bitmaps and other such things.  Any suggestions for a better way is welcome, but this works, my globals are all grouped together so I can easily track them.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> initialize<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
   <span class="c">// other initialize stuff here</span>

   timer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_timer"><span class="a">al_create_timer</span></a><span class="k2">(</span><span class="n">1</span>.<span class="n">0</span> <span class="k3">/</span> <span class="n">60</span><span class="k2">)</span><span class="k2">;</span>
   queue <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_event_queue"><span class="a">al_create_event_queue</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>queue, <a href="http://www.allegro.cc/manual/al_get_keyboard_event_source"><span class="a">al_get_keyboard_event_source</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>queue, <a href="http://www.allegro.cc/manual/al_get_display_event_source"><span class="a">al_get_display_event_source</span></a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>queue, <a href="http://www.allegro.cc/manual/al_get_timer_event_source"><span class="a">al_get_timer_event_source</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_start_timer"><span class="a">al_start_timer</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

and then I have the <span class="source-code">wait_for_keypress<span class="k2">(</span><span class="k2">)</span></span> in it&#39;s own function...</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> wait_for_keypress<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">do</span>
    <a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">while</span> <span class="k2">(</span>event.type <span class="k3">!</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_DOWN<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Now that I have this working, I think it should be easier to translate it into an input class if I so choose.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 28 Jun 2012 20:17:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In a C++ allegro 5 project, I&#39;d probably setup a &quot;world&quot; controller class that takes input from allegro via a method, then either acts on that directly, or passes input to various entities in the world. Or both.</p><p>Either way, my game entities wouldn&#39;t normally take ALLEGRO_EVENT&#39;s rather they&#39;d have callback methods for keyUP, keyDOWN or maybe a higher level engine specific event object.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/610536/958904#target">Neil Roy</a> said:</div><div class="quote"><p>Now that I have this working, I think it should be easier to translate it into an input class if I so choose.</p></div></div><p>You could do that, but it&#39;d completely block any animation you might want to do, and if the display context is &quot;lost&quot; you&#39;d end up with a completely black screen (or filled with random contents). So that solution isn&#39;t entirely ideal. I might suggest setting a flag that your event loop checks for to &quot;pause&quot; the game (block events from being passed to the world entities, but still handle basic stuff like display flipping, and any other basic stuff), and wait for a keypress, or even a specific key. Depending.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 29 Jun 2012 02:02:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have this in my <span class="source-code">main<span class="k2">(</span><span class="k2">)</span></span> currently.  I still have a lot of work to do on this project and quit a bit will change.  I just wanted a simple way to wait for a key press on an opening title screen that will only be seen once.  </p><p>This works so far, I&#39;m feeling less  and less comfortable with C++, currently my project is being compiled as C99.  But I may still switch back, old habits die hard. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></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">while</span><span class="k2">(</span><span class="k3">!</span>done<span class="k2">)</span>
<span class="number">  2</span>   <span class="k2">{</span>
<span class="number">  3</span>      <span class="k1">do</span>
<span class="number">  4</span>      <span class="k2">{</span>
<span class="number">  5</span>         <a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span>         <span class="k1">if</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_DISPLAY_CLOSE<span class="k2">)</span> done <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span>         <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_DOWN<span class="k2">)</span>
<span class="number"> 10</span>         <span class="k2">{</span>
<span class="number"> 11</span>            <a href="http://www.allegro.cc/manual/al_get_keyboard_state"><span class="a">al_get_keyboard_state</span></a><span class="k2">(</span><span class="k3">&amp;</span>keys<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>            <span class="k1">if</span><span class="k2">(</span>event.keyboard.keycode <span class="k3">=</span><span class="k3">=</span> ALLEGRO_KEY_ESCAPE<span class="k2">)</span> done <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 13</span>            <span class="c">// code removed for brevity</span>
<span class="number"> 14</span>         <span class="k2">}</span>
<span class="number"> 15</span>         <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_UP<span class="k2">)</span>
<span class="number"> 16</span>         <span class="k2">{</span>
<span class="number"> 17</span>            <a href="http://www.allegro.cc/manual/al_get_keyboard_state"><span class="a">al_get_keyboard_state</span></a><span class="k2">(</span><span class="k3">&amp;</span>keys<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>            <span class="c">// code removed for brevity</span>
<span class="number"> 19</span>         <span class="k2">}</span>
<span class="number"> 20</span>
<span class="number"> 21</span>         <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_TIMER<span class="k2">)</span>
<span class="number"> 22</span>         <span class="k2">{</span>
<span class="number"> 23</span>            <span class="c">// code removed for brevity         </span>
<span class="number"> 24</span>            redraw <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 25</span>         <span class="k2">}</span>
<span class="number"> 26</span>      <span class="k2">}</span>
<span class="number"> 27</span>      <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_is_event_queue_empty"><span class="a">al_is_event_queue_empty</span></a><span class="k2">(</span>queue<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>
<span class="number"> 29</span>      <span class="k1">if</span><span class="k2">(</span>redraw<span class="k2">)</span>
<span class="number"> 30</span>      <span class="k2">{</span>
<span class="number"> 31</span>         redraw <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 32</span>
<span class="number"> 33</span>         <a href="http://www.allegro.cc/manual/al_clear_to_color"><span class="a">al_clear_to_color</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 34</span>
<span class="number"> 35</span>         <a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span>font_Amaze, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">0</span><span class="k2">)</span>, display_w<span class="k3">/</span><span class="n">2</span>, display_h<span class="k3">/</span><span class="n">2</span><span class="k3">-</span><span class="n">50</span>, ALLEGRO_ALIGN_CENTRE, <span class="s">"Do stuff here."</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 36</span>
<span class="number"> 37</span>         <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 38</span>      <span class="k2">}</span>
<span class="number"> 39</span>   <span class="k2">}</span>
</div></div><p>

In my old game (this is a new version, I am rewriting) I had code in  that detected when the game lost focus (alt+tab) and paused the game.  I&#39;m not sure how that works now, if that is what you are talking about.  I just started on this recently so it isn&#39;t too far along to change.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Fri, 29 Jun 2012 04:10:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can detect when the display/window is tab&#39;ed out of, or display is lost for any other reason.</p><p>But if you&#39;re looping in your wait_for_keypress function, you can&#39;t handle any of that. So I just suggested moving that wait logic into the event loop. when you&#39;re waiting for a keypress, just skip most of the logic.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 29 Jun 2012 04:19:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OH, you mean in my wait for key press, change it so that it can detect when the game loses focus?  (also in the main game loop)</p><p>If it looses focus, skip any key processing until it regains it back.</p><p>Also, I used to have my while() loop set up like you originally suggested, then I changed it to add in  that do while() loop, now I am wondering if I shouldn&#39;t go back to the way you suggested so that if the game loses focus I could more easily jump to the draw section briefly to update the screen?</p><p>So much to think about...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Fri, 29 Jun 2012 04:40:00 +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/610536/958950#target">Neil Roy</a> said:</div><div class="quote"><p>OH, you mean in my wait for key press, change it so that it can detect when the game loses focus? (also in the main game loop)</p></div></div><p>That, or make &quot;wait_for_keypress&quot; just set a flag, that the main game loop notices and takes care of.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 29 Jun 2012 04:43:28 +0000</pubDate>
	</item>
</rss>
