<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bigdavedev.com</title>
	<atom:link href="http://bigdavedev.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://bigdavedev.com</link>
	<description></description>
	<lastBuildDate>Mon, 26 Sep 2011 15:09:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Errors in applications: Part I</title>
		<link>http://bigdavedev.com/?p=196</link>
		<comments>http://bigdavedev.com/?p=196#comments</comments>
		<pubDate>Thu, 05 May 2011 17:24:19 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Object Orientated]]></category>
		<category><![CDATA[RAII]]></category>

		<guid isPermaLink="false">http://bigdavedev.com/?p=196</guid>
		<description><![CDATA[Introduction Let&#8217;s face it, when we write some code it must at some point go wrong &#8211; we can&#8217;t write perfect code first time, every time&#8230;  For this reason it is necessary to have some way of notifying users that something bad has happened and some way of recording what has gone wrong. This short [...]]]></description>
			<content:encoded><![CDATA[<h1>Introduction</h1>
<p>Let&#8217;s face it, when we write some code it must at some point go wrong &#8211; we can&#8217;t write perfect code first time, every time&#8230;  For this reason it is necessary to have some way of notifying users that something bad has happened and some way of recording what has gone wrong.</p>
<p>This short article series will talk about logs and exceptions in C++.  Part i focuses on logging errors to a txt file, while part ii will focus on exception handling.  Both of these systems that are presented are used in my project and the code provided at the end of the article has been compiled and can be used as is.</p>
<p><span id="more-196"></span></p>
<h1>How it works</h1>
<p>Possibly the most useful feature when debugging our applications is the faithful log file.  We all have our own ways of doing this and here I will share with you my way of doing it.  I really like C++ IO streams and have used the insertion operator (&lt;&lt;) for logging to a file.  I will break down the interface into the first draft and then add the features in gradually till you have the full thing.  The basic interface looks like this:</p>
<pre><span style="color: #3366ff;">class </span>CLog
{
<span style="color: #3366ff;">public</span>:
    CLog(<span style="color: #3366ff;">void</span>);

    ~CLog(<span style="color: #3366ff;">void</span>);

    <span style="color: #3366ff;">template </span>&lt;<span style="color: #3366ff;">typename </span>T&gt;
    std::ostream&amp; <span style="color: #3366ff;">operator </span>&lt;&lt; (<span style="color: #3366ff;">const </span>T&amp; value);

    std::ostream&amp; <span style="color: #3366ff;">operator </span>&lt;&lt; (std::ostream&amp; (*fn)(std::ostream&amp; os));

    <span style="color: #3366ff;">void</span> Flush();

<span style="color: #3366ff;">private</span>:
    std::stringstream  buffer;
    std::ofstream      output;
};</pre>
<p>This interface allows us to write code like the following:</p>
<pre>CLog Log;
Log &lt;&lt; <span style="color: #ff0000;">"Add an error code to the log: "</span> &lt;&lt; errorCode;</pre>
<p>thanks to the insertion operators passing back a reference to an ostream.  This is exactly how std::cout works <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   The above example uses the first operator overload which is a template to allow typesafe insertion, unlike with printf!  The second one may confuse you, but it is necessary as it lets us use the stream manipulators like std::endl and std::flush.  They are passed to the stream as a function pointer.  One final note about the contents of our current interface is the two streams we have as members.  The first is a stringstream and the second is an ofstream.  Why do we have a stringstream when we only need a filestream?  Simple, I wanted to remove some of the overhead of writing to a file continuously by piling all the info into a buffer first and then manually flushing the log into the file.  There is a still a fair bit of overhead associated with this method, but that is the downside to streams really&#8230;</p>
<h1>Improvements</h1>
<p>This is a really good start and it works like we want it to.  It&#8217;s identical to using a regular C++ stream.  However, the user of the log has to specify when to flush and when to add new lines.  Since I&#8217;m lazy and want a log to be formatted automagically for me, I was stuck.  I can&#8217;t append a new line at the end of a call to the insertion operator since that would wreck the chaining we worked so hard for.  The solution I&#8217;ve used is a fairly common one; it is a helper class.  The helper class will now be used to chain the insertion operators and format our buffer for us.  We take advantage of object lifetime and add the end of line formatting in the helper class&#8217; destructor to complete the functionality of it.  Our class now looks like:</p>
<pre><span style="color: #3366ff;">class </span>CLog
{
<span style="color: #3366ff;">private</span>:
    <span style="color: #3366ff;">class </span>CLogHelper;

    <span style="color: #3366ff;">friend class</span> CLogHelper;

<span style="color: #3366ff;">public</span>:
    CLog(<span style="color: #3366ff;">void</span>);

    ~CLog(<span style="color: #3366ff;">void</span>);

    <span style="color: #3366ff;">template </span>&lt;<span style="color: #3366ff;">typename </span>T&gt;
    CLogHelper <span style="color: #3366ff;">operator </span>&lt;&lt; (<span style="color: #3366ff;">const </span>T&amp; value);

    CLogHelper&amp; <span style="color: #3366ff;">operator </span>&lt;&lt; (std::ostream&amp; (*fn)(std::ostream&amp; os));

    <span style="color: #3366ff;">void </span>Flush();

<span style="color: #3366ff;">private</span>:
    std::stringstream  buffer;
    std::ofstream      output;

<span style="color: #3366ff;">private</span>:
    <span style="color: #3366ff;">class </span>CLogHelper
    {
    <span style="color: #3366ff;">public</span>:
        CLogHelper(CLog * Out);
        ~CLogHelper();

        <span style="color: #3366ff;">template </span>&lt;<span style="color: #3366ff;">typename </span>T&gt;
        CLogHelper&amp; <span style="color: #3366ff;">operator </span>&lt;&lt; (<span style="color: #3366ff;">const </span>T&amp; value);

        CLogHelper&amp; <span style="color: #3366ff;">operator </span>&lt;&lt; (std::ostream&amp; (*fn)(std::ostream&amp; os));

    <span style="color: #3366ff;">private</span>:
        CLog * Log;
    };
};</pre>
<p>The major changes then:</p>
<ul>
<li>We have added a private helper class.  This class is brilliant in its simplicity:
<ul>
<li>When the user now puts text into log the Logs insertion operator makes a call to the helper class&#8217; constructor when it returns to create a scoped helper object</li>
<li>The helper class calls its insertion operator to add any extra info into the buffer if needed</li>
<li>At the end of the last call to the insertion operator we climb back up the stack to clean up after ourselves and this consequently adds a single new line to the buffer and calls our flush function which outputs to the file</li>
</ul>
</li>
<li>We have created a prototype of the helper class and made it a friend so that it can access our private buffer</li>
<li>Finally, we have changed the return type of the insertion operators to return instances of the helper class to allow for chaining</li>
</ul>
<p>Seems like a long-winded way to get what I wanted, but it works and is in fact quite simple and transparent.  The end user will never care about the helper class as there is no need to make any special calls.  Just create and use.  Simple.  But I&#8217;m not done yet!</p>
<h1>Taking it one step further</h1>
<p>What we have at this point is a very nice logging system that, on the face of it, acts like a regular stream and is just as easy to use, with the added bonus that we don&#8217;t need to worry about adding new lines <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />   So what more can we do I hear you ask?  Simple, we&#8217;re going to add some extra formatting to the start of our messages.  Surfing through a log can be very tedious work.  It can often be like looking for a needle in a haystack&#8230;  So wouldn&#8217;t it be nice have some tags for the output?  Something like [ERROR] for errors or [RECOVERED] for recovering from a [FATAL ERROR] for example.  We can also make it so that the log will automagically add a default tag if none is specified (I know, I&#8217;m lazy&#8230;).  This requires a little more work and of course some additions to the log class.  I&#8217;ll just post the important parts for this since the rest of the class remains unchanged.  What I intend is to have is something like the following:</p>
<pre>Log &lt;&lt; <span style="color: #ff0000;">"Logging started on "</span> &lt;&lt; GetDate() &lt;&lt; <span style="color: #ff0000;">" at "</span> &lt;&lt; GetTime();
Log(CLog::ERROR) &lt;&lt; <span style="color: #ff0000;">"Something bad happened <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> "</span>;</pre>
<p>Which would provide the following output in the log:</p>
<pre>[INFO ] Logging started on 05/05/11 at 16:38:59
[ERROR] Something bad happened <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </pre>
<p>Very handy don&#8217;t you think?  We can achieve this very simply with a lovely enum and a couple of functions:</p>
<pre><span style="color: #3366ff;"><span style="color: #339966;">// CLog class</span> enum </span>LogLevel
{
    FATAL_ERROR = 0,
    ERROR,
    RECOVERED,
    INFO,
    DEBUG,
    WARNING
};

CLogHelper <span style="color: #3366ff;">operator </span>()(<span style="color: #3366ff;">const </span>LogLevel level);
<span style="color: #3366ff;">void </span>ChangeDefaultLevel(<span style="color: #3366ff;">const </span>LogLevel &amp; newLevel);

<span style="color: #339966;">// CLogHelper class</span>
CLogHelper(CLog * Out, <span style="color: #3366ff;">const </span>LogLevel lev);</pre>
<p>The enum is fairly self explanatory, they are simply our tag IDs.  The parenthesis operator takes an entry from this enum as a parameter and makes a call to the extra helper class constructor.  This constructor uses the enum parameter in a switch to insert the required tag into the buffer before returning.  The other constructor for this class is unchanged.</p>
<p>As for inserting the default tag, when the log is created we make the assumption that the default tag is [INFO].  This tag gets added in during the call to the FIRST insertion operator only.  We ensure that this is the case be using a flag that is true when can insert the flag and false otherwise.  We take care to set this flag in the new helper class constructor so that we don&#8217;t accidentally insert two tags.  This should never happen anyway, but lets be safe about it <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h1>Conclusion</h1>
<p>These final changes, then, allow us to create an easy to read log, as well as an easy to use interface.  I have created a small sample program that uses the log which I will close this post with.  It shows how to use the various pieces of the interface to create very helpful debug info:</p>
<pre>CLog Log;
Log &lt;&lt; <span style="color: #ff0000;">"Logging enabled for: "</span> &lt;&lt; programName &lt;&lt; std::endl;

<span style="color: #3366ff;">try</span>
{
    Log &lt;&lt; <span style="color: #ff0000;">"Log can be used like a normal IO stream <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> "</span>;
    Log &lt;&lt; <span style="color: #ff0000;">"The insertion operator (&lt;&lt;) can be chained "</span> &lt;&lt;  <span style="color: #ff0000;">"like a normal iostream."</span>;
    Log &lt;&lt; <span style="color: #ff0000;">"There is no need for a std::endl."</span>;
    Log &lt;&lt; <span style="color: #ff0000;">"Although you can use one to add a blank line between outputs"</span> &lt;&lt; std::endl;
    Log &lt;&lt; <span style="color: #ff0000;">"See what I mean?"</span> &lt;&lt; std::endl;
    Log(CLog::DEBUG) &lt;&lt; <span style="color: #ff0000;">"Using the parenthesis () operator with a log level allows you to customise the output a bit"</span>;

    <span style="color: #339966;">// We can change the default tag with one function call</span>
    Log.ChangeDefaultLevel(CLog::RECOVERED);
    Log &lt;&lt; <span style="color: #ff0000;">"Now the default tag has been changed!"</span>;

    <span style="color: #3366ff;">bool </span>throwup = <span style="color: #3366ff;">true</span>;
    <span style="color: #3366ff;">if</span>(throwup)
    {
        Log(CLog::WARNING) &lt;&lt; <span style="color: #ff0000;">"I'm warning you!"</span>;
        <span style="color: #3366ff;">throw <span style="color: #000000;">std::string</span></span>(<span style="color: #ff0000;">"I did warn you..."</span>);
    }
}
<span style="color: #3366ff;">catch</span>(std::string&amp; e)
{
    Log(CLog::FATAL_ERROR) &lt;&lt; e;
}</pre>
<p>The above creates the following output:</p>
<pre>[INFO]        Log opened...
[INFO]        Logging enabled for: Logger.exe

[INFO]        Log can be used like a normal IO stream <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
[INFO]        The insertion operator (&lt;&lt;) can be chained like a normal iostream.
[INFO]        There is no need for a std::endl.
[INFO]        Although you can use one to add a blank line between outputs

[INFO]        See what I mean?

[DEBUG]       Using the parenthesis () operator with a log level allows you to customise the output a bit
[RECOVERED]   Now the default tag has been changed!
[WARNING]     I'm warning you!
[FATAL ERROR] I did warn you...</pre>
<p>I hope that you find this logger to be quite helpful.  A read through the code will help you to understand it a lot better.  You can find the source code <a href="http://bigdavedev.com/downloads/Logger.zip">here</a>.  The next article will talk about exception handling in C++ and how it can save your life!</p>
]]></content:encoded>
			<wfw:commentRss>http://bigdavedev.com/?feed=rss2&#038;p=196</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DAVE Engine</title>
		<link>http://bigdavedev.com/?p=188</link>
		<comments>http://bigdavedev.com/?p=188#comments</comments>
		<pubDate>Mon, 04 Apr 2011 20:40:46 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[DAVE Engine]]></category>
		<category><![CDATA[Portfolio]]></category>

		<guid isPermaLink="false">http://bigdavedev.com/?p=187</guid>
		<description><![CDATA[This is the first post about the DAVE Engine (Dynamics, Audio and Visual Effect Engine)! This engine is my own personal project that I have undertaken in order to further my understanding of critical game components.  So far the engine features a sound device that is capable of streaming ogg files and playing wavs.  The [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post about the DAVE Engine (Dynamics, Audio and Visual Effect Engine)!</p>
<p>This engine is my own personal project that I have undertaken in order to further my understanding of critical game components.  So far the engine features a sound device that is capable of streaming ogg files and playing wavs.  The renderer is under construction (will support OpenGL2.1, OpenGL3.x and DirectX9.0c)</p>
<p>Feel free to look at the source <a href="http://bigdavedev.com/downloads/DAVE">here</a> or alternatively, download the project <a href="http://bigdavedev.com/DAVE.zip">here</a> (note that you require OpenAL, OggVorbis libraries (I link statically) and the boost headers installed to compile)</p>
<p>Just a short post to get this out there.  If you want to add something to the engine then leave me a message and we&#8217;ll chat!</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://bigdavedev.com/?feed=rss2&#038;p=188</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some handy template functions</title>
		<link>http://bigdavedev.com/?p=168</link>
		<comments>http://bigdavedev.com/?p=168#comments</comments>
		<pubDate>Tue, 16 Nov 2010 15:01:33 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://bigdavedev.com/?p=168</guid>
		<description><![CDATA[I&#8217;ve been working on the DAVE Engine recently &#8211; my own engine: Dynamics And Visual Effects &#8211; and I have been creating some useful utility functions that help me clear memory and destroy COM objects.  I have also created some factory style template functions that help me create instances of my own objects.  It makes [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on the DAVE Engine recently &#8211; my own engine: Dynamics And Visual Effects &#8211; and I have been creating some useful utility functions that help me clear memory and destroy COM objects.  I have also created some factory style template functions that help me create instances of my own objects.  It makes the code a lot more readable and easier to write since you don&#8217;t need to use the somewhat ugly boost::shared_ptr calls all the time!</p>
<p>I will go through my factory style functions first.  It is a simple inline template function that is also overloaded to handle polymorphism:</p>
<pre><span style="color: #3366ff;">template</span>&lt;<span style="color: #3366ff;">class </span>Object&gt;<span style="color: #3366ff;"> inline </span>boost::shared_ptr&lt;Object&gt; CreateNew()
{
     <span style="color: #3366ff;">return </span>boost::shared_ptr&lt;Object&gt;(<span style="color: #3366ff;">new </span>Object);
}

<span style="color: #3366ff;">template</span>&lt;<span style="color: #3366ff;">class </span>Base, <span style="color: #3366ff;">class </span>Derived&gt;<span style="color: #3366ff;"> inline </span>boost::shared_ptr&lt;Base&gt; CreateNew()
{
      <span style="color: #3366ff;">return </span>boost::shared_ptr&lt;Base&gt;(<span style="color: #3366ff;">new </span>Derived);
}</pre>
<p><span id="more-168"></span>The use of these functions is relatively intuitive, especially the first one, however I will demonstrate the function in action with a small example below:</p>
<pre><span style="color: #3366ff;"><span style="color: #000000;">FILE : shape.h</span> #ifndef</span> _SHAPE_H_
<span style="color: #3366ff;">#define</span> _SHAPE_H_

<span style="color: #3366ff;">class </span>CShape
{
<span style="color: #3366ff;">public</span>:
     CShape() {}
     <span style="color: #3366ff;">virtual </span>~CShape() {}

    <span style="color: #3366ff;"> virtual float</span> Area() = 0;

<span style="color: #3366ff;">private</span>:
     <span style="color: #3366ff;">float </span>l, b;
};

<span style="color: #3366ff;">class </span>CSquare
 : <span style="color: #3366ff;">public </span>CShape
{
<span style="color: #3366ff;">public</span>:
     CSquare() : CShape() {}
     <span style="color: #3366ff;">virtual </span>~CSquare() {}

    <span style="color: #3366ff;"> virtual float</span> Area () { return l*b; }
};

<span style="color: #3366ff;">typedef </span>boost::shared_ptr&lt;CShape&gt; CShapePtr;

<span style="color: #3366ff;">#endif</span> <span style="color: #339966;">// _SHAPE_H_</span>

FILE : main.cpp<span style="color: #3366ff;"> int </span>main(<span style="color: #3366ff;">void</span>)
{
     ...
     CShapePtr Square = CreateNew&lt;CShape, CSquare&gt;();
     <span style="color: #3366ff;">float </span>area = Square-&gt;Area();
     ...
}</pre>
<p>Notice that I have typedef&#8217;d away the boost syntax to CShapePtr.  This makes what we are creating more obvious to someone not familiar with boost, namely, a pointer to a CShape object of type CSquare.  The limitation with these functions is that only the default constructor can be invoked.  These functions can be inherited from a template factory class containing these functions as static members with multiple overloads, but I find that the way these functions are works just fine for me &#8211; only ever program what you need, not what you want or think you might need <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Now lets move on to the destruction functions.  They are very simple to use as well and they should be fairly intuitive to use as well:</p>
<pre><span style="color: #3366ff;">template</span>&lt; <span style="color: #3366ff;">typename </span>T &gt;
<span style="color: #3366ff;">inline </span><span style="color: #3366ff;">void </span>Destroy(T t)
{
     <span style="color: #3366ff;">if</span>(t)
     {
          <span style="color: #3366ff;">delete </span>t;
          t = 0;
     }
}

<span style="color: #3366ff;">template</span>&lt; <span style="color: #3366ff;">typename </span>T &gt;<span style="color: #3366ff;"> inline </span><span style="color: #3366ff;">void </span>DestroyArray(T t)
{
     <span style="color: #3366ff;">if</span>(t)
     {
          <span style="color: #3366ff;">delete</span>[] t;
          t = 0;
     }
}

<span style="color: #3366ff;">template</span>&lt; typename T &gt;<span style="color: #3366ff;"> inline </span><span style="color: #3366ff;">void </span>Release(T t)
{
     <span style="color: #3366ff;">if</span>(t)
     {
          t-&gt;Release();
          t = 0;
     }
}</pre>
<p>Like I said, simple.  Note that you could also write these as macros, but I would advise against that since macros are almost impossible to debug.  Besides, I am using C++ not C!  As you can see, there are no typedefs required for these functions.  To delete something, just call the appropriate functions, passing the datatype into the &lt;&gt;&#8217;s and the object in the ()&#8217;s like so:</p>
<pre>Destroy&lt;<span style="color: #3366ff;">int<span style="color: #000000;">*</span></span>&gt;(ptr);
DestroyArray&lt;<span style="color: #3366ff;">char<span style="color: #000000;">*</span></span>&gt;(array);
Release&lt;IUnkown*&gt;(COMptr);</pre>
<p>I know that there will be some OO junkies out there that may not like my classless approach to these functions, but to them I say that C++ is a multi-paradigm language that actually encourages you to mix paradigms now and again to make programming a lot easier.  Feel free to use this code in your projects everyone!  If you come up with better solutions then let me know about them just e-mail me: d.brown@bigdavedev.com</p>
]]></content:encoded>
			<wfw:commentRss>http://bigdavedev.com/?feed=rss2&#038;p=168</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Portfolio Piece: OpenGL Post-processing</title>
		<link>http://bigdavedev.com/?p=120</link>
		<comments>http://bigdavedev.com/?p=120#comments</comments>
		<pubDate>Thu, 21 Oct 2010 22:31:38 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[Post-Processing]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Object Orientated]]></category>
		<category><![CDATA[RAII]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://bigdavedev.com/?p=120</guid>
		<description><![CDATA[Overview I created this project yesterday as an exercise in fast and effective programming.   I decided to go with a topic I knew really well that offered a great challenge, namely, post-processing.  I gave myself 12 hours to complete the project and I am very satisfied with the results.  I used only three  resources for [...]]]></description>
			<content:encoded><![CDATA[<h2>Overview</h2>
<p>I created this project yesterday as an exercise in fast and effective programming.   I decided to go with a topic I knew really well that offered a great challenge, namely, post-processing.  I gave myself 12 hours to complete the project and I am very satisfied with the results.  I used only three  resources for coding this application:</p>
<ol>
<li>OpenGL Registry</li>
<li>Nehe Lesson1 basecode</li>
<li>An article on GameDev.net about abstraction</li>
</ol>
<p>What I&#8217;m left with is an application that has a strong, reusable, robust and readable object hierarchy incorporating shaders, framebuffer obects, renderbuffer objects and renderable textures.</p>
<h2>The Application</h2>
<p>On start up, the application reads from a configuration file.  This file only contains the relative address to the post-processing shader that is to be used.  Currently, the choices are:</p>
<ol>
<li>Combined.frag &#8211; An unmodified rendering of the rendertarget&#8217;s contents,</li>
<li>BlackAndWhite.frag &#8211; A black and white filter for the rendertarget contents,</li>
<li>Invert.frag &#8211; Inverts the colours in the rendertarget.</li>
</ol>
<p>The application then proceeds to acquire all the resources, such as shaders, framebuffers, textures, renderable objects, etc before the rendering takes place.  In the rendering function forward rendering takes place followed by processing the rendered scene in a second pass.  Below are some screenshots of the application:</p>
<p><a href="http://localhost/wordpress/wp-content/uploads/2010/10/All.jpg" target="_blank"><img class="size-thumbnail wp-image-126 aligncenter" title="All" src="http://localhost/wordpress/wp-content/uploads/2010/10/All-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p><span id="more-120"></span>Part of the challenge I set myself was effective programming.  I take effective to mean code that does the job it is meant to, is readable and reusable.  A goal that I feel I accomplished with great success.  After reading through <a href="http://www.gamedev.net/reference/articles/article2103.asp" target="_blank">this</a> article I decided to follow the structure presented, which left me with a very plug-able system.  As an example, the framebuffer class makes good use of the hierarchy.  When we break it down into base components we see that it is an OpenGL object and uses the normal Gen, Bind and Destroy functions.  It is also part of a group of buffer objects that are now very useful in the OpenGL API &#8211; and mandatory in the core profile for version 3.x/4.x &#8211; so we can create a base class to handle all of those object types.  The resulting code looks like:</p>
<pre><span style="color: #3366ff;">template</span>&lt;<span style="color: #3366ff;">class </span>CObjectType&gt;
<span style="color: #3366ff;">class </span>CGLObject
{
<span style="color: #3366ff;">public</span>:
     CGLObject() : ID(CObjectType::GenerateID())
     {}

     <span style="color: #3366ff;">virtual </span>~CGLObject()
     {
          CObjectType::Destroy(ID);
     }

     <span style="color: #3366ff;">void </span>Bind() <span style="color: #3366ff;">const </span>     {
          CObjectType::Bind(ID);
     }

     <span style="color: #3366ff;">void </span>UnBind() <span style="color: #3366ff;">const </span>     {
          CObjectType::UnBind();
     }

     <span style="color: #3366ff;">unsigned int</span> GetID() <span style="color: #3366ff;">const </span>     {
          <span style="color: #3366ff;">return </span>ID;
     }

<span style="color: #3366ff;">protected</span>:
    <span style="color: #3366ff;"> unsigned int</span> ID;
};</pre>
<p>which gives us a base class for handling any OpenGL object we may choose to create.  Note that this is also a RAII based class.  The programmer inheriting from this class need not worry about creating or destroying the OpenGL object as that is handled in the constructor and destructor respectively.  The next class is the base class for buffer object types like framebuffers, renderbuffers or vertexbuffers:</p>
<pre><span style="color: #3366ff;">template</span>&lt; GLenum bufferType &gt;
<span style="color: #3366ff;">class </span>CBufferObject
{
<span style="color: #3366ff;">public</span>:
     <span style="color: #3366ff;">static </span>GLuint GenerateID()
     {
         GLuint ID;
         <span style="color: #3366ff;">switch</span>(bufferType)
         {
         <span style="color: #3366ff;">case </span>GL_FRAMEBUFFER:
             glGenFramebuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_RENDERBUFFER:
             glGenRenderbuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_ARRAY_BUFFER:
             glGenBuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         };
         <span style="color: #3366ff;">return </span>ID;
     }

     <span style="color: #3366ff;">static void</span> Bind(GLuint ID)
     {
         <span style="color: #3366ff;">switch</span>(bufferType)
         {
         <span style="color: #3366ff;">case </span>GL_FRAMEBUFFER:
             glBindFramebuffer(GL_FRAMEBUFFER, ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_RENDERBUFFER:
             glBindRenderbuffer(GL_RENDERBUFFER, ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_ARRAY_BUFFER:
             glBindBuffer(GL_ARRAY_BUFFER, ID);
             <span style="color: #3366ff;">break</span>;
         };
     }

     <span style="color: #3366ff;">static void</span> UnBind()
     {
         <span style="color: #3366ff;">switch</span>(bufferType)
         {
         <span style="color: #3366ff;">case </span>GL_FRAMEBUFFER:
             glBindFramebuffer(GL_FRAMEBUFFER, 0);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_RENDERBUFFER:
             glBindRenderbuffer(GL_RENDERBUFFER, 0);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_ARRAY_BUFFER:
             glBindBuffer(GL_ARRAY_BUFFER, 0);
             <span style="color: #3366ff;">break</span>;
         };
     }

     <span style="color: #3366ff;">static void</span> Destroy(GLuint ID)
     {
         <span style="color: #3366ff;">switch</span>(bufferType)
         {
         <span style="color: #3366ff;">case </span>GL_FRAMEBUFFER:
             glDeleteFramebuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_RENDERBUFFER:
             glDeleteRenderbuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         <span style="color: #3366ff;">case </span>GL_ARRAY_BUFFER:
             glDeleteBuffers(1, &amp;ID);
             <span style="color: #3366ff;">break</span>;
         };
     }
};</pre>
<p>Although a rather polluted looking template, it does allow us to have one base class to plug-in to the CGLObject class I posted first.  It also means that a programmer creating their own buffer class can plug this template into the CGLObject class and not have to worry about coding all the required template functions.  It&#8217;s all about re-usability in this hierarchy afterall.  Finally, we can now create the CFramebuffer class.  I will only post the class declaration as the rest of the code is not related to this topic &#8211; full source code can be downloaded at the bottom of this post though <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .  The declaration is as follows:</p>
<pre><span style="color: #3366ff;">class </span>CFramebuffer
 : <span style="color: #3366ff;">public </span>CGLObject&lt; CBufferObject&lt; GL_FRAMEBUFFER &gt; &gt;
{
    ...
};</pre>
<p>If you read the article I linked to it should be fairly obvious by now what this does.  The CFramebuffer class inherits CGLObject for it&#8217;s OpenGL handle functionality.  We plug in CBufferObject to CGLObject as we are creating a buffer type object and, specifically, we are creating a GL_FRAMEBUFFER type of buffer object, so this gets plugged into CBufferObject and our inheritance is complete.  When we create an instance of CFramebuffer, the CGLObject constructor gets automatically called, which creates a handle to our framebuffer object.  We add any other buffers or textures to the framebuffer with similar classes and when it comes time to use the framebuffer the bind function inherited from CGLObject gets called and similarly, when it&#8217;s time to stop using it, we call UnBind(), which simply binds the window framebuffer for us.  At the end of the objects lifetime the OpenGL handle to the object gets destroyed in CGLObject&#8217;s destructor and we are clear of any resource leaking!</p>
<h2>Limitations</h2>
<p>There are, however, some limitations to this code.  The biggest limitation is that, although I have coded an AddTexture function for the CFramebuffer class that allows you to add any number of textures, the number of draw buffers is hardcoded to 3.   I did not think of a way to get round this problem, although perhaps some sort of dynamic array would be the answer, I just didn&#8217;t have the time to look into it &#8211; zero hour was just 3 hours away at that time and I still had shaders to write&#8230;</p>
<p>For all the talk I give about the importance of RAII, I completely forgot to use my favourite boost library to wrap pointers &#8211; the ever wonderful shared_ptr&lt;&gt; class <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> .  This means that I have done all the memory management manually for this project.  Not such a big deal this time round for a relatively small project, however the lack of these boost::shared_ptr&#8217;s disturbs me if it were to be a bigger project with many shaders and sceneobjects floating around &#8211; after all, humans forget&#8230;</p>
<p>That said, these small things are the only limitations in software, which I am proud of.  The hardware limitations are a little less forgiving though, as the application relies on the availability of shaders and framebuffers.  However, according to Valve&#8217;s hardware review, most people have GPUs that are more than capable of running this application, so my hardware concern lies with a small percentage of people who don&#8217;t have a powerful enough GPU, or who run something that is not NVIDIA based&#8230;</p>
<h2>Downloads</h2>
<p><a href="http://bigdavedev.com/downloads/Lesson01.zip">source code</a></p>
<p><a href="http://bigdavedev.com/downloads/Framebuffer.zip">executable</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bigdavedev.com/?feed=rss2&#038;p=120</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My development machine has arrived!</title>
		<link>http://bigdavedev.com/?p=115</link>
		<comments>http://bigdavedev.com/?p=115#comments</comments>
		<pubDate>Tue, 05 Oct 2010 19:45:31 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Update]]></category>

		<guid isPermaLink="false">http://bigdavedev.com/?p=115</guid>
		<description><![CDATA[After a very long wait, Pickfords have finally made good their promise to deliver my posessions to my new flat here in Gothenburg! Finally I can get on with my graphics programming love affair and perhaps also produce the next set of tutorials on OpenGL3.2! I look forward to getting on with them now that [...]]]></description>
			<content:encoded><![CDATA[<p>After a very long wait, Pickfords have finally made good their promise to deliver my posessions to my new flat here in Gothenburg!  Finally I can get on with my graphics programming love affair and perhaps also produce the next set of tutorials on OpenGL3.2!  I look forward to getting on with them now that I have this awesome machine at my feet <img src='http://bigdavedev.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>On a side note, my PS3 has suddenly started working again, of its own accord&#8230;  This is very strange, but very delightful!  All in all, it&#8217;s going to be a great week ahead in getting stuff done!</p>
]]></content:encoded>
			<wfw:commentRss>http://bigdavedev.com/?feed=rss2&#038;p=115</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

