<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Need some tips to make fast allegro programs</title>
		<link>http://www.allegro.cc/forums/view/615151</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 03 Mar 2015 23:10:02 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I&#39;m developing allegro 5. I know pretty much already and I was making a game but I noticed that the program starts to get very slow, maybe I&#39;m doing some stupid thing that consumes too much memory/fetch time etc... I don&#39;t know.</p><p>Anyway, I just need a tutorial or some advices on what to do, which details on the code to pay attention to make it run fast and lean.</p><p>Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Sun, 01 Mar 2015 05:37:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Most important performance tip for Allegro is to load all your bitmaps AFTER initializing the screen, otherwise they will be loaded as slow memory bitmaps, not fast screen bitmaps. For more tips, we&#39;d need to see some of your code, if you are willing to share it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sun, 01 Mar 2015 05:59:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, yes, I&#39;ll post a template of my project with the most significant parts:</p><p>I have three main headers<br />a &quot;common&quot; header that #includes all of the libraries that both main.cpp and other_files.cpp have access, like</p><p>#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#include &lt;allegro.h&gt;<br />#include &lt;allegro5/allegro_image.h&gt;<br />#include &quot;my_headers.h&quot;<br />...<br />etc</p><p>The first lines of main are the inicialization of allegro and the display</p><p>right after, I load the map reading a 500x500 .png file and converting each pixel into a data in a int[500][500] matrix, each int is a correspondence to a 32x32 tile.</p><p>The function that draws the map onto display:<br />void draw_map_reg(int mapa[][MAX_MAP_SIZE], int desloc_x, int desloc_y, ALLEGRO_BITMAP* tiles); where mapa is the int matrix quoted, and *tiles is a bitmap that contains 32x32 pixels tiles, desloc_x and y are the offset.</p><p>   for(i = 0 ; i &lt; NUM_BLOCK_X ; i++)<br />    {<br />        for(j = 0 ; j &lt; NUM_BLOCK_Y+1 ; j++)<br />        {<br />            if(i+desloc_x&lt;MAX_MAP_SIZE &amp;&amp; j+desloc_y&lt;MAX_MAP_SIZE &amp;&amp; i+desloc_x &gt;= 0 &amp;&amp; j+desloc_y&gt;=0)<br />            desenha_tile(tiles, mapa[i+desloc_x][j+desloc_y], i, j);<br />        }<br />    }</p><p>the if statement within is to avoid accessing the matrix outside its limits. The desenha_tile stands for draw_a_tile, it is a switch between the value of mapa[i+desloc_x][j+desloc_y]. For example:<br /> case 0:<br />                    al_draw_bitmap_region(tiles, GRAMA1, BLOCK_SIZE, BLOCK_SIZE, i*BLOCK_SIZE, j*BLOCK_SIZE, 0);<br />mapa[i+desloc_x][j+desloc_y]<br />Where GRAMA1 is a macro like:<br />#define GRAMA1 3 * BLOCK_SIZE , 1 * BLOCK_SIZE<br />it gets the region of the bitmap where the tile is.</p><p>I think the main thing is printing the map, the other stuff done is just printing a player with a sprite, also using draw_bitmap_region</p><p>Btw, thanks for the help man. What can I do to make things run faster on the code?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Sun, 01 Mar 2015 21:55:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not sure but it looks like you&#39;re drawing the entire 500x500 tile map of 32x32 tiles giving you a whopping 16000x16000 pixels to be drawn. That&#39;s bound to be slow. You need to only draw the tiles that are actually visible on the screen! </p><p>Secondly it&#39;s not a great idea to use a (png) image file for storing tile map data. You could for example use the .tmx map format of tiled (<a href="http://www.mapeditor.org/">http://www.mapeditor.org/</a>), which will also make the tile map easier to edit. That&#39;s what I do in my RPG engine/game at least.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sun, 01 Mar 2015 23:30:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, I&#39;ll fix it. What else can I do? My Graphics Routines are all like:<br />create *bitmap, set it using al_load_bitmap, then I start using it later. Where to use parallelism on the code, etc...?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Mon, 02 Mar 2015 02:31:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Normally I would load all graphics after opening the screen but before starting the drawing loop, normally when a new &quot;level&quot; is loaded. I wouldn&#39;t do too much loading while the game play is ongoing to avoid slowdowns.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Mon, 02 Mar 2015 03:35:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok. I&#39;ve just saw your engine on github, great job man! <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Mon, 02 Mar 2015 17:02:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;re welcome, and also welcome to use my engine if you like, as long as you respect the license. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Tue, 03 Mar 2015 03:27:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I decided to build my own &quot;tiled&quot; because I thought it would be easy. It happens that I&#39;m having the same problems I had with the main game: very slow processing. I don&#39;t think that is my computer is <span class="cuss"><span>fuck</span></span>ed up since I run many heavy games without problem... Anyway, I&#39;ll post the whole code of the &quot;map maker&quot; on the attachments  for you to see, maybe you can help me find wtf is making it run so slow... <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Tue, 03 Mar 2015 04:54:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you&#39;re not using texture atlasing, you should. Especially for tile based graphics. It&#39;s rather important.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 03 Mar 2015 08:40:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615151/1011004#target">anon176</a> said:</div><div class="quote"><p> It happens that I&#39;m having the same problems I had with the main game: very slow processing</p></div></div><p>What do you mean by slow? Low framerate?</p><p>You might want to change this</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>draw<span class="k2">)</span>
</pre></div></div><p>
to
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>draw <span class="k3">&amp;</span><span class="k3">&amp;</span> <a href="http://www.allegro.cc/manual/al_is_event_queue_empty"><span class="a">al_is_event_queue_empty</span></a><span class="k2">(</span>fila_de_eventos<span class="k2">)</span><span class="k2">)</span>
</pre></div></div><p>

Also use <span class="source-code"><a href="http://www.allegro.cc/manual/al_hold_bitmap_drawing"><span class="a">al_hold_bitmap_drawing</span></a></span> if you use the same bitmap many times in a row. This will improve performance although even without you shouldn&#39;t run into performance problems quickly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (l j)</author>
		<pubDate>Tue, 03 Mar 2015 09:32:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>By slow I just mean that it is taking too much time to answer. I put this al_hold_bitmap_drawing and it didn&#39;t changed to much. I decided to drop FPS rate to 10 and it get a little better, though I know my computer can run with a much higher rate.</p><p>Also, why to draw only if it has no event on the queue? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>Btw, I put the code of another program I&#39;m making in my last post, if you could please take a look I&#39;d be thankful. Maybe my code is full of noob errors that I don&#39;t know... <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>For example: the draw_tile function I have draws using a single .png where all tiles are stored and then It call the tile using al_draw_bitmap_region with the region of the tile</p><p>I thought that If I change all multiplications from * to &lt;&lt; in the tiles where its X or Y position is a multiple of power of 2... will it make the code at least a little faster? like:</p><p>#define TILE_1 0 * BLOCK_SIZE , 4 * BLOCK_SIZE to</p><p>#define TILE_1 0 * BLOCK_SIZE , (BLOCK_SIZE &lt;&lt; 2)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anon176)</author>
		<pubDate>Tue, 03 Mar 2015 18:42:26 +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/615151/1011015#target">anon176</a> said:</div><div class="quote"><p>Also, why to draw only if it has no event on the queue? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p></div></div><p>If you don&#39;t handle all events in the queue every frame, you are only handling one event per frame. You will quickly back up a ton of events in the queue and lag behind.</p><p>Hm, so you are using atlasing, using held drawing should help if you&#39;re using any other bitmaps. Basically it just makes sure it doesn&#39;t actually send any of the drawing data to the gpu till you switch bitmaps, which should help a lot.</p><p>Something like:
</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">void</span> draw<span class="k2">(</span><span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>   <a href="http://www.allegro.cc/manual/al_hold_bitmap_drawing"><span class="a">al_hold_bitmap_drawing</span></a><span class="k2">(</span><span class="k1">true</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>   <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> MAP_HEIGHT<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  6</span>      <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> j <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> MAP_WIDTH<span class="k2">;</span> j<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  7</span>         Tile <span class="k3">&amp;</span>t <span class="k3">=</span> atlas.tiles<span class="k2">[</span>map<span class="k2">[</span>i<span class="k2">]</span><span class="k2">[</span>j<span class="k2">]</span><span class="k2">]</span><span class="k2">;</span> <span class="c">// yay for C++ and references</span>
<span class="number">  8</span>         <a href="http://www.allegro.cc/manual/al_draw_bitmap_region"><span class="a">al_draw_bitmap_region</span></a><span class="k2">(</span>atlas.bmp, t.x, t.y, TILE_WIDTH, TILE_HEIGHT, map.x <span class="k3">+</span> TILE_WIDTH <span class="k3">*</span> j, map.y <span class="k3">+</span> TILE_HEIGHT <span class="k3">*</span> i<span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>      <span class="k2">}</span>
<span class="number"> 10</span>   <span class="k2">}</span>
<span class="number"> 11</span>
<span class="number"> 12</span>   <span class="c">// draw other things, allegro will decide when to flush the held drawing</span>
<span class="number"> 13</span>   <span class="c">// just make sure to batch drawing that comes from the same bitmaps together</span>
<span class="number"> 14</span>   <a href="http://www.allegro.cc/manual/al_hold_bitmap_drawing"><span class="a">al_hold_bitmap_drawing</span></a><span class="k2">(</span><span class="k1">false</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span><span class="k2">}</span>
</div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I thought that If I change all multiplications from * to &lt;&lt; in the tiles where its X or Y position is a multiple of power of 2... will it make the code at least a little faster?</p></div></div><p>If you enable compiler optimizations, that sort of thing should be done for you. You should use what is more readable, and only consider optimizations that you know will help significantly (look into profiling your code to figure out what is taking the most time).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 03 Mar 2015 23:10:02 +0000</pubDate>
	</item>
</rss>
