<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Using a struct containing a BITMAP</title>
		<link>http://www.allegro.cc/forums/view/591772</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 07 Jun 2007 02:48:58 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi, I&#39;m new here and am not great with programming in C but I understand the basics anyway. I&#39;m doing the following to create a struct called SPRITE containing a BITMAP and four integers that are pretty self-explanatory:</p><p>typedef struct SPRITE<br />{<br />	BITMAP *picture;<br />	int x1;<br />	int y1;<br />	int x2; <br />	int y2;<br />} SPRITE;</p><p>That&#39;s stored in a header file along with a function that initialises my programme:</p><p>void main_init()<br />{<br />	allegro_init();<br />	install_keyboard();<br />	install_timer();</p><p>	LOCK_VARIABLE(speed_counter);<br />	LOCK_FUNCTION(increment_speed_counter);<br />	install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));</p><p>	set_color_depth(16);<br />	set_gfx_mode(GFX_AUTODETECT, 1024, 768, 0, 0);<br />}<br />END_OF_FUNCTION(main_init);</p><p>That function is called as the first command in main(). After setting up the bitmaps for the background and double-buffer, I create a new sprite by doing this:</p><p>SPRITE char_one = {load_bitmap(&quot;char1.bmp&quot;, NULL), 0, 0, (char_one.x1 + char_one.picture-&gt;w), (char_one.y1 + char_one.picture-&gt;h)};</p><p>I know the rest of the program works, this is my latest addition to it. When I compile the program, it compiles without any errors or warnings however when I run it, the creen initialises and then the program aborts with a segmentation fault. I&#39;ve read through the forums here and all I can think of is that the struct is being defined before allegro and the gfx_mode is initialised. I know this would create a seg fault because a gfx function is being used before the gfx_mode is initialised, am I rightt? When I comment out the &quot;SPRITE char_one = {...&quot; then the program executes perfectly.</p><p>So, is there anyway to make the struct be defined after allegro is initialised? Any help would be well appreciated because I&#39;m totally pulling my hair out <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (howieson2003)</author>
		<pubDate>Wed, 06 Jun 2007 22:45:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
SPRITE char_one = {load_bitmap(&quot;char1.bmp&quot;, NULL), 0, 0, (char_one.x1 + char_one.picture-&gt;w), (char_one.y1 + char_one.picture-&gt;h)};
</p></div></div><p>
It seems to me that you are trying to use char_one before it is completed declared.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Wed, 06 Jun 2007 22:57:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can you try in this way?
</p><div class="source-code snippet"><div class="inner"><pre>SPRITE char_one<span class="k2">;</span>
char_one.picture <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_bitmap" target="_blank"><span class="a">load_bitmap</span></a><span class="k2">(</span><span class="s">"char1.bmp"</span>, NULL<span class="k2">)</span><span class="k2">;</span>
char_one.x1 <span class="k3">=</span> char_one.y1 <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
char_one.x2 <span class="k3">=</span> char_one.x1 <span class="k3">+</span> char_one.picture-&gt;w<span class="k2">;</span>
char_one.y2 <span class="k3">=</span> char_one.y1 <span class="k3">+</span> char_one.picture-&gt;h<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Wed, 06 Jun 2007 22:57:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nevermind.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SonShadowCat)</author>
		<pubDate>Wed, 06 Jun 2007 22:58:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@ Onewing:</p><p>Do you mean that when I try to create the x2 and y2 values, it causes a seg fault because I&#39;m looking for information from the struct and it hasn&#39;t been completed yet?</p><p>@ Paul whoknows:</p><p>Thanks, it works perfectly that way! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /> But why? Is it like I said above? Or some other reason that I don&#39;t understand?</p><p>Thanks anyway peeps!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (howieson2003)</author>
		<pubDate>Wed, 06 Jun 2007 23:21:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
it causes a seg fault because I&#39;m looking for information from the struct and it hasn&#39;t been completed yet?
</p></div></div><p>
Yeah, it&#39;s kind of like that.  It&#39;s like you&#39;re trying to turn on the car while it&#39;s still on the assembly line getting built.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Wed, 06 Jun 2007 23:37:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nice one! It&#39;s like the world all makes sense now! <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> Cheers
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (howieson2003)</author>
		<pubDate>Thu, 07 Jun 2007 00:07:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s stored in a header file along with a function that initialises my programme:
</p></div></div><p>
Please don&#39;t put code in headers. It makes baby Jesus cry.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Thu, 07 Jun 2007 00:10:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What&#39;s the point of the second x and y coordinates? Being derived from other attributes of the <tt>struct</tt> make it unneeded.</p><p>Removing those coordinates would make your declaration work, assuming that&#39;s valid code to declare a <tt>struct</tt>, I dunno because I always declare mine the way Paul showed you.</p><p>And gnolam is saying, which is something I agree with, only declarations (functions, classes, etc) should go in header (.h, .hpp) files and all of the code should go into source files (.c, .cpp). This way, you don&#39;t have to recompile all of the code every time you make a change.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Thu, 07 Jun 2007 02:48:58 +0000</pubDate>
	</item>
</rss>
