<?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>Limegarden.net &#187; Game development</title>
	<atom:link href="http://limegarden.net/category/game-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://limegarden.net</link>
	<description>Personal site of Wouter Lindenhof</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:15:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Comments</title>
		<link>http://limegarden.net/2010/07/06/comments/</link>
		<comments>http://limegarden.net/2010/07/06/comments/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 06:31:43 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[Game development]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Style]]></category>

		<guid isPermaLink="false">http://limegarden.net/?p=353</guid>
		<description><![CDATA[This part is almost straight from my programming style document. This might sound stupid, but try to comment every other line (unless you have a lot of repeating or similar tasks). By doing so you get two advantages: Someone unskilled in programming or unfamiliar with what you are doing is able to read your code. [...]]]></description>
			<content:encoded><![CDATA[<p>This part is almost straight from my programming style document. </p>
<p>This might sound stupid, but try to comment every other line (unless you have a lot of repeating or similar tasks). By doing so you get two advantages:</p>
<ol>
<li>Someone unskilled in programming or unfamiliar with what you are doing is able to read your code.</li>
<li>You know what you are doing</li>
</ol>
<p>The first advantage is important unless you are working alone and never expect to see your code again. In that case you should really ask yourself if you should even be writing that code.</p>
<p>The second one is important even if you have no trouble reading code. Let's take a look at the following example.</p>
<pre class="brush: cpp; ">
for(int index_person = 0; index_person &lt; persons.size(); ++index_person)
{
    for(int index_kids = 0; index_kids &lt; persons[index_person].kids.size(); ++index_kids)
    {
        /* ... */
    }
}
</pre>
<p>And then take a look at this example:</p>
<pre class="brush: cpp; ">
/* Iterating through the persons in the lists */
for(int index_person = 0; index_person &lt; persons.size(); ++index_person)
{
    /* Iterating through the kids of the persons in the list */
    for(int index_kids = 0; index_kids &lt; persons[index_person].kids.size(); ++index_kids)
    {
        /* checking if any of the persons in the list have a kid who is dead */
        /* ... */
    }
}
</pre>
<p>In the last example I only have to track back to the first comment prior to my line of code to know what I'm doing here. It's a pain to write so many comments but it makes code a whole lot easier to read.</p>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/07/06/comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reverse updates</title>
		<link>http://limegarden.net/2010/06/09/reverse-updates/</link>
		<comments>http://limegarden.net/2010/06/09/reverse-updates/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 06:45:02 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[Game development]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://limegarden.net/?p=314</guid>
		<description><![CDATA[It has been a while since I last posted something, but that is because I have been busy with my graduation (in case you were wondering). Anyway, today, while I was walking the dog, I suddenly realized how to fix a bottleneck for a certain game idea. The game idea was that the hero can [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I last posted something, but that is because I have been busy with my graduation (in case you were wondering).</p>
<p>Anyway, today, while I was walking the dog, I suddenly realized how to fix a bottleneck for a certain game idea. The game idea was that the hero can interact and affect lives of other characters in the game directly, the bottleneck however was that each character affects another character, which affects another character and so on. A simple example would be if you decide to buy a weapon or not. If you do buy a weapon the merchant will be able to stay in business, buy some bread. When he buys bread, the baker will be able to buy the next shipment of grain from the miller and the miller we be able to pay the farmer and everybody will be happy. Now if you don't the miller might not be able to some bread (he needs to eat also) which means that he goes out of business. Since he is the only miller in the entire region, there will be no more bread for quite some time and everybody dies from the hunger.</p>
<p>The problem with that game idea is that if you even simulate a small village (say 25 people) you will have a lot of relationships to go through. Even if it goes in one direction it will still be 1 hero times 1 person times 25 people times 24 people times 23 people et cetera. The total amount of relationships within the village would be 1.6E+25 (16 with 24 zeros behind it). Even if we say that any action will have not have any affect after 7 relationships that would still be 2.4 billion relationships that are affected. If we limit the amount of relationships to 7 for each person (so 7x7x7x7x7x7x7) it will be 823543 of relationships that need to be updated when the hero does something. And maybe one of those persons affected will need to perform an action that affects the community which means the entire process starts over again.</p>
<p>Of course the game is real time and the other people in the village don't require you to perform any action so if you have 3 or 4 villages you will have almost a constant rate of large social updates. Until this morning it was my firm belief that the above game idea should be hard if not impossible to execute unless you have some monster system. </p>
<p>I always thought in forward direction, what happened now dictates the future, until I thought about reversing the flow, the past is dictated by the future.</p>
<p>Lets take the previous example of buying a weapon or not. Although this is a fact now by the player, the game does not have access at that information at the current time. Let's say you meet the miller and find out he is alive. No matter what happened in the past, the future dictates past events that no matter what the miller survived and kept his job. And the only way that he has kept his job if the baker bought his grain and the only way that the baker could buy his grain was if some paid for his bread. If you made a major investment by buying a weapon (say a gold weapon) then he might credit you for it. If you weren't the cause something else must have happened for the influx of money that allowed the community to stay alive.</p>
<p>Now lets take the same example but the miller is dead. The past generated from that point on could dictate that the hero not buying could have caused it. Or if he did buy the past could credit something else (disease, war, bad crops et cetera).<br />
So what is so different about the reverse update compared to the forward update that it allows us to compact information in such a manner that the above game idea is possible?</p>
<p>Simply put: Forward updates are simulations, calculating exact future behavior, while reverse updates are emulations, imitation of past behavior. A forward update always has one cause (generally the hero) but has many effects (the merchant, the baker, the miller and the farmer). A reverse update has one effect (the baker) and will look for one cause for its current state which is a linear path. </p>
<p>I'm not certain if the solution always works as the past needs to be consistent (you can't undo the past) and certain actions always have a forward update, but for now the game idea seems possible again.</p>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/06/09/reverse-updates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Large scene rendering</title>
		<link>http://limegarden.net/2010/04/26/large-scene-rendering/</link>
		<comments>http://limegarden.net/2010/04/26/large-scene-rendering/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 08:41:15 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[Game development]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://limegarden.net/?p=308</guid>
		<description><![CDATA[There is one thing I don't like about floating point and integers and that is that they consist out of only certain amount of bits. An integer is at least 32 bits (on 32 systems at least). A float is also 32 bits. A double is 64 bits. Since I'm currently thinking about a space [...]]]></description>
			<content:encoded><![CDATA[<p>There is one thing I don't like about floating point and integers and that is that they consist out of only certain amount of bits. An integer is at least 32 bits (on 32 systems at least). A float is also 32 bits. A double is 64 bits.</p>
<p>Since I'm currently thinking about a space game, I was wondering how to create a huge battle field (a whole solar system) in real time while having all the precision I need. </p>
<p>Doubles are not fast and take a lot of space and although I don't worry about space, speed is a bigger issue. If I want a huge battle and here I'm going to throw some numbers: One side can have as maximum 85 huge ships, 3010 fighters, makes 3960 weapon slots, which makes 19800 bullets flying around. And that is only one side. Since I need two sides for a war, I need to double the values (190 ships, 6020 fighters, 7920 hardpoints, 39600 bullets). Lets say that everything is represented by one location (which a 3D vector) it would mean 53730 locations. 53730 locations times 3 would be 161190 and that times <code class="codecolorer cpp geshi"><span class="cpp"><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span></span></code> would be 161190 * 8 bytes = 1289520 bytes (about 1MB) which is a lot to start with even if you ignore the fact that the problem is more that I have a lot than the doubles are bigger.</p>
<p>As I was considering the problem I realized something else. When I'm rendering, I will need to use floats. DirectX 9 requires floats and although I could send doubles it would half my data bandwidth. So what ever I choose, in the end I will need to use floats unless I want to have some penalty.</p>
<p>Using doubles for vertices are is a bad idea for the sake of precision alone.</p>
<p>But then I started thinking...</p>
<p>I don't need to render using doubles, I can simply render everything with floats and to ensure that precision is maintained. If something is not within the safe area of floating point rendering (let's say floats can have a maximum of 1024, and yes I now it can be a lot more) but the object in question is 2048 units away, I still want to render it. You can't simply let a sun disappear because it is beyond the range of your numbers, that would be weird.</p>
<p>Instead everything that is more than 1024 units away will be rendered first (farthest away first) and the distance also scales it down. Than I clear the Z buffer and render everything in the 1024 range.</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Sun has a size of 100<br />
Sun is 2048 units away<br />
Sun needs to be rendered.<br />
<br />
To render it but maintain the correct look:<br />
Scale sun down so that at 1024 it would have the same <br />
size on screen as if it was rendered at 2048.</div></td></tr></tbody></table></div>
<p>It was so simple that I thought it was silly I never thought about it. How you scale it however depends on the view and projection matrix. </p>
<p>I'm still not certain if I want to use doubles, but for my second problem I have a solution. Now I need to find one for my first.</p>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/04/26/large-scene-rendering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combo hit!! in code</title>
		<link>http://limegarden.net/2010/04/06/combo-hit-in-code/</link>
		<comments>http://limegarden.net/2010/04/06/combo-hit-in-code/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 07:17:24 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[code snippit]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://limegarden.net/?p=302</guid>
		<description><![CDATA[I have always wondered how hard it would be to write a combo system. Not that hard I guess. And after a bit of morning programming I already got it working. The only reason it took longer than anticipated was because of muscle memory. One of the features I wanted to test was the delay. [...]]]></description>
			<content:encoded><![CDATA[<p>I have always wondered how hard it would be to write a combo system. Not that hard I guess. And after a bit of morning programming I already got it working. </p>
<p>The only reason it took longer than anticipated was because of muscle memory. One of the features I wanted to test was the delay. For example you want to do the "asdf" combo, but you are for some reason not fast enough, than the combo should not start. Simulating this is easy, just begin the combo and stop somewhere for a second and then complete the combo. So "asd", pause and then "f".<br />
However when I tried that for some reason the combo was sometimes completed. Only after adding the debug messages I noticed that I often automatically did complete the combo. The problem was muscle memory.</p>
<p>Anyway below is the code and if anyone wants to extend it (wrong next key, combo breakers, roman cancel, and follow-up combos) feel free to do so and let me know.</p>
<pre class="brush: c++; ">

/*******************************************************************************
 * The MIT License
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2010 Wouter Lindenhof (http://limegarden.net)
 *
 * Demonstration of a simple ComboHit system
 *******************************************************************************/

#include &lt;Windows.h&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#pragma comment(lib, &quot;Winmm.lib&quot;)

#define DEBUGLOG 1 // Set to 0 to turn debug messages off

class KeyHit
{
public:
	UINT m_Key;
	UINT m_Delay;
	UINT m_Waiting;
public:
	KeyHit(UINT key, UINT delay) : m_Key(key), m_Delay(delay), m_Waiting(0) { }
};

class HitCombo
{
	UINT m_SequenceIndex;
	std::vector&lt;KeyHit&gt; m_Keys;
public:
	HitCombo() : m_SequenceIndex(0) { }

	void Cancel();
	void Update(DWORD ms);
	operator bool();
	HitCombo&amp; operator &lt;&lt; (const KeyHit&amp; hit);
};

int main(int argc, const char* argv[])
{
	std::cout &lt;&lt; &quot;-----------------------------------------------&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;This is a combo key tester: &quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;Press \&quot;ASDF\&quot; quickly to do a combo hit&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;Press \&quot;Wouter\&quot; quickly to write my name&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;Press SHIFT and then escape to quit&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;-----------------------------------------------&quot; &lt;&lt; std::endl;
	HitCombo QuitApplication, ComboHit, WouterCombo;
	QuitApplication &lt;&lt; KeyHit(VK_SHIFT, 250) &lt;&lt; KeyHit(VK_ESCAPE, 250);
	ComboHit		&lt;&lt; KeyHit(&#039;A&#039;, 250) &lt;&lt; KeyHit(&#039;S&#039;, 250) &lt;&lt; KeyHit(&#039;D&#039;, 250)
					&lt;&lt; KeyHit(&#039;F&#039;, 250);
	WouterCombo		&lt;&lt; KeyHit(&#039;W&#039;, 250) &lt;&lt; KeyHit(&#039;O&#039;, 250) &lt;&lt; KeyHit(&#039;U&#039;, 250)
					&lt;&lt; KeyHit(&#039;T&#039;, 250) &lt;&lt; KeyHit(&#039;E&#039;, 250) &lt;&lt; KeyHit(&#039;R&#039;, 250);

	DWORD lastTime = timeGetTime();
	DWORD curTime = lastTime;
	DWORD difference = 0;

	while(true)
	{
		curTime = timeGetTime();
		difference = curTime - lastTime;
		lastTime = curTime;
		if(ComboHit)
		{
			std::cout &lt;&lt; &quot;You did a combo hit!!&quot; &lt;&lt; std::endl;
			WouterCombo.Cancel();
			QuitApplication.Cancel();
		}
		if(WouterCombo)
		{
			std::cout &lt;&lt; &quot;You wrote &#039;Wouter&#039;, good for you!&quot; &lt;&lt; std::endl;
			ComboHit.Cancel();
			QuitApplication.Cancel();
		}
		if(QuitApplication)
		{
			std::cout &lt;&lt; &quot;You quit the application!&quot; &lt;&lt; std::endl;
			ComboHit.Cancel();
			WouterCombo.Cancel();
			break;
		}
		ComboHit.Update(difference);
		QuitApplication.Update(difference);
		WouterCombo.Update(difference);
	}

	return 0;
}

void HitCombo::Cancel() {
	m_SequenceIndex = 0;
}

// Implementation
HitCombo&amp; HitCombo::operator &lt;&lt;(const KeyHit &amp;hit) {
	m_Keys.push_back(hit);
	return *this;
}

HitCombo::operator bool() {
	return m_SequenceIndex == m_Keys.size();
}
void HitCombo::Update(DWORD ms)
{
	if(m_SequenceIndex &lt; m_Keys.size())
	{
		KeyHit&amp; hit = m_Keys[m_SequenceIndex];
		hit.m_Waiting += ms;
		if(GetAsyncKeyState(hit.m_Key) != 0)
		{
			if(hit.m_Waiting &lt; hit.m_Delay)
			{
				hit.m_Waiting = 0;
				m_SequenceIndex++;
#if DEBUGLOG = 1
				std::cout &lt;&lt; &quot;you pressed key &quot; &lt;&lt; (char)hit.m_Key &lt;&lt; std::endl;
#endif
			}else
			{
#if DEBUGLOG = 1
				std::cout &lt;&lt; &quot;Delay too long&quot; &lt;&lt; std::endl;
#endif
				hit.m_Waiting = 0;
				m_SequenceIndex = 0;
			}
			if(m_SequenceIndex == 0)
			{
				hit.m_Waiting = 0;
			}
		}else if(m_SequenceIndex==0)
		{
			hit.m_Waiting = 0;
		}else
		{
			if(hit.m_Waiting &gt; hit.m_Delay)
			{
				m_SequenceIndex = 0;
				hit.m_Waiting = 0;
#if DEBUGLOG
				std::cout &lt;&lt; &quot;Delay too long&quot; &lt;&lt; std::endl;
#endif
			}
		}
	}else
	{
		m_SequenceIndex = 0;
		KeyHit&amp; hit = m_Keys[m_SequenceIndex];
		hit.m_Waiting = 0;
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/04/06/combo-hit-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nova’s Achilles heel</title>
		<link>http://limegarden.net/2010/03/31/novas-achilles-heel/</link>
		<comments>http://limegarden.net/2010/03/31/novas-achilles-heel/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 07:59:23 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[Game development]]></category>
		<category><![CDATA[Nova]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://limegarden.net/2010/03/31/novas-achilles-heel/</guid>
		<description><![CDATA[Nova’s Achilles heel is the math and a lot needs to be improved there. Instead of attempting to improve I thought I would use a high quality math library instead (in this case vmmlib). Using SVN external I added it so that it would automatically fetch the latest version from their SVN and be done [...]]]></description>
			<content:encoded><![CDATA[<p>Nova’s Achilles heel is the math and a lot needs to be improved there. Instead of attempting to improve I thought I would use a high quality math library instead (in this case <a href="http://vmmlib.sourceforge.net/" target="_blank">vmmlib</a>). Using <a href="http://svnbook.red-bean.com/en/1.5/svn.advanced.externals.html" target="_blank">SVN external</a> I added it so that it would automatically fetch the latest version from their SVN and be done with it. Problem is that I’m using DirectX which uses row major matrices and vmmlib was written to be used with OpenGL which uses column major matrices. So after a huge rewrite and removing the old math headers I tried to run Brick and see if it worked, I had hoped that by setting the effect files to use column major matrices I would let it use one system. Seeing the result, it either didn’t work or something else had gone wrong. </p>
<p>I don’t know what exactly went wrong (besides the obvious) but I guess that instead of using another library I just have to finish and improve my own math library. It’s a good thing SVN allows me to revert. <img src='http://limegarden.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A good math library is essential for a game developer. It has to be easy to use and clear but I also want answers on the following questions when I’m using it:</p>
<ul>
<li>When I define a quaternion is it written as W-X-Y-Z or as X-Y-Z-W? </li>
<li>Does a multiplication operator between two matrices do a matrix multiplication or an unit multiplication? </li>
<li>Also how safe are the functions? </li>
<li>How do I prevent hidden cost? </li>
<li>And how easy is it to cast from one to another? Vector3&lt;float&gt; to Vector3&lt;int&gt; might seems obvious but it requires code to support it. And I could also cast Vector3 to Vector2. </li>
</ul>
<p>Then there are the bigger question about optimization, like do we use SIMD or not.</p>
<p>The biggest problem is of course to ensure that all math is correct. Most of the time I use it by instinct, but in this case I actually have to relearn all the math again, double check, ensure that I’m using the right version, triple check. Obviously I experience it as a pain, but it as they say: </p>
<blockquote><p>No pain, no gain!</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/03/31/novas-achilles-heel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smart pointers: A dumb idea?</title>
		<link>http://limegarden.net/2010/03/24/smart-pointers-a-dumb-idea/</link>
		<comments>http://limegarden.net/2010/03/24/smart-pointers-a-dumb-idea/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 19:46:24 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[IUnknownk]]></category>
		<category><![CDATA[Nova]]></category>
		<category><![CDATA[smart pointer]]></category>

		<guid isPermaLink="false">http://limegarden.net/?p=298</guid>
		<description><![CDATA[A while ago I wrote a smart pointer for Nova and I have been wondering on whether or not I should use it. And if I did choose to use it to what degree. Smart pointers are useful, I use the boost variant often enough, but like everything you shouldn’t fully depend on it. Pointers [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote a smart pointer for Nova and I have been wondering on whether or not I should use it. And if I did choose to use it to what degree. </p>
<p>Smart pointers are useful, I use the boost variant often enough, but like everything you shouldn’t fully depend on it. Pointers are in many cases good enough. The only thing why smart pointers are so useful is that you don’t have to worry about the lifetime of the object (when the object is destroyed). </p>
<p>In many cases the only time you worry about is when data needs to be shared, for example a texture, as you don’t want to load the same data twice. But another simple technique is the <a href="http://en.wikipedia.org/wiki/IUnknown" target="_blank">IUnknown interface</a> which creates an object that will delete itself when it has been released as many times as it was created. You still don’t have to worry about the lifetime of the object, you only have to remind you to call release when you no longer need an object. If nothing requires that object the call to release will cause it to delete itself.</p>
<p>Here is the basic code:</p>
<div class="codecolorer-container cpp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* IBaseObject.h */</span><br />
<span style="color: #0000ff;">class</span> IBaseObject<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> m_ReferenceCounter<span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; IBaseObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">virtual</span> ~IBaseObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> AddRef<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<span style="color: #ff0000; font-style: italic;">/* IBaseObject.cpp */</span><br />
<span style="color: #339900;">#include &quot;IBaseObject.h&quot;</span><br />
IBaseObject<span style="color: #008080;">::</span><span style="color: #007788;">IBaseObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_ReferenceCounter<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">&#125;</span><br />
IBaseObject<span style="color: #008080;">::</span>~IBaseObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> IBaseObject<span style="color: #008080;">::</span><span style="color: #007788;">AddRef</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">++</span>m_ReferenceCounter<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> IBaseObject<span style="color: #008080;">::</span><span style="color: #007788;">Release</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000040;">--</span>m_ReferenceCounter<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>m_ReferenceCounter <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> m_ReferenceCounter<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>After some thought on the issue I have decided that IUnknown is far superior to a smart pointer. The majority of the smart pointers I have encountered and written work on the same basic principle. Each smart pointer has a pointer to the object and also a pointer to an integer in which you store the amount of copies. That means that if you look at the code it will look something like this:</p>
<div class="codecolorer-container cpp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span><br />
<span style="color: #0000ff;">class</span> smart_ptr<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; T<span style="color: #000040;">*</span> m_Data<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> m_Copies<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>When you think about how smart_ptr really works, you will think how stupid the idea is. For every copy of the object you will have two extra pointers, assuming 32 bit system, that would be 8 bytes extra for every copy.<br />
The second issue is a bit more hidden but if you have this function:</p>
<div class="codecolorer-container cpp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> CObject<span style="color: #008080;">;</span>&nbsp; <span style="color: #666666;">// Just an object</span><br />
<span style="color: #0000ff;">void</span> accidental_copy<span style="color: #008000;">&#40;</span>smart_ptr<span style="color: #000080;">&lt;</span>CObject<span style="color: #000080;">&gt;</span> copyParam<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/** does something **/</span> <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>You will be making an copy of the object, which means that you will have an extra construct and destruct function to take in account. Which means that a smart pointer not only increases the amount of memory needed, but also reduces the speed. With a pointer you won't have the above problems.</p>
<p>There is however one huge advantage that a smart pointer has but IUnknown doesn't: Smart pointers can work with virtually any type while to use IUnknown the object needs to be derived from it. However since I'm writing the code for Nova I can just make sure that all objects derive from IUnknown. </p>
<p>To add an conclusion, I have decided to before using smart pointers I should first try and see if there is another solution. </p>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/03/24/smart-pointers-a-dumb-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing for the iPhone</title>
		<link>http://limegarden.net/2009/05/06/developing-for-the-iphone/</link>
		<comments>http://limegarden.net/2009/05/06/developing-for-the-iphone/#comments</comments>
		<pubDate>Wed, 06 May 2009 00:39:38 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[Game development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://limegarden.net/2009/05/06/developing-for-the-iphone/</guid>
		<description><![CDATA[For those who follow me on twitter this post is old news or it might be a minor refreshment as the majority of the content has already been posted on twitter. At my internship I have been developing for the iPhone and boy oh boy, can I make a list of things to complain about. [...]]]></description>
			<content:encoded><![CDATA[<p>For those who follow me on twitter this post is old news or it might be a minor refreshment as the majority of the content has already been posted on twitter.</p>
<p>At my internship I have been developing for the iPhone and boy oh boy, can I make a list of things to complain about. Luckily there are also a few good things and they balance each other but it&rsquo;s a pain when you cannot unleash every bit of skill you have. Let&rsquo;s start with the sour part.</p>
<ol>
<li>There is no stencil buffer. I think that the stencil buffer is as old as Duke Nukem 3D, which was released in 1993. It&rsquo;s used for shadows and a few other things. However for some reason it is not supported on the iPhone (at V2.2.1 but this might change in the future) and it makes no sense.</li>
<li>Secondly the iPhone is under clocked (meaning the CPU is running at lower speed than what it should run at). The majority of the hardware these days are able to dynamically set the clock speed of the hardware. I can accept the reason why apple has done it (preserving the battery) but when it comes to games it&rsquo;s like with racing cars. You don&rsquo;t want a F1 car being capped at 120 KM/H because you aren&rsquo;t suppose to go faster than that on the highway. In apple&rsquo;s defense, the iPhone has never been intended to be primarily a gaming device.</li>
<li>Third is apple is really strict in what is allowed or not and some parts of the iPhone (like the photo camera) should only be accessed by calling a function and then except you have no longer control until a picture has been taken using the graphical user interface apple designed. Using it directly in some other fashion increases the chance that the application will be rejected. On the other hand some applications are allowed because some big guys back it (google is one of the companies that ignored the guidelines of apple and still got accepted).</li>
</ol>
<p>There are still a few other problems (no multitasking for example, API might break with new firmware update) with the iPhone but let&rsquo;s move on to the sweet part of the iPhone.</p>
<p>Actually the only one I can think of is that the distribution of applications can only be done through the apple app store. There is no such thing for the windows mobile or symbian. This way apple can also guarantee the quality of the applications, however I find it a mood point since an application that is poorly written is unlikely to be successful.</p>
<p>Actually there are a few more things, the emulator is good (better than the one for the Nintendo DS) and the API is reasonable documented (inconsistence in quality, but overall reasonable).</p>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2009/05/06/developing-for-the-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
