<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] Threading a game</title>
		<link>http://www.allegro.cc/forums/view/608082</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 14 Aug 2011 23:13:12 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have been trying to wrap my head around threads and I have come up with two questions:<br />1. Is there a way to send events from one thread to another? In particular user events, so that I can send costum information(name of button pressed, upgrade used, etc.). I was thinking of having an event queue in the shared data class which would listen to such user events.<br />2. Is it possible to use functions of an object as the main function of a thread? I could than have a AI::Think function which would try to find the other player&#39;s cannons etc.</p><p>Also, according to <a href="http://www.cplusplus.com/forum/lounge/30606/">this thread</a>, threading can make modularising your application way easier if you got the mutexes right. Did I understand that correctly?</p><p>Any help?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Thu, 11 Aug 2011 22:52:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1. Send them all to a &#39;global&#39; queue. &quot;Listening&quot; is a big word.<br />2. With Boost maybe? No, otherwise.</p><p>I don&#39;t know what the link is about. Just fiddle around until you understand it <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Thu, 11 Aug 2011 23: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/608082/927961#target">weapon_S</a> said:</div><div class="quote"><p> Send them all to a &#39;global&#39; queue.</p></div></div><p>
How do you mean? What I thought of doing was having an event queue in a data class(where you can put a mutex on) and sending costum events to it so that the parent thread can get informed about what happened.</p><p>Example of usage:<br />1. Game initialisation<br />2. Main thread starts AI thread<br />3. AI thinks while main thread is handling user input etc.<br />4. AI wants to make a move and sends an event with all necessary data in it to the event queue in the data class<br />5. main thread listens to the event queue(how should this happen? there is always the possibility of a mutex being on the queue at the time) in the data class and reacts accordingly by making the move.<br />6. Go back to step 3
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Thu, 11 Aug 2011 23:39:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, I believe event queues were explicitly designed to be thread safe. So you can send events from one thread and receive events in another.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 11 Aug 2011 23:48:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I just came up with this to let this happen:<br />in the data class, I should have an ALLEGRO_USER_EVENT_SOURCE* member, which is set when the thread first starts. Then, in the main thread, I register this event source in the event queue of the main thread, and whenever the AI makes its move, it uses al_emit_user_event to notify the main thread it made a move.</p><p>Is my logic sound/should this work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 12 Aug 2011 00:12:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yup, though you may also have the option of emitting fake input events. Which ever works best for your case.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 12 Aug 2011 00:20:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think the AI will want to just say what he wants to do, instead of faking a mouse click or something like that. I&#39;m also thinking of putting user input in another thread with the same principle: as soon as the player really chooses to send a unit(scout,bomb,other upgrade) to the field, the main thread will be noticed trough a user event.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 12 Aug 2011 00:27:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Some people prefer to simulate input events for AI, so it can be recorded in the same way user input is recorded, for replays, save games, and cheat detection. But I think if you&#39;re emitting user events for the player as well, you can use those to do all that instead of raw input.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 12 Aug 2011 00:34:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah... I think it is easier to let the user input thread figure out what the user wants to do and then send the corresponding event to the main thread. Otherwise, I would have to put extra code in at the AI output part to calculate all the coordinates of mouse clicks etc.</p><p>One technical question that still hasn&#39;t been answered: How do I make a function pointer to a member function? Because that&#39;s what I&#39;ll need if I want to use the AI&#39;s Think() function as main function of the thread.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 12 Aug 2011 00:39:28 +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/608082/927953#target">J-Gamer</a> said:</div><div class="quote"><p>
2. Is it possible to use functions of an object as the main function of a thread? I could than have a AI::Think function which would try to find the other player&#39;s cannons etc.
</p></div></div><p>
Yes, you can use class methods, they just have to be static, and you have to bind the parameters somehow.</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="number">  2</span><span class="k1">class</span> ArgumentBinder <span class="k2">{</span>
<span class="number">  3</span><span class="k1">private</span> <span class="k2">:</span>
<span class="number">  4</span>   <span class="k1">int</span> p1<span class="k2">;</span>
<span class="number">  5</span>   Data<span class="k3">*</span> d<span class="k2">;</span>
<span class="number">  6</span><span class="k1">public</span> <span class="k2">:</span>
<span class="number">  7</span>   <span class="k1">static</span> <span class="k1">void</span> Process<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thread , <span class="k1">void</span><span class="k3">*</span> <span class="k1">this</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  8</span>      <span class="k2">(</span><span class="k2">(</span>ArgumentBinder<span class="k3">*</span><span class="k2">)</span><span class="k1">this</span><span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>ClassMethod<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>   <span class="k2">}</span>
<span class="number"> 10</span>
<span class="number"> 11</span>   <span class="k1">void</span> ClassMethod<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 12</span>      <span class="c">// do stuff with members</span>
<span class="number"> 13</span>   <span class="k2">}</span>
<span class="number"> 14</span><span class="k2">}</span>
<span class="number"> 15</span>
<span class="number"> 16</span>ArgumentBinder bound_data<span class="k2">;</span>
<span class="number"> 17</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thread <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_thread"><span class="a">al_create_thread</span></a><span class="k2">(</span>ArgumentBinder::Process , <span class="k3">&amp;</span>bound_data<span class="k2">)</span><span class="k2">;</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 12 Aug 2011 05:37:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks! I&#39;ll try that.</p><p>EDIT:<br />This is what I&#39;ve come up with so far:
</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="p">#ifndef __THREADED_H__</span>
<span class="number">  2</span><span class="p">#define __THREADED_H__</span>
<span class="number">  3</span><span class="k1">class</span> Threaded<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="k1">class</span> ThreadedData
<span class="number">  6</span><span class="k2">{</span>
<span class="number">  7</span>    public:
<span class="number">  8</span>        <a href="http://www.allegro.cc/manual/ALLEGRO_MUTEX"><span class="a">ALLEGRO_MUTEX</span></a><span class="k3">*</span> mutex<span class="k2">;</span>
<span class="number">  9</span>        <a href="http://www.allegro.cc/manual/ALLEGRO_COND"><span class="a">ALLEGRO_COND</span></a><span class="k3">*</span> cond<span class="k2">;</span>
<span class="number"> 10</span>        <span class="k1">bool</span> ready<span class="k2">;</span>
<span class="number"> 11</span>        Threaded<span class="k3">*</span> thr<span class="k2">;</span> <span class="c">//used in Threaded::InitThread</span>
<span class="number"> 12</span>        ThreadedData<span class="k2">(</span><span class="k2">)</span> <span class="k2">:</span> mutex<span class="k2">(</span><a href="http://www.allegro.cc/manual/al_create_mutex"><span class="a">al_create_mutex</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span>,cond<span class="k2">(</span><a href="http://www.allegro.cc/manual/al_create_cond"><span class="a">al_create_cond</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span>,ready<span class="k2">(</span><span class="k1">false</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 14</span>
<span class="number"> 15</span><span class="k1">class</span> Threaded
<span class="number"> 16</span><span class="k2">{</span>
<span class="number"> 17</span>    private:
<span class="number"> 18</span>    public:
<span class="number"> 19</span>        <span class="k1">static</span> <span class="k1">void</span> InitThread<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr, <span class="k1">void</span><span class="k3">*</span> data<span class="k2">)</span>
<span class="number"> 20</span>        <span class="k2">{</span>
<span class="number"> 21</span>            <span class="k2">(</span>ThreadedData<span class="k3">*</span><span class="k2">)</span>data-&gt;thr-&gt;Thread_function<span class="k2">(</span><span class="k2">(</span>ThreadedData<span class="k3">*</span><span class="k2">)</span>data<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>        <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 23</span>        <span class="k1">virtual</span> <span class="k1">void</span> Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr,ThreadedData<span class="k3">*</span> data<span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 24</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 25</span><span class="p">#endif</span>
</div></div><p>
These are classes you should derive from, like this:
</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">class</span> ThreadedUIData <span class="k2">:</span> <span class="k1">public</span> ThreadedData
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    public:
<span class="number">  4</span>        <span class="c">//other variables you need to be shared between your class and the parent thread</span>
<span class="number">  5</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k1">class</span> ThreadedUI <span class="k2">:</span> <span class="k1">public</span> ThreadedData
<span class="number">  8</span><span class="k2">{</span>
<span class="number">  9</span>    private:
<span class="number"> 10</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>
<span class="number"> 11</span>        
<span class="number"> 12</span>    public:
<span class="number"> 13</span>        <span class="k1">void</span> Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr,ThreadedData<span class="k3">*</span> data<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 15</span>
<span class="number"> 16</span><span class="k1">void</span> ThreadedUI::Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr, ThreadedData<span class="k3">*</span> data<span class="k2">)</span>
<span class="number"> 17</span><span class="k2">{</span>
<span class="number"> 18</span>    <span class="c">//initialize variables</span>
<span class="number"> 19</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>
<span class="number"> 20</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_mouse_event_source"><span class="a">al_get_mouse_event_source</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</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="number"> 22</span>    
<span class="number"> 23</span>    
<span class="number"> 24</span>    <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>    ThreadedUIData<span class="k3">*</span> ui_data <span class="k3">=</span> <span class="k2">(</span>ThreadedUIData<span class="k3">*</span><span class="k2">)</span>data<span class="k2">;</span>
<span class="number"> 26</span>    <span class="c">//do something with the fields of ThreadedUIData* ui_data</span>
<span class="number"> 27</span>    <a href="http://www.allegro.cc/manual/al_signal_cond"><span class="a">al_signal_cond</span></a><span class="k2">(</span>data-&gt;cond<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>    data-&gt;ready <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 29</span>    <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 30</span>    
<span class="number"> 31</span>    <span class="c">//main thread loop</span>
<span class="number"> 32</span>    <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>al_thread_should_stop<span class="k2">(</span>thr<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 33</span>    <span class="k2">{</span>
<span class="number"> 34</span>        <span class="c">//check the event queue for input events and send a costum event if something important has been selected</span>
<span class="number"> 35</span>    <span class="k2">}</span>
<span class="number"> 36</span><span class="k2">}</span><span class="k2">;</span>
</div></div><p>
Or am I doing something terribly wrong here?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sat, 13 Aug 2011 23:37:13 +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/608082/927980#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> Some people prefer to simulate input events for AI, so it can be recorded in the same way user input is recorded, for replays, save games, and cheat detection.</p></div></div><p>Err, what? I&#39;ve never heard of that before. <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>Even if you did do that, how would you prevent the user input from affecting the AI player?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 14 Aug 2011 00:10:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>IIRC that&#39;s how they did it in Quake, all actors have the same input interface, some are controlled by AI.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Sun, 14 Aug 2011 00:13:16 +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/608082/928173#target">BAF</a> said:</div><div class="quote"><p>Even if you did do that, how would you prevent the user input from affecting the AI player?</p></div></div><p>The AI is essentially just another player with access to no more control or information as the player is.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Aug 2011 00:16:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, but why would you simulate input events? How would you prevent the user from making those same inputs, thereby screwing wth the AI?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 14 Aug 2011 00:56:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The &quot;inputs&quot; to an actor are just software interfaces, not actually hardwired to the mouse or anything.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Sun, 14 Aug 2011 00:59:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, but TF was talking about emitting fake input events. If you&#39;re faking input events to control the AI, then it looks the same as normal input events.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 14 Aug 2011 01:01:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The AI actors inputs are sent &quot;events&quot; by the AI.  Your protagonist actor is sent &quot;events&quot; from the mouse and keyboard, etc.</p><p>Think of a computer controlled car that has servos hooked to the steering wheel and pedals, and you&#39;re not in it.  You race this car by driving another car normally.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Sun, 14 Aug 2011 01:04:21 +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/608082/928179#target">BAF</a> said:</div><div class="quote"><p>Yes, but TF was talking about emitting fake input events. If you&#39;re faking input events to control the AI, then it looks the same as normal input events.</p></div></div><p>Depends. If you went that low level, you could simulate a second mouse/keyboard. But &quot;Input Events&quot; does not necessarily mean low level input events. But yeah, if Allegro doesn&#39;t have a way to distinguish between multiple low level input devices, that would make it difficult to go that low level.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Aug 2011 01:05:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, I understand. I am questioning TF&#39;s solution of handling threading by putting fake input events into the queue from the other thread.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 14 Aug 2011 01:07:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This:
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608082/928181#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> But &quot;Input Events&quot; does not necessarily mean low level input events</p></div></div><p>It&#39;s not a very hard concept to grasp. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Sun, 14 Aug 2011 01:08:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The original description was ambiguous at best, and that was what I was referring to. I understand the intended meaning...</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608082/927980#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> Some people prefer to simulate input events for AI, so it can be recorded in the same way user input is recorded, for replays, save games, and cheat detection.
</p></div></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 14 Aug 2011 01:10:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t think you would put them on the same queue. For example, you could have an Entity class that has a ALLEGRO_EVENT *GetInput(void) method that could return ALLEGRO_EVENTS like key presses and releases and the &quot;Game&quot; class would handle them. Then you can do things like:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> num_entities<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
    <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> <span class="k3">*</span>e <span class="k3">=</span> entity<span class="k2">[</span>i<span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>GetInput<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">if</span> <span class="k2">(</span>e<span class="k2">)</span> <span class="k2">{</span>
        <span class="k1">if</span> <span class="k2">(</span>e-&gt;type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEYBOARD<span class="k2">)</span> <span class="k2">{</span>
            <span class="k1">switch</span> <span class="k2">(</span>e-&gt;keyboard.keycode<span class="k2">)</span> <span class="k2">{</span>
                ...
            <span class="k2">}</span>
        <span class="k2">}</span>
    <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

And you could have some typedef:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">typedef</span> <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> <span class="k3">*</span><span class="k2">(</span>input_getter<span class="k2">)</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Then your player class could have</p><div class="source-code snippet"><div class="inner"><pre>private:
    input_getter input<span class="k2">;</span>
</pre></div></div><p>

And when you wanted to change input you could just set &#39;input&#39; to PhysicalInput(), ScriptInput(), NoInput() or whatever.</p><p>None of this really relies on using ALLEGRO_EVENTs though. But generally having a standard interface to get input from can be useful.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 14 Aug 2011 01:15: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/608082/928184#target">BAF</a> said:</div><div class="quote"><p>The original description was ambiguous at best, and that was what I was referring to. I understand the intended meaning...</p></div></div><p>It was meant to cover both low level injecting, and using higher level events, but both would still be input simulation as far as the engine is concerned.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Aug 2011 01:59:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Bugfix: The code I posted had 2 errors, which are fixed in this renewed code:
</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="p">#ifndef __THREADED_H__</span>
<span class="number">  2</span><span class="p">#define __THREADED_H__</span>
<span class="number">  3</span><span class="k1">class</span> Threaded<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="k1">class</span> ThreadedData
<span class="number">  6</span><span class="k2">{</span>
<span class="number">  7</span>    public:
<span class="number">  8</span>        <a href="http://www.allegro.cc/manual/ALLEGRO_MUTEX"><span class="a">ALLEGRO_MUTEX</span></a><span class="k3">*</span> mutex<span class="k2">;</span>
<span class="number">  9</span>        <a href="http://www.allegro.cc/manual/ALLEGRO_COND"><span class="a">ALLEGRO_COND</span></a><span class="k3">*</span> cond<span class="k2">;</span>
<span class="number"> 10</span>        <span class="k1">bool</span> ready<span class="k2">;</span>
<span class="number"> 11</span>        Threaded<span class="k3">*</span> thr<span class="k2">;</span> <span class="c">//used in Threaded::InitThread</span>
<span class="number"> 12</span>        ThreadedData<span class="k2">(</span><span class="k2">)</span> <span class="k2">:</span> mutex<span class="k2">(</span><a href="http://www.allegro.cc/manual/al_create_mutex"><span class="a">al_create_mutex</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span>,cond<span class="k2">(</span><a href="http://www.allegro.cc/manual/al_create_cond"><span class="a">al_create_cond</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span>,ready<span class="k2">(</span><span class="k1">false</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 13</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 14</span>
<span class="number"> 15</span><span class="k1">class</span> Threaded
<span class="number"> 16</span><span class="k2">{</span>
<span class="number"> 17</span>    private:
<span class="number"> 18</span>    public:
<span class="number"> 19</span>        <span class="k1">static</span> <span class="k1">void</span> InitThread<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr, <span class="k1">void</span><span class="k3">*</span> data<span class="k2">)</span>
<span class="number"> 20</span>        <span class="k2">{</span>
<span class="number"> 21</span>            <span class="k2">(</span><span class="k2">(</span>ThreadedData<span class="k3">*</span><span class="k2">)</span>data<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>thr-&gt;Thread_function<span class="k2">(</span>thr,<span class="k2">(</span>ThreadedData<span class="k3">*</span><span class="k2">)</span>data<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>        <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 23</span>        <span class="k1">virtual</span> <span class="k1">void</span> Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr,ThreadedData<span class="k3">*</span> data<span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 24</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 25</span>
<span class="number"> 26</span><span class="k1">class</span> ThreadedUIData <span class="k2">:</span> <span class="k1">public</span> ThreadedData
<span class="number"> 27</span><span class="k2">{</span>
<span class="number"> 28</span>    public:
<span class="number"> 29</span>        <span class="c">//other variables you need to be shared between your class and the parent thread</span>
<span class="number"> 30</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 31</span>
<span class="number"> 32</span><span class="k1">class</span> ThreadedUI <span class="k2">:</span> <span class="k1">public</span> ThreadedData
<span class="number"> 33</span><span class="k2">{</span>
<span class="number"> 34</span>    private:
<span class="number"> 35</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>
<span class="number"> 36</span>
<span class="number"> 37</span>    public:
<span class="number"> 38</span>        <span class="k1">void</span> Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr,ThreadedData<span class="k3">*</span> data<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 39</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 40</span>
<span class="number"> 41</span><span class="k1">void</span> ThreadedUI::Thread_function<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a><span class="k3">*</span> thr, ThreadedData<span class="k3">*</span> data<span class="k2">)</span>
<span class="number"> 42</span><span class="k2">{</span>
<span class="number"> 43</span>    <span class="c">//initialize variables</span>
<span class="number"> 44</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>
<span class="number"> 45</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_mouse_event_source"><span class="a">al_get_mouse_event_source</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 46</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="number"> 47</span>
<span class="number"> 48</span>
<span class="number"> 49</span>    ThreadedUIData<span class="k3">*</span> ui_data <span class="k3">=</span> <span class="k2">(</span>ThreadedUIData<span class="k3">*</span><span class="k2">)</span>data<span class="k2">;</span>
<span class="number"> 50</span>    <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>ui_data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 51</span>    <span class="c">//do something with the fields of ThreadedUIData</span>
<span class="number"> 52</span>    ui_data-&gt;ready <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 53</span>    <a href="http://www.allegro.cc/manual/al_signal_cond"><span class="a">al_signal_cond</span></a><span class="k2">(</span>ui_data-&gt;cond<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 54</span>    <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>ui_data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 55</span>
<span class="number"> 56</span>    <span class="c">//main thread loop</span>
<span class="number"> 57</span>    <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_get_thread_should_stop"><span class="a">al_get_thread_should_stop</span></a><span class="k2">(</span>thr<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 58</span>    <span class="k2">{</span>
<span class="number"> 59</span>        <span class="c">//do stuff</span>
<span class="number"> 60</span>    <span class="k2">}</span>
<span class="number"> 61</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 62</span>
<span class="number"> 63</span><span class="p">#endif</span>
</div></div><p>

EDIT: about input, I&#39;m thinking of making costum events for using upgrades(scout,bomb,tunneler,...) so that those can be sent to the main game logic thread instead of raw input. I&#39;ll let the UI thread find out which upgrade the player wants to place where.(High-level input FTW <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />)</p><p>EDIT2: Are allegro calls thread-safe?(Like al_get_display_width/height,...)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 14 Aug 2011 15:22:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I also have a threading related question. Guess I&#39;ll just ask it here:</p><p>When should I use mutexes? I know you&#39;re supposed to use them when, for example, accessing linked lists or something similiar. However, somewhere I read that you have to use them even when you&#39;re just using shared variables or even shared functions. Is that correct? I can hardly imagine, because that would surely make a lot of mutexes.</p><p>Also can someone here recommend good thread setups? Right now I have one thread for the logic loop and one thread for the screen loop. For a simple 2D platformer like I&#39;m making, I guess, that&#39;s alright, although I&#39;m only using up to two cores, but generally speaking: Is there any recommended thread setup for games? How many threads should you use and what for?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Sun, 14 Aug 2011 18:53: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/608082/928210#target">J-Gamer</a> said:</div><div class="quote"><p> Are allegro calls thread-safe?
</p></div></div><p>Yes and no. It&#39;s not documented, and I&#39;m not even sure which calls are and aren&#39;t. Other than events, you should basically assume the calls are not thread safe.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608082/928213#target">RPG Hacker</a> said:</div><div class="quote"><p> Guess I&#39;ll just ask it here:
</p></div></div><p>Don&#39;t hijack other people&#39;s threads.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Sun, 14 Aug 2011 19:05:10 +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/608082/928215#target">Matthew Leverton</a> said:</div><div class="quote"><p> Don&#39;t hijack other people&#39;s threads.</p></div></div><p>I don&#39;t see the problem, as it&#39;s perfectly related to the thread&#39;s title and can most likely help the thread creator as well. If you still think my post doesn&#39;t fit then tell me and I&#39;ll create a new one.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Sun, 14 Aug 2011 20:05:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To answer your question:<br />AFAIK the only time you need to use mutexes is when there&#39;s the possibility two threads will try to access the same variable at the same time. That&#39;s the whole point of mutexes and conditions.<br />I didn&#39;t know that you need them when accessing lists etc... seems strange.</p><p>EDIT: about thread setup, I was thinking of doing it this way:<br />Main thread where the game logic is executed<br />User input thread<br />AI thread<br />Render thread</p><p>The UI and AI threads send costum events to the main thread, which I explained before.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 14 Aug 2011 20:11:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the response.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608082/928219#target">J-Gamer</a> said:</div><div class="quote"><p> AFAIK the only time you need to use mutexes is when there&#39;s the possibility two threads will try to access the same variable at the same time. That&#39;s the whole point of mutexes and conditions.<br /> I didn&#39;t know that you need them when accessing lists etc... seems strange.</p></div></div><p>Well, if accessing linked lists (talking about shared lists here of course) without a mutex, one thread could delete an element another thread is using, causing it to be deadlocked, so it seems logical to me to use a mutex in this situation. I just don&#39;t know if using regular shared variables necessarily requires mutexes. Is it that critical if two threads access a variable at the same time? Sure: When using a variable multiple times in a thread it may be a good idea to make a copy of it, anyways, just to prevent it from being changed mid-thread, but let&#39;s assume we have a variable that the logic thread writes to, while the render thread only reads it. Can it have critical effects if both threads access the variable at the same time, anyways?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Sun, 14 Aug 2011 20:28:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the render thread asks for it while the logic thread is changing it, yes(certainly for larger variables). You could get garbage values because the processor is halfway trough its writing process.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 14 Aug 2011 20:32:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I see. I guess it&#39;s &quot;rather be safe than sorry&quot; then and I shouldn&#39;t hesitate to use mutexes. Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Sun, 14 Aug 2011 20:52:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can also go too crazy using mutexes (mutices?<span class="ref"><sup>[<a href="#">1</a>]</sup></span>).  Balance is key.</p><p>I&#39;m working on solving a race condition<span class="ref"><sup>[<a href="#">2</a>]</sup></span> at the moment, which can happen with overuse of mutexes.  In my particular case there is design flaw causing the mutex to try to lock itself twice.  For instance:
</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> theOracle<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">int</span> theAdvisersBuddy<span class="k2">(</span><span class="k2">)</span>
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>    <span class="k1">int</span> r <span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
<span class="number">  6</span>    <span class="k1">if</span><span class="k2">(</span>notSoSure<span class="k2">)</span>
<span class="number">  7</span>        r <span class="k3">+</span><span class="k3">=</span> theOracle<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>    <span class="k1">return</span> r<span class="k2">;</span>
<span class="number">  9</span><span class="k2">}</span>
<span class="number"> 10</span>
<span class="number"> 11</span><span class="k1">int</span> adviser<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 12</span><span class="k2">{</span>
<span class="number"> 13</span>    <span class="k1">int</span> r <span class="k3">=</span> <span class="n">13</span><span class="k2">;</span>
<span class="number"> 14</span>    r <span class="k3">+</span><span class="k3">=</span> theAdvisersBuddy<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span><span class="k2">}</span>
<span class="number"> 16</span>
<span class="number"> 17</span><span class="k1">int</span> theOracle<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 18</span><span class="k2">{</span>
<span class="number"> 19</span>    mutex.lock<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>    <span class="k1">int</span> r <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span>%<span class="n">5</span><span class="k2">;</span>
<span class="number"> 21</span>    r <span class="k3">+</span><span class="k3">=</span> adviser<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>    mutex.unlock<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>    <span class="k1">return</span> r<span class="k2">;</span>
<span class="number"> 24</span><span class="k2">}</span>
<span class="number"> 25</span>
<span class="number"> 26</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"The oracle has spoken: %d"</span>, theOracle<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

In this case the oracle must consult his adviser to give his answer.  Little does the Oracle know, the adviser has a buddy.  The adviser always consults his buddy before telling the oracle what to do.  When the buddy is unsure what to give the adviser, he consults the oracle.</p><p>Here is the backtrace (flipped for learner&#39;s sake):
</p><ol><li><p>printf() &lt;- The output statement
</p></li><li><p>theOracle() =&gt; the mutex is now locked!
</p></li><li><p>adviser() &lt;- the oracle is consulting his adviser
</p></li><li><p>theAdvisersBuddy() &lt;- the adviser is consulting his buddy
</p></li><li><p>theOracle() =&gt; The mutex is locked a second time!</p></li></ol><p>Since you can&#39;t lock a mutex twice, the program will freeze on the line &#39;mutex.lock()&#39; inside theOracle.
</p><div class="ref-block"><h2>References</h2><ol><li><a href="http://stackoverflow.com/questions/866837/plural-form-of-word-mutex">http://stackoverflow.com/questions/866837/plural-form-of-word-mutex</a></li><li><a href="http://stackoverflow.com/questions/34510/what-is-a-race-condition">http://stackoverflow.com/questions/34510/what-is-a-race-condition</a></li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 14 Aug 2011 21:56:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can&#39;t recursive mutexes do that (assuming all that code is running on one thread)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Sun, 14 Aug 2011 22:14:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I suppose that&#39;s true.  My example is not ideal then.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 14 Aug 2011 23:13:12 +0000</pubDate>
	</item>
</rss>
