<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Help with modifying Mappy code to work with multithreading</title>
		<link>http://www.allegro.cc/forums/view/611392</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 10 Nov 2012 03:19:29 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey everyone, I was hoping someone with experience using mappy could help me out. I&#39;m using multithreading. In my main thread I&#39;m displaying the map and doing normal logic stuff. I&#39;m using another thread to load maps. By using another thread to load maps I&#39;ve needed change mappy&#39;s draw code to convert memory bitmaps to video bitmaps. </p><p>I did made this change in the <span class="source-code"> MapDrawBG<span class="k2">(</span><span class="k2">)</span></span> function. with this code:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> MapDrawBG <span class="k2">(</span><span class="k1">int</span> mapxo, <span class="k1">int</span> mapyo, <span class="k1">int</span> mapx, <span class="k1">int</span> mapy,<span class="k1">int</span> mapw, <span class="k1">int</span> maph<span class="k2">)</span>
<span class="k2">{</span>
    ......
    
    <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_bitmap_flags"><span class="a">al_get_bitmap_flags</span></a><span class="k2">(</span><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="k2">)</span> blkdatapt-&gt;bgoff<span class="k2">)</span> <span class="k3">&amp;</span> ALLEGRO_MEMORY_BITMAP<span class="k2">)</span>
    <span class="k2">{</span>
    <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span> Temp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_clone_bitmap"><span class="a">al_clone_bitmap</span></a><span class="k2">(</span><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="k2">)</span> blkdatapt-&gt;bgoff<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span><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="k2">)</span>blkdatapt-&gt;bgoff<span class="k2">)</span><span class="k2">;</span>
    <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="k2">)</span>blkdatapt-&gt;bgoff <span class="k3">=</span> Temp<span class="k2">;</span>
    <span class="k2">}</span>
    
    ......
<span class="k2">}</span>
</pre></div></div><p>
Thanks to &quot;kazzmir&quot; for this.</p><p>However the program crashes when the <span class="source-code"> MapFreeMem<span class="k2">(</span><span class="k2">)</span></span> function is called in my second thread. <br />for example:<br />1. Initial map loads (in my main thread)  <br />2. walk into a new area<br />3. new map loads (in the second thread)<br />4. walk back to the previous area<br />5. Crash (in the second thread)</p><p>Specifically at this part:</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">void</span> MapFreeMem<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  ......
<span class="number">  4</span>  <span class="k1">if</span> <span class="k2">(</span>abmTiles <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>    i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> 
<span class="number">  7</span>
<span class="number">  8</span>    <span class="k1">while</span> <span class="k2">(</span>abmTiles<span class="k2">[</span>i<span class="k2">]</span><span class="k3">!</span><span class="k3">=</span>NULL<span class="k2">)</span>
<span class="number">  9</span>    <span class="k2">{</span>
<span class="number"> 10</span>      <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a> <span class="k2">(</span>abmTiles<span class="k2">[</span>i<span class="k2">]</span><span class="k2">)</span><span class="k2">;</span> <span class="c">//Crash Happens Here </span>
<span class="number"> 11</span>      i<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span> 
<span class="number"> 12</span>    <span class="k2">}</span>
<span class="number"> 13</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a> <span class="k2">(</span>abmTiles<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>    abmTiles <span class="k3">=</span> NULL<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="k2">}</span>
</div></div><p> </p><p>I&#39;m hoping some with a lot of experience with mappy can help me out. I&#39;ve uploaded the complete source for mappy (my modified version) for anyone who needs/wants to look at it. </p><p>The mappy source code, with the addition of mulithreading, is kind of difficult for me to understand. One of these days I&#39;ll need to re-write it to be 100% C++. But in the mean time if someone could help me tackle this problem that be great.</p><p>Thanks,<br />-Ryan
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Thu, 08 Nov 2012 08:24:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As said by Trent in your other topic, you can&#39;t access graphics stuff from two threads at the same time.<br />Releasing video bitmaps is one kind of &quot;access&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 08 Nov 2012 16:19:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Audric: If i read what you and Trent said correctly, it seems like you guys are saying the call to <span class="source-code">MapFreeMem<span class="k2">(</span><span class="k2">)</span></span> in my second thread is cause of the error. Am I correct?</p><p>Regardless I just spent a bunch of time modifying my code to make it so <span class="source-code">MapFreeMem<span class="k2">(</span><span class="k2">)</span></span> is only called in my Main thread. Yet, I get the exact same error, at the exact same spot, in my call to <span class="source-code">MapFreeMem<span class="k2">(</span><span class="k2">)</span></span>. before.</p><p>Any ideas?</p><p>Thanks,<br />-Ryan
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Fri, 09 Nov 2012 11:31:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not that it will help but:</p><p>1. There is a C++ version of mappy, it&#39;s at the mappy website, I think he calls it the universal version or something like that</p><p>2. If you think moving to Allegro 5 might help in any way, there is a version available here: <a href="http://www.allegro.cc/forums/thread/606962">http://www.allegro.cc/forums/thread/606962</a></p><p>3. Could you use memory bitmaps not video, they should be ok across threads
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Fri, 09 Nov 2012 18:40:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Neil Walker: I am using Allegro 5.0.5 currently, and I&#39;m using a modified version of your converted mappy code. <br />  Also, &quot;your&quot; mappy code isn&#39;t 100% C++ it uses a lot of C stuff in it. It still works with C++, but still uses old C features. I want to one day take out all that C stuff and replace it with it&#39;s C++ counterpart. I couldn&#39;t find the universal version you mentioned. Everything I found still uses C in some way.<br />  The problem with using memory bitmaps is that they are extremely slow. I already tried running memory bitmaps only on my old laptop. The difference being a game that was playable with minimal lag (using video) and a game that was completely unresponsive and laggy (using memory). </p><p>Right now what i have it doing is:<br />Loading the data for the map in a second thread.<br />I&#39;m then replacing that data with a converted video version in my main thread. <br />When I load go to load another new map I use the MapFreeMem() function in my main thread. <br />That is when I get the error.  </p><p>Is this still wrong? I&#39;m starting to run out of ideas for how to do this.   </p><p>EDIT: Finally Fixed my problem! </p><p>For anyone else who may need help on this in the future:<br />1. Switch to using Allegro 5.1.X<br />2. Replace this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span> Temp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_clone_bitmap"><span class="a">al_clone_bitmap</span></a><span class="k2">(</span><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="k2">)</span> blkdatapt-&gt;bgoff<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span><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="k2">)</span>blkdatapt-&gt;bgoff<span class="k2">)</span><span class="k2">;</span>
<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="k2">)</span>blkdatapt-&gt;bgoff <span class="k3">=</span> Temp<span class="k2">;</span>
</pre></div></div><p>

with this:
</p><div class="source-code snippet"><div class="inner"><pre>al_convert_bitmap<span class="k2">(</span><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="k2">)</span> blkdatapt-&gt;bgoff<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Thanks to @kazzmir for showing me this alternate method.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Sat, 10 Nov 2012 03:19:29 +0000</pubDate>
	</item>
</rss>
