<?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"
	>

<channel>
	<title>The Programmicon</title>
	<atom:link href="http://www.programmicon.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.programmicon.com</link>
	<description>Exploring programming.</description>
	<pubDate>Wed, 02 Jul 2008 15:20:34 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Improving Lua&#8217;d Bayts Performance</title>
		<link>http://www.programmicon.com/2008/02/13/improving-luad-bayts-performance/</link>
		<comments>http://www.programmicon.com/2008/02/13/improving-luad-bayts-performance/#comments</comments>
		<pubDate>Wed, 13 Feb 2008 18:32:40 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/?p=29</guid>
		<description><![CDATA[
In my last installment, we added the ability to create new steering behaviors to Bayts using the Lua scripting language. Unfortunately, the resulting implementation was horribly slow. So today we&#8217;ll explore using a very important tool: the profiler. For this article, I&#8217;ll be using Shark, Apple&#8217;s free profiler. These techniques apply just as well to [...]]]></description>
			<content:encoded><![CDATA[
<p>In my last installment, we added the ability to create new steering behaviors to Bayts using the Lua scripting language. Unfortunately, the resulting implementation was horribly slow. So today we&#8217;ll explore using a very important tool: the profiler. For this article, I&#8217;ll be using <a href="http://developer.apple.com/tools/sharkoptimize.html">Shark</a>, Apple&#8217;s free profiler. These techniques apply just as well to profilers on other platforms. 
<span id="more-29"></span></p>

<p>It&#8217;s been several months since I last looked at this material, mainly because I&#8217;ve been spending time with my new son, so some of it is a little rusty in my memory. Hopefully I get it all right!</p>

<p>Any time you&#8217;re confronted with a performance issue, the first thing you should do is fire up a profiler. Often, it can be tempting to just plow into the code and search for hot spots by hand, and you may even have a good lot of success going that route. But chances are, it&#8217;ll take you a lot longer then if you use a profiler. You might end up making micro-optimizations that have no real impact on performance, but makes your code less maintainable, and that is something that you should definitely avoid.</p>

<p>So, on that note, we&#8217;ll run Bayts in Shark and see what we can come up with. For this test, I&#8217;m going to use the Time Profile mode, with a time limit of 30 seconds. These are usually the default settings for Shark. Since Bayts is currently so simple, with no user interaction and no real launch time work, we can just launch the app and let it run for the 30 seconds, then stop it when it&#8217;s done, and we&#8217;ll get a pretty good sample. In more complicated projects, you&#8217;ll often need to get to a certain point in the game before starting the sampling. Either way, you&#8217;ll want to launch the app, then find it in Shark&#8217;s processes list and either click Shark&#8217;s Start button, or press Option+Esc.</p>

<p>Running the current version of Bayts through Shark&#8217;s Time Profile will give you something looking a little like this:</p>

<div style="text-align:center;"><a href="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-unoptimized-1.jpg"><img src="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-unoptimized-1-thumb2.jpg" alt="Shark Profile 1" border="0" width="300" height="150" /></a></div>

<p>From this, we can see that we&#8217;re spending a bit over 13% of our time in a lua function, luaS_newlstr. Well, we probably could easily blame Lua at this point, but it would be better to see where this function is getting called from, it might still be due to inefficiencies in our code. Shark lets us drill down for a closer look:</p>

<div style="text-align:center;"><a href="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-unoptimized-2.jpg"><img src="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-unoptimized-2-thumb1.jpg" alt="Shark Profile 2" border="0" width="300" height="150" /></a></div>

<p>Now we see that nearly all of the time in luaS&#95;newlstr is within one of our functions, lua&#95;pushbayt. As you&#8217;ll recall from <a href="http://www.programmicon.com/2008/01/29/luad-bayts/">last time</a>, this function pushes all of the information about a Bayt object onto Lua&#8217;s stack so it can be used in a Lua function.</p>

<p>I didn&#8217;t dig too much into the Lua code, but I&#8217;m willing to bet that there&#8217;s not too much we could do to improve the performance of luaS&#95;newlstr. So how about avoiding it altogether, or at least try to minimize how much we call it?</p>

<p>To start, we need to look at what code path actually leads us through luaS&#95;newlstr - we aren&#8217;t calling it directly from lua&#95;pushbayt. Fortunately, it&#8217;s not too hard to figure out, especially since we already have a snapshot of the appropriate call stack in Shark. Looks like there&#8217;s a function in between, lua&#95;setfield. A quick glance at the source for lua&#95;pushbayt confirms, we call this function a boatload of times:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8915640"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8915640').style.display='block';document.getElementById('plain_synthi_48889c8915640').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
void lua_pushbayt(lua_State* L, const Bayt&#038; b)
{
    lua_createtable(L, 0, 12);

    int tableIdx = lua_gettop(L);

    lua_pushvector(L, b.getPosition());
    lua_setfield(L, tableIdx, &#034;position&#034;);

    lua_pushvector(L, b.getLastPosition());
    lua_setfield(L, tableIdx, &#034;last_position&#034;);
    ...
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8915640"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8915640').style.display='block';document.getElementById('styled_synthi_48889c8915640').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> lua_pushbayt<span style="color: #66cc66;">&#40;</span>lua_State* L, <span style="color: #993333;">const</span> Bayt&amp; b<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_createtable<span style="color: #66cc66;">&#40;</span>L, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> tableIdx = lua_gettop<span style="color: #66cc66;">&#40;</span>L<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushvector<span style="color: #66cc66;">&#40;</span>L, b.<span style="color: #202020;">getPosition</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_setfield<span style="color: #66cc66;">&#40;</span>L, tableIdx, <span style="color: #ff0000;">&quot;position&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushvector<span style="color: #66cc66;">&#40;</span>L, b.<span style="color: #202020;">getLastPosition</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_setfield<span style="color: #66cc66;">&#40;</span>L, tableIdx, <span style="color: #ff0000;">&quot;last_position&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &#8230; </div></li></ol></div></div>

<p>It goes on from there for quite a while. </p>

<p>Do we really need to put <em>all</em> of this information on Lua&#8217;s stack? Let&#8217;s take a look at the one steering behavior we implemented in Lua:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89190d8"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89190d8').style.display='block';document.getElementById('plain_synthi_48889c89190d8').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
function SteeringTest(bayt, friends, enemies)
   local change = { [0] = 0, [1] = 0, [2] = 0 }
   local i

   for i = 1, friends.NearbyBaytCount - 1, 1 do
      local index = friends.indexes[i]

      if friends[index].id ~= bayt.id then
         change = vectorAdd(change, friends[index].velocity)
      end
   end

   change = setVectorMagnitude(change, bayt.minUrgency)

   return change
end
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89190d8"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89190d8').style.display='block';document.getElementById('styled_synthi_48889c89190d8').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="lua" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">function</span> SteeringTest<span style="color: #66cc66;">&#40;</span>bayt, friends, enemies<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">local</span> change = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">local</span> i</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">for</span> i = <span style="color: #cc66cc;">1</span>, friends.NearbyBaytCount - <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">local</span> index = friends.indexes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> friends<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span>.id ~= bayt.id <span style="color: #b1b100;">then</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;change = vectorAdd<span style="color: #66cc66;">&#40;</span>change, friends<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span>.velocity<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">end</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">end</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;change = setVectorMagnitude<span style="color: #66cc66;">&#40;</span>change, bayt.minUrgency<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">return</span> change</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">end</span> </div></li></ol></div></div>

<p>In this example, it looks like we inspect only 3 fields from any given Bayt: id, velocity, and minUrgency. So, for this function at least, we&#8217;re pushing a whole lot of stuff onto Lua&#8217;s stack that never gets used. And we know that doing so is somewhat expensive. What to do?</p>

<p>We could assume this function is pretty exemplary and try only pushing these three pieces of data onto the stack. But that is a major assumption backed by almost no evidence, and has the potential to be far too limiting to people wishing to write new scripts.</p>

<p>Another solution is to not push any data about the Bayt onto the stack, but instead just give the Lua script a handle which it can pass back to some functions we&#8217;ll supply to get the data it needs. That seems like a much better general solution. So let&#8217;s give it a shot.</p>

<p>Lua has a mechanism called &#8220;light user data&#8221; which allows you to give some Lua code an opaque piece of data that the runtime is not allowed to change, but which can be passed back into your custom code. We&#8217;ll use that mechanism to push handles to Bayts onto the Lua stack, instead of the whole object as we did before. What will be the handle? Why, a pointer to the Bayt, of course!</p>

<p>Now our huge lua&#95;pushbayt function has been reduced to a single line of code:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c891db12"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c891db12').style.display='block';document.getElementById('plain_synthi_48889c891db12').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
void lua_pushbayt(lua_State* L, const Bayt&#038; b)
{
    lua_pushlightuserdata(L, const_cast<Bayt*>(&#038;b));
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c891db12"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c891db12').style.display='block';document.getElementById('styled_synthi_48889c891db12').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> lua_pushbayt<span style="color: #66cc66;">&#40;</span>lua_State* L, <span style="color: #993333;">const</span> Bayt&amp; b<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushlightuserdata<span style="color: #66cc66;">&#40;</span>L, const_cast&lt;Bayt*&gt;<span style="color: #66cc66;">&#40;</span>&amp;b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>Of course, we have to provide a way for the Lua script to get data it&#8217;s interested in. This will be done with a set of functions taking the form:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c891f669"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c891f669').style.display='block';document.getElementById('plain_synthi_48889c891f669').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
extern &#034;C&#034; int lua_getBaytPosition(lua_State *L)
{
    validateArgumentCount(L, 1);
    
    Bayt* b = lua_tobayt(L, lua_gettop(L));

    if (b)
    {
        lua_pushvector(L, b->getPosition());
    }
    else
    {
        lua_pushvector(L, BaytVector(3, 0.0));
    }

    return 1;    
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c891f669"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c891f669').style.display='block';document.getElementById('styled_synthi_48889c891f669').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #ff0000;">&quot;C&quot;</span> <span style="color: #993333;">int</span> lua_getBaytPosition<span style="color: #66cc66;">&#40;</span>lua_State *L<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; validateArgumentCount<span style="color: #66cc66;">&#40;</span>L, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Bayt* b = lua_tobayt<span style="color: #66cc66;">&#40;</span>L, lua_gettop<span style="color: #66cc66;">&#40;</span>L<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; lua_pushvector<span style="color: #66cc66;">&#40;</span>L, b-&gt;getPosition<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">else</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; lua_pushvector<span style="color: #66cc66;">&#40;</span>L, BaytVector<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>;&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>There is a function like this for every member of a Bayt object that we want to expose to Lua script. The function lua&#95;tobayt is as straightforward as lua&#95;pushbayt:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8922d1b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8922d1b').style.display='block';document.getElementById('plain_synthi_48889c8922d1b').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
Bayt* lua_tobayt(lua_State* L, int index)
{
    return static_cast<Bayt*>(lua_touserdata(L, index));
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8922d1b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8922d1b').style.display='block';document.getElementById('styled_synthi_48889c8922d1b').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Bayt* lua_tobayt<span style="color: #66cc66;">&#40;</span>lua_State* L, <span style="color: #993333;">int</span> index<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">return</span> static_cast&lt;Bayt*&gt;<span style="color: #66cc66;">&#40;</span>lua_touserdata<span style="color: #66cc66;">&#40;</span>L, index<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>So, what does this buy us? Quite a bit, actually. We&#8217;ve gone from &#8220;go-get-a-cup-of-coffee-between-frames&#8221; slow to, on my machine, around 80fps. For comparison, the pure C++ version runs at about 115fps on this machine. Hmm, still losing quite a bit. But, then again, we knew we would when switching to using a scripting language. Still seems like a lot, though&#8230;  Let&#8217;s look at Shark again.</p>

<div style="text-align:center;"><a href="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-optimized.gif"><img src="http://www.programmicon.com/wp-content/uploads/2008/06/bayts-optimized-thumb.gif" alt="Shark Profile 2" border="0" width="300" height="150" /></a></div>

<p>It does look like we spend a little bit of extra time allocating and deallocating memory, but nothing too significant. Technically, we could probably do better than we are right now, but to be honest, I don&#8217;t think it&#8217;s worth it at the moment. If this system becomes more complex, and involves more scripting, then we&#8217;ll revisit the issue, but for now, it&#8217;s Good Enough.</p>

<p>The improved version of Bayts can be found, as always, in the Google Code SVN repository:</p>

<p><a href="http://programmicon.googlecode.com/svn/tags/LuaScripting-0.03">http://programmicon.googlecode.com/svn/tags/LuaScripting-0.03</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2008/02/13/improving-luad-bayts-performance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lua&#8217;d Bayts</title>
		<link>http://www.programmicon.com/2008/01/29/luad-bayts/</link>
		<comments>http://www.programmicon.com/2008/01/29/luad-bayts/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 17:33:41 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2008/01/29/luad-bayts/</guid>
		<description><![CDATA[


Before I start, I want to share some pretty important news. On January 8, 2008 at 7:45 PM Central, my son Sean was born. He was about 3 weeks early, but is nonetheless a healthy and (apparently) happy baby. You can read more about him at my wife&#8217;s blog, yellowpop.

Lua is a very popular scripting [...]]]></description>
			<content:encoded><![CDATA[


<p>Before I start, I want to share some pretty important news. On January 8, 2008 at 7:45 PM Central, my son Sean was born. He was about 3 weeks early, but is nonetheless a healthy and (apparently) happy baby. You can read more about him at my wife&#8217;s blog, <a href="http://www.yellowpop.com">yellowpop</a>.</p>

<p><a href="http://www.lua.org">Lua</a> is a very popular scripting system for games. There are a lot of very good reasons to use Lua in your game. It&#8217;s fast, lightweight, and free. Embedding the Lua runtime in your application is supposed to be very easy. Today, we&#8217;ll take a look at doing just that. We&#8217;re going to embed Lua into <a href="http://www.programmicon.com/2007/09/12/introducing-bayts/">Bayts</a>, making it possible to script new steering behaviors in Lua.</p>

<p><span id="more-28"></span></p>

<p>If you want to compile the code for this example, follow the appropriate &#8220;Getting Started&#8221; link in the heading of this page. When it&#8217;s time to check code out of SVN, you&#8217;ll want to use the URL https://programmicon.googlecode.com/svn/tags/LuaScripting-0.02. If you&#8217;ve already checked out earlier code, you can save yourself some time by switching to the new tag instead. Inside your workspace:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89672e5"><div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89672e5').style.display='block';document.getElementById('plain_synthi_48889c89672e5').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
svn switch https://programmicon.googlecode.com/svn/tags/LuaScripting-0.02
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89672e5"><div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89672e5').style.display='block';document.getElementById('styled_synthi_48889c89672e5').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="bash" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svn switch https://programmicon.googlecode.com/svn/tags/LuaScripting<span style="color: #cc66cc;">-0.02</span> </div></li></ol></div></div>

<p>This will bring your local workspace up to date. Unfortunately, because I did some restructuring of the ThirdParty directory between IntroducingBayts-0.01 and LuaScripting-0.02, you&#8217;ll still end up re-grabbing all of the third party libraries. I promise I won&#8217;t do it again, so in the future you won&#8217;t have to!</p>

<p>Lua is distributed as a library written in C. It can be built for many platforms, and is pretty easy to configure for your specific needs (for example, you can replace the underlying C type used to represent numbers to an integral type, for use on an embedded platform that doesn&#8217;t have hardware floating point number support).</p>

<p>Integrating Lua into your code is pretty straightforward. There is a relatively simple interface for calling Lua functions from C, and calling C functions from Lua. Arguments and return values in both cases are pushed onto a stack that Lua maintains. I won&#8217;t go into the specifics of how this is done, because there already exists excellent <a href="http://www.lua.org/manual/5.1/manual.html#3">documentation</a> for doing so. Instead, I&#8217;ll focus on the specifics of integrating Lua into Bayts.</p>

<h4>Design</h4>

<p>The ultimate goal of this exercise is to make it possible to create new steering behaviors for Bayts using the Lua scripting language. It should be as easy as possible for the script writer to create new steering scripts. Fortunately, the task is fairly simple, so creating a clean interface in this case is easy. We will require the Lua script to have just one function, which will take as arguments the bayt in question, as well as the list of friendly bayts and (for future expansion) the list of enemy bayts. The function definition in Lua might look like this:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c896866d"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c896866d').style.display='block';document.getElementById('plain_synthi_48889c896866d').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
function SteeringBehavior(bayt, friends, enemies)
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c896866d"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c896866d').style.display='block';document.getElementById('styled_synthi_48889c896866d').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="lua" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">function</span> SteeringBehavior<span style="color: #66cc66;">&#40;</span>bayt, friends, enemies<span style="color: #66cc66;">&#41;</span> </div></li></ol></div></div>

<p>Bayts will be represented by a Lua table, with fields for each of the publicly accessible class member variables of the C++ representation of the bayt. The bayts will be mutable, but we will be passing them by value, so changes made to a bayt in Lua will be discarded.</p>

<p>Vectors will also be represented by Lua tables, and will have a functional interface for performing operations on them. For instance, to add two vectors, the Lua scripter would call a function called vectorAdd:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8969ddb"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8969ddb').style.display='block';document.getElementById('plain_synthi_48889c8969ddb').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
newVector = vectorAdd(vectorA, vectorB)
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8969ddb"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8969ddb').style.display='block';document.getElementById('styled_synthi_48889c8969ddb').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="lua" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">newVector = vectorAdd<span style="color: #66cc66;">&#40;</span>vectorA, vectorB<span style="color: #66cc66;">&#41;</span> </div></li></ol></div></div>

<p>The function must return a vector, which will be the behavior&#8217;s contribution to the updated velocity vector.</p>

<h4>Implementation, Vectors</h4>

<p>Since pushing bayts on to the Lua stack will require us to push vectors onto the Lua stack, we&#8217;ll cover vectors first. As stated in the design, vectors will be represented as a Lua table, which will be indexed by number, like a C array. We could index it by the coordinate names, or even use both. Lua tables are versatile enough that a single table can be indexed by both numbers and strings. However, for simplicity&#8217;s sake, we&#8217;ll stick with just the numbers. Contrary to what is apparently Lua tradition, the table will be 0-based, like a C array. So, the x-coordinate will be at index 0, y at 1, and z at 2, which is the same representation as the C++ vector class we&#8217;re using.</p>

<p>With this in mind, the code to push and pop a vector onto and off the Lua stack is simple enough for me to present it here in whole:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c896b165"><div class="synthi_header" style="font-weight:bold;"> C++ <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c896b165').style.display='block';document.getElementById('plain_synthi_48889c896b165').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
void lua_pushvector(lua_State* L, const BaytVector&#038; v)
{
    lua_createtable(L, 3, 0);

    int tableIdx = lua_gettop(L);

    lua_pushnumber(L, 0);
    lua_pushnumber(L, v[0]);
    lua_settable(L, tableIdx);

    lua_pushnumber(L, 1);
    lua_pushnumber(L, v[1]);
    lua_settable(L, tableIdx);

    lua_pushnumber(L, 2);
    lua_pushnumber(L, v[2]);
    lua_settable(L, tableIdx);
}

BaytVector lua_tovector(lua_State* L, int index)
{
    BaytVector retval(3);

    lua_pushnumber(L, 0);
    lua_gettable(L, index);
    retval[0] = lua_tonumber(L, lua_gettop(L));
    lua_pop(L, 1);

    lua_pushnumber(L, 1);
    lua_gettable(L, index);
    retval[1] = lua_tonumber(L, lua_gettop(L));
    lua_pop(L, 1);

    lua_pushnumber(L, 2);
    lua_gettable(L, index);
    retval[2] = lua_tonumber(L, lua_gettop(L));
    lua_pop(L, 1);
    
    return retval;
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c896b165"><div class="synthi_header" style="font-weight:bold;"> C++ <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c896b165').style.display='block';document.getElementById('styled_synthi_48889c896b165').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="cpp" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">void</span> lua_pushvector<span style="color: #000000;">&#40;</span>lua_State* L, <span style="color: #0000ff;">const</span> BaytVector&amp; v<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_createtable<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">int</span> tableIdx = lua_gettop<span style="color: #000000;">&#40;</span>L<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, v<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_settable<span style="color: #000000;">&#40;</span>L, tableIdx<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, v<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_settable<span style="color: #000000;">&#40;</span>L, tableIdx<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">2</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, v<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_settable<span style="color: #000000;">&#40;</span>L, tableIdx<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BaytVector lua_tovector<span style="color: #000000;">&#40;</span>lua_State* L, <span style="color: #0000ff;">int</span> index<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; BaytVector retval<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_gettable<span style="color: #000000;">&#40;</span>L, index<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; retval<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#93;</span> = lua_tonumber<span style="color: #000000;">&#40;</span>L, lua_gettop<span style="color: #000000;">&#40;</span>L<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pop<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_gettable<span style="color: #000000;">&#40;</span>L, index<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; retval<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#93;</span> = lua_tonumber<span style="color: #000000;">&#40;</span>L, lua_gettop<span style="color: #000000;">&#40;</span>L<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pop<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushnumber<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">2</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_gettable<span style="color: #000000;">&#40;</span>L, index<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; retval<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #000000;">&#93;</span> = lua_tonumber<span style="color: #000000;">&#40;</span>L, lua_gettop<span style="color: #000000;">&#40;</span>L<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pop<span style="color: #000000;">&#40;</span>L, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">return</span> retval;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div></div>

<p>If you&#8217;ve looked at the Lua API documentation, you&#8217;ll see that we&#8217;re following the API&#8217;s naming convention for functions to push and pop values onto and off of the Lua stack. This is purely for consistency, and is not a requirement of the Lua API.</p>

<p>Our Lua vector library provides a fairly complete set of mathematical operations, which are implemented C++ functions, which we make available to Lua. Here is the function to add two vectors, for example:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89749be"><div class="synthi_header" style="font-weight:bold;"> C++ <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89749be').style.display='block';document.getElementById('plain_synthi_48889c89749be').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
extern &#034;C&#034; int lua_vectorAdd(lua_State *L)
{
    int n = lua_gettop(L);
    BaytVector v(3, 0.0);
    for (int i = 1; i <= n; ++i)
    {
        v+= lua_tovector(L, i);
    }
    lua_pushvector(L, v);
    return 1;
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89749be"><div class="synthi_header" style="font-weight:bold;"> C++ <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89749be').style.display='block';document.getElementById('styled_synthi_48889c89749be').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="cpp" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">extern</span> <span style="color: #666666;">&quot;C&quot;</span> <span style="color: #0000ff;">int</span> lua_vectorAdd<span style="color: #000000;">&#40;</span>lua_State *L<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">int</span> n = lua_gettop<span style="color: #000000;">&#40;</span>L<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; BaytVector v<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">0.0</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> i = <span style="color: #0000dd;">1</span>; i &lt;= n; ++i<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; v+= lua_tovector<span style="color: #000000;">&#40;</span>L, i<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lua_pushvector<span style="color: #000000;">&#40;</span>L, v<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div></div>

<p>This function, and others like it, actually take a variable number of arguments, so you can add up several vectors all in one function call. You can see we use the lua&#95;tovector function listed above to convert from the Lua vector arguments to the C++ vectors we can actually operate on. The final result is then pushed back onto the stack, using the lua&#95;pushvector function, and the function returns 1, telling Lua that we&#8217;ve placed a single return value on the stack.</p>

<p>The other functions in the vector library take this same basic form.</p>

<h4>Implementation, Bayts</h4>

<p>Pushing bayts onto the Lua stack is pretty much the same as doing the vectors, only with a whole lot more information. We put every value that is available to C++ steering behaviors in the Lua table, indexed by sensible names such as &#8220;velocity&#8221; and &#8220;desired&#95;speed&#8221;.</p>

<p>There are really no operations other than data retrieval that the Lua script can perform on a bayt, so the only functionality provided is to push and pop bayts onto and off of the Lua stack. </p>

<h4>Implementation, Nearby bayt lists</h4>

<p>In order to create a worthwhile steering behavior, the script needs to be able to access information about other nearby bayts. We&#8217;ll provide a list each for nearby friendly bayts and nearby enemy bayts (for future use).</p>

<p>As with the vectors and bayts, the bayt lists will be implemented as a table. We&#8217;ll take advantage of the fact that Lua tables can be indexed by both strings and numbers for this purpose. All of the bayts in the list will be indexed by number, starting with 0, like a C array. Other information, such as the number of nearby bayts, their indexes in the bayt list, and their distances from the bayt in question have a string index, like records.</p>

<h4>Implementation, Lua Script Manager</h4>

<p>To tie everything together, we&#8217;ll create a Lua Script Manager. This script manager will take on the task of loading Lua scripts and making them available to the steering engine. For the purposes of this exercise, the script manager will be exceedingly simple. It will load all of the Lua scripts it finds in a specified directory. It will also provide a means by which a Lua steering behavior can be added to a bayt flock. For now, the names of Lua steering behavior functions will have to be hard coded in the C++ code. This will be addressed at some time in the future.</p>

<h4>Implementation, The Code</h4>

<p>All of the Lua specific functionality is isolated in its own directory in the source tree, called Script/Lua. Look there for further details about the C++ implementation of the integration of Lua into Bayts.</p>

<h4>Lua Steering Behavior</h4>

<p>For the purposes of this example, I rewrote the MatchHeadingBehavior behavior in Lua. You&#8217;ll see in main.cpp that I&#8217;ve commented out the C++ version. You&#8217;ll find the Lua version in the LuaScripts directory, in a file called test.lua. Here&#8217;s how it looks:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8978455"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8978455').style.display='block';document.getElementById('plain_synthi_48889c8978455').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
function SteeringTest(bayt, friends, enemies)
   local change = { [0] = 0, [1] = 0, [2] = 0 }
   local i

   for i = 1, friends.NearbyBaytCount - 1, 1 do
      local index = friends.indexes[i]

      if friends[index].id ~= bayt.id then
        change = vectorAdd(change, friends[index].velocity)
      end
   end

   change = setVectorMagnitude(change, bayt.minUrgency)

   return change
end
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8978455"><div class="synthi_header" style="font-weight:bold;"> LUA <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8978455').style.display='block';document.getElementById('styled_synthi_48889c8978455').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="lua" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">function</span> SteeringTest<span style="color: #66cc66;">&#40;</span>bayt, friends, enemies<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">local</span> change = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">local</span> i</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">for</span> i = <span style="color: #cc66cc;">1</span>, friends.NearbyBaytCount - <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">local</span> index = friends.indexes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> friends<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span>.id ~= bayt.id <span style="color: #b1b100;">then</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; change = vectorAdd<span style="color: #66cc66;">&#40;</span>change, friends<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span>.velocity<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">end</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">end</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;change = setVectorMagnitude<span style="color: #66cc66;">&#40;</span>change, bayt.minUrgency<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #b1b100;">return</span> change</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">end</span> </div></li></ol></div></div>

<p>We start by creating a Lua table which mimics the form we use for Bayt vectors, initialized to 0. I&#8217;d say this is actually a design flaw - the scripter shouldn&#8217;t have to know how the data types are represented.</p>

<p>Next, we loop through the nearby bayts, and add their velocity to our contribution. Just like in the C++ version, we finish up by making sure the result vector has a magnitude equal to the bayt&#8217;s minimum urgency, and returning the contribution.</p>

<h4>Some Notes</h4>

<p>The very first thing you will probably notice when you run this version of Bayts is that it is <b>slow</b>. We&#8217;re talking go-get-a-cup-of-coffee-between-frames slow. There are several very good reasons for this performance issue. In my next post, we&#8217;ll tackle the performance and see if we can&#8217;t get it back up to a more reasonable level.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2008/01/29/luad-bayts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Boost</title>
		<link>http://www.programmicon.com/2007/11/28/boost/</link>
		<comments>http://www.programmicon.com/2007/11/28/boost/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 13:26:59 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/11/28/boost/</guid>
		<description><![CDATA[It&#8217;s time to introduce another 3rd party library that some of the code we work on here will depend on: Boost. Hopefully, many of you are already familiar with Boost, and already have an up-to-date version of it installed. For those of you who don&#8217;t already know about it, I strongly suggest you check it [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time to introduce another 3rd party library that some of the code we work on here will depend on: <a href="http://www.boost.org">Boost</a>. Hopefully, many of you are already familiar with Boost, and already have an up-to-date version of it installed. For those of you who don&#8217;t already know about it, I strongly suggest you check it out. It&#8217;s basically a collection of very useful, well designed and tested utility libraries, such as regular expressions, filename/path management, and serialization (which I discussed before in <a href="http://www.programmicon.com/2007/03/07/the-un-fun-stuff/">The Un-Fun Stuff</a>). Many of the people who develop the Boost library are also heavily involved in designing the C++ Standard Library, and many of Boost&#8217;s sub-libraries are on track to be included in future versions of the C++ Standard Library.
<span id="more-26"></span></p>

<p>I know there are a number of people out there who dislike Boost. From what I&#8217;ve seen, they are generally the same people who dislike the C++ Standard Library. It really seems to be an almost religious thing, and you can read many arguments for and against both libraries on other blogs, newsgroups, and gopher. Well, maybe not gopher so much&#8230;</p>

<p>I&#8217;m not going to try to sway those of you who dislike Boost or the C++ Standard Library. But I am going to be using them both, heavily, in the code presented here. Just a fair warning.</p>

<p>So, if you don&#8217;t already have Boost installed, please grab it and install it. There&#8217;s a very good <a href="http://boost.org/more/getting_started/index.html">Getting Started</a> guide at the Boost website. The next batch of code I present here depends on the Boost Filesystem library, and it requires at least version 1.34.1. If you already have a 1.33.x version, you&#8217;ll need to upgrade. Fortunately, Boost installs itself into versioned directories by default, so you can have 1.33 installed side-by-side with 1.34, if need be.</p>

<p><strong>UPDATE, 7/2/08:</strong> Boost is now in version 1.35. I&#8217;ve got ahead and made the switch, and I won&#8217;t promise that code presented here doesn&#8217;t depend on it. In other words, if you plan to follow along, then you&#8217;ll need Boost 1.35!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/11/28/boost/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Scripting</title>
		<link>http://www.programmicon.com/2007/09/25/script/</link>
		<comments>http://www.programmicon.com/2007/09/25/script/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 14:14:46 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/09/25/script/</guid>
		<description><![CDATA[Pretty well any major game needs some sort of scripting ability. You may be able to get by without it in some very simple games, but even, say, a classic shoot-em-up could benefit. Think, for instance, of scripted boss battles. You may well disagree, and that&#8217;s fine, but I&#8217;m going to leave it at that.



So [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty well any major game needs some sort of scripting ability. You may be able to get by without it in some very simple games, but even, say, a classic shoot-em-up could benefit. Think, for instance, of scripted boss battles. You may well disagree, and that&#8217;s fine, but I&#8217;m going to leave it at that.</p>

<p><span id="more-20"></span></p>

<p>So now that we all agree (it was my persuasive argument, wasn&#8217;t it?), we&#8217;re going to consider some of the options for adding scripting abilities to a game engine. Along the way, we&#8217;ll add some scripting abilities to <a href="http://www.programmicon.com/2007/09/12/introducing-bayts/">Bayts</a>. In particular, we&#8217;ll make it possible to create new steering behaviors using script.</p>

<p>As with most things, there are really two main options for adding scripting support to your game. Use a pre-existing, off-the-shelf solution, or develop your own. Several more decisions have to be made once you pick one of those two options, but that&#8217;s where it all starts. </p>

<p>If you&#8217;ve read any of my previous posts, you&#8217;ll probably guess that using an off-the-shelf solution would be my preference. You may be right but, more than most situations, this one is not quite so clear-cut.</p>

<p>For one thing, even if you use an off-the-shelf solution, it&#8217;s not actually going to work out of the box. The very nature of scripting support requires a fair amount of customization on your part. While it doesn&#8217;t seem terribly likely, it is absolutely possible that the glue for an off-the-shelf solution would outweigh the work of creating a new solution for a particular problem domain.</p>

<p>With that in mind, we&#8217;ll start by examining a very popular open source scripting language called <a href="http://www.lua.org/">Lua</a>. It&#8217;s light weight, highly portable, and is used in many games, both commercial and otherwise. We may then take a look at some other off-the-shelf solutions, and include them in our study, for completeness&#8217; sake.</p>

<p>Finally, we&#8217;ll take a look at creating our own small domain specific language. We&#8217;ll go through the process of defining the requirements of the language, coming up the syntax and semantics, and implementing it as an extension to Bayts. Unlike Lua, this will not be a general purpose language. We will be including features we need to define steering behaviors &mdash; and nothing else.</p>

<p>This exercise is not intended as a competition, and there will be no winner or loser. Each of the items we&#8217;ll be covering has its advantages and uses. Which one is right depends on your particular situation. Different jobs require different tools. Hopefully by the time we&#8217;re done, we&#8217;ll be better able to figure out when to reach for the Ball Pein, and when to reach for the Cross Pein.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/09/25/script/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introducing Bayts</title>
		<link>http://www.programmicon.com/2007/09/12/introducing-bayts/</link>
		<comments>http://www.programmicon.com/2007/09/12/introducing-bayts/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 13:28:02 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/09/12/introducing-bayts/</guid>
		<description><![CDATA[The code base for any given modern video game is a huge, complicated beast. There are countless code paths, and many actions are deferred, so the code that acts on a piece of data may well be far removed, both in space and time, from the code which created the data.

So, we&#8217;re not going to [...]]]></description>
			<content:encoded><![CDATA[<p>The code base for any given modern video game is a huge, complicated beast. There are countless code paths, and many actions are deferred, so the code that acts on a piece of data may well be far removed, both in space and time, from the code which created the data.</p>

<p>So, we&#8217;re not going to be creating a game engine, at least not today. Instead, I decided to start with something smaller, and simpler, upon which we can more easily add new features, the sorts of features that a modern game engine will need.</p>

<p>With that, I&#8217;d like to introduce you to Bayts, an implementation of flocking behavior. The name was suggested as a joke by my good friend <a href="http://www.magicalwasteland.com">Matthew</a>. A joke it may have been, but it stuck.</p>

<p><span id="more-15"></span></p>

<p>If you aren&#8217;t already familiar with flocking behavior, take a look at <a href="http://www.red3d.com/cwr/boids/">Boids</a>, the earliest known implementation. There are also articles about it in <a href="http://www.amazon.com/Game-Programming-Gems/dp/1584500492">Game Programming Gems</a> and <a href="http://www.amazon.com/Game-Programming-Gems/dp/1584500549">Game Programming Gems 2</a>. You should really have a look at least at the Boids page, because I am not going to go into any detail about what flocking behaviors are or how they work, at least not in this post.</p>

<p><a href="http://www.programmicon.com/wp-content/uploads/2007/08/bayts_screenshot.gif"><img src="http://www.programmicon.com/wp-content/uploads/2007/08/bayts_screenshot.thumbnail.gif" alt="Bayts Screenshot" title="Bayts Screenshot" /></a> The implementation we&#8217;re going to be working on is influenced pretty heavily by the one in <a href="http://www.amazon.com/Game-Programming-Gems/dp/1584500492">Game Programming Gems</a>. In many ways, though, it&#8217;s simpler. For example, the camera is fixed, and the bayts are represented by AAB&#8217;s, rather than arrows pointing in the direction of movement. This is intentional; I wanted the code to be as simple as possible, while still being flexible.</p>

<h4>ANN aside, or more Other People&#8217;s Code</h4>

<p>One of the problems that must be solved for implementing flocking behavior is a nearest neighbor search. Most example implementations of flocking behaviors that I&#8217;ve seen use a brute-force search, simply iterating over every single member of the flock, calculating its distance from the current member, and keeping track of the closest one(s). This works fine for small flocks, but very quickly gets bogged down when the number of members increases. There are alternative algorithms available for the nearest neighbor search, such as KD-trees, but these algorithms tend to be complex enough that they overshadow the actual flocking behavior algorithms. Which is, I suspect, why most flocking example code you find just sticks with the brute-force search.</p>

<p>Well, to be perfectly honest, I wasn&#8217;t terribly happy with that situation. I wanted our flock to have hundreds, maybe even thousands of members. But I also didn&#8217;t want to be overwhelmed trying to implement a better nearest neighbor algorithm. So I did what I always recommend - I got lazy. I searched around a bit, and found <a href="http://www.cs.umd.edu/~mount/ANN">ANN: A Library for Approximate Nearest Neighbor Searching</a>.</p>

<p>ANN is a C++ library for performing exact and approximate nearest neighbor searches, and is released under the <a href="http://www.gnu.org/copyleft/lesser.html">LGPL</a>. The library supports a few different data structures and algorithms for performing the nearest neighbor search, including KD-trees and box-decomposition trees. The interface is pretty straightforward. All of this makes it pretty much a perfect match for what I was looking for. </p>

<p>Bayts also depends on a couple other open source libraries:</p>

<ul>
<li><a href="http://www.hep.upenn.edu/~mjarvis/tmv/">TMV</a>, which I&#8217;ve <a href="http://www.programmicon.com/2007/04/27/laziness-ie-using-someone-elses-code/">discussed before</a>. I&#8217;ve modified the version in the source repository so that it can be built with Visual C++ 2005. I&#8217;ve sent the patch to the original author, and he&#8217;s planning to include it in the next release.</li>
<li><a href="http://freeglut.sourceforge.net/">freeglut</a>, an open source implementation of <a href="http://en.wikipedia.org/wiki/OpenGL_Utility_Toolkit">GLUT</a>. (Not used on Mac OS X).</li>
</ul>

<p>All of the dependent libraries are already in the source repository, so you will not have to go and get them individually.</p>

<h4>Get It</h4>

<p>I&#8217;m not really going to discuss Bayts any more in this post, as it&#8217;s already gone on long enough, but it will form the basis of several discussions going forward. If you want to get Bayts and try it out, you should follow these directions:</p>

<ul>
<li><a href="http://www.programmicon.com/getting-started-mac-os-x">Mac OS X</a></li>
<li><a href="http://www.programmicon.com/getting-started-windows">Windows</a></li>
<li><a href="http://www.programmicon.com/getting-started-linux">Linux</a></li>
</ul>

<p>The directions are somewhat general, since they will be basically the same for future Programmicon projects, but since this is the first time I&#8217;m presenting code in this way, all of the examples in the instructions are for Bayts. If you follow the instructions, you should end up with a working Bayts executable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/09/12/introducing-bayts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Some Notes</title>
		<link>http://www.programmicon.com/2007/08/27/some-notes/</link>
		<comments>http://www.programmicon.com/2007/08/27/some-notes/#comments</comments>
		<pubDate>Mon, 27 Aug 2007 13:30:59 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/08/27/some-notes/</guid>
		<description><![CDATA[As I mentioned in Where&#8217;s The Code, I will be posting code from this blog up on a Google code svn server. You may notice a new sidebar, which contains the information you&#8217;ll need to retrieve the code.

I&#8217;d also like to point out that all of the projects I&#8217;ll be presenting here will be using [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in <a href="http://www.programmicon.com/2007/02/22/wheres-the-code/">Where&#8217;s The Code</a>, I will be posting code from this blog up on a Google code svn server. You may notice a new sidebar, which contains the information you&#8217;ll need to retrieve the code.</p>

<p>I&#8217;d also like to point out that all of the projects I&#8217;ll be presenting here will be using <a href="http://www.cmake.org/">CMake</a> for its build system. Even if you have no plans to download the code I present here, I strongly recommend you check CMake out. It&#8217;s very powerful, but much easier to deal with then plain Makefiles, and definitely less of a hassle than the GNU Autotools, especially for Windows users. In a nutshell, you create some build configuration files, just like with every other build system. Then you run your project through the CMake tool, and it will generate a project for your native build system - be it make, Xcode, Visual Studio, etc. </p>

<p>The creators of CMake strongly recommend that you perform &#8220;out-of-source&#8221; builds. In other words, your build results, intermediate files, and anything else that the build process generates goes into a totally separate directory from your source files. For people used to using an IDE, this is probably nothing shocking. But, while Autotools and make and allow you to do this, it&#8217;s not necessarily obvious how, and it seems that few people do it. Since CMake makes it so stupidly easy, I don&#8217;t see a reason not to follow the recommendation, and tons of reasons to follow it. For one thing, it just keeps your source directory nice and clean. Just check out the <a href="http://www.cmake.org/HTML/RunningCMake.html">Running CMake</a> documentation.</p>

<p>One final note. Most all of the code I post here is cross-platform, and I&#8217;ve tested it on OS X, Windows XP, and Linux. There will be exceptions, which I will note when I post.</p>

<p>With all that said, I&#8217;ll be introducing our first project shortly&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/08/27/some-notes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Laziness (ie, using someone else&#8217;s code)</title>
		<link>http://www.programmicon.com/2007/04/27/laziness-ie-using-someone-elses-code/</link>
		<comments>http://www.programmicon.com/2007/04/27/laziness-ie-using-someone-elses-code/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 13:16:55 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/04/27/laziness-ie-using-someone-elses-code/</guid>
		<description><![CDATA[It has been said that laziness, to a point, is a virtue for programmers. Actually, most people leave out the &#8220;to a point&#8221; part, but I&#8217;m guessing it&#8217;s implied. As I&#8217;ve mentioned before, I take this philosophy pretty seriously. If I can avoid writing code well, by golly, I&#8217;ll avoid writing it.

So, today I&#8217;m going [...]]]></description>
			<content:encoded><![CDATA[<p>It has been said that laziness, to a point, is a virtue for programmers. Actually, most people leave out the &#8220;to a point&#8221; part, but I&#8217;m guessing it&#8217;s implied. As I&#8217;ve mentioned before, I take this philosophy pretty seriously. If I can avoid writing code well, by golly, I&#8217;ll avoid writing it.
<span id="more-12"></span>
So, today I&#8217;m going to avoid writing code, and I&#8217;m going to do so by using code someone else has written. I think many programmers actually have difficulty with this idea. I know that there was a time in my life that I did. Every now and again, I still do. I had this silly name for using someone else&#8217;s code: &#8220;black box programming.&#8221; And I hated it. So I wrote every last little routine, every single little bit of code a program required, regardless of whether there were implementations already freely available. The result, I hoped, would be that I would learn more about programming by doing this. Now, I&#8217;m not saying it wasn&#8217;t educational, but, the <strong>real</strong> result was that I never got anything done. Ever.
<p style="border: 0px none ; margin: 0px; padding: 0px; display: none; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: absolute; width: 122px; height: 122px; z-index: 2147483646; left: 226px; top: 853px" id="eG_nodes"><img src="chrome://easygestures/skin/menu.png" style="border: 0px none ; margin: 0px; padding: 0px; display: block; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; z-index: 2147483645; width: 122px; height: 122px; opacity: 1" /></p></p>

<p><p style="border: 0px none ; margin: 0px; padding: 0px; display: inline; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: absolute; z-index: 2147483647; left: 226px; top: 853px"><img src="chrome://easygestures/skin/link.png" style="border: 0px none ; margin: 0px; padding: 0px; display: block; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: absolute; left: 45px; top: 45px; width: 32px; height: 32px; visibility: hidden" /><input style="border-style: solid; border-color: #dddddd; border-width: 4px 2px 8px; font-family: Arial,sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal; background-color: white; color: black; text-align: center; position: absolute; z-index: 2147483647; visibility: hidden" /><img src="chrome://easygestures/skin/matchCase_Off.png" onclick="var matchCase = this.src.search('matchCase_On')!=-1; this.src=this.src.replace( (matchCase?'On':'Off') , (matchCase?'Off':'On'));" style="border: 0px none ; margin: 0px; padding: 0px; display: block; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: absolute; z-index: 2147483647; visibility: hidden" /><img src="chrome://easygestures/skin/altMenuSign.png" style="border: 0px none ; margin: 0px; padding: 0px; display: block; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: absolute; left: 93px; top: -2px; visibility: hidden; clip: rect(0px, 0px, 9px, 0px)" />
<p style="border: 0px none ; margin: 0px; padding: 0px; display: block; float: none; color: black; background-image: url('chrome://easygestures/skin/contextMenuSign.png'); background-color: red; min-height: 0pt; min-width: 0pt; line-height: 0.8; visibility: hidden"><img src="chrome://easygestures/skin/contextMenuSign.png" style="position: absolute; left: 27px; top: -25px; visibility: hidden" /><img src="chrome://easygestures/skin/contextMenuSign.png" style="position: absolute; left: 23px; top: -21px" /></p></p>

<p>Not to say there aren&#8217;t reasons to not want to use someone else&#8217;s code. There&#8217;s a lot of issues to consider. It can be difficult to assess the actual quality of the code. And, while doing so, you have to be careful not to pick nits (&#8221;This is supposed to be C++ code, but all of the files have a .C extension! This code must be <strong>horrible</strong>!&#8221;). There&#8217;s also documentation. Some very nice code comes with no documentation, some horrible code comes with extensive documentation. Licensing. If you&#8217;re planning to ever sell or give away your software, you&#8217;d better be sure the code you use has a license you can deal with. And, of course, finding the code in the first place can seem to be almost as much of a chore as just writing it yourself.</p>

<p>Almost, but often not quite. The benefits you can reap if you find a nice, well written, well documented bit of code that fits your needs can easily outweigh all of those issues, except perhaps if it has a nasty license. And, sometimes, even that can be dealt with.</p>

<p>This post started yesterday, when I discovered a subtle, but fatal bug in some vector math code I had written (for this blog, in fact). It was dumb, just a typo, but it took longer then I would have liked to track down. After that, I decided it would probably be a good idea to put together a test suite for the code, and find any other issues that might be lurking before I move on.</p>

<p>But, see, I&#8217;m lazy. And it was almost time to go to work, anyway.</p>

<p>I realized that there were probably dozens - nay, hundreds - of open source vector math libraries out there. If I were lucky, I&#8217;d be able to find a really good one, and heck, maybe it&#8217;d even have support for matrix math, too. Which I&#8217;d also be needing in the future.</p>

<p>So I went a-searchin&#8217;. I started with trusty old Google, but decided I ought to narrow the search a bit and took a peak at Sourceforge, where I found (the creatively named) <a href="http://www.hep.upenn.edu/~mjarvis/tmv/">Template Matrix/Vector Class Library for C++<img src="chrome://easygestures/skin/xLink.png" style="border: 0px none ; margin: 0px; padding: 0px; display: inline; float: none; color: black; background-image: none; background-color: transparent; min-height: 0pt; min-width: 0pt; line-height: 0.8; position: relative; left: 0px; top: 0px; width: 16px; height: 16px" /></a> by Mike Jarvis. TMV for short.</p>

<p>This library is definitely overkill for the little projects I&#8217;ll be using it for. But, it does everything I need, uses a coding style I enjoy, includes a very thorough test suite, and is well documented. The only thing that might bother me about it is that it&#8217;s licensed under the GPL 2, but since I&#8217;m just using it for little open sourced demo programs anyway, I&#8217;d say that&#8217;s fine.</p>

<p>Now I have nice vector math functionality, and complete matrix math support for when I&#8217;ll need it in the future. And thanks to the test suite, I can be confident in the results I get out of it. I barely lifted a finger.</p>

<p>This is just one example. In a future episode, I will be introducing you to yet another piece of someone else&#8217;s code that I&#8217;m using.</p>

<p>Anyway, at this point I&#8217;m rambling. The point of all of this is that there are lots of problems that need solving when developing software. Many of them have been solved over, and over, and over again. Do you really need to be spending your time solving them again? Well, maybe if your boss tells you to. But if you have the ability to be lazy about it, and find an already existing solution to use then, well, why wouldn&#8217;t you? I heartily recommend laziness.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/04/27/laziness-ie-using-someone-elses-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Un-Fun Stuff</title>
		<link>http://www.programmicon.com/2007/03/07/the-un-fun-stuff/</link>
		<comments>http://www.programmicon.com/2007/03/07/the-un-fun-stuff/#comments</comments>
		<pubDate>Wed, 07 Mar 2007 13:21:19 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/03/07/the-un-fun-stuff/</guid>
		<description><![CDATA[People who are interested in game programming typically have one or two specific areas that they consider The Fun Stuff. For many, it&#8217;s the graphics stuff, trying to figure out how to push more polys, with more detail while maintaining a reasonable framerate. For others, it&#8217;s AI, building smarter, nastier opponents for the player to [...]]]></description>
			<content:encoded><![CDATA[<p>People who are interested in game programming typically have one or two specific areas that they consider The Fun Stuff. For many, it&#8217;s the graphics stuff, trying to figure out how to push more polys, with more detail while maintaining a reasonable framerate. For others, it&#8217;s AI, building smarter, nastier opponents for the player to face. There might even be a handful who love nothing more than hacking low level network code, so even a player on a gimpy dial-up connection gets to enjoy the action. But I don&#8217;t believe I&#8217;ve ever encountered anyone who&#8217;s said &#8220;I want to get into game programming because I just love figuring out how to serialize data to disk!&#8221; or &#8220;If only I could get a job dealing with localization code in video games!&#8221; Maybe there are a few of you out there, but for most of us, this sort of thing falls under the category of The Un-Fun Stuff.
<span id="more-8"></span>
Unfortunately, the Un-Fun Stuff still needs to be written, and is still a vital part of developing a production quality game engine. So, I&#8217;ve decided I might as well get some of these items done and out of the way. In particular, I&#8217;ll be tackling data serialization. </p>

<p>If you&#8217;re not familiar with the concept of data serialization, the basic idea is to take a data structure and put it into a flattened form that can then be written to disk, sent across a network, or copied to another chunk of memory. Once a data structure has been serialized, you should be able to turn around and deserialize it, and get the same data structure. Sounds easy initially, but it turns out there are quite a few complications. Perhaps the biggest complications come from dealing with pointers and references. When you deserialize the data structure, you have no guarantee - indeed, no expectation - that pieces of the structure will be in the same locations in memory. Generally speaking, this problem is solved by keeping track of every pointer in the structure, what they&#8217;re supposed to point to, and their actual locations in the new data structure. Then, once every item to be deserialized is built up, you walk through all of these pointers and fix them up (usually referred to as linking) to point to the new, correct memory locations.</p>

<p>Now, this is probably a good time to mention that I am lazy. If I can avoid writing code, I usually do. </p>

<p>With all of that said, I&#8217;m going to pull a fast one on you. We&#8217;re not going to implement serialization today. No sir! You see, the Boost library already contains pretty nice support for data serialization. It does very nearly everything I want, it&#8217;s got a pretty nice interface, and boy-oh-boy does it save a lot of work. That&#8217;s not to say it&#8217;s a free ride, but certainly a lot better than starting from scratch. There&#8217;s plenty of great <a href="http://boost.org/libs/serialization/doc/index.html">documentation</a> for it on the <a href="http://www.boost.org">Boost web site</a>. A very quick summary: you&#8217;ll have to write some fairly simple functions to serialize your data. The serialization library already knows how to handle PODs, pointers, and references. So, an example might look like this:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89da2cd"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89da2cd').style.display='block';document.getElementById('plain_synthi_48889c89da2cd').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
class foo
{
   public:
      foo() : a(0), b('a') {}
   private:
      int a;
      char b;

      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &#038; ar, const unsigned int version)
      {
         ar & a;
         ar & b;
      }
};
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89da2cd"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89da2cd').style.display='block';document.getElementById('styled_synthi_48889c89da2cd').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class foo</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;public:</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : a<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, b<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;a&#8217;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;private:</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> a;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> b;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; friend class boost::<span style="color: #202020;">serialization</span>::<span style="color: #202020;">access</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; template&lt;class Archive&gt;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> serialize<span style="color: #66cc66;">&#40;</span>Archive &amp; ar, <span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> version<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ar &amp; a;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ar &amp; b;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div></li></ol></div></div>

<p>The first thing you might notice is the strange use of the &amp; operator in function serialize. This operator is equivelent to &gt;&gt; when serializing data, and &lt;&lt; when deserializing data. In simple cases such as this, this allows you to write a single function to handle both operations. Of course, not all situations are this simple, but there are plenty of tools in the serialization library to allow you to handle the more complicated cases.</p>

<p>One thing the Boost serialization library does not have out of the box is a portable binary archive format. There is an example in the documentation that can deal with different endianness of integers, and has the added benefit that it does some very minor compression of integral data. It does not, however, deal with floating point numbers. Also, the endianness of the archive data is hard coded to little endian. So, even if your primary platform is big endian, you&#8217;ll either have to change the archive code itself or always pay for byte swaps, even though they&#8217;re unnecessary. So, I&#8217;ve decided to create a custom portable binary archive format.</p>

<p>I should say here that, by portable, what I really mean is portable between most common desktop platforms. Right now, that means to me x86 and PPC. So we will only need to handle swapping bytes between little and big endian formats. Both platforms use IEEE 754 floating point representation, so floats and doubles will also require nothing more than a byte swap.</p>

<p>Fortunately, Boost Serialization makes it fairly easy to create your own archive format, and the process of doing so is well documented. I&#8217;ll just talk about the archive output code here; the input code is nearly identical and, as always, can be found in the <a href="http://code.google.com/p/programmicon/source">Programmicon SVN Repository</a>. </p>

<p>First of all, I&#8217;ll use the endianness utilities I talked about before to handle all of the byte swapping. Also, the archive itself will have a template parameter to specify the endianness of the archive. The <a href="http://boost.org/libs/serialization/doc/derivation.html">Boost Serialization documentation</a> is pretty good about explaining how to start creating your own archive format, so I won&#8217;t repeat all of that. Instead, I&#8217;ll just focus on the specifics of our portable format.</p>

<p>Since our archive will be a binary format, we&#8217;ll start by subclassing boost::archive::binary<em>oarchive</em>impl. I haven&#8217;t bothered to look into the actual code for binary<em>oarchive</em>impl, but the way we have to inherit from it makes it pretty clear it uses the <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern">Curiously Recurring Template Pattern</a>:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89de14b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89de14b').style.display='block';document.getElementById('plain_synthi_48889c89de14b').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
template<Endianness dataEndianness>
class portable_oarchive :
    public boost::archive::binary_oarchive_impl<portable_oarchive<dataEndianness> >
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89de14b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89de14b').style.display='block';document.getElementById('styled_synthi_48889c89de14b').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;Endianness dataEndianness&gt;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class portable_oarchive :</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public boost::<span style="color: #202020;">archive</span>::<span style="color: #202020;">binary_oarchive_impl</span>&lt;portable_oarchive&lt;dataEndianness&gt; &gt; </div></li></ol></div></div>

<p>In order to handle swapping our bytes, we will need to provide a set of save() functions, overridden for each data type we want to handle. We also need to provide a templatized version of this function to handle anything we don&#8217;t want to byte swap (such as strings and whatnot). Here&#8217;s a chunk of the code:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c89e008b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c89e008b').style.display='block';document.getElementById('plain_synthi_48889c89e008b').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
// Here's our fall through function. Any data type we don't directly handle will be handled here. Not that this means anything 
// that comes through this function will not be byte swapped. Since we will be overridding this function for all of the types we
// DO want byte swapped, that will be just fine.
template<class T>
void save(const T &#038; t)
{
   boost::archive::binary_oarchive_impl<derived_t>::save(t);
}

// Here's an example of a data type that we DO want to byte swap.  We'll need a function like this for every type we swap.
void save(int16 t)
{
   t = swapBytes<dataEndianness, HostEndianness>(t);
   this->save_binary(&#038;t, sizeof(int16));
}
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c89e008b"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c89e008b').style.display='block';document.getElementById('styled_synthi_48889c89e008b').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// Here&#8217;s our fall through function. Any data type we don&#8217;t directly handle will be handled here. Not that this means anything </span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// that comes through this function will not be byte swapped. Since we will be overridding this function for all of the types we</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// DO want byte swapped, that will be just fine.</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;class T&gt;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> save<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">const</span> T &amp; t<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;boost::<span style="color: #202020;">archive</span>::<span style="color: #202020;">binary_oarchive_impl</span>&lt;derived_t&gt;::<span style="color: #202020;">save</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// Here&#8217;s an example of a data type that we DO want to byte swap.&nbsp; We&#8217;ll need a function like this for every type we swap.</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> save<span style="color: #66cc66;">&#40;</span>int16 t<span style="color: #66cc66;">&#41;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;t = swapBytes&lt;dataEndianness, HostEndianness&gt;<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;this-&gt;save_binary<span style="color: #66cc66;">&#40;</span>&amp;t, <span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span>int16<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>The &#8220;default&#8221; save function is pretty simple, just passing its responsibility on to the base class. The override for the int16 data type isn&#8217;t much more complicated.  We simply swap bytes using one of the endianness tools I presented earlier. This is effectively a no-op if dataEndianness and HostEndianness are the same. After that, we pass the data on to a function that comes from binary<em>oarchive</em>impl to actually write the data to the archive. And, that&#8217;s it! We just provide similar functions for each data type we want to handle, specifically int16, uint16, int32, uint32, int64, uint64, float, and double. </p>

<p>That&#8217;s really pretty much all there is to the portable archive format.  I think the actual code for this thing is smaller than this stupid blog post! Of course, there are still all of the details of how to integrate this stuff into your code, and deciding what data actually needs to be serialized. But that is all outside of the scope of this post, which has gone on for long enough already, so I&#8217;ll leave that stuff as an exercise for the reader. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/03/07/the-un-fun-stuff/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Where&#8217;s The Code?</title>
		<link>http://www.programmicon.com/2007/02/22/wheres-the-code/</link>
		<comments>http://www.programmicon.com/2007/02/22/wheres-the-code/#comments</comments>
		<pubDate>Thu, 22 Feb 2007 14:47:40 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/02/22/wheres-the-code/</guid>
		<description><![CDATA[Most of the code I talk about here can be found at the The Programmicon Google Code page under the Source tab. You&#8217;ll need an SVN client of some sort. All of the code is released under the New BSD License.
]]></description>
			<content:encoded><![CDATA[<p>Most of the code I talk about here can be found at the <a href="http://code.google.com/p/programmicon/">The Programmicon Google Code</a> page under the Source tab. You&#8217;ll need an SVN client of some sort. All of the code is released under the New BSD License.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmicon.com/2007/02/22/wheres-the-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Template Magic</title>
		<link>http://www.programmicon.com/2007/02/22/template-magic/</link>
		<comments>http://www.programmicon.com/2007/02/22/template-magic/#comments</comments>
		<pubDate>Thu, 22 Feb 2007 14:42:28 +0000</pubDate>
		<dc:creator>Andy Molloy</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://www.programmicon.com/2007/02/22/template-magic/</guid>
		<description><![CDATA[Well, what I&#8217;m going to talk about today isn&#8217;t really magic, but it does illustrate some things you can do with C++ templates that, while certainly not uncommon, may not come immediately to mind. We&#8217;re going to use templates to implement some code for dealing with byte-swapping (endianness issues). I originally wrote this content as [...]]]></description>
			<content:encoded><![CDATA[<p>Well, what I&#8217;m going to talk about today isn&#8217;t really magic, but it does illustrate some things you can do with C++ templates that, while certainly not uncommon, may not come immediately to mind. We&#8217;re going to use templates to implement some code for dealing with byte-swapping (endianness issues). I originally wrote this content as part of an upcoming post, but that post was already getting to be several pages long. So, I&#8217;ve pulled the info out into this post, and will finish that one soon.
<span id="more-9"></span>
For quite a long time, I used a simple, but ugly, set of byte swapping functions I had written early in my C++ programming experience. It was filled with a million little functions like:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8a3b7c6"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8a3b7c6').style.display='block';document.getElementById('plain_synthi_48889c8a3b7c6').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
int hostToLittle(int);
unsigned int hostToLittle(unsigned int);
int hostToBig(int);
unsigned int hostToBig(unsigned int);
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8a3b7c6"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8a3b7c6').style.display='block';document.getElementById('styled_synthi_48889c8a3b7c6').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> hostToLittle<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> hostToLittle<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> hostToBig<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> hostToBig<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>; </div></li></ol></div></div>

<p>and on and on and on. Each function body has a preprocessor switch, determining whether it should actually swap the bytes or not. It was big, it was nasty, and it was a pain to maintain. But, it worked.</p>

<p>Now, I&#8217;m planning to put my code out for the whole wide world to see, and while maybe there&#8217;s only one or two of you who will actually look at it, I sure didn&#8217;t want to put that mess out there. So, I set out to clean it up.</p>

<p>The first step was simple, I just moved the actual byte swapping code out of the individual functions for each data type. The result was three new functions, one each for 2 bytes, 4 bytes, and 8 bytes. That at least opened up the possibility of creating platform specific implementations (such as using lwbrx on ppc, for instance). There&#8217;s still a ton of code, though, and lots of preprocessor conditionals.</p>

<p>So, I decided to consider using templates to simplify the code further. For some strange reason, my first thought was not to templatize all of those functions for each different supported data type. I know that is probably the more obvious first step, but it simply didn&#8217;t occur to me. Instead, I thought I could use templates to get rid of most of those preprocessor conditionals.</p>

<p>I use boost&#8217;s endian.hpp header to determine the host&#8217;s endianness. This header provides a handful of preprocessor macros that tell can help you determine the endianness of your host. The first step was to get this information out of the hands of the preprocessor, and give it to the compiler. That was easy:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8a3daae"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8a3daae').style.display='block';document.getElementById('plain_synthi_48889c8a3daae').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
enum Endianness
{
   BigEndian,
   LittleEndian,
#if defined(BOOST_BIG_ENDIAN)
   HostEndianness = BigEndian
#elif defined(BOOST_LITTLE_ENDIAN)
   HostEndianness = LittleEndian
#else
#error BeeLib only supports big and little endian systems.
#endif
};
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8a3daae"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8a3daae').style.display='block';document.getElementById('styled_synthi_48889c8a3daae').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">enum</span> Endianness</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;BigEndian,</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;LittleEndian,</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#if defined(BOOST_BIG_ENDIAN)</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;HostEndianness = BigEndian</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#elif defined(BOOST_LITTLE_ENDIAN)</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;HostEndianness = LittleEndian</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#else</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#error BeeLib only supports big and little endian systems.</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#endif</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div></li></ol></div></div>

<p>You may notice that error condition and wonder &#8220;what the heck is that doing there, aren&#8217;t all machines big or little endian?&#8221; The truth is, any machine this code is likely to run on is, in fact, big or little endian. But there also exists a class of CPUs that are middle-endian, and perhaps even a few other oddities. While I doubt anyone will ever try to build this on something so exotic, if they do, this will show them exactly where and what the error is, instead of in some other part of code where they try to use HostEndianness.</p>

<p>Initially, I wrote a template class, with all of those same functions from before as static members, something like this:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8a3fe22"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8a3fe22').style.display='block';document.getElementById('plain_synthi_48889c8a3fe22').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
template<Endianness in, Endianness out>
class ByteSwapper
{
   static int swapBytes(int);
   static unsigned in swapBytes(unsigned int);
   ...
};
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8a3fe22"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8a3fe22').style.display='block';document.getElementById('styled_synthi_48889c8a3fe22').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;Endianness in, Endianness out&gt;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class ByteSwapper</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> swapBytes<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #993333;">static</span> <span style="color: #993333;">unsigned</span> in swapBytes<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&#8230;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div></li></ol></div></div>

<p>and then template specialization for each of the four possible combinations of endiannesses. That&#8217;s right, four implementations of each of those functions:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8a42528"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8a42528').style.display='block';document.getElementById('plain_synthi_48889c8a42528').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
template<> int swapBytes<BigEndian, BigEndian>(int in) { return in; }
template<> int swapBytes<LittleEndian, LittleEndian>(int in) { return in; }
template<> int swapBytes<BigEndian, LittleEndian>(int in) { return swapBytes4(in); }
template<> int swapBytes<LittleEndian, BigEndian>(int in) { return swapBytes4(in); }
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8a42528"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8a42528').style.display='block';document.getElementById('styled_synthi_48889c8a42528').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;BigEndian, BigEndian&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> in; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;LittleEndian, LittleEndian&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> in; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;BigEndian, LittleEndian&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> swapBytes4<span style="color: #66cc66;">&#40;</span>in<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;LittleEndian, BigEndian&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> swapBytes4<span style="color: #66cc66;">&#40;</span>in<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>Ick! That is, I think, worse than the original! Surely we can do better than that!</p>

<p>Of course we can! The next iteration let me cut that list of functions in half, by adding a template parameter to decide if we should swap bytes or not. I never checked this version into source control, so this is from memory, and it almost certainly has some mistakes, but it looked something like this:</p>

<div class="synthi_code" style="display:none;" id ="plain_synthi_48889c8a463a8"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_48889c8a463a8').style.display='block';document.getElementById('plain_synthi_48889c8a463a8').style.display='none';return false">Show Styled Code</a>]:</span></div><pre style="width:100%;overflow:auto;">
template<Endianness in, Endianness out, bool shouldSwap = in == out>
class ByteSwapper
...
template<> int swapBytes<in, out, true>(int in) { return swapBytes4(in); }
template<> int swapBytes<in, out, false>(int in) { return in; }
</pre></div><div class="synthi_code" style="display:block;" id ="styled_synthi_48889c8a463a8"><div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_48889c8a463a8').style.display='block';document.getElementById('styled_synthi_48889c8a463a8').style.display='none';return false">Show Plain Code</a>]:</span></div><div class="c" style="font-family: monospace;"><ol><li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;Endianness in, Endianness out, bool shouldSwap = in == out&gt;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class ByteSwapper</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&#8230;</div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #202020;">template</span>&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;in, out, true&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> swapBytes4<span style="color: #66cc66;">&#40;</span>in<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;&gt; <span style="color: #993333;">int</span> swapBytes&lt;in, out, false&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> in<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> in; <span style="color: #66cc66;">&#125;</span> </div></li></ol></div></div>

<p>Now that&#8217;s not so bad, but, you guessed it, we can still do better! What&#8217;s stopping us, for instance, from templatizing on the type of data we&#8217;re being asked to swap, so we only need one pair 