<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>classes: a pain in the ass.</title>
		<link>http://www.allegro.cc/forums/view/590665</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 23 Mar 2007 21:01:53 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>you know this thing they call classes? i hate it.. if i could, i would stick to structures and having all the code in one file, but after a while you get kinda tired of never finding what you&#39;re looking for.. so this morning i started to learn about classes (for real..), seams like it can&#39;t be mastered in one hour..</p><p>i think classes is very confusing.. a long time ago i thought classes was used to link files of code making it easier to manage your project.. i don&#39;t have to use classes right? isn&#39;t there a tutorial on this <span class="cuss"><span><span class="cuss"><span>shit</span></span></span></span>??</p><p>The question is: How do i alter use_triple_buffering in configuration.h from initialization.cpp function dynamic_initialization?(example) i&#39;ve tried many things and it to complex to google..</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">/*main.cpp -.-*/</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include "initialization.h"</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>initialization initialization<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">int</span> main<span class="k2">(</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>  initialization.static_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  initialization.dynamic_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>  </td></tr><tr><td class="number">12</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">13</td><td>  <span class="k2">{</span><span class="c">//main loop</span></td></tr><tr><td class="number">14</td><td>    </td></tr><tr><td class="number">15</td><td>    </td></tr><tr><td class="number">16</td><td>    </td></tr><tr><td class="number">17</td><td>  <span class="k2">}</span><span class="c">//main loop</span></td></tr><tr><td class="number">18</td><td>  </td></tr><tr><td class="number">19</td><td>  initialization.de_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span></td></tr></tbody></table></div></div><p>

</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">/*initialization.cpp :)*/</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include "initialization.h"</span></td></tr><tr><td class="number">4</td><td><span class="p">#include "configuration.h"</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="c">//con</span></td></tr><tr><td class="number">7</td><td>initialization::initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span></td></tr><tr><td class="number">8</td><td><span class="c">//des</span></td></tr><tr><td class="number">9</td><td>initialization::~initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td><span class="c">//Static initialization</span></td></tr><tr><td class="number">12</td><td><span class="k1">void</span> initialization::static_initialization<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">13</td><td><span class="k2">{</span></td></tr><tr><td class="number">14</td><td>  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  </td></tr><tr><td class="number">16</td><td>  <a href="http://www.allegro.cc/manual/install_timer" target="_blank"><span class="a">install_timer</span></a><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/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  <a href="http://www.allegro.cc/manual/install_mouse" target="_blank"><span class="a">install_mouse</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  </td></tr><tr><td class="number">20</td><td>  <a href="http://www.allegro.cc/manual/set_config_file" target="_blank"><span class="a">set_config_file</span></a><span class="k2">(</span><span class="s">"config.ini"</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td><span class="k2">}</span></td></tr><tr><td class="number">22</td><td>&#160;</td></tr><tr><td class="number">23</td><td><span class="c">//Dynamic initialization</span></td></tr><tr><td class="number">24</td><td><span class="k1">void</span> initialization::dynamic_initialization<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">25</td><td><span class="k2">{</span></td></tr><tr><td class="number">26</td><td>  <a href="http://www.allegro.cc/manual/install_sound" target="_blank"><span class="a">install_sound</span></a><span class="k2">(</span>DIGI_AUTODETECT, MIDI_NONE, NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>  </td></tr><tr><td class="number">28</td><td>  <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span><span class="n">16</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td><span class="k2">}</span></td></tr><tr><td class="number">30</td><td>&#160;</td></tr><tr><td class="number">31</td><td><span class="k1">void</span> initialization::de_initialization<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">32</td><td><span class="k2">{</span></td></tr><tr><td class="number">33</td><td>  <a href="http://www.allegro.cc/manual/clear_keybuf" target="_blank"><span class="a">clear_keybuf</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

</p><div class="source-code snippet"><div class="inner"><pre><span class="c">/*initialization.h :S*/</span>
<span class="p">#ifndef INITIALIZATION_H</span>
<span class="p">#define INITIALIZATION_H</span>

<span class="k1">class</span> initialization
<span class="k2">{</span>
  public:
    initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    ~initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">void</span> static_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">void</span> dynamic_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">void</span> de_initialization<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
<span class="p">#endif</span>
</pre></div></div><p>

</p><div class="source-code snippet"><div class="inner"><pre>
<span class="c">/*configuration.cpp :|*/</span>
<span class="p">#include "configuration.h"</span>
<span class="c">//con</span>
configuration::configuration<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span>
<span class="c">//des</span>
configuration::~configuration<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span>
</pre></div></div><p>

</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td><span class="c">/*configuration.h :(*/</span></td></tr><tr><td class="number">3</td><td><span class="p">#ifndef CONFIGURATION_H</span></td></tr><tr><td class="number">4</td><td><span class="p">#define CONFIGURATION_H</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">class</span> configuration</td></tr><tr><td class="number">7</td><td><span class="k2">{</span></td></tr><tr><td class="number">8</td><td>  public:</td></tr><tr><td class="number">9</td><td>    configuration<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>    ~configuration<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    </td></tr><tr><td class="number">12</td><td>    <span class="k1">bool</span> use_triple_buffering<span class="k2">;</span>  </td></tr><tr><td class="number">13</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td><span class="p">#endif</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Thu, 22 Mar 2007 14:28:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The question is: How do i alter use_triple_buffering in configuration.h from initialization.cpp function dynamic_initialization?
</p></div></div><p>
You need a reference in initialization to an object of type configuration. And you need to add a setter to the configuration class (like <i>void set_use_tb() { use_triple_buffer = true; } </i>), but I think you&#39;re not using classes the way they&#39;re meant to be used.</p><p>For instance, a class should contain all the properties that make up a state, plus the operations to manipulate that state.</p><p>So, in your case I would do a game class, that will initialize things in its constructor (there&#39;s no need to add that static_initialization(), if you put that code in the constructor it will be called when the variable in created).</p><p>[edit]</p><p>The main difference between structs and classes is that the latters let you specify properties and operations to be executed on them in a single, close, object.</p><p>[edit2]</p><p>Another point: it&#39;s not always the best to separate things in the code just because they are in real life (above all if you then need a reference to keep the objects together, like <i>initialization</i> and <i>configuration</i>).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Marco Radaelli)</author>
		<pubDate>Thu, 22 Mar 2007 14:37:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would just like to add that this is not the right way to use OO. This is just procedural programming pretending to be something else, so no wonder it&#39;s coming off as strange...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Thu, 22 Mar 2007 15:21:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Marco Radaelli said:</div><div class="quote"><p>
 but I think you&#39;re not using classes the way they&#39;re meant to be used
</p></div></div><p>neither do i.. thank you,</p><div class="quote_container"><div class="title">X-G said:</div><div class="quote"><p>

I would just like to add that this is not the right way to use OO. This is just procedural programming pretending to be something else, so no wonder it&#39;s coming off as strange...
</p></div></div><p> <br />what is OO?<br />pretending!? - -
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Thu, 22 Mar 2007 16:30:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
<a href="http://en.wikipedia.org/wiki/Object-oriented_programming">lrn2wikipedia</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Thu, 22 Mar 2007 16:48:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You don&#39;t need a class for your allegro game initialization, that can be simple done with some ifs.<br /><a href="http://www.cplusplus.com/doc/tutorial/classes.html">Nice tutorial</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Thu, 22 Mar 2007 22:31:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you really want to learn this stuff a book is probably the best way to go.  Probably not the &#39;dive right in&#39; answer you seem to be looking for but Object Orientated programming is a completely different mindset to structured programming and you need to learn some new concepts.</p><p>I&#39;ve read a lot in the past and most were good enough to get you through all the basics.  While a I can&#39;t remember what the best book I&#39;ve read is, any of the following were good enough should be at your library, school etc.</p><p>Teach yourself C++ in 24 hours (don&#39;t be suckered by the title...)<br />Dummies guide to C++ (don&#39;t let the &#39;Dummies guide&#39; bit blind your judgement in thinking the book is crap)</p><p>Once you start learning a bit you&#39;ll wonder why you didn&#39;t start earlier!  It&#39;s really useful stuff. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Matt Weir.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Fri, 23 Mar 2007 02:37:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
you know this thing they call classes? i hate it.. if i could, i would stick to structures
</p></div></div><p>
Go for it. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Structs are just classes whose members default to public instead of private. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>.... yes, I know what you&#39;re saying, but I had to throw that out there. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Fri, 23 Mar 2007 06:07:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Teach yourself C++ in 24 hours (don&#39;t be suckered by the title...)
</p></div></div><p>
Bad choice.<br />I used it, and it doesn&#39;t even cover all the basic language features. It also advises you to create your own string class, and it doesn&#39;t really cover the STL (nor does it encourage you to use it).
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Dummies guide to C++ (don&#39;t let the &#39;Dummies guide&#39; bit blind your judgement in thinking the book is crap)
</p></div></div><p>
Haven&#39;t really tried that.</p><p>My choice would have to be Bruce Eckel&#39;s <a href="http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html">Thinking in C++</a>. Available on-line for free, but in my experience, a printed book is always nice to have next to your computer.</p><p>OT:<br />You don&#39;t need to use classes just to divide your project into several files. Here&#39;s a quick-start guide for multi-file project management:<br />1. Group your existing functions and definitions by functional category. For example, &quot;Allegro setup&quot;, &quot;Screen buffering&quot;, &quot;Sprite management&quot;, &quot;Game entities&quot;, &quot;Main loop&quot;, &quot;GUI&quot;.<br />2. For each of these categories, make a .c or .cpp file that contains everything that belongs into the category.<br />3. Create a &quot;header&quot; for each .cpp file, with the same name but ending in .h or .hpp. Put guards around it:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifndef ALLEGRO_SETUP_H</span>
<span class="p">#define ALLEGRO_SETUP_H</span>

<span class="c">// everything else goes here.</span>

<span class="p">#endif</span>
</pre></div></div><p>
4. For each function that is needed in another module, put the function declaration into the header of its own module, and include the header into the .cpp file of the module that needs it. For example, the main loop will probably use a function called flip() from the screen buffer module. So you have:
</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">// ------ screenbuf.h ----</span></td></tr><tr><td class="number">2</td><td><span class="p">#ifndef SCREENBUF_H</span></td></tr><tr><td class="number">3</td><td><span class="p">#define SCREENBUF_H</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">void</span> flip<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// defined in screenbuf.cpp</span></td></tr><tr><td class="number">6</td><td><span class="c">// ...</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="p">#endif</span></td></tr><tr><td class="number">9</td><td><span class="c">// ------ mainloop.cpp ----</span></td></tr><tr><td class="number">10</td><td><span class="p">#include "mainloop.h"</span></td></tr><tr><td class="number">11</td><td><span class="p">#include "screenbuf.h" // this is so we can use flip()</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="c">//...</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><span class="k1">void</span> graphics_update<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>  <span class="c">// Render the scene to backbuf</span></td></tr><tr><td class="number">17</td><td>  flip<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
5. Put all type definitions (including struct types) into the header file for their module.<br />6. For each variable that you want to be accessible from the outside, put an &quot;extern&quot; reference in the module&#39;s header:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// ---- module.h ----</span>
<span class="p">#ifndef MODULE_H</span>
<span class="p">#define MODULE_H</span>

<span class="k1">extern</span> <span class="k1">int</span> this_is_globally_available<span class="k2">;</span>

<span class="p">#endif</span>
<span class="c">// ---- module.cpp ----</span>
<span class="k1">int</span> this_is_globally_available <span class="k3">=</span> <span class="n">23</span><span class="k2">;</span>
</pre></div></div><p>
7. NEVER include a .c or .cpp file.<br />8. NEVER include a .c or .cpp file.</p><p>When you come to realize that certain groups of variables and functions &quot;belong together&quot;, then you have a candidate for a class. But I suggest you read up on OO design before doing actual coding; and it&#39;s probably a very bad idea to apply OO design to an already existing project. Design in OO paradigm from the start, or keep the project procedural.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 23 Mar 2007 15:39:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if i could, i would stick to structures and having all the code in one file, but after a while you get kinda tired of never finding what you&#39;re looking for
</p></div></div><p>

Um... You&#39;re allowed to use more than one file when you use structs... <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Fri, 23 Mar 2007 18:49:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
think classes is very confusing.. a long time ago i thought classes was used to link files of code making it easier to manage your project.. i don&#39;t have to use classes right?
</p></div></div><p>
Classes are just structs with a few extra features.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 23 Mar 2007 19:27:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Personally for books I&#39;d recommend &quot;C++ the complete reference&quot; which is significantly better then the Java book by the same author. It&#39;s really easy to read and does cover MOST of the important concepts you&#39;ll need to learn.</p><p>However for indepth knowledge you&#39;ll need &quot;The C++ Programming Language&quot;, which is THE C++ book.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Hard Rock)</author>
		<pubDate>Fri, 23 Mar 2007 20:45:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> thanks!!</p><p>i&#39;m more of a writing person than a reading one.. in all aspects of life.<br />but i guess you need to read to know how to write.. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Fri, 23 Mar 2007 21:01:53 +0000</pubDate>
	</item>
</rss>
