<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Threading Problems</title>
		<link>http://www.allegro.cc/forums/view/608527</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 07 Oct 2011 04:48:04 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m having a little problem, which, I think, is related to threads. First I&#39;ll explain the situation.</p><p>I have split my game into two threads: The logic thread (running at 25 FPS) and the display thread (running at any FPS, 60 with my current settings). It&#39;ll only be a simple Mario-styled platformer, so two threads should be enough. Since I love internationalization I included a language feature, which I wanted to be powerful, yet easy to use and player-friendly. Language files called &quot;lang<i>[name]</i>.lng&quot; are saved in the language folder. The game then reads strings from the currently selected language file. Anyways, I also wanted to have a proper language selection screen that recognizes all valid language files in the folder, so that the player doesn&#39;t have to mess around with the config file. The screen looks somewhat like this:</p><p><span class="remote-thumbnail"><span class="json">{"name":"langscreen1.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/7\/7797045a3e2a3cb983ada6cdf4da0cb6.png","w":320,"h":240,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/7\/7797045a3e2a3cb983ada6cdf4da0cb6"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/7/7/7797045a3e2a3cb983ada6cdf4da0cb6-240.jpg" alt="langscreen1.png" width="240" height="180" /></span></p><p>To make the selection as fast as possible (while still keeping it player-friendly and easy to program) only the graphics (flag, name) of the currently selected language file are loaded. The pathes are read from the language files themselfs. This means: Whenever you press left or right, the graphics currently in RAM are cleared and the new graphics are loaded. This happens in the logic thread. Since I obviously also need the graphics for the display thread I&#39;m using mutexes. Now for the problem: In some rare cases when I press left or right and the new graphics are supposed to load some of the graphics stay black or keep their magic pink. So instead of<br /><span class="remote-thumbnail"><span class="json">{"name":"langscreen3.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/9\/e9f3e76ea90d7f77a1f06d2877ac8e5f.png","w":320,"h":240,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/9\/e9f3e76ea90d7f77a1f06d2877ac8e5f"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/e/9/e9f3e76ea90d7f77a1f06d2877ac8e5f-240.jpg" alt="langscreen3.png" width="240" height="180" /></span><br />I get<br /><span class="remote-thumbnail"><span class="json">{"name":"langscreen2.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94cc8a060c5d791d06eb2716e683dac0.png","w":320,"h":240,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94cc8a060c5d791d06eb2716e683dac0"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/9/4/94cc8a060c5d791d06eb2716e683dac0-240.jpg" alt="langscreen2.png" width="240" height="180" /></span><br />Since this error stays until I press left/right again I suppose it can only be happening while loading the graphics. I&#39;m also pretty sure it&#39;s a threading related problem, since I can&#39;t think of any other reason for this. Here are some code extracts with the concerning functions.</p><p>Loading new graphics:
</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="c">/* Load language graphics */</span>
<span class="number">  2</span><span class="k1">void</span> LoadLangGFX<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangFlagGFX, <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangNameGFX, <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangArrowGFX, <span class="k1">char</span> <span class="k3">*</span>Language<span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>    <span class="c">/* Destroy old graphics */</span>
<span class="number">  5</span>    <span class="k1">if</span> <span class="k2">(</span>LangFlagGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number">  6</span>    <span class="k2">{</span>
<span class="number">  7</span>        <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span>LangFlagGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>        LangFlagGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number">  9</span>    <span class="k2">}</span>
<span class="number"> 10</span>    <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 11</span>    <span class="k2">{</span>
<span class="number"> 12</span>        <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span>LangNameGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>        LangNameGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 14</span>    <span class="k2">}</span>
<span class="number"> 15</span>
<span class="number"> 16</span>    <span class="c">/* Load arrow graphic */</span>
<span class="number"> 17</span>    <span class="k1">if</span> <span class="k2">(</span>LangArrowGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 18</span>    <span class="k2">{</span>
<span class="number"> 19</span>        LangArrowGFX <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span><span class="s">"graphics\\RPG_Hacker-Lang_Arrow_Left.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>        <span class="k1">if</span> <span class="k2">(</span>LangArrowGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 21</span>            Shutdown<span class="k2">(</span><span class="n">10</span>, <span class="s">"graphics\\RPG_Hacker-Lang_Arrow_Left.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>    <span class="k2">}</span>
<span class="number"> 23</span>
<span class="number"> 24</span>    <span class="k1">char</span> <span class="k3">*</span>FilePath <span class="k3">=</span> NULL<span class="k2">;</span>      <span class="c">// Path to graphic to be loaded</span>
<span class="number"> 25</span>
<span class="number"> 26</span>    <span class="c">/* Load flag graphic */</span>
<span class="number"> 27</span>    FilePath <span class="k3">=</span> GetLangString<span class="k2">(</span>Language, <span class="s">"icon"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>    <span class="k1">if</span> <span class="k2">(</span>FilePath <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 29</span>    <span class="k2">{</span>
<span class="number"> 30</span>        LangFlagGFX <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span>FilePath<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 31</span>        <span class="k1">delete</span> FilePath<span class="k2">;</span>
<span class="number"> 32</span>    <span class="k2">}</span>
<span class="number"> 33</span>    <span class="k1">else</span>
<span class="number"> 34</span>        LangFlagGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 35</span>
<span class="number"> 36</span>    <span class="k1">if</span> <span class="k2">(</span>LangFlagGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 37</span>    <span class="k2">{</span>
<span class="number"> 38</span>        LangFlagGFX <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span><span class="s">"graphics\\RPG_Hacker-Flag_XX.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 39</span>        <span class="k1">if</span> <span class="k2">(</span>LangFlagGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 40</span>            Shutdown<span class="k2">(</span><span class="n">10</span>, <span class="s">"graphics\\RPG_Hacker-Flag_XX.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 41</span>    <span class="k2">}</span>
<span class="number"> 42</span>
<span class="number"> 43</span>    <span class="c">/* Load name graphic */</span>
<span class="number"> 44</span>    FilePath <span class="k3">=</span> GetLangString<span class="k2">(</span>Language, <span class="s">"namegfx"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 45</span>    <span class="k1">if</span> <span class="k2">(</span>FilePath <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 46</span>    <span class="k2">{</span>
<span class="number"> 47</span>        LangNameGFX <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span>FilePath<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 48</span>        <span class="k1">delete</span> FilePath<span class="k2">;</span>
<span class="number"> 49</span>    <span class="k2">}</span>
<span class="number"> 50</span>    <span class="k1">else</span>
<span class="number"> 51</span>        LangNameGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 52</span>
<span class="number"> 53</span>    <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 54</span>    <span class="k2">{</span>
<span class="number"> 55</span>        LangNameGFX <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span><span class="s">"graphics\\RPG_Hacker-Name_XX.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 56</span>        <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">=</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 57</span>            Shutdown<span class="k2">(</span><span class="n">10</span>, <span class="s">"graphics\\RPG_Hacker-Name_XX.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 58</span>    <span class="k2">}</span>
<span class="number"> 59</span>
<span class="number"> 60</span>    <span class="c">/* Convert transparency masks */</span>
<span class="number"> 61</span>    <span class="k1">if</span> <span class="k2">(</span>LangArrowGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 62</span>        <a href="http://www.allegro.cc/manual/al_convert_mask_to_alpha"><span class="a">al_convert_mask_to_alpha</span></a><span class="k2">(</span>LangArrowGFX, <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">0</span>, <span class="n">255</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 63</span>    <span class="k1">if</span> <span class="k2">(</span>LangFlagGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 64</span>        <a href="http://www.allegro.cc/manual/al_convert_mask_to_alpha"><span class="a">al_convert_mask_to_alpha</span></a><span class="k2">(</span>LangFlagGFX, <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">0</span>, <span class="n">255</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 65</span>    <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 66</span>        <a href="http://www.allegro.cc/manual/al_convert_mask_to_alpha"><span class="a">al_convert_mask_to_alpha</span></a><span class="k2">(</span>LangNameGFX, <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">0</span>, <span class="n">255</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 67</span><span class="k2">}</span>
</div></div><p>

Removing old graphics:
</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="c">/* Destroy language graphics in RAM */</span>
<span class="number">  2</span><span class="k1">void</span> DestroyLangGFX<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangFlagGFX, <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangNameGFX, <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangArrowGFX<span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>    <span class="k1">if</span> <span class="k2">(</span>LangFlagGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number">  5</span>    <span class="k2">{</span>
<span class="number">  6</span>        <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span>LangFlagGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>        LangFlagGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number">  8</span>    <span class="k2">}</span>
<span class="number">  9</span>    <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">!</span><span class="k3">=</span> NULL<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_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span>LangNameGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>        LangNameGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 13</span>    <span class="k2">}</span>
<span class="number"> 14</span>    <span class="k1">if</span> <span class="k2">(</span>LangArrowGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 15</span>    <span class="k2">{</span>
<span class="number"> 16</span>        <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>        LangArrowGFX <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 18</span>    <span class="k2">}</span>
<span class="number"> 19</span><span class="k2">}</span>
</div></div><p>

Jump to next language file when pressing right (it&#39;s the same for left except for the first line).
</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="k2">[</span>...<span class="k2">]</span>
<span class="number">  2</span><span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>CursorKey<span class="k2">(</span>RightKey<span class="k2">)</span><span class="k2">)</span>
<span class="number">  3</span>                 <span class="k2">{</span>
<span class="number">  4</span>                     <span class="c">/* Find next language */</span>
<span class="number">  5</span>                     LangNext<span class="k2">(</span>Language<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span>                     <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>LangMutex<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>                     DestroyLangGFX<span class="k2">(</span>LangFlagGFX, LangNameGFX, LangArrowGFX<span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>                     LoadLangGFX<span class="k2">(</span>LangFlagGFX, LangNameGFX, LangArrowGFX, Language<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>                     <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>LangMutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>                     <a href="http://www.allegro.cc/manual/al_rest"><span class="a">al_rest</span></a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>
<span class="number"> 13</span>                     LangArrowTimer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>      <span class="c">// For blinking arrows</span>
<span class="number"> 14</span>                     <a href="http://www.allegro.cc/manual/al_play_sample"><span class="a">al_play_sample</span></a><span class="k2">(</span>SoundEffect<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, <span class="n">1</span>.<span class="n">0</span>, <span class="n">0</span>.<span class="n">0</span>, <span class="n">1</span>.<span class="n">0</span>, ALLEGRO_PLAYMODE_ONCE, NULL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>
<span class="number"> 16</span>                 <span class="k2">}</span>
<span class="number"> 17</span><span class="k2">[</span>...<span class="k2">]</span>
</div></div><p>

Display thread (only the part, where the concerning graphics are displayed):
</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="k2">[</span>...<span class="k2">]</span>
<span class="number">  2</span>                 <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>LangMutex<span class="k2">)</span><span class="k2">;</span>
<span class="number">  3</span>                 <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>DisplayMutex<span class="k2">)</span><span class="k2">;</span>       <span class="c">// This mutex protects the ALLEGRO_DISPLAY and probably isn't necessary</span>
<span class="number">  4</span>
<span class="number">  5</span>                 <a href="http://www.allegro.cc/manual/al_set_target_bitmap"><span class="a">al_set_target_bitmap</span></a><span class="k2">(</span>Backbuffer<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>LangFlagGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number">  8</span>                     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>
<span class="number">  9</span>                         LangFlagGFX, <span class="k2">(</span>BackbufferWidth<span class="k2">(</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>LangFlagGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span>,
<span class="number"> 10</span>                         <span class="k2">(</span>GameHeight <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>LangFlagGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">5</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>
<span class="number"> 12</span>                 <span class="k1">if</span> <span class="k2">(</span>LangNameGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 13</span>                     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>
<span class="number"> 14</span>                         LangNameGFX, <span class="k2">(</span>BackbufferWidth<span class="k2">(</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>LangNameGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span>,
<span class="number"> 15</span>                         <span class="k2">(</span>GameHeight <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">+</span>
<span class="number"> 16</span>                         <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>LangFlagGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">5</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>                 <span class="k1">if</span> <span class="k2">(</span>LangArrowTimer <span class="k3">&gt;</span><span class="k3">=</span> FPS <span class="k3">/</span> <span class="n">2</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> LangArrowTimer <span class="k3">&lt;</span><span class="k3">=</span> FPS <span class="k3">&amp;</span><span class="k3">&amp;</span> LangArrowGFX <span class="k3">!</span><span class="k3">=</span> NULL<span class="k2">)</span>
<span class="number"> 19</span>                 <span class="k2">{</span>
<span class="number"> 20</span>                     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>
<span class="number"> 21</span>                         LangArrowGFX, <span class="k2">(</span>BackbufferWidth<span class="k2">(</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">20</span>,
<span class="number"> 22</span>                         <span class="k2">(</span>GameHeight <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">5</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>                     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>
<span class="number"> 24</span>                         LangArrowGFX, <span class="k2">(</span>BackbufferWidth<span class="k2">(</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">20</span>,
<span class="number"> 25</span>                         <span class="k2">(</span>GameHeight <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>LangArrowGFX<span class="k2">)</span> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">5</span>, ALLEGRO_FLIP_HORIZONTAL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>                 <span class="k2">}</span>
<span class="number"> 27</span>
<span class="number"> 28</span>                 <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>DisplayMutex<span class="k2">)</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>LangMutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 30</span>                 <a href="http://www.allegro.cc/manual/al_rest"><span class="a">al_rest</span></a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 31</span><span class="k2">[</span>...<span class="k2">]</span>
</div></div><p>

Any ideas?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Thu, 06 Oct 2011 04:52:45 +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/608527/933198#target">RPG Hacker</a> said:</div><div class="quote"><p> The logic thread (running at 25 FPS) and the display thread (running at any FPS, 60 with my current settings).</p></div></div><p>Why to run the display thread faster than the logic thread? you&#39;ll be redrawing the same all the time... Most of the time what it most CPU and GPU takes is the drawing process.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>  It&#39;ll only be a simple Mario-styled platformer, so two threads should be enough.</p></div></div><p>Actually just one thread would be more than enough.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> This means: Whenever you press left or right, the graphics currently in RAM are cleared and the new graphics are loaded.</p></div></div><p>I wouldn&#39;t do that if I were you, remember that loading graphics is very slow, depending in the size of your image you can easily notice the screen getting stuck if you have another animation running. You say this happens in the logic thread, and what&#39;s the logic thread? the one that initialize allegro and create a display or another thread?. Because if it&#39;s another thread you would be loading the image as a memory bitmap and that &quot;mode&quot; is very slow when drawing.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Since I obviously also need the graphics for the display thread I&#39;m using mutexes.</p></div></div><p>What?? <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /> are you drawing in both threads?</p><p><span class="source-code">LoadLangGFX<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">&amp;</span>LangFlagGFX</span></p><p>Man what&#39;s that? `*&amp;` ? I have never seen that before.</p><p>I haven&#39;t see all your code but you should never be using a mutx to protect the display pointer... that sounds very crazy <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> .
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Thu, 06 Oct 2011 06:08:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should just load all flag images at once (or, store them all in one file; then only draw the appropriate portion at a time). File I/O is very slow by comparison to how fast the computer is running. Memory is very cheap these days. I have 8 billion bytes, ffs! Your few kB of images are not going to be noticed. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> This is a case of premature (and backwards) optimization. Don&#39;t worry about it. Load all of the flag images, let the user select his language, and optionally unload the flag images.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 06 Oct 2011 06:40:14 +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/608527/933198#target">RPG Hacker</a> said:</div><div class="quote"><p>
Whenever you press left or right, the graphics currently in RAM are cleared and the new graphics are loaded. This happens in the logic thread.
</p></div></div><p>
Your logic thread doesn&#39;t have a display attached or associated with it, so neither do the graphics that are loaded in it. That may be the reason they are showing up blank. They will also be memory bitmaps, which are slow.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608527/933215#target">AMCerasoli</a> said:</div><div class="quote"><p>
LoadLangGFX(ALLEGRO_BITMAP *&amp;LangFlagGFX</p><p>Man what&#39;s that? `*&amp;` ? I have never seem that before.
</p></div></div><p>
It is a reference to an ALLEGRO_BITMAP pointer. It works just like any other reference would.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 06 Oct 2011 06:59:50 +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/608527/933215#target">AMCerasoli</a> said:</div><div class="quote"><p> Why to run the display thread faster than the logic thread? you&#39;ll be redrawing the same all the time... Most of the time what it most CPU and GPU takes is the drawing process.</p></div></div><p>I&#39;m using interpolation, so the display thread can run at any FPS and still profit.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> You say this happens in the logic thread, and what&#39;s the logic thread? the one that initialize allegro and create a display or another thread?. Because if it&#39;s another thread you would be loading the image as a memory bitmap and that &quot;mode&quot; is very slow when drawing.</p></div></div><p>The logic thread is basically main().</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> What?? <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /> are you drawing in both threads?</p></div></div><p>No. In the logic thread I only <b>load</b> the graphics and in the display thread I display them.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Man what&#39;s that? `*&amp;` ? I have never seen that before.</p></div></div><p>It&#39;s called &quot;call by reference&quot;. Google it. In this case a pointer is passed by reference, therefore I have both, a * and a &amp;.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> I haven&#39;t see all your code but you should never be using a mutx to protect the display pointer... that sounds very crazy <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> .</p></div></div><p>Well, the logic/main thread creates and destroys the display (it has to in my case), so I guess it IS a shared variable, but I guess it probably won&#39;t ever lead to conflicts. I just started with threaded programming and wanted to make absolutely sure not to mess anything up, so I used mutexes in this case.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608527/933219#target">bamccaig</a> said:</div><div class="quote"><p> You should just load all flag images at once (or, store them all in one file; then only draw the appropriate portion at a time). File I/O is very slow by comparison to how fast the computer is running. Memory is very cheap these days. I have 8 billion bytes, ffs! Your few kB of images are not going to be noticed. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> This is a case of premature (and backwards) optimization. Don&#39;t worry about it. Load all of the flag images, let the user select his language, and optionally unload the flag images.</p></div></div><p>Problem is just that I wanted to keep the code as simple as possible and also make it recognize language files that have just been moved to the language folder. Then again I guess it&#39;s the better idea. I could make a linked list of structs that contain the language name and the pointers to the graphics. That would also make the whole file accessing a lot easier. I guess it isn&#39;t that bad if languages aren&#39;t recognized during run-time.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608527/933221#target">Edgar Reynaldo</a> said:</div><div class="quote"><p> Your logic thread doesn&#39;t have a display attached or associated with it, so neither do the graphics that are loaded in it. That may be the reason they are showing up blank. They will also be memory bitmaps, which are slow.</p></div></div><p>I doubt this is the problem, since it happens so rarely. Also it&#39;s not always just blank graphics, sometimes the magic pink isn&#39;t converted to transparency. It also stays like that until I change the language. If it really was a problem with the display it would most likely only last for a frame or so and then be fixed. On top of that I&#39;m not even drawing on the display directly. I&#39;m drawing on a backbuffer I created myself in the logic thread, so if the image shows up only partially on the screen that means the error must have already been on the Backbuffer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Thu, 06 Oct 2011 15:29:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why reload the arrows too? I&#39;m thinking, but I can&#39;t really come up with intelligent remarks about how you use threads...<span class="ref"><sup>[<a href="#">1</a>]</sup></span> &quot;Don&#39;t&quot; perhaps?
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608527/933256#target">RPG Hacker</a> said:</div><div class="quote"><p> If it really was a problem with the display it would most likely only last for a frame or so and then be fixed.
</p></div></div><p>Nah, it would probably crash entirely <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> You&#39;re doing something weird with the loading and I personally have no idea what would happen. Threads combined with loading GFX scare me.<br />Wait... How are you calling the display-thread? Which one is main?
</p><div class="ref-block"><h2>References</h2><ol><li>You could use threads &quot;<i>to make the selection as fast as possible (while still keeping it player-friendly and easy to program)</i>&quot;, but I think that is off-topic. But I keep thinking in terms of the latter use of threads.</li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Thu, 06 Oct 2011 17:07: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/608527/933271#target">weapon_S</a> said:</div><div class="quote"><p> Why reload the arrows too?</p></div></div><p>I just didn&#39;t feel like writing two seperate functions for destroying the graphics, so I just made it reload the arrow graphics, too.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Wait... How are you calling the display-thread? Which one is main?</p></div></div><p>What do you mean by &quot;how&quot;? Do you mean 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="k2">[</span>...<span class="k2">]</span>
<span class="number"> 2</span>    <span class="c">/* Create thread */</span>
<span class="number"> 3</span>    DisplayThread <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>DisplayGame, NULL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 4</span>    <a href="http://www.allegro.cc/manual/al_start_thread"><span class="a">al_start_thread</span></a><span class="k2">(</span>DisplayThread<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 5</span>
<span class="number"> 6</span>    <span class="c">/* Run game */</span>
<span class="number"> 7</span>    RunGame<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 8</span>    <a href="http://www.allegro.cc/manual/al_join_thread"><span class="a">al_join_thread</span></a><span class="k2">(</span>DisplayThread, NULL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 9</span><span class="k2">[</span>...<span class="k2">]</span>
</div></div><p>

This is from main(), so main() starts DisplayThread (which loops until you exit the game) and then goes into the logic loop with RunGame() (also loops until you exit). Nothing happens in DisplayThread until a few preparations in RunGame() are done, so it&#39;s OK to start it first here.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Thu, 06 Oct 2011 18:12:43 +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/608527/933256#target">RPG Hacker</a> said:</div><div class="quote"><p>
Problem is just that I wanted to keep the code as simple as possible and also make it recognize language files that have just been moved to the language folder. Then again I guess it&#39;s the better idea. I could make a linked list of structs that contain the language name and the pointers to the graphics. That would also make the whole file accessing a lot easier. I guess it isn&#39;t that bad if languages aren&#39;t recognized during run-time.
</p></div></div><p>
Your original solution (multiple threads and repeatedly loading/unloading) is the opposite of simple, which is exactly why you&#39;re having trouble. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Take your own advice and make it simpler. You can still make graphics recognizable at run-time by waiting to load the flags until the last second, but then loading them all. Whenever the user gets to the flag screen (or just before, if you can predict it), load up all detected flag files. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> When they leave the flag screen destroy all of the flag bitmaps. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608527/933282#target">RPG Hacker</a> said:</div><div class="quote"><p>I just didn&#39;t feel like writing two seperate functions for destroying the graphics, so I just made it reload the arrow graphics, too.</p></div></div><p>
That is a very bad sign. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> Get rid of that laziness.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 06 Oct 2011 19:43:07 +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/608527/933292#target">bamccaig</a> said:</div><div class="quote"><p> Your original solution (multiple threads and repeatedly loading/unloading) is the opposite of simple, which is exactly why you&#39;re having trouble. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Take your own advice and make it simpler. You can still make graphics recognizable at run-time by waiting to load the flags until the last second, but then loading them all. Whenever the user gets to the flag screen (or just before, if you can predict it), load up all detected flag files. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> When they leave the flag screen destroy all of the flag bitmaps. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p></div></div><p>I guess I&#39;ll do that. I just hope it&#39;ll really fix the bug.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> That is a very bad sign. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> Get rid of that laziness.</p></div></div><p>This is just for testing purposes, anyways. I will get rid of it once the selection screen is done.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Thu, 06 Oct 2011 20:33:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When I said the resources you loaded didn&#39;t have a display attached to them, I meant it. Displays and OpenGL/DX contexts are thread local in Allegro 5. This can lead to all kinds of bizarre behaviour when loading resources in a different thread than the one where you created your display. Search the forums if you don&#39;t believe me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 07 Oct 2011 02:48: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/608527/933357#target">Edgar Reynaldo</a> said:</div><div class="quote"><p> When I said the resources you loaded didn&#39;t have a display attached to them, I meant it. Displays and OpenGL/DX contexts are thread local in Allegro 5. This can lead to all kinds of bizarre behaviour when loading resources in a different thread than the one where you created your display. Search the forums if you don&#39;t believe me.</p></div></div><p>I believe you. The thing is just that - as stated above - in my game the logic thread does both: Create a display AND load graphics. The display thread only transfers the logic thread&#39;s graphics to the logic thread&#39;s display. I don&#39;t know if THAT causes any problems, but I don&#39;t really have another choice than to create the display in the logic thread because I&#39;m reading window settings from the config file.</p><p>Anyways, I&#39;m done rewriting my code now the way bamccaig suggested. I can&#39;t tell for sure if this fixed the bug since it only occured when loading graphics, which happens only a single time now. I&#39;d have to restart the program hundreds of times to find out whether the bug is really fixed or just still pretty rare. Since I don&#39;t feel like that I&#39;ll just assume it&#39;s fixed for now. Thanks for helping out!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (RPG Hacker)</author>
		<pubDate>Fri, 07 Oct 2011 04:48:04 +0000</pubDate>
	</item>
</rss>
