<?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>Intermediaware &#187; Beautiful hacks</title>
	<atom:link href="http://intermediaware.com/blog/category/beautiful-hacks/feed" rel="self" type="application/rss+xml" />
	<link>http://intermediaware.com/blog</link>
	<description>indie video game developer blgo</description>
	<lastBuildDate>Fri, 29 Mar 2013 19:18:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>C/C++: Pseudo Loops</title>
		<link>http://intermediaware.com/blog/cc-pseudo-loops</link>
		<comments>http://intermediaware.com/blog/cc-pseudo-loops#comments</comments>
		<pubDate>Fri, 29 Mar 2013 04:24:57 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=410</guid>
		<description><![CDATA[Do you think the loop inside following code does make any sense? .gist { font-size: .8em; } Well, it does, because you can break out of it in a very elegant way: You also will see something like that very often in macros. This is because, something like that: will expand into this: And this [...]]]></description>
			<content:encoded><![CDATA[Do you think the loop inside following code does make any sense?
<span id="more-410"></span>

<style>
.gist { font-size: .8em; }
</style>
<script src="https://gist.github.com/JochenHeizmann/5268700.js"></script>

Well, it does, because you can break out of it in a very elegant way:

<script src="https://gist.github.com/JochenHeizmann/7d4638c41fae03e2a930.js"></script>

<br/>
<hr/>
<br/>

You also will see something like that very often in macros. This is because, something like that:

<script src="https://gist.github.com/JochenHeizmann/497967b2280f6e7e83ad.js"></script>

will expand into this:

<script src="https://gist.github.com/JochenHeizmann/7829b42c4728e8dc2910.js"></script>

And this will produce an error. 

If you put a pseudo loop around it, everything works fine:

<script src="https://gist.github.com/JochenHeizmann/4d5155044bb973c97313.js"></script>]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/cc-pseudo-loops/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hack Of The Day: C++ Class Method Pointers</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-c-class-method-pointers</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-c-class-method-pointers#comments</comments>
		<pubDate>Wed, 07 Dec 2011 15:13:48 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=247</guid>
		<description><![CDATA[Function pointers are very handy in C++. But they don't work on class methods, here is a neat workaround, that even works with virtual methods.]]></description>
			<content:encoded><![CDATA[Function pointers are very handy in C++. But they don't work on class methods, here is a neat workaround, that even works with virtual methods.
<span id="more-247"></span>
<br/>

<script src="https://gist.github.com/1443125.js?file=main.cpp"></script>]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-c-class-method-pointers/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Hello World in Paint</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-hello-world-in-paint</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-hello-world-in-paint#comments</comments>
		<pubDate>Sat, 16 Apr 2011 22:29:32 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=6</guid>
		<description><![CDATA[What is your favorite IDE/Editor for coding C++? Well, it seems like some programmers prefer Paint over Visual Studio:]]></description>
			<content:encoded><![CDATA[
    What is your favorite IDE/Editor for coding C++? Well, it seems like some programmers prefer Paint over Visual Studio:<span id="more-6"></span>

<iframe title="YouTube video player" src="http://www.youtube.com/embed/IYLm8uclr0I" allowfullscreen="" frameborder="0" height="390" width="640"></iframe>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-hello-world-in-paint/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Render circles without trigonometry</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-render-circles-without-trigonometry</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-render-circles-without-trigonometry#comments</comments>
		<pubDate>Sat, 12 Mar 2011 22:12:34 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=12</guid>
		<description><![CDATA[The Bresenham line algorithm avoids trigonometry and uses integer math to calculate the points of a line, circle or ellipse. Here is a small example (implemented in C++ / SFML): #include &#60;SFML/Graphics.hpp&#62; void drawEllipse(sf::RenderWindow &#38;w, int cx, int cy, int r1, int r2) { sf::Shape point = sf::Shape::Circle(0, 0, 1, sf::Color::White); int x = -r1, [...]]]></description>
			<content:encoded><![CDATA[
    The Bresenham line algorithm avoids trigonometry and uses integer math to calculate the points of a line, circle or ellipse.<span id="more-12"></span>

Here is a small example (implemented in C++ / SFML):


  <pre>#include &lt;SFML/Graphics.hpp&gt;

void drawEllipse(sf::RenderWindow &amp;w, int cx, int cy, int r1, int r2) {
    sf::Shape point = sf::Shape::Circle(0, 0, 1, sf::Color::White);
    
    int x = -r1, y = 0;
    int err = r1 * r1 - (2 * r1 - 1) * r2 * r2, e2;
    
    do {
        point.SetPosition(cx - x, cy + y) ; w.Draw(point);
        point.SetPosition(cx + x, cy + y) ; w.Draw(point);
        point.SetPosition(cx + x, cy - y) ; w.Draw(point);
        point.SetPosition(cx - x, cy - y) ; w.Draw(point);
    
        e2 = 2 * err;
        
        if (e2 &gt;= (x * 2 + 1) * r2 * r2)
            err += (++x * 2 + 1) * r2 * r2;
        
        if (e2 

To learn more about the theory behind the algorithm, please have a look at the Midpoint Circle Algorithm at Wikipedia.</pre>

  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-render-circles-without-trigonometry/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Array-like access to fields</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-array-like-access-to-fields</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-array-like-access-to-fields#comments</comments>
		<pubDate>Sat, 26 Feb 2011 02:12:23 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=14</guid>
		<description><![CDATA[Sometimes it is useful if you are able to access fields of a struct or class as an array. For example, if you have the following struct: struct Point3D { float x,y,z; }; but you require to access the x,y and z-fields as an array, you can use an union: union Point3D { float coord[3]; [...]]]></description>
			<content:encoded><![CDATA[
    Sometimes it is useful if you are able to <strong>access fields</strong> of a <em>struct</em> or <em>class</em> <strong>as an array</strong>.<span id="more-14"></span>

For example, if you have the following <em>struct</em>:
<div class="CodeRay">
  <div class="code"><pre>struct Point3D {
    float x,y,z;
};</pre></div>
</div>


but you require to access the x,y and z-fields as an array, you can use an union:

<div class="CodeRay">
  <div class="code"><pre>union Point3D {
    float coord[3];
    struct {
        float x,y,z;
    };
};

Point3D p;
p.x = 1;
p.coord[1] = 2; // the same as  p.y = 2;
p.z = 3;</pre></div>
</div>


Another possibility is to create a class and overload the []-operator:

<div class="CodeRay">
  <div class="code"><pre>class Point3D {
public:
    float&amp; operator[](int x) { 
        assert(x &lt;= 2 &amp;&amp; x &gt;= 0); 
        if (x == 0) return this-&gt;x; 
        if (x == 1) return this-&gt;y; 
        return this-&gt;z;
    }

    float x,y,z;
};

Point3D p;
p.x = 1;
p[1] = 2;
p.z = 3;</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-array-like-access-to-fields/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Use references for parameter passing</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-use-references-for-parameter-passing</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-use-references-for-parameter-passing#comments</comments>
		<pubDate>Fri, 25 Feb 2011 04:33:03 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=15</guid>
		<description><![CDATA[If you have functions with lots of parameters you should consider using a struct or class and pass the parameters by reference. A reference or pointer is 4 bytes (on 32Bit systems). A function or method with a lot of parameters passed by value is more expansive than passing by reference because each parameter is [...]]]></description>
			<content:encoded><![CDATA[
    If you have functions with lots of parameters you should consider using a <em>struct</em> or <em>class</em> and pass the parameters by reference.<span id="more-15"></span>

A reference or pointer is 4 bytes (on 32Bit systems). A function or method with a lot of parameters passed by value is more expansive than passing by reference because <strong>each parameter is copied on the stack</strong>.

If you have complex parameters like classes you should never pass them by value, because the constructors and destructors will be invoked.

Pass by value:
<div class="CodeRay">
  <div class="code"><pre>int doStuff(int a, int b) {
    return a + b;
}

doStuff(1, 2);</pre></div>
</div>


<br />

Pass by reference:
<div class="CodeRay">
  <div class="code"><pre>struct someData {
    int a, b;
};

int doStuff(const someData &amp;foo) {
    return foo.a + foo.b;
}

someData foo;
foo.a = 1;
foo.b = 2;

doStuff(foo);</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-use-references-for-parameter-passing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Access private members in unit tests</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-access-private-members-in-unit-tests</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-access-private-members-in-unit-tests#comments</comments>
		<pubDate>Thu, 17 Feb 2011 15:32:02 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=18</guid>
		<description><![CDATA[Sometimes it could be very useful to be able to test private members or methods in your unit tests. But how could this be achieved? In C/C++ you can do this by using a simple macro: #define protected public #define private public]]></description>
			<content:encoded><![CDATA[
    Sometimes it could be very useful to be able to <strong>test private members or methods</strong> in your unit tests. But how could this be achieved?<span id="more-18"></span>

In C/C++ you can do this by using a simple macro:
<div class="CodeRay">
  <div class="code"><pre>#define protected public
#define private public</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-access-private-members-in-unit-tests/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Interfaces in C++</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-interfaces-in-c</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-interfaces-in-c#comments</comments>
		<pubDate>Fri, 04 Feb 2011 19:06:37 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=20</guid>
		<description><![CDATA[I lately talked with a friend about which language is the best and he told me that C++ doesn't have Interfaces like Java. Well, that's no true, you can create Interfaces in C++ like this: #include &#60;iostream&#62; class IRenderable { public: virtual ~IRenderable() {} virtual void Render() = 0; }; class MyRendererableNode : public IRenderable [...]]]></description>
			<content:encoded><![CDATA[
    I lately talked with a friend about which language is the best and he told me that C++ doesn't have Interfaces like Java.<span id="more-20"></span>

Well, that's no true, you can create Interfaces in C++ like this:

<div class="CodeRay">
  <div class="code"><pre>#include &lt;iostream&gt;

class IRenderable {
public:
    virtual ~IRenderable() {}
    virtual void Render() = 0;
};

class MyRendererableNode : public IRenderable {
public:
    virtual void Render() {
        std::cout &lt;&lt; &quot;Rendering stuff...&quot; &lt;&lt; std::endl;
    }
};

int main (int argc, char * const argv[]) {
    // IRenderable foo; &lt;-- this is not allowed
    MyRendererableNode foo;
    foo.Render();
}</pre></div>
</div>


Don't forget the virtual destructor, so the following code won't do strange things:

<div class="CodeRay">
  <div class="code"><pre>IRenderable *foo = new MyRendererableNode();
foo-&gt;Render();
delete foo, foo = NULL;</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-interfaces-in-c/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Safe delete</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-safe-delete</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-safe-delete#comments</comments>
		<pubDate>Thu, 03 Feb 2011 22:48:29 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=21</guid>
		<description><![CDATA[If you free up memory in C++, you should also set the pointer to NULL. If you double delete memory, your application will crash! Because I'm a lazy coder, I'm using a macro or template in nearly every of my C++-Projects to get rid of it: template&#60;class T&#62; inline void safeDelete (T*&#38; p) { delete [...]]]></description>
			<content:encoded><![CDATA[
    If you free up memory in C++, you should also set the pointer to <em>NULL</em>. If you double delete memory, your application will crash!<span id="more-21"></span>

Because I'm a lazy coder, I'm using a macro or template in nearly every of my C++-Projects to get rid of it:

<div class="CodeRay">
  <div class="code"><pre>template&lt;class T&gt;
inline void safeDelete (T*&amp; p) { 
    delete p; 
    p = NULL; 
}</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-safe-delete/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hack of the day: Random sort</title>
		<link>http://intermediaware.com/blog/hack-of-the-day-random-sort</link>
		<comments>http://intermediaware.com/blog/hack-of-the-day-random-sort#comments</comments>
		<pubDate>Wed, 02 Feb 2011 03:00:42 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Beautiful hacks]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intermediaware.com/blog/?p=22</guid>
		<description><![CDATA[@dennisosimon and myself are currently prototyping various things using haxe. Today we wanted to shuffle the elements of an array around. As haxe doesn't support a shuffle-method for arrays out of the box, we came up with a very simple workaround: Haxe provides an easy way to sort items. You can use the sort-method with [...]]]></description>
			<content:encoded><![CDATA[
    <a href="http://twitter.com/#!/dennisosimon">@dennisosimon</a> and myself are currently prototyping various things using <a href="http://haxe.org">haxe</a>. 

Today we wanted to <strong>shuffle the elements of an array</strong> around. As <a href="http://haxe.org">haxe</a> doesn't support a shuffle-method for arrays out of the box, we came up with a very simple workaround:<span id="more-22"></span>

<em><a href="http://haxe.org">Haxe</a></em> provides an <strong>easy way to sort items</strong>. You can use the sort-method with a function pointer as parameter to sort the elements according to your own comparison function.

What we've done is, that we simply just returned random values to achieve some kind of random sort algorithm:

<div class="CodeRay">
  <div class="code"><pre>var indices:Array&lt;Int&gt; = new Array&lt;Int&gt;();
for (i in 1...13) {
    indices.push(i);
}
indices.sort(randomSort);

private function randomSort(x:Int, y:Int):Int {
    return Math.random() &gt; 0.5 ? 1 : -1;
}</pre></div>
</div>


While this seems like a very easy solution with only a few lines of code, <a href="http://ilumbo.org/">Pimm Hogeling</a> and <a href="http://twitter.com/#!/43ryn">@43ryn</a> pointed out, that it isn't a good idea to use it, because in the worst case it could end in an infinite loop. You can read about the details <a href="http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html">here</a>. 

A better and also fast way is to implement the <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher&ndash;Yates shuffle</a>:

<div class="CodeRay">
  <div class="code"><pre>public function shuffleArray(arr : Array&lt;Int&gt;) { 
    var tmp : Int, j : Int, i : Int = arr.length;
    while (i &gt; 0) {
        j = Std.int(Math.random() * i);
        tmp = arr[--i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}</pre></div>
</div>
  ]]></content:encoded>
			<wfw:commentRss>http://intermediaware.com/blog/hack-of-the-day-random-sort/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
