<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Page flipping with timers</title>
		<link>http://www.allegro.cc/forums/view/586368</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 10 Jul 2006 07:05:06 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m currently using page flipping with my new project, but it has a few problems.  It flickers occasionally and I don&#39;t think I&#39;m doing it correctly.  I&#39;m using an accurate timer richard phillps(sp?) gave to me as well.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td> <span class="c">//Called in initalization</span></td></tr><tr><td class="number">2</td><td> page1 <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_video_bitmap" target="_blank"><span class="a">create_video_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a>, <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td> page2 <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_video_bitmap" target="_blank"><span class="a">create_video_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a>, <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td> activepage <span class="k3">=</span> page2<span class="k2">;</span></td></tr><tr><td class="number">5</td><td> <a href="http://www.allegro.cc/manual/rectfill" target="_blank"><span class="a">rectfill</span></a><span class="k2">(</span>page1, <span class="n">0</span>, <span class="n">0</span>, page1-&gt;w, page1-&gt;h, BLACK<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td> <a href="http://www.allegro.cc/manual/rectfill" target="_blank"><span class="a">rectfill</span></a><span class="k2">(</span>page2, <span class="n">0</span>, <span class="n">0</span>, page2-&gt;w, page2-&gt;h, BLACK<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td> <span class="c">//Main Loop</span></td></tr><tr><td class="number">9</td><td> <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">10</td><td> <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>  mx <span class="k3">=</span> <a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  my <span class="k3">=</span> <a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  handleMouse<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>  <span class="k1">if</span><span class="k2">(</span>game <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">15</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>   <span class="k1">if</span><span class="k2">(</span>checkTimer<span class="k2">(</span><span class="n">100</span><span class="k2">)</span> <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">17</td><td>   <span class="k2">{</span></td></tr><tr><td class="number">18</td><td>    resetTimer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>    <span class="k1">if</span> <span class="k2">(</span>activepage <span class="k3">=</span><span class="k3">=</span> page1<span class="k2">)</span></td></tr><tr><td class="number">20</td><td>     activepage <span class="k3">=</span> page2<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>    <span class="k1">else</span></td></tr><tr><td class="number">22</td><td>     activepage <span class="k3">=</span> page1<span class="k2">;</span></td></tr><tr><td class="number">23</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">24</td><td>   detectSelection<span class="k2">(</span>mx, my<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>   <a href="http://www.allegro.cc/manual/show_video_bitmap" target="_blank"><span class="a">show_video_bitmap</span></a><span class="k2">(</span>activepage<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">27</td><td> <span class="k2">}</span></td></tr></tbody></table></div></div><p>

I don&#39;t think theres anything wrong with my code, but I want to check.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Sun, 09 Jul 2006 07:13:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Which page do you draw to and where? You must draw to the page that is NOT being shown on screen at the time.</p><p>You should separate logic from drawing. Right now it seems you have it all mixed together.</p><p>This is how it hould be done:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>done<span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">while</span> <span class="k2">(</span>timer_ticked<span class="k2">)</span> <span class="k2">{</span>
        process_input<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
        process_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>

    draw_to_active_page<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    show_active_page<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    swap_pages<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Sun, 09 Jul 2006 12:47:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ahh ok, I was doing it inproperly.  (I had drawing code in the detectSelection and the handleMouse)  I went ahead and fixed it up, and this is how it looks.  </p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">void</span> mainLoop<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td> <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">4</td><td> <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>  mx <span class="k3">=</span> <a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>  my <span class="k3">=</span> <a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <span class="k1">if</span><span class="k2">(</span>game <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>   <span class="k1">if</span><span class="k2">(</span>checkTimer<span class="k2">(</span><span class="n">100</span><span class="k2">)</span> <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">10</td><td>   <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>    resetTimer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>    handleMouse<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>    detectSelection<span class="k2">(</span>mx, my<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">15</td><td>   clearScreen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>   updateMenu<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>   <a href="http://www.allegro.cc/manual/show_video_bitmap" target="_blank"><span class="a">show_video_bitmap</span></a><span class="k2">(</span>activepage<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>   swapPages<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">20</td><td> <span class="k2">}</span></td></tr><tr><td class="number">21</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

I believe that&#39;s the correct way to do it, but I have two questions.</p><p>In handleMouse, I just pick out what action to do depending on what I have selected  when I click on the left mouse button, is that considered logic?</p><p>And whenever I close the game and I&#39;m selecting something, it occassionally crashes the game.  I&#39;m not sure why, but this is the code I have for when I close the game:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> endGame<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
 <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>page1<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>page2<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>activepage<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/unload_datafile" target="_blank"><span class="a">unload_datafile</span></a><span class="k2">(</span>media<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/allegro_exit" target="_blank"><span class="a">allegro_exit</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

It&#39;s called right after the mainLoop is called if you wanted to know.  I don&#39;t think there is anything wrong with that, but obviously something is wrong.  Oh, and  thanks miran.  <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Sun, 09 Jul 2006 22:06:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>are you drawing the mouse to any of those bitmaps you destroy?</p><p>destroying a bitmap to which the mouse is drawn will crash your game.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Sun, 09 Jul 2006 22:21:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, I only draw the mouse to the screen when the game is initalized.</p><p>EDIT: To test your theory, I ran scare_mouse(); when the game is ending and it fixed most the problem, but whenever I have my mouse in the lower parts of the screen when I close it, it closes fine, but it has a stupid error saying &quot;Shimp Pong has encountered an error and needs to close&quot;.  I don&#39;t think this is anything to worry about, but it&#39;s still annoying.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Sun, 09 Jul 2006 22:29:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You must make sure that you call show_mouse(NULL) before destroying the surface the mouse is on.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Mon, 10 Jul 2006 01:43:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, well does scare_mouse() do the same thing?  Or do I actually need to call show_mouse(NULL); ?  And do I even need to destroy the activepage bitmap when I close the game?  Shawn doesn&#39;t in his page flipping example.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 01:52:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>scare_mouse just tells allegro not to SHOW the mouse cursor.</p><p>but the mouse is still ASSIGNED to the selected bitmap.</p><p>with show_mouse(NULL) you deselect the mouse from any bitmap.</p><p>Maybe this is not 100% correct in technical terms, but that should explain what it does.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Mon, 10 Jul 2006 01:59:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Oh, well does scare_mouse() do the same thing?
</p></div></div><p>
Certainly not.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
scare_mouse just tells allegro not to SHOW the mouse cursor.
</p></div></div><p>
Not quite - it tells <i>Allegro</i> not to draw a soft mouse cursor. If the mouse cursor is drawn by teh OS, it does nothing.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Or do I actually need to call show_mouse(NULL);
</p></div></div><p>
Depends: <i>if</i> the mouse is displayed to screen (which it normally is), then you only need to call show_mouse(NULL) if you explicitly call set_gfx_mode().</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And do I even need to destroy the activepage bitmap when I close the game?
</p></div></div><p>
Yes, you do. In practice, Allegro may free all video bitmaps when it closes down (memory bitmaps are immaterial, they are freed automatically by the OS), but you should always free memory that you allocate.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Shawn doesn&#39;t in his page flipping example.
</p></div></div><p>
That&#39;s an oversight, then.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Mon, 10 Jul 2006 02:11:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ahh ok, thanks for clearing that up for me guys.  It turns out that the scare_mouse(); wasn&#39;t doing the trick, so I changed it to show_mouse(NULL) and it did fixed it up good.  Thanks a lot guys.  If you don&#39;t mind I have one more question, and that&#39;s if I call 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> <span class="k3">*</span>marquee<span class="k2">;</span>
</pre></div></div><p>

as a global variable, do I need to call
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span>marquee<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
in the end game script?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 02:42:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, but you also have to malloc() or new it someplace too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Mon, 10 Jul 2006 03:08:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh ok, I &quot;malloced&quot; it before hand, I was just wondering if it was required since it was a global variable.  Thanks a lot guys. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 03:15:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre> <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>page1<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>page2<span class="k2">)</span><span class="k2">;</span>
 <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>activepage<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Slight problem there, you never actually created a third bitmap, <i>activepage</i> is just a &quot;pointer&quot; or alias for which ever of page1 or page2 is the <i>activepage</i>, so you really aught not to be calling destroy_bitmap on it, since the bitmap it was pointing to, has already been destroyed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Jul 2006 05:14:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well I still declared it as a global variable, but it&#39;s only a pointer so I don&#39;t need to destroy it after all?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 05:23:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nope. Only call <i>destroy_*</i> on items that were specifically created with <i>create_*</i>. Otherwise you&#39;re freeing it twice, which is a rather bad thing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Jul 2006 05:25:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if the mouse is displayed to screen (which it normally is), then you only need to call show_mouse(NULL) if you explicitly call set_gfx_mode().
</p></div></div><p>What? How does one implicitly call set_gfx_mode?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Mon, 10 Jul 2006 06:41:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How does one implicitly call set_gfx_mode?
</p></div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Jul 2006 06:44:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok well I removed it and it works well, but shouldn&#39;t I set the activepage to NULL anyway when I close the game?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 06:52:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It is a good habit to get into.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Jul 2006 06:54:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh ok, I went ahead and did that.  Thanks a lot guys. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paladin)</author>
		<pubDate>Mon, 10 Jul 2006 07:05:06 +0000</pubDate>
	</item>
</rss>
