<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] Just Sharing a simple screen shot function</title>
		<link>http://www.allegro.cc/forums/view/613224</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 06 Sep 2013 17:55:35 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I just whipped up this screenshot function for my Allegro 5 project I thought I would share if anyone wants it.  It doesn&#39;t have all the bells and whistles, it basically saves a screenshot in  the same folder as your executable.  It can be easily modified to save elsewhere, just provide the path as part of the gamename you send to it I suppose. </p><p>It saves with the date + the time, so if you supply a game name like my &quot;DeluxePacman&quot; it would save it as &quot;DeluxePacman_20130905_144600.png&quot; for example.  It uses 24hour time and the full year.  The order is &quot;YYYYMMDD_HHMMSS&quot; so it will be listed in the folder in order that it was taken.  This also easily allows you to take multiple screenshots with no limits, one per second.</p><p>If you prefer another format, just change PNG to JPG or BMP or whatever Allegro supports.  I tested it and it works for me just fine.</p><p><s>I don&#39;t know if there is a way to automatically detect physfs and perhaps have the function automatically call `al_set_standard_file_interface()` and `al_set_physfs_file_interface()` or not, I just whipped this up quickly, but it would be nice if that functionality could be added.  I suppose it should probably be left up to the person though.</s></p><p>Edit: I just updated this code and used Elias&#39; suggestion to store the current file interface state, then switch to standard file interface, save the bitmap and restore the old state to what it was.  Great solution.  Works perfectly. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p><b>a5_screenshot.c</b>
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include "a5_screenshot.h"</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">bool</span> a5_screenshot<span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>gamename<span class="k2">)</span>
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>   time_t rawtime<span class="k2">;</span>
<span class="number">  6</span>   <span class="k1">struct</span> tm <span class="k3">*</span>timeinfo<span class="k2">;</span>
<span class="number">  7</span>   <span class="k1">char</span> filename<span class="k2">[</span><span class="n">80</span><span class="k2">]</span>, timestr<span class="k2">[</span><span class="n">80</span><span class="k2">]</span><span class="k2">;</span>
<span class="number">  8</span>   <span class="k1">bool</span> saved<span class="k2">;</span>
<span class="number">  9</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_STATE"><span class="a">ALLEGRO_STATE</span></a> state<span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span>   <a href="http://www.allegro.cc/manual/al_store_state"><span class="a">al_store_state</span></a><span class="k2">(</span><span class="k3">&amp;</span>state, ALLEGRO_STATE_NEW_FILE_INTERFACE<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>
<span class="number"> 13</span>   <a href="http://www.allegro.cc/manual/al_set_standard_file_interface"><span class="a">al_set_standard_file_interface</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>
<span class="number"> 15</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a><span class="k2">(</span><span class="k3">&amp;</span>rawtime<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>   timeinfo <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_538.html" target="_blank">localtime</a><span class="k2">(</span><span class="k3">&amp;</span>rawtime<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_760.html" target="_blank">strftime</a><span class="k2">(</span>timestr, <span class="n">80</span>, <span class="s">"%Y%m%d_%H%M%S"</span>, timeinfo<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_732.html" target="_blank">snprintf</a><span class="k2">(</span>filename, <span class="n">80</span>, <span class="s">"%s_%s.png"</span>, gamename, timestr<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>
<span class="number"> 21</span>   saved <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_save_bitmap"><span class="a">al_save_bitmap</span></a><span class="k2">(</span>filename, <a href="http://www.allegro.cc/manual/al_get_target_bitmap"><span class="a">al_get_target_bitmap</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>
<span class="number"> 23</span>   <a href="http://www.allegro.cc/manual/al_restore_state"><span class="a">al_restore_state</span></a><span class="k2">(</span><span class="k3">&amp;</span>state<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>
<span class="number"> 25</span>   <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>saved<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span>   <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 28</span><span class="k2">}</span>
</div></div><p>


<b>a5_screenshot.h</b>
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifndef _a5_screenshot_h_</span>
<span class="p">#define _a5_screenshot_h_</span>

<span class="p">#include "allegro5/allegro.h"</span>
<span class="p">#include &lt;stdio.h&gt;</span>

<span class="k1">bool</span> a5_screenshot<span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>gamename<span class="k2">)</span><span class="k2">;</span>

<span class="p">#endif //_a5_screenshot_h_</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 05 Sep 2013 23:53:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_new_file_interface"><span class="a">al_get_new_file_interface</span></a></span> to get the current file interface (and reset back to that if it&#39;s not the standard one). Or you can use <span class="source-code"><a href="http://www.allegro.cc/manual/al_store_state"><span class="a">al_store_state</span></a></span> and <span class="source-code"><a href="http://www.allegro.cc/manual/al_restore_state"><span class="a">al_restore_state</span></a></span> with <span class="source-code">ALLEGRO_STATE_NEW_FILE_INTERFACE</span> to store/restore the current file interface.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 06 Sep 2013 00:38:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Great idea Elias, thanks.  I updated my original posts code to reflect your suggestion.  Works great.</p><p>Edit: I tried this out with <span class="source-code">a5_screenshot<span class="k2">(</span><span class="s">"Screenshots/DeluxePacman2"</span><span class="k2">)</span><span class="k2">;</span></span> and it saved it into the &quot;Screenshots&quot; folder as expected.  Of course, the folder has to already exist, but it does work which is nice.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Fri, 06 Sep 2013 01:44:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How reliable is it that <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_target_bitmap"><span class="a">al_get_target_bitmap</span></a><span class="k2">(</span><span class="k2">)</span></span> gives you the front buffer (ie, what&#39;s currently on the screen) rather than the back buffer (ie, where stuff that is drawn actually goes)?</p><p>I assumed (based on manual) that there was no reliable cross-platform way to do that...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Fri, 06 Sep 2013 17:28:38 +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/613224/989690#target">Evert</a> said:</div><div class="quote"><p>I assumed (based on manual) that there was no reliable cross-platform way to do that...
</p></div></div><p>
Even if there isn&#39;t, couldn&#39;t you bypass this by calling the screenshot function after drawing your current frame(and before flipping the screen)? At 60fps, there normally isn&#39;t too much difference between two frames, and the user won&#39;t notice the screenshot is one frame off.</p><p>EDIT: this might be relevant: <a href="http://stackoverflow.com/questions/2661936/asynchronous-readback-from-opengl-front-buffer-using-multiple-pbos">http://stackoverflow.com/questions/2661936/asynchronous-readback-from-opengl-front-buffer-using-multiple-pbos</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 06 Sep 2013 17:36:06 +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/613224/989691#target">J-Gamer</a> said:</div><div class="quote"><p>Even if there isn&#39;t, couldn&#39;t you bypass this by calling the screenshot function after drawing your current frame(and before flipping the screen)? At 60fps, there normally isn&#39;t too much difference between two frames, and the user won&#39;t notice the screenshot is one frame off.</p></div></div><p>One problem is that there&#39;s no guarantee that the backbuffer even contains what you drew, or that theres only two pages, there could be many more.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 06 Sep 2013 17:48:30 +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/613224/989692#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>One problem is that there&#39;s no guarantee that the backbuffer even contains what you drew, or that theres only two pages, there could be many more.</p></div></div><p>
I meant taking a screenshot of the next frame instead of trying to get the current front buffer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 06 Sep 2013 17:52:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah, I missed that part. Sorry. Yeah, I expect that would work.</p><p>Or you could draw to a screen sized bitmap, and save that off.</p><p>It seems like some games do that, as it takes them some time to actually create and save a screenshot.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 06 Sep 2013 17:53:19 +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/613224/989694#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>Or you could draw to a screen sized bitmap, and save that off.It seems like some games do that, as it takes them some time to actually create and save a screenshot.
</p></div></div><p>
You could easily redraw the current frame to that bitmap, instead of using the next frame as a screenshot. That&#39;s a less hackish way of doing it, I suppose.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Fri, 06 Sep 2013 17:55:35 +0000</pubDate>
	</item>
</rss>
