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

<channel>
	<title>voyce</title>
	<atom:link href="http://www.voyce.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voyce.com</link>
	<description>Programming and debugging tidbits</description>
	<lastBuildDate>Tue, 23 Apr 2013 01:11:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>A short (and round) history of the button</title>
		<link>http://www.voyce.com/index.php/2013/04/23/a-short-and-round-history-of-the-button/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-short-and-round-history-of-the-button</link>
		<comments>http://www.voyce.com/index.php/2013/04/23/a-short-and-round-history-of-the-button/#comments</comments>
		<pubDate>Tue, 23 Apr 2013 01:10:56 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[push button;gui]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1590</guid>
		<description><![CDATA[The push button. It&#8217;s truly the blunt instrument of UI design. While most other controls provide some indication of the type of operation they&#8217;re performing &#8211; sliders are adjusting a value, a switch is moving between two states &#8211; buttons just mean &#8220;do something&#8221;. What? The only way to tell is to press it and [...]]]></description>
				<content:encoded><![CDATA[<p><div id="attachment_1675" class="wp-caption alignleft" style="width: 110px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/macos6_okcancel.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/macos6_okcancel.png" alt="Early MacOS OK and Cancel buttons" width="100" height="85" class="size-full wp-image-1675" /></a><p class="wp-caption-text">Early MacOS OK and Cancel buttons</p></div>The push button. It&#8217;s truly the blunt instrument of UI design. While most other controls provide some indication of the type of operation they&#8217;re performing &#8211; sliders are adjusting a value, a switch is moving between two states &#8211; buttons just mean &#8220;do something&#8221;. What? The only way to tell is to press it and see. But this shouldn&#8217;t be the case.<br />
<span id="more-1590"></span></p>
<h3>From mechanical to digital</h3>
<p>In the pre-digital, mechanical world, buttons as we know them were pretty rare. Toggling switches were much more common. But when buttons did exist, by necessity they would have a direct physical connection to the thing they were acting on and usually some immediate effect. Unfortunately in the digital realm this relation no longer holds, and that&#8217;s what make it so easy to misuse buttons in a user interface.</p>
<h3>UX &#8211; &#8220;Press me&#8221;</h3>
<p>So, what experience do users have of the buttons in your application&#8217;s UI?</p>
<p>The biggest issue is: how will they know the button&#8217;s true purpose (without pressing it to see)? Will it open a file or delete the contents of your hard disk? In the wrong hands it could do either of those things, or none. Your only clue is the label. In much the same way as the &#8220;drink me&#8221; and &#8220;eat me&#8221; labels in Alice&#8217;s Wonderland: the appearance of the thing isn&#8217;t congruent with its effect.  </p>
<h4>The easy way out</h4>
<p>Unfortunately the very non-specificity of a button is what makes it so easy to over-use by inexperienced UI creators. If they haven&#8217;t thought carefully about the specific operations their users are performing, the quickest thing to do is have pressing a button be the action that invokes whatever functionality you need. </p>
<p>The Apple human interface guidelines stress that you should use a verb to indicate what your button will do, and this is a useful guideline when deciding whether to use them at all. You should only use a button to perform a single well-defined action in a context where the corresponding noun is clear. If it&#8217;s not granular enough, maybe you need to break things down into smaller tasks. Think of examples like &#8216;Cancel&#8217; (whatever operation is underway), &#8216;Close&#8217; (the document you&#8217;re working on).   </p>
<p>So how did we get to the current state-of-the-art in buttons?</p>
<h3>A visual history</h3>
<h4>The early years</h4>
<p><div id="attachment_1687" class="wp-caption alignright" style="width: 102px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/bios.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/bios.png" alt="" title="bios" width="92" height="85" class="size-full wp-image-1687" /></a><p class="wp-caption-text">BIOS-style buttons</p></div>Even back in the pre-GUI days of the terminal there were buttons. All you need is some ASCII or ANSI characters to draw a box and you&#8217;ve got yourself a button. Of course it also helps if you add a drop shadow or some inverting to indicate a press. Here we can even see a nascent use of the double-border to indicate the default operation.</p>
<p><div id="attachment_1662" class="wp-caption alignleft" style="width: 199px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/word_dos_ok_cancel.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/word_dos_ok_cancel.png" alt="" title="word_dos_ok_cancel" width="189" height="49" class="size-full wp-image-1662" /></a><p class="wp-caption-text">OK and Cancel from DOS Word</p></div>Anyone who&#8217;s tweaked their BIOS will also be familiar with the text-based buttons there. Unfortunately in these text-based environments, evolution hit a bit of a dead end.<br />
<br clear="all"/></p>
<p><div id="attachment_1689" class="wp-caption alignleft" style="width: 111px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/nextstep_apply.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/nextstep_apply.png" alt="" title="nextstep_apply" width="101" height="39" class="size-full wp-image-1689" /></a><p class="wp-caption-text">NeXTStep goes kinda 3D</p></div>Meanwhile around 1998, the forerunner of MacOS, NeXTStep, was blazing a trail with chiseled effect buttons. Probably because it only ran on hardware that was ridiculously high-end for the time.</p>
<h4>Round rect</h4>
<p><div id="attachment_1675" class="wp-caption alignright" style="width: 110px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/macos6_okcancel.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/macos6_okcancel.png" alt="" title="macos6_okcancel" width="100" height="85" class="size-full wp-image-1675" /></a><p class="wp-caption-text">Early MacOS OK and Cancel buttons</p></div>In the early days of the Macintosh it became obvious that its creative users wouldn&#8217;t make do with boring old rectangular buttons, so instead they got ones with nicely rounded corners. Actually, this was more of a clever workaround for the 1-bit, black and white display. In that environment how could you distinguish between some non-interactive text in a rectangle, and a button? The use of rounded corners served as a simple and obvious visual indicator of this difference. Rounded rectangles became so ubiquitous in the UI that functions to draw them existed in the low-level QuickDraw library, see <code>FrameRoundRect</code> in <a href="http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/ImagingWithQuickDraw.pdf">Inside Macintosh: Imaging with QuickDraw</a> from the early 90s.  </p>
<p><div id="attachment_1688" class="wp-caption alignleft" style="width: 87px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/windows1_ok.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/windows1_ok.png" alt="" title="windows1_ok" width="77" height="29" class="size-full wp-image-1688" /></a><p class="wp-caption-text">OK button from Windows 1.0 circa 1990.</p></div>Strikingly similar is the OK button from the first, DOS-based, version of Windows, that appeared around 1990.<br />
<br clear="all"/></p>
<p><div id="attachment_1694" class="wp-caption alignleft" style="width: 100px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/win30_ok.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/win30_ok.png" alt="" title="win30_ok" width="90" height="40" class="size-full wp-image-1694" /></a><p class="wp-caption-text">Windows 3.0 OK button circa 1990</p></div>As Windows evolved, so did its buttons. Version 3.0 introduced 256 colour VGA and the button style previously seen in NeXTStep. </p>
<h4>The Borland years</h4>
<p><div id="attachment_1665" class="wp-caption alignleft" style="width: 84px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/borland.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/borland.png" alt="" title="borland" width="74" height="97" class="size-full wp-image-1665" /></a><p class="wp-caption-text">Borland-style OK and Cancel buttons</p></div>Around the time of Windows 3.1, Borland were king of the Windows development tool-set with their (VB precursor) Delphi and C++ compiler. These also shipped with a GUI library (the Visual Component Library) that enabled you to make full use of the 16 colours that were available to include an image with the button. So now you knew that, err, OK was good, Cancel was bad.  </p>
<p><div id="attachment_1668" class="wp-caption alignright" style="width: 198px"><a href="http://www.voyce.com/wp-content/uploads/2013/04/mac_os8.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/mac_os8.png" alt="" title="mac_os8" width="188" height="36" class="size-full wp-image-1668" /></a><p class="wp-caption-text">MacOS 8/9 buttons</p></div>As time moved on and Windows became 95, 98, NT and XP, and MacOS 7, 8, 9 and X, resolutions and colour-depths improved across the board. Buttons became more heavily embossed, and that was about it. But, as the functionality that people put behind the button become more and more complex, sometimes simple word (preferably verb) labels were no longer enough.</p>
<h4>Shiny!</h4>
<div class="alignleft">
<a href="http://www.voyce.com/wp-content/uploads/2013/04/macosx.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/macosx.png" alt="" title="macosx" width="233" height="39" class="alignleft size-full wp-image-1669" /></a><br clear="all"/><a href="http://www.voyce.com/wp-content/uploads/2013/04/win7.png"><img src="http://www.voyce.com/wp-content/uploads/2013/04/win7.png" alt="" title="win7" width="179" height="38" class="alignleft size-full wp-image-1670" /></a></div>
<p>Nowadays things are getting serious. We have graphical processing units that can generate squillions of pixels per second even on a run-of-the-mill machine. What shall we do with all this processing power? Let&#8217;s make our buttons shiny! And transparent. And with a gradient and specular highlight. And let&#8217;s make them pulse.</p>
<p>However shiny our buttons get, remember that they&#8217;re not a catch-all for every action in your user interface. The golden rule is to use the most specific control that maps closely to the operation that is occurring from the user&#8217;s point-of-view.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2013/04/23/a-short-and-round-history-of-the-button/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An iOS Lava Lamp using OpenGL ES shaders</title>
		<link>http://www.voyce.com/index.php/2013/01/21/an-ios-lava-lamp-using-opengl-es-shaders/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=an-ios-lava-lamp-using-opengl-es-shaders</link>
		<comments>http://www.voyce.com/index.php/2013/01/21/an-ios-lava-lamp-using-opengl-es-shaders/#comments</comments>
		<pubDate>Mon, 21 Jan 2013 23:56:55 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1599</guid>
		<description><![CDATA[Catchy title, eh? This little experiment came about as I&#8217;ve been working on an iOS app where I decided to use an embedded OpenGL view, via GLKit, for a bit more flexibility than a plain-old UIView. This found me falling head-first down a rabbit-hole of OpenGL ES shaders. I ended up putting together a little [...]]]></description>
				<content:encoded><![CDATA[<p><div id="attachment_1619" class="wp-caption alignright" style="width: 210px"><a href="http://www.voyce.com/wp-content/uploads/2013/01/lavalamp.png"><img src="http://www.voyce.com/wp-content/uploads/2013/01/lavalamp-200x300.png" alt="screenshot" title="Lava lamp screenshot" width="200" height="300" class="size-medium wp-image-1619" /></a><p class="wp-caption-text">Screenshot of the finished lava lamp effect</p></div>Catchy title, eh? This little experiment came about as I&#8217;ve been working on an iOS app where I decided to use an embedded OpenGL view, via GLKit, for a bit more flexibility than a plain-old UIView. This found me falling head-first down a rabbit-hole of OpenGL ES shaders. I ended up putting together a little demo that emulates a lava lamp using a nifty bit of GLSL code.<br />
<span id="more-1599"></span></p>
<h3>A bit of background &#8211; shaders</h3>
<p>OpenGL ES 2.0 introduced the idea of programmable shaders to replace the &#8216;fixed function pipeline&#8217; of previous versions. What does this mean? You can write little programs that are invoked to modify vertices (points in the model) or fragments (pixels on the screen). Other types of shaders have been introduced in other versions of Open GL for desktop platforms, but vertex and fragment shaders are your limit on iOS/Open GL ES 2.0. </p>
<p>The shader programs are written in a C-like language, and then compiled (at app run-time) into code that can then be run directly by the GPU. That means they get executed on the highly data-parallel graphics hardware, running the same code on multiple lightweight &#8216;threads&#8217; (not your run-of-the-mill OS threads).</p>
<p>It&#8217;s possible to write shader programs that get quite complex. You can define and call your own functions, use common functions &#8211; for helping with things like vector manipulation and linear interpolation &#8211; and make use of built-in OpenGL variables to read and write key data. Here&#8217;s an example of getting the length of a 2D vector, the straight-line distance between 2 points.<br />
<code><br />
float dist = length(vec2(balls[i].xy - p));<br />
</code><br />
That code also demonstrates the use of <a href="http://en.wikipedia.org/wiki/Swizzling_(computer_graphics)">swizzling</a>; a concise way of accessing and mapping elements from one matrix or vector to another. Specifically <code>balls[i]</code> is a 3D vector with x, y and z components and we&#8217;re creating a 2D vector from only x and y. </p>
<h3>Make a quad and paint it</h3>
<p>In the case of our app, we&#8217;re not doing much in the vertex shader, we&#8217;re just going to pass vertices through unchanged. All we need to do is draw a single, screen-aligned quad (consisting of 2 triangles), and then &#8220;paint&#8221; it using our fragment shader. <div id="attachment_1607" class="wp-caption alignleft" style="width: 244px"><a href="http://www.voyce.com/wp-content/uploads/2013/01/vertex-diagram.png"><img src="http://www.voyce.com/wp-content/uploads/2013/01/vertex-diagram-234x300.png" alt="Vertices" title="vertex-diagram" width="234" height="300" class="size-medium wp-image-1607" /></a><p class="wp-caption-text">Drawing a quad with a triangle strip</p></div></p>
<p>By using an orthographic projection we can remove the need to take any perspective adjustments into account and just use the screen width and height for the size of the quad. Using a combination of UIView and <a href="http://developer.apple.com/library/mac/#documentation/GLkit/Reference/GLKit_Collection/_index.html">GLKit</a>, we can easily create the project matrix we need:<br />
<code><br />
    float w = self.view.bounds.size.width;<br />
    float h = self.view.bounds.size.height;<br />
    _projectionMatrix = GLKMatrix4MakeOrtho(-(w/2), w/2,<br />
                                            -(h/2), h/2,<br />
                                            0.0, 1.0);<br />
</code><br />
I also set the <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html">contentScaleFactor on UIView</a> to 1.0 &#8211; it defaults to 2.0 on Retina devices &#8211; in order to reduce the actual number of pixels that get drawn.</p>
<h3>The meat: The metaballs</h3>
<p>So now that we&#8217;ve got our quad on the screen, what are we actually going to draw into it? There are lots of metaball tutorials and descriptions around, <a href="http://www.niksula.hut.fi/~hkankaan/Homepages/metaballs.html">this</a> is a very useful one. With a little inspiration from <a href="http://stackoverflow.com/a/3587981/1719">this stackoverflow answer</a>, I got it coded up in GLSL:<br />
<script src="https://gist.github.com/4543060.js"></script><br />
Here I&#8217;m also setting a graduated colour in the fragment shader by returning the level rather then simply a boolean &#8220;in or out&#8221; flag. This gives a pleasing &#8216;glow&#8217; to the blobs.</p>
<p><div id="attachment_1627" class="wp-caption alignright" style="width: 220px"><a href="http://www.voyce.com/wp-content/uploads/2013/01/metaballs_formula.png"><img src="http://www.voyce.com/wp-content/uploads/2013/01/metaballs_formula.png" alt="Metaballs formula" title="metaballs_formula" width="210" height="306" class="size-full wp-image-1627" /></a><p class="wp-caption-text">Calculating the pixel value with one metaball</p></div>The crux of the algorithm involves dividing the radius of the blob by its distance from the point being rendered, as we can see in this diagram. We do the same thing for each blob, and the aggregate gives us the resulting colour for each pixel.    </p>
<p>This isn&#8217;t a particularly efficient implementation. It gives me a solid 30 FPS on an iPhone 4S or iPad 2 with 5 balls, but increasing that number quickly starts to impact the performance. I&#8217;m sure there are some optimisations that could be made (e.g. marching cubes), but that wasn&#8217;t really what this experiment was about. </p>
<h3>Debugging</h3>
<p>XCode provides excellent support for debugging OpenGL apps; albeit only on an actual device, not the the simulator. While your app is running, look for the somewhat hidden button or select Product|Debug|Capture OpenGL ES Frame, it will grab the state of your app and let you explore all of your state at will, including the very powerful ability to edit and recompile shaders on the fly. Very nice. </p>
<p>You can also use View|Show Debug Navigator to see the frame rate as your app is running. There&#8217;s a good introduction to what you can do in the official Apple docs <a href="http://developer.apple.com/library/ios/#recipes/xcode_help-debugger/articles/debugging_opengl_es_frame.html">here</a>.</p>
<h3>The Result</h3>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/UruF83RA3Z0?rel=0" frameborder="0" allowfullscreen></iframe><br />
It looks pretty nice, I think. I added some code to manage the balls, giving them an x, y and z (radius) direction so that they move and grow, as well as a bit of adjustment when they reach the top and bottom to make things look a little more realistic. I also added a small adjustment to the colours across the width of the display, to provide an interesting visual effect (it was originally intended to give a sheen, as if the blobs where behind cylindrical glass, but this was how it ended up instead)!</p>
<p>So, I&#8217;m sure most of you are just going &#8216;yeah, yeah, great description, but SHOW ME THE CODE&#8217;, so <a href="https://github.com/voyce/LavaLamp" target="_blank">here it is</a>. Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2013/01/21/an-ios-lava-lamp-using-opengl-es-shaders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Examining PDB files with DBH</title>
		<link>http://www.voyce.com/index.php/2012/09/24/examining-pdb-files-with-dbh/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=examining-pdb-files-with-dbh</link>
		<comments>http://www.voyce.com/index.php/2012/09/24/examining-pdb-files-with-dbh/#comments</comments>
		<pubDate>Mon, 24 Sep 2012 22:37:09 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WinDbg]]></category>
		<category><![CDATA[dbghelp]]></category>
		<category><![CDATA[dbh]]></category>
		<category><![CDATA[pdb]]></category>
		<category><![CDATA[symbols]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1577</guid>
		<description><![CDATA[Wow, it&#8217;s been a ridiculously long time since I&#8217;ve blogged. I think it&#8217;s time I put something up just to break the curse, and this seemed like a good, and hopefully useful, place to start. Time to polish some of these dusty drafts into published gems. Ever been in that situation where you (or someone [...]]]></description>
				<content:encoded><![CDATA[<p>Wow, it&#8217;s been a ridiculously long time since I&#8217;ve blogged. I think it&#8217;s time I put something up just to break the curse, and this seemed like a good, and hopefully useful, place to start. Time to polish some of these dusty drafts into published gems.</p>
<p><a href="http://www.voyce.com/wp-content/uploads/2012/09/breakpoint2.png"><img src="http://www.voyce.com/wp-content/uploads/2012/09/breakpoint2.png" alt="" title="Invalid breakpoint" width="48" height="48" class="alignleft size-full wp-image-1582" /></a>Ever been in that situation where you (or someone else) finds that Visual Studio just won&#8217;t set a breakpoint in some source code that you&#8217;re <em>sure</em> should be being used? You&#8217;ll see the hollow breakpoint icon and something like &#8216;The breakpoint will not currently be hit. No symbols have been loaded for this document&#8217;.<br />
<span id="more-1577"></span><br />
The first, and most obvious, step is to make sure that a PDB file is actually being loaded at all. Look in the modules window and check that Symbol Status is Symbols Loaded and there&#8217;s a symbol file listed. Assuming there is, how can we dig a little deeper and find out what the PDB file actually contains? Remember: the PDB will only contain symbols for code that&#8217;s actually used in your binary, so that will exclude any dead code stripped out by the linker.</p>
<p>Of course, there are a couple of existing mechanisms to examine PDBs: you could write something yourself using the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms679309(v=vs.85).aspx">Debug Help Library</a> (aka dbghelp) directly &#8211; a little too labour-intensive for this case &#8211; or you could use the <code>x</code> (eXamine symbols) command in WinDbg (or in the immediate window of Visual Studio). E.g. this will dump all of the symbols (probably a lot of them) in module testapp:</p>
<pre>x testapp!*
</pre>
<p>Of course, in order to use a debugger to do this, you&#8217;d have to have a crash dump or live session with the DLL loaded.</p>
<p>Wouldn&#8217;t it be cool if there was some way of examining the PDBs offline? Well, it turns out there is, it goes by the name of <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff540437(v=vs.85).aspx">DBH</a>, it ships alongside WinDbg in the Debugging Tools For Windows package and it&#8217;s incredibly useful. It works in one of two modes: either an interactive REPL or batch mode with a single command passed as an argument.</p>
<p>Its target can be a running process, the PDB file itself, or a DLL file. In the latter case, and assuming you&#8217;ve got _NT_SYMBOL_PATH properly set-up, it will identify and download the symbols automatically based on the DLL identity. With the &#8220;noisy&#8221; option enabled, we can see what it does in a simple case where symbols are co-located: </p>
<pre>
C:>dbh -n testapp.exe
verbose mode on.
DBGHELP: No header for testapp.exe.  Searching for image on disk
DBGHELP: C:\Users\Administrator\dev\testapp\Debug\testapp.exe - OK
DBGHELP: testapp - private symbols &#038; lines
         C:\Users\Administrator\dev\testapp\Debug\testapp.pdb
</pre>
<p>To dig deeper, let&#8217;s look inside the file for some destructors, i.e. all symbols containing a tilde:</p>
<pre>
C:>dbh testapp.pdb enum *~*

 index            address     name
     1            1011400 :   testClass::~testClass
</pre>
<p>But for the particular scenario we&#8217;re discussing, we&#8217;d like to see which source files are indexed by the PDB, rather than which symbols. You can do that with the <code>src</code> option on the command line:</p>
<pre>
C:>dbh testapp.pdb src
...
c:\users\administrator\dev\testlib\testlibclass.h
c:\program files (x86)\microsoft visual studio 10.0\vc\include\vadefs.h
...
f:\dd\public\sdk\inc\winnls.h
...
</pre>
<p>Here I&#8217;ve shown some of the files that highlight the fact that the PDB file contains the source that we built, the VC headers that are compiled into it, <em>and</em> the headers that have been compiled into the version of the CRT that we&#8217;re using.</p>
<p>So, now, given a compiled DLL and it&#8217;s PDB file, we can quickly answer the question &#8220;why can&#8217;t I set a breakpoint in file X?&#8221; by checking that the source file is actually included in the PDB.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/09/24/examining-pdb-files-with-dbh/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Beginning F#: Records</title>
		<link>http://www.voyce.com/index.php/2012/03/01/beginning-fsharp-records/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=beginning-fsharp-records</link>
		<comments>http://www.voyce.com/index.php/2012/03/01/beginning-fsharp-records/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 17:53:08 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[guidattribute fsharp]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1410</guid>
		<description><![CDATA[An introductory look at record types, a useful F# language feature.]]></description>
				<content:encoded><![CDATA[<p>In the second of an unknown number of parts in my series of <a href="http://www.voyce.com/index.php/2010/11/29/fsharp-positive-discrimination/">Beginning F#</a> posts, I&#8217;ll be talking about record types. They&#8217;re a useful and powerful F# feature that you&#8217;ll probably find yourself using very widely. I&#8217;ll take a look at what they are, how they&#8217;re used and how they integrate with the rest of the language.<br />
<span id="more-1410"></span></p>
<h3>What are they?</h3>
<p>In technical terms, they&#8217;re an algebraic data type. I won&#8217;t attempt to precisely define that myself, because <a href="http://merrigrove.blogspot.com/2011/12/another-introduction-to-algebraic-data.html">this page</a> does a much better job than I ever could. Suffice it to say that a record type is a combination (product) of named values of other types. </p>
<p>For example we can take an int and a string and construct a record type like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> MyRecord <span style="color: #a52a2a;">=</span>
    <span style="color: #6c6;">&#123;</span>
    foo : <span style="color: #06c; font-weight: bold;">int</span>
    bar : <span style="color: #06c; font-weight: bold;">string</span>
    <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>Of course, you can then use these record types as fields in other records:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> AnotherRecord <span style="color: #a52a2a;">=</span>
    <span style="color: #6c6;">&#123;</span>
    r : MyRecord
    x : <span style="color: #06c; font-weight: bold;">string</span>
    <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>It might look like this is similar to using a C# <code>struct</code>, but there are some significant differences. For instance, record types can&#8217;t be generic like structs can, and they&#8217;re .NET reference types, not value types, although they don&#8217;t necessarily <i>act</i> like reference types, we&#8217;ll talk about that later.</p>
<h3>Benefits</h3>
<p>So why might you want to use records? </p>
<ul>
<li>They help to encourage strong typing. You can use tuples in some of the situations where you&#8217;d use records, but in my opinion the ability to name fields gives records the edge when it comes to code readability.</li>
<li>They encourage immutability. Although (some would say unfortunately) they don&#8217;t require it, see below.</li>
<li>They integrate well with other language features such as pattern matching</li>
<li>They&#8217;re concise</li>
</ul>
<h3>Defining records</h3>
<p>The syntax for defining new record types is pretty straightforward.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> MyRecord <span style="color: #a52a2a;">=</span>
    <span style="color: #6c6;">&#123;</span>
    foo : <span style="color: #06c; font-weight: bold;">int</span>
    bar : <span style="color: #06c; font-weight: bold;">string</span>
    <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>This is similar to F# <a href="http://msdn.microsoft.com/en-us/library/dd233205.aspx">class definition syntax</a>, but notice there are no implicit (<code>type MyRecord () =...</code>) or explicit constructors. Records have to be constructed in a specific way using record expressions that specify all field values:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> r <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> foo <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><span style="color: #a52a2a;">;</span> bar <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;a&quot;</span> <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>Note that, as you&#8217;d expect, the F# compiler will attempt to infer the type, you don&#8217;t have to declare the type of r. If there&#8217;s ambiguity in the current scope, with multiple types having the same fields you can specify the type explicitly on either side, or fully specify one or all of the fields.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">// Specify type of record</span>
<span style="color: #06c; font-weight: bold;">let</span> r2 <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> foo <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><span style="color: #a52a2a;">;</span> bar <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;a&quot;</span> <span style="color: #6c6;">&#125;</span> : MyRecord
<span style="color: #5d478b; font-style: italic;">// Specify type of function</span>
<span style="color: #06c; font-weight: bold;">let</span> r3 : MyRecord <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> foo <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><span style="color: #a52a2a;">;</span> bar <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;a&quot;</span> <span style="color: #6c6;">&#125;</span> 
<span style="color: #5d478b; font-style: italic;">// Specify field</span>
<span style="color: #06c; font-weight: bold;">let</span> r4 <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> MyRecord.<span style="color: #060;">foo</span> <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><span style="color: #a52a2a;">;</span> bar <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;a&quot;</span> <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>Another extremely useful feature of record types is that as well as fields, they can also include member functions, static functions, and even implement interfaces:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Vector <span style="color: #a52a2a;">=</span>
    <span style="color: #6c6;">&#123;</span>
    X : <span style="color: #06c; font-weight: bold;">float</span>
    Y : <span style="color: #06c; font-weight: bold;">float</span>
    Z : <span style="color: #06c; font-weight: bold;">float</span>
    <span style="color: #6c6;">&#125;</span>
    <span style="color: #5d478b; font-style: italic;">/// Member function has access to fields </span>
    <span style="color: #06c; font-weight: bold;">member</span> this.<span style="color: #060;">add</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> this.<span style="color: #060;">X</span> <span style="color: #a52a2a;">+</span> this.<span style="color: #060;">Y</span> <span style="color: #a52a2a;">+</span> this.<span style="color: #060;">Z</span>
    <span style="color: #5d478b; font-style: italic;">/// Static member, here is passed an instance as a parameter </span>
    <span style="color: #06c; font-weight: bold;">static</span> <span style="color: #06c; font-weight: bold;">member</span> foo2 v <span style="color: #a52a2a;">=</span> v.<span style="color: #060;">X</span> <span style="color: #a52a2a;">+</span> v.<span style="color: #060;">Y</span> <span style="color: #a52a2a;">+</span> v.<span style="color: #060;">Z</span>
    <span style="color: #5d478b; font-style: italic;">/// Implement an interface with members</span>
    <span style="color: #06c; font-weight: bold;">interface</span> IDisposable <span style="color: #06c; font-weight: bold;">with</span> 
        <span style="color: #06c; font-weight: bold;">member</span> this.<span style="color: #060;">Dispose</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>At this point they&#8217;re starting to look an awful lot like good old-fashioned OO classes. The big difference is really in how they&#8217;re constructed; it&#8217;s done in one-shot via record expressions, rather than with a multi-step instance create and modify. Because of this you can&#8217;t provide any code that runs at construction time, as you would with <code>do</code> statements in F# classes.</p>
<h3>Modifying records</h3>
<p>One of the surprising things &#8211; that I only discovered recently &#8211; is that you can actually modify record fields post-construction, provided they&#8217;re explicitly declared as mutable:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Person <span style="color: #a52a2a;">=</span>
    <span style="color: #6c6;">&#123;</span>
    Name : <span style="color: #06c; font-weight: bold;">string</span>
    <span style="color: #06c; font-weight: bold;">mutable</span> Age : <span style="color: #06c; font-weight: bold;">int</span>
    <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>I consider this pretty nasty, and I have to say that I&#8217;ve never actually used records in this way. It&#8217;s typical of the way in which F# uses functional programming idioms like immutability in a pragmatic way, encouraging good practice but not strictly requiring it.</p>
<p>Personally I prefer to pretend that records are immutable once they&#8217;re constructed, and that modifying them requires copying. Luckily there&#8217;s a concise way of creating copies with modified field values using <code>with</code>. Here&#8217;s a function that takes an instance of a record type and returns a copy with a modified <code>foo</code> field:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> newMyRecord r <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> r <span style="color: #06c; font-weight: bold;">with</span> foo <span style="color: #a52a2a;">=</span> r.<span style="color: #060;">foo</span> <span style="color: #a52a2a;">+</span> <span style="color: #c6c;">1</span> <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<h3>Pattern matching</h3>
<p>A nice aspect of records is their ease of use in pattern matching. For instance you can very simply match against an individual field in the record, by using the underscore &#8216;wildcard&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> hasZeroY <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">function</span>
    | <span style="color: #6c6;">&#123;</span> X<span style="color: #a52a2a;">=</span>_<span style="color: #a52a2a;">;</span> Y<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span>.<span style="color: #a52a2a;">;</span> Z<span style="color: #a52a2a;">=</span>_ <span style="color: #6c6;">&#125;</span> <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">true</span>
    | _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">false</span></pre></td></tr></table></div>

<p>Or you can even just specify an individual field; useful if you&#8217;ve got many fields:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"> <span style="color: #06c; font-weight: bold;">let</span> xIsZero <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">function</span> <span style="color: #6c6;">&#123;</span>X<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span>.<span style="color: #6c6;">&#125;</span> <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">true</span> | _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">false</span></pre></td></tr></table></div>

<h3>Structural equality</h3>
<p>By default, record types use structural equality. This means that although under the covers they&#8217;re reference types, that&#8217;s not how they&#8217;re compared; the actual field values are used instead. This is an important difference between using simple C# classes and F# records. </p>
<p>Because of the default reference equality semantics in C# (which essentially compares pointers), this code will output &#8220;False&#8221;, despite the obvious similarity of <code>r1</code> and <code>r2</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> CSharpRecord <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> a<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">var</span> r1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CSharpRecord<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> a<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">var</span> r2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CSharpRecord<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> a<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>r1 <span style="color: #008000;">==</span> r2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Whereas this returns &#8216;true&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> FSharpRecord <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> a : <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #6c6;">&#125;</span>
<span style="color: #06c; font-weight: bold;">let</span> r1 <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> a<span style="color: #a52a2a;">=</span><span style="color: #c6c;">1</span> <span style="color: #6c6;">&#125;</span>
<span style="color: #06c; font-weight: bold;">let</span> r2 <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> a<span style="color: #a52a2a;">=</span><span style="color: #c6c;">1</span> <span style="color: #6c6;">&#125;</span>
r1 <span style="color: #a52a2a;">=</span> r2</pre></td></tr></table></div>

<p>Apparently there&#8217;s a performance bug in the currently released version of F# whereby records with lots and lots of fields (circa 300), will take a long time to compare. Personally, I&#8217;d argue that if you&#8217;ve got records with 300 fields, you&#8217;ve got bigger problems to solve, but you can at least work-around it by disabling structural equality and comparison with the <code>NoComparison</code> and <code>NoEquality</code> attributes:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">&lt;</span>NoComparison<span style="color: #a52a2a;">;</span>NoEquality<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#93;</span>
<span style="color: #06c; font-weight: bold;">type</span> MyRecord <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> X : float<span style="color: #a52a2a;">;</span> Y : float<span style="color: #a52a2a;">;</span> Z : <span style="color: #06c; font-weight: bold;">float</span> <span style="color: #6c6;">&#125;</span></pre></td></tr></table></div>

<p>This removes the custom <code>Equals</code> and <code>CompareTo</code> implementation that would otherwise be generated by the compiler.</p>
<h3>How do they look?</h3>
<p>If you do a bit of ILSpying (Reflector is <em>so</em> last year), you&#8217;ll probably be surprised at the amount of boilerplate that&#8217;s been generated on your behalf by the compiler.</p>
<p>Most of it is the interfaces and overloads required for structural equality, but we also get 3 items for each field: the actual CLR field that provides the storage, a getter method and a property that calls the getter. It&#8217;s more code than you&#8217;d have for the vanilla C# class we used above, but it&#8217;s not particularly bad.</p>
<p>And to non-F# consumers, records look like perfectly normal types:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">var</span> fr1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FSharpRecord<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>fr1<span style="color: #008000;">.</span><span style="color: #0000FF;">a</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h3>Summary</h3>
<p>So there you go; a whistle-stop tour of records. If you want more info you can check out the <a href="http://msdn.microsoft.com/en-us/library/dd233184.aspx">MSDN page</a>, or, if you&#8217;re feeling hardcore, the <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html">language spec</a>, but in the meantime&#8230; have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/03/01/beginning-fsharp-records/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.NET DLLs Loaded Twice</title>
		<link>http://www.voyce.com/index.php/2012/02/28/net-dlls-loaded-twice/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=net-dlls-loaded-twice</link>
		<comments>http://www.voyce.com/index.php/2012/02/28/net-dlls-loaded-twice/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 22:23:20 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[address space]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[VMMap]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1204</guid>
		<description><![CDATA[There's a bug in 32-bit .NET 2.0 where assemblies are loaded twice, wasting valuable address space.]]></description>
				<content:encoded><![CDATA[<p>If, like me, you&#8217;re still squeezing yourself into 32-bit Windows processes, you&#8217;re probably, also like me, constantly keeping an eye on the virtual address space usage of your application. If you happen to have used something like vmmap to take a peek at your memory contents, maybe you&#8217;ve noticed something strange with some .NET assemblies: they&#8217;re loaded twice! What&#8217;s going on&#8230;?<br />
<span id="more-1204"></span><br />
Let&#8217;s see how this looks in practice. </p>
<p>I created a .NET 3.5 app that does nothing except load a library. The library (doublelib.dll) contains a class, so we can reference it from the app, and a large uncompressed 24-bit BMP to waste a bit of space. In real life you&#8217;ll find lots and lots of code will also serve to waste space.<div id="attachment_1517" class="wp-caption alignright" style="width: 310px"><a href="http://www.voyce.com/wp-content/uploads/2012/02/vmmap_double_highlight.png"><img src="http://www.voyce.com/wp-content/uploads/2012/02/vmmap_double_highlight-300x260.png" alt="vmmap screenshot" title="vmmap_double_highlight" width="300" height="260" class="size-medium wp-image-1517" /></a><p class="wp-caption-text">vmmap screenshot</p></div></p>
<p>If we ramp up <a href="http://live.sysinternals.com/vmmap.exe">vmmap</a> and select the Images type, we can clearly see the 1.6mb DLL loaded twice. Click on the image for the full size screen shot.</p>
<p>You&#8217;ll notice that this only affects DLLs that aren&#8217;t in the GAC, and only affects .NET 2.0-based apps.</p>
<p>So let&#8217;s cut to the chase: there&#8217;s a hotfix available, and you can grab it from here: <a href="http://support.microsoft.com/kb/981266">http://support.microsoft.com/kb/981266</a>. It was reported a long time ago on MS Connect, but it seemed as if it had ended up in the <code>wontfix</code> pile&#8230; Then suddenly, a few months ago, a fix appeared. The only caveat is that it&#8217;s an opt-in thing; I guess this, and the delay, is probably because 2.0 is a &#8220;legacy&#8221; &#8211; although still officially supported &#8211; version of the runtime. MS seem reluctant to mess with what&#8217;s broadly working.</p>
<p>So, if you&#8217;re squeezed for memory, give the hotfix a try and reclaim your address space&#8230;!</p>
<h3>Postscript</h3>
<p>There&#8217;s a terrible bit of advice/misinformation on that KB page about the hotfix. It suggests one of the fixes is to:</p>
<blockquote><p>Run the application on a 64-bit operating system. 64-bit operating systems have a large virtual address space.</p></blockquote>
<p>This might be true for a 64-bit process on a 64-bit OS (where it&#8217;ll get a whopping 16TB), but in the case of a 32-bit app (which is specifically what the bug affects), you&#8217;re only going to get a relatively small amount of additional address space by running on 64-bit Windows; in the realm of a few hundred extra MB out of a theoretical maximum (excluding use of special switches) of 2GB. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/02/28/net-dlls-loaded-twice/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>30 years ago at CES&#8230;</title>
		<link>http://www.voyce.com/index.php/2012/01/15/30-years-ago-at-ces/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=30-years-ago-at-ces</link>
		<comments>http://www.voyce.com/index.php/2012/01/15/30-years-ago-at-ces/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 13:10:46 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[retro]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1455</guid>
		<description><![CDATA[How different did things look at CES 30 years ago? Very.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.voyce.com/wp-content/uploads/2012/01/candvg_logo.png"><img src="http://www.voyce.com/wp-content/uploads/2012/01/candvg_logo.png" alt="candvg_logo" title="candvg_logo" width="200" height="167" class="alignleft size-full wp-image-1456" /></a>The other day while I was looking through some of my ancient copies of Computer and Video Games magazine (&#8220;the first fun computer magazine!&#8221;) I discovered some coverage of the 1982 Consumer Electronics Show. It&#8217;s such a contrast to today&#8217;s shiny, immaculately produced, PR-fest that I couldn&#8217;t help but scan it in for everyone to see.<br />
<span id="more-1455"></span><br />
To get things into context, this is 1982: The state of the art in home computing is ZX81s sold in kit form, Vic20s, PET 8K, Tandy TRS-80. But as the magazine says, home computing&#8217;s not where it&#8217;s at in the US:</p>
<blockquote><p>
&#8220;The 25th Consumer Electronics Show&#8230; showed very clearly that one thing continues to dominate the media and electronics business in the USA. This is not the personal computer &#8211; which seems to be causing far more interest in Europe than in America &#8211; but the video game.&#8221;
</p></blockquote>
<p>So while in the UK we were busy typing in listings from games magazines, in the US Atari, Mattel were creating ready-to-play cartridge based games.</p>
<p><a href="http://www.voyce.com/wp-content/uploads/2012/01/frogger_small.png"><img src="http://www.voyce.com/wp-content/uploads/2012/01/frogger_small.png" alt="frogger_small" title="frogger_small" width="195" height="250" class="alignright size-full wp-image-1459" /></a>Getting compelling press coverage was made difficult by the fact that it was impossible to get decent screen shots. Think how much of the pages in modern games mags are covered with glossy, full-colour, high-resolution screen shots. Without these, you&#8217;re left with nothing but words to describe these visual wonders. Luckily, you didn&#8217;t need much of a vocabulary to describe the games of the time; they were either based on existing analogue games, or they were derived from the small pool of original video game ideas:</p>
<blockquote><p>
&#8220;Munchie, which is similar to PacMan&#8221; &#8230; &#8220;Cosmic Raiders is a fast-action Defender style games&#8221; &#8230; &#8220;Solar Conqueror is an Asteroids game.&#8221;
</p></blockquote>
<p>Of course, some of this &#8220;re-purposing&#8221; was pukka; Coleco made table-top versions of things like PacMan, Donkey Kong, and Frogger (image above). You can imagine how faithful these arcade conversions actually were&#8230;</p>
<p>We&#8217;ve made quite some progress in the last 30 years, both in terms of game mechanics and graphics. It&#8217;s best summed up by 2 images, I&#8217;m sure you can guess which is from 1982, and which from 2012?<br />
<a href="http://www.voyce.com/wp-content/uploads/2012/01/frogbog.png"><img src="http://www.voyce.com/wp-content/uploads/2012/01/frogbog.png" alt="frogbog" title="frogbog" width="269" height="200" class="alignnone size-full wp-image-1463" /></a><a href="http://www.voyce.com/wp-content/uploads/2012/01/zeldahd.png"><img src="http://www.voyce.com/wp-content/uploads/2012/01/zeldahd.png" alt="zeldahd" title="zeldahd" width="356" height="200" class="alignnone size-full wp-image-1464" /></a></p>
<p>Click through these images for the full scans on flickr.</p>
<table>
<tr>
<td>
<a href="http://www.flickr.com/photos/ianvoyce/6700584931/" title="C&amp;VG March 1982 cover by ianvoyce, on Flickr"><img src="http://farm8.staticflickr.com/7003/6700584931_2d8a0a4c60_t.jpg" width="71" height="100" alt="C&amp;VG March 1982 cover"></a></td>
<td>
<a href="http://www.flickr.com/photos/ianvoyce/6700587409/" title="C&amp;VG CES 1982 coverage 1 by ianvoyce, on Flickr"><img src="http://farm8.staticflickr.com/7174/6700587409_65b6a1a129_t.jpg" width="71" height="100" alt="C&amp;VG CES 1982 coverage 1"></a></td>
<td>
<a href="http://www.flickr.com/photos/ianvoyce/6700589945/" title="C&amp;VG CES 1982 coverage 2 by ianvoyce, on Flickr"><img src="http://farm8.staticflickr.com/7011/6700589945_4bc78d3843_t.jpg" width="71" height="100" alt="C&amp;VG CES 1982 coverage 2"></a></td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/01/15/30-years-ago-at-ces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tax Avoidance 2.0</title>
		<link>http://www.voyce.com/index.php/2012/01/06/tax-avoidance-2-0/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tax-avoidance-2-0</link>
		<comments>http://www.voyce.com/index.php/2012/01/06/tax-avoidance-2-0/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 00:05:21 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[Finance]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1430</guid>
		<description><![CDATA[Tax avoidance is rife in the new (and existing) generation of tech companies.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.voyce.com/wp-content/uploads/2012/01/dislike.png"><img src="http://www.voyce.com/wp-content/uploads/2012/01/dislike.png" alt="dislike" title="dislike" width="210" height="95" class="alignleft size-full wp-image-1443" /></a>Warning: This is a bit off-topic for my blog, but I had to write it and get it off my chest. </p>
<p>In these times of financial woe, people love to complain about all the big, nasty, faceless companies avoiding paying tax. They inevitably moan about it on Facebook, Google and Twitter (and sometimes even do something about it in the real, physical, world). But what about those companies themselves? The Web 2.0 behemoths are now some of the largest companies in the world, and they&#8217;ve decided &#8211; just like the evil old companies &#8211; that they don&#8217;t like paying all that tax. Don&#8217;t be evil&#8230; and don&#8217;t pay any tax if you can help it. But how do they do it?<br />
<span id="more-1430"></span></p>
<h2>The Numbers</h2>
<p>An Irish Times <a href="http://www.irishtimes.com/newspaper/finance/2011/1223/1224309416349.html">newspaper article</a> caught my eye the other day. It discusses the rapid growth of Facebook&#8217;s Dublin operation in 2010, whose accounts have just been published (as they must be by law, despite FB&#8217;s apparent reluctance). The headline figure is that:</p>
<blockquote><p>
[Revenue] is up from €15.2 million to <b>€229.1 million</b>
</p></blockquote>
<p>That&#8217;s a pretty significant increase! But that&#8217;s only revenue, how much of it was left over as profit?</p>
<blockquote><p>
Facebook Ireland generated a pretax profit of <b>€1.9 million</b>
</p></blockquote>
<p>What?! Where did the other €227 million go? </p>
<p>According to the article it mostly disappeared into a mysterious black hole known as &#8220;administrative expenses&#8221;:</p>
<blockquote><p>
Administrative expenses increased &#8230; to €221.6 million as a result of &#8230; royalties paid to other Facebook entities for services provided during the year.
</p></blockquote>
<h2>How do they do that?</h2>
<p>Is it me or does that sound suspicious? Well, it should do, because it is. </p>
<p>It turns out that Ireland&#8217;s significantly lower rate of corporation tax (12% vs 26% in the UK, and one of the lowest in the world) isn&#8217;t the only reason why companies like Facebook, <a href="http://www.guardian.co.uk/business/ireland-business-blog-with-lisa-ocarroll/2011/mar/24/google-ireland-tax-reasons-bermuda">Google et al move there</a>. There&#8217;s also the very useful fact that, unlike most developed economies, Ireland chose not to introduce any legislation to prevent companies siphoning off their revenue to low-or-no-tax regimes like the Cayman islands or Bermuda.</p>
<p>So that&#8217;s where the &#8220;missing&#8221; €221 million has ended up: it&#8217;s been paid from the Irish part of the business to the tax-haven subsidiary, leaving only a small amount subject to corporation tax in Ireland. This practise is known in tax circles as the &#8220;double Irish&#8221;, and it (along with its Dutch equivalent) seems to be fairly widely used and well understood, even having its own <a href="http://en.wikipedia.org/wiki/Double_Irish_arrangement">wikipedia entry</a>. Of course Facebook now funnels as much as possible of the revenue from the UK (its second biggest market) through the Ireland operation to get the most out of this arrangement. That explains the large increase in the numbers since last year. </p>
<p>There are many high-profile tech companies that have Dublin operations and may be avoiding tax in this way, including:</p>
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>LinkedIn</li>
<li>Google</li>
<li>Microsoft</li>
<li>Oracle</li>
</ul>
<p>That&#8217;s quite a roll call.</p>
<h2>The Result</h2>
<p>The result of all these shenanigans for Facebook? It pays a ridiculously small effective tax rate of less than 1%, even better than <a href="http://www.businessweek.com/technology/google-tax-cut/google-terminal.html">Google&#8217;s 2.4% rate</a>.  And given Ireland&#8217;s <a href="http://news.businessweek.com/article.asp?documentKey=1376-LXBO661A1I4H01-4A40DCQ22ID111VEL291CSBQT6">current economic state</a> I can&#8217;t imagine their government will be introducing more restrictive tax legislation any time soon in an attempt to crack down on it. They can&#8217;t risk these big companies abandoning their new homes in Dublin&#8217;s <a href="http://www.irishtimes.com/newspaper/finance/2011/0930/1224305000761.html">Silicon Dock</a>.</p>
<p>Of course, you should feel free to &#8216;like&#8217; this blog post on Facebook, after all, what will it cost you?</p>
<p>(* Disclaimer: These are solely my opinions, not those of my employer or anyone else).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/01/06/tax-avoidance-2-0/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Looking back at 2011</title>
		<link>http://www.voyce.com/index.php/2012/01/04/looking-back-at-2011/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=looking-back-at-2011</link>
		<comments>http://www.voyce.com/index.php/2012/01/04/looking-back-at-2011/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 23:07:47 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[2011]]></category>
		<category><![CDATA[kinect]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WinDbg]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1408</guid>
		<description><![CDATA[A quick look back at some of the stuff I've been doing this year.]]></description>
				<content:encoded><![CDATA[<p>Well, we&#8217;re a few days into 2012 and no armageddon yet, so it&#8217;s probably safe to take a quick glance back over our shoulder at some of the technical stuff that&#8217;s flashed past in the preceding 12 months.<br />
<span id="more-1408"></span></p>
<h2>Work: Using F# in anger</h2>
<p>During working hours (and sometimes beyond) I&#8217;ve been developing a large project using lots of F#, a little bit of C# and a bit too much XAML. It&#8217;s quite an interesting mix, with technologies that have a vastly different outlook on life: F# with it&#8217;s strong, compile-time type checking and XAML with its, err, not-so-strong type checking. In fact, XAML and WPF play so fast and loose with types, that errors in the binding mechanism only happen at run-time and are almost always eaten silently. As you can imagine, this makes for some fun debugging. All I can say is thank goodness for <a href="http://snoopwpf.codeplex.com/">Snoop</a>.</p>
<p>We have a quite a substantial amount of ViewModel code in F# (tens of thousands of lines), much of it using <code>Async</code> to allow us to create responsive UIs without requiring tons of boilerplate code. Generally it works nicely, but I do have some concerns about the amount of thread hopping going on, and how that will affect performance. Especially as we break things down into pretty small pieces of functionality, sometimes just a few simple operations, and each is run on the thread pool. I don&#8217;t have any concrete figures to go on here, and I&#8217;m hoping we don&#8217;t end up in a situation where we sacrifice user productivity for developer productivity. Time (and perfmon) will tell.</p>
<h3>Debugging</h3>
<p>I&#8217;ve done less hardcore WinDbg stuff this year. In fact, a couple of times I&#8217;ve been guilty of jumping in with the debugger where it probably would&#8217;ve been more effective to take a high-level look first. A case of having a debugger-shaped hammer, and everything looking like a nail.</p>
<h3>Moving to 64bit</h3>
<p>A very positive thing that happend this year was the move to 64-bit Windows 7 for development at work, meaning that Visual Studio actually stays up without crashing for more than an hour. Previously I&#8217;d been having a terrible time: the combination of a couple of large-ish F# projects and 32-bit XP caused frequent VS out-of-memory errors, which was very frustrating. Luckily the move to Windows 7 has resolved it, and I can even run more than a handful of apps at the same time; what a luxury. </p>
<h2>Home: iOS</h2>
<p>After a long day at work, I finally get to go home, put the kids to bed and then my feet up, and break-out Xcode for a bit of iOS hacking. This year I&#8217;ve been feeling like I&#8217;m finally getting my head around some of the things that are fundamental in iOS development; from Objective-C concepts like release/retain semantics to iOS specifics, view controllers, table views etc. Previously I&#8217;ve been guilty of some copy-and-paste coding, mostly because it&#8217;s pretty slow going getting up to speed with new tech (and the myriad iOS APIs) when you&#8217;re only getting to do it for a few hours while knackered after work. Well, that&#8217;s my excuse anyway.</p>
<p>I&#8217;ve been trying to finish off some of the apps that I&#8217;ve started, but not shipped. I tend to procrastinate a bit (massive understatement), and of course things aren&#8217;t helped by the development &#8216;fat tail&#8217;, where the last 20% &#8211; polishing and getting things &#8216;just right&#8217; &#8211; takes at least 80% of the time. My 2010 post on <a href="http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/">Core Animation flip clocks</a> continues to get a lot of traffic, and I finally bit the bullet and posted a (free!) <a href="http://itunes.com/apps/flipclock">app</a> that uses it and it&#8217;s getting a lot of downloads, which is nice. I also had a look at iAd, which I&#8217;ll reserve judgement on for the time being, at the minute it&#8217;s not looking particularly lucrative&#8230;</p>
<p>As well as iOS stuff, I also took a look at OpenGL. I tend to do this every now and again, each time dipping in a little further and understanding a bit more. This time round it was looking at shaders (in OpenGL ES 2.0) and integrating with <a href="http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/">Bullet physics</a>. My learning was certainly helped by this excellent <a href="http://arcsynthesis.org/gltut/">Learning Modern 3D Graphics Programming</a> tutorial.</p>
<h3>Kinect and F#</h3>
<p>Also while at home I finally got around to setting-up by my Kinect to do a bit of F# hacking. In the end I didn&#8217;t produce <a href="http://www.voyce.com/index.php/2011/09/05/kinect-sdk-with-f/">very much</a>, but it was pretty good fun anyway.</p>
<h2>Bring on 2012</h2>
<p>So, to sum it up: more of the same really, I guess. Although this year there&#8217;s definitely been a bit more of a focus on UI development, in both WPF and iOS, which I&#8217;ve been enjoying. It does tend to feel a bit schizophrenic, switching between technologies and platforms at the end of the working day. It can be interesting to identify the common themes and significant differences in the two approaches to GUI development. </p>
<p>This year I&#8217;m hoping to make my blogging a bit more regular, but who knows if I&#8217;ll be able to stick to it&#8230; If there&#8217;s anything you&#8217;d particularly like me to focus on, feel free to leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2012/01/04/looking-back-at-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing animated shapes and text in Core Animation layers</title>
		<link>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=drawing-animated-shapes-and-text-in-core-animation-layers</link>
		<comments>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 12:30:01 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[CALayer]]></category>
		<category><![CDATA[Core Animation]]></category>
		<category><![CDATA[Core Graphics]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1370</guid>
		<description><![CDATA[Using paths and text to simple create composite animated graphics with Core Graphics, UIKit and Core Animation.]]></description>
				<content:encoded><![CDATA[<p><div id="attachment_1399" class="wp-caption alignleft" style="width: 148px"><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_hello.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_hello.png" alt="Star and text" title="star_hello" width="138" height="149" class="size-full wp-image-1399" /></a><p class="wp-caption-text">Star and text</p></div>The other day I was overcome by the desire to create an animated start-burst, price-tag type graphic with iOS. Time to break out some Core Graphics and Core Animation code. On the way to getting it going, I came across some interesting gotchas, which I thought it&#8217;d be useful to talk about here.<br />
<span id="more-1370"></span></p>
<h3>Drawing the basic shape</h3>
<p>The first step is to draw the &#8216;star&#8217; shape. I decided to create a re-usable path, although at the minute I&#8217;m actually only drawing it once, so the benefits are limited. Doing this involves using the <code>CGPath...</code> versions of the Core Graphics functions, that add themselves to a mutable path objects, rather than being drawn directly on the currently active graphics context.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    CGMutablePathRef r <span style="color: #002200;">=</span> CGPathCreateMutable<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">// Always have to move to a start point</span>
    CGPathMoveToPoint<span style="color: #002200;">&#40;</span>r, <span style="color: #a61390;">NULL</span>, x1, y1<span style="color: #002200;">&#41;</span>; 
    CGPathAddLineToPoint<span style="color: #002200;">&#40;</span>r, <span style="color: #a61390;">NULL</span>, x2, y2<span style="color: #002200;">&#41;</span>;
    CGPathCloseSubpath<span style="color: #002200;">&#40;</span>r<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Once you&#8217;ve got created the path you can merrily set various stroke and fill colours and draw it over-and-over to your hearts content.</p>
<p><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_algo.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_algo.png" alt="star_algo" title="star_algo" width="145" height="160" class="alignleft size-full wp-image-1383" /></a>The star drawing is parameterised in a few ways: we can set the inner radius (r2), outer radius (r1) and the number of points, which must be divisible by 3. We draw the points in groups of three, the first on the outer radius, then the inner, then the last on the outer. In that way the sections tesselate into a complete circle.</p>
<h3>Using it with CALayer</h3>
<p>So now we&#8217;ve got some code to draw the shape we want, we need a way to get it onto the screen, using <code>CALayer</code>s. There are 3 main ways of providing content for layers:</p>
<ul>
<li>Use a delegate that implements <code>drawInContext</code> (and don&#8217;t forget to call <code>setNeedsDisplay</code> at least once to cause it be drawn!)</li>
<li>Set the content to a <code>CGImageRef</code>. Meh&#8230; that&#8217;s going to mean it&#8217;s a bitmap, with all the associated aliasing/scaling issues.</li>
<li>Subclassing. Nah.</li>
</ul>
<p>We&#8217;ll use the first approach; we can create a simple <code>NSObject</code>-derived class that can manage the layer hierarchy (more of that later) and implement the required selector on it. In that function we can switch on the layer we&#8217;re being asked to draw, and do the appropriate handling:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CALayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theLayer
        inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGContextRef<span style="color: #002200;">&#41;</span>context 
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>theLayer <span style="color: #002200;">==</span> _textLayer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>theLayer <span style="color: #002200;">==</span> _starLayer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h3>Animating</h3>
<p>We&#8217;re only going to be doing fairly basic rotation animation here, so we can use <code>CABasicAnimation</code>. We use the key-value coding support to specify the <code>transform.rotation</code> property as the target. This is an alias for rotation around the Z axis, which is pointing &#8220;out&#8221; of the screen. We rotate from 0 to 2*pi radians, repeating essentially indefinitely by specifying a large repeatCount.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    CABasicAnimation <span style="color: #002200;">*</span>animation <span style="color: #002200;">=</span> 
        <span style="color: #002200;">&#91;</span>CABasicAnimation animationWithKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;transform.rotation&quot;</span><span style="color: #002200;">&#93;</span>;
    animation.duration<span style="color: #002200;">=</span><span style="color: #2400d9;">8.0</span>;
    animation.repeatCount<span style="color: #002200;">=</span>HUGE_VALF;
    animation.autoreverses<span style="color: #002200;">=</span><span style="color: #a61390;">NO</span>;
    animation.fromValue<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#93;</span>;
    animation.toValue<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>TWOPI<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_starLayer addAnimation<span style="color: #002200;">:</span>animation forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotation&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<h3>Drawing the text</h3>
<p>This was an interesting one. I originally started looking at a <code>CATextLayer</code>-based solution, but was surprised to find that it&#8217;s quite difficult to get vertical alignment within a rectangle. Instead, I decided to use the <code>NSString</code> UIKit additions that provide enough drawing and &#8211; importantly &#8211; measuring functions for us to work out exactly where we need to place the text.    </p>
<p>One important thing to note here is that we&#8217;re potentially mixing Core Graphics and UIKit functions here. They have different expectations about how to get hold of the required graphics context; with Core Graphics it&#8217;s always passed explicitly, whereas UIKit will grab the current context. This means that if you try and call <code>drawInRect</code> within your <code>drawLayer</code> function, you&#8217;ll see errors like &#8220;Invalid context: 0&#215;0&#8243; on the console, and no output.</p>
<p>The solution is simple when you know how: tell UIKit about your explicit context, like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CALayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theLayer
        inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGContextRef<span style="color: #002200;">&#41;</span>context 
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #11740a; font-style: italic;">// Let UIKit know about this context</span>
    UIGraphicsPushContext<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">// Because this function uses it internally...</span>
    <span style="color: #002200;">&#91;</span>myString drawInRect<span style="color: #002200;">:</span>r 
                     withFont<span style="color: #002200;">:</span>font
            lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeClip 
                    alignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;
    UIGraphicsPopContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>By measuring the text before we draw, we can align it centrally vertically and fill the space horizontally, letting iOS worry about the horizontal alignment. <code>sizeWithFont</code> takes into account a bounding rectangle and our desired breaking/clipping options:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">        CGSize sz <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>s sizeWithFont<span style="color: #002200;">:</span>font 
                 constrainedToSize<span style="color: #002200;">:</span>theLayer.bounds.size 
                     lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeClip<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<h3>Setting up a layer hierarchy</h3>
<p>Given that I wanted some parts of the thing to rotate, and others to be static, I needed to create multiple layers and put them together. This is very easily achieved by adding layers to the <code>subLayers</code> collection of our root layer, then we return the root layer and add that to the view.</p>
<p><div id="attachment_1391" class="wp-caption alignright" style="width: 170px"><a href="http://www.voyce.com/wp-content/uploads/2011/12/star_layers1.png"><img src="http://www.voyce.com/wp-content/uploads/2011/12/star_layers1.png" alt="Layer arrangement" title="star_layers" width="160" height="194" class="size-full wp-image-1391" /></a><p class="wp-caption-text">Layer arrangement</p></div>The layer set-up looks like this, with the root being empty, and having first the star layer, then the text layer added to the sublayers. This is just because <code>addSublayer</code> appends the sublayer, instead we could use the <code>insertSublayer</code> overloads to be explicit about the ordering we desire.<br />
<br clear="all"/><br />
The set-up function returns the root layer, and then we add that to our view:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    _star <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StarLayer alloc<span style="color: #002200;">&#93;</span> initWithRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self.view.layer<span style="color: #002200;">&#93;</span> addSublayer<span style="color: #002200;">:</span>_star.root<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<h3>Next Steps</h3>
<p>Here&#8217;s a red star and random text sitting a bit incongruously in a prototype of a spelling app I&#8217;m writing. </p>
<p><iframe width="200" height="165" src="http://www.youtube.com/embed/kAgYThdoy_c?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>It would be good to create a whole load of stars (probably not with text in) and shoot them across the screen in a star-burst by generating a random direction/speed vector and animating their speeds, opacity and scale to make them fade out.</p>
<p>You can check out the code <a href="https://github.com/voyce/StarLayer">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2011/12/03/drawing-animated-shapes-and-text-in-core-animation-layers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating a physics-based OpenGL iOS app</title>
		<link>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-a-physics-based-opengl-ios-app</link>
		<comments>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 22:40:50 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Bullet]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=1013</guid>
		<description><![CDATA[If you know a bit of OpenGL you can integrate the powerful Bullet physics API into your iPad or iPhone app.]]></description>
				<content:encoded><![CDATA[<p><img src="http://www.voyce.com/wp-content/uploads/2010/09/puppet_grab.png" alt="puppet_grab" title="puppet_grab" width="128" height="128" class="alignleft size-full wp-image-1030" />With the success of iOS games like Angry Birds and its flocks of imitators, there are lots of people looking at creating physics-based games, so I decided to try and create a simple demo using OpenGL ES and the Bullet physics engine.<br />
<span id="more-1013"></span></p>
<h2>OpenGL</h2>
<p>The 3D work I&#8217;d done previously on iOS was using OpenGL ES 1.1, so as part of doing this, I also needed to get up to speed with the changes in version 2.0. The move involves replacing the fixed function pipeline approach with the use of shaders crafted in &#8220;shader language&#8221;, specifically <a href="http://www.opengl.org/documentation/glsl/">GLSL</a>. This approach is more flexible, but it means you take full responsibility for combining all the relevant matrices (model, projection etc), rather than letting OpenGL do it for you.</p>
<p>There are 2 distinct types of shader programs that operate on either vertices (vertex shaders) or pixels (fragment shaders). The steps required to create shader programs mirror the process of creating &#8220;normal&#8221; programs: they&#8217;re loaded, compiled and linked. Once you&#8217;ve got a program loaded, you can initialise it by setting variables of various kinds: attribute, uniform or varying. These are used for vertex and colour data, constants and passing data between vertex and fragment shaders respectively. If you&#8217;re interested in learning more, I suggest you check out some of the plethora of internet and dead-tree material on OpenGL. I&#8217;ll only be doing pretty basic stuff here.   </p>
<h2>Bullet</h2>
<p>I decided to try using <a href="http://bulletphysics.org/wordpress/">Bullet</a> as the physics engine. It looks very powerful and is widely used in various games and <a href="http://bulletphysics.org/wordpress/?p=241">movies</a>; the only bad thing I can say about it is that decent documentation is a bit lacking. I found the best way to get help was googling for API names on the Bullet forum; not a very efficient way of doing things. Maybe I&#8217;ve just been spoilt by the copious Apple and MS docs (yeah, yeah). </p>
<h3>Building it</h3>
<p>Luckily Bullet comes with an Xcode project &#8220;in the box&#8221;, so with a bit of <code>autoconf</code> magic, it was soon up and building the demo apps. Although there are loads of files in there, the core library itself isn&#8217;t too large or complex. As such, I decided to create a separate static library project targeting the iOS simulator (i386) and devices (armv6/7).</p>
<h3>API basics</h3>
<p>My intention is to use only a small subset of the Bullet API, encompassing rigid bodies and a couple of simple constraint types. For the purposes of the demo, I&#8217;m going to create the pretty standard set-up of a large static ground-box, along with a collection of more dynamic objects.  </p>
<p>The key part of joining up Bullet with your existing API is the <a href="http://www.bulletphysics.com/Bullet/BulletFull/classbtMotionState.html">btMotionState</a> class. It enables the API to callback into your code with updated positions at each &#8216;tick&#8217; of the physics engine, and you can then alter your geometry appropriately. I created a simple class that derives from <code>btMotionState</code>, implements the required pure virtual methods and contains a reference to my world object. Bullet calls the subclassed methods and we pass the transformations on to the object.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> QuadMotionState <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> btMotionState <span style="color: #008000;">&#123;</span>
    Quad <span style="color: #000040;">*</span>_object<span style="color: #008080;">;</span>
    btTransform _transform<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    QuadMotionState<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btTransform <span style="color: #000040;">&amp;</span>initialpos, Quad <span style="color: #000040;">*</span>node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        _object <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
        _transform <span style="color: #000080;">=</span> initialpos<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> ~QuadMotionState<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> setNode<span style="color: #008000;">&#40;</span>Quad <span style="color: #000040;">*</span>node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        _object <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> getWorldTransform<span style="color: #008000;">&#40;</span>btTransform <span style="color: #000040;">&amp;</span>worldTrans<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span>
        worldTrans <span style="color: #000080;">=</span> _transform<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> setWorldTransform<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btTransform <span style="color: #000040;">&amp;</span>worldTrans<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// Pass the worldTrans to _object</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3>Debugging</h3>
<p>Now, obviously <em>you</em> will write the perfect code first time, and will have no need for any kind of diagnostics. But I am not that perfect; I need to see the difference between what I&#8217;ve told the API to do, and what it&#8217;s actually doing. You can do this by creating an object implementing <code>btIDebugDraw</code> and passing it to the (slightly oddly named) setDebugDrawer function on your world:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">btIDebugDraw <span style="color: #000040;">*</span>debug <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> DebugDrawHelper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
m_dynamicsWorld<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setDebugDrawer<span style="color: #008000;">&#40;</span>debug<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>The most important function on <code>btIDebugDraw</code> is <code>drawLine</code>. It will be called repeatedly to draw axes and bounding boxes in a variety of colours to indicate the object&#8217;s state. We can use the <code>Shader</code> we created earlier to give us an easy way of generating these debug visuals. All we need is a very simple shader, so can declare it inline:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">program.<span style="color: #007788;">loadShaders</span><span style="color: #008000;">&#40;</span>
		<span style="color: #FF0000;">&quot;attribute vec4 position;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;uniform mat4 projection;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;void main () {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;gl_Position = position * projection;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, 
&nbsp;
		<span style="color: #FF0000;">&quot;uniform highp vec3 color;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;void main () {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;gl_FragColor = vec4(color.r, color.g, color.b, 0.0);<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Of course, anything more complex than this and you&#8217;ll want to put it into a separate file, but this program allows us to pass in the start and end vertex and its colour to draw the line in our implementation of <code>DrawLine</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> drawLine<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> from,<span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> to,<span style="color: #0000ff;">const</span> btVector3<span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        glUseProgram<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the projection matrix</span>
        GLint pu <span style="color: #000080;">=</span> glGetUniformLocation<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;projection&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glUniformMatrix4fv<span style="color: #008000;">&#40;</span>pu, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">0</span>, _projection.<span style="color: #007788;">Pointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the colour of the line</span>
        GLint puc <span style="color: #000080;">=</span> glGetUniformLocation<span style="color: #008000;">&#40;</span>program.<span style="color: #007788;">getProgram</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;color&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        vec3 v <span style="color: #000080;">=</span> vec3<span style="color: #008000;">&#40;</span>color.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, color.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, color.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glUniform3fv<span style="color: #008000;">&#40;</span>puc, <span style="color: #0000dd;">1</span>, v.<span style="color: #007788;">Pointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Set the line vertices</span>
        <span style="color: #0000ff;">float</span> tmp<span style="color: #008000;">&#91;</span> <span style="color: #0000dd;">6</span> <span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> from.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, from.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, from.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
            to.<span style="color: #007788;">getX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, to.<span style="color: #007788;">getY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, to.<span style="color: #007788;">getZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
        glVertexAttribPointer<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">3</span>, GL_FLOAT, <span style="color: #0000ff;">false</span>, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>tmp<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glEnableVertexAttribArray<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        glDrawArrays<span style="color: #008000;">&#40;</span> GL_LINES, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><div id="attachment_1327" class="wp-caption alignright" style="width: 236px"><a href="http://www.voyce.com/wp-content/uploads/2010/09/physics_debug_screenshot.png"><img src="http://www.voyce.com/wp-content/uploads/2010/09/physics_debug_screenshot-226x300.png" alt="Bullet debug output" title="physics_debug_screenshot" width="226" height="300" class="size-medium wp-image-1327" /></a><p class="wp-caption-text">Bullet debug output</p></div> You&#8217;ll end up with a display showing your quads, along with their orientation and state (whether they&#8217;re &#8220;active&#8221; in the simulation). It looks something like this:<br />
<br clear="all"/></p>
<h3>Populating the world</h3>
<p>So in order to have something nice to look at, we&#8217;re gonna need to put some &#8216;things&#8217; into our world. And in order to make it act like the real world we&#8217;ll need to give those things some physical properties.</p>
<p>I created a simple class to render a textured box. This OpenGL object then needs to be represented in the physics worlds by a rigid body object, which in turn contains a collision detection object that closely matches the dimensions of the original object. In fact, in the case of a box, the collision detection object matches exactly, otherwise you&#8217;ll need to create an approximation using the more advanced classes. We use a <code>btBoxShape</code> to initialise our <code>btRigidBody</code> instance, along with setting its mass, inertia, and making a link to the state object that I mentioned above.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">btRigidBody <span style="color: #000040;">*</span>addShape<span style="color: #008000;">&#40;</span>Quad <span style="color: #000040;">*</span>shape, btScalar mass <span style="color: #000080;">=</span> <span style="color:#800080;">1.0f</span>, <span style="color: #0000ff;">float</span> restitution <span style="color: #000080;">=</span> <span style="color:#800080;">0.2f</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Set the initial transform based on the current location of the object</span>
        btTransform transform<span style="color: #008080;">;</span>
	transform.<span style="color: #007788;">setIdentity</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	transform.<span style="color: #007788;">setOrigin</span><span style="color: #008000;">&#40;</span>btVector3<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	btCollisionShape<span style="color: #000040;">*</span> cshape <span style="color: #000080;">=</span> 
            <span style="color: #0000dd;">new</span> btBoxShape<span style="color: #008000;">&#40;</span>btVector3<span style="color: #008000;">&#40;</span>btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getWidth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>,
                                     btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getHeight<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>,
                                     btScalar<span style="color: #008000;">&#40;</span>shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getDepth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        btVector3 localInertia<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>,<span style="color: #0000dd;">0</span>,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mass <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span>
            cshape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>calculateLocalInertia<span style="color: #008000;">&#40;</span>mass,localInertia<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        QuadMotionState <span style="color: #000040;">*</span>state <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QuadMotionState<span style="color: #008000;">&#40;</span>transform, shape<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	btRigidBody<span style="color: #008080;">::</span><span style="color: #007788;">btRigidBodyConstructionInfo</span> rbInfo<span style="color: #008000;">&#40;</span>mass, state, cshape, localInertia<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	btRigidBody<span style="color: #000040;">*</span> body <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> btRigidBody<span style="color: #008000;">&#40;</span>rbInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	body<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setDamping<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.85</span>, <span style="color:#800080;">0.85</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	body<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setRestitution<span style="color: #008000;">&#40;</span>restitution<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Add the rigid body to the Bullet physics world</span>
	m_dynamicsWorld<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addRigidBody<span style="color: #008000;">&#40;</span>body<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Establish the link between our object and the physics object</span>
	shape<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setPhysicsObject<span style="color: #008000;">&#40;</span>body<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> body<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>To make things look a bit more polished I added some textures to the quads, along with very simple normal mapping to get some lighting effects. I also added the ability to rotate the view and zoom in and out by using <code>UIGestureRecognizer</code>s and changing the camera transform appropriately. The &#8220;finishing&#8221; touch (yeah, right), is the ability to tap to add a new quad.</p>
<h3>The results</h3>
<p>Here&#8217;s how the final thing looks. It&#8217;s very basic, I&#8217;ve hardly touched the surface of the Bullet API &#8211; there are no constraints in play, for instance &#8211; but it&#8217;s still kind of fun to play with. It&#8217;s running fairly slowly here, what with it being in the simulator, and being recorded. On a physical iPad it&#8217;s much snappier.<object width="420" height="315"><param name="movie" value="http://www.youtube.com/v/vbQ_EC_YlFY?version=3&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/vbQ_EC_YlFY?version=3&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
Let me know if you&#8217;d be interested to see some more coverage of some of Bullet&#8217;s constraints and hinges features. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2011/11/18/creating-a-physics-based-opengl-ios-app/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.436 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-22 12:17:16 -->
