<?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>GoGo-Robot &#187; Developer Blog</title>
	<atom:link href="http://www.gogo-robot.com/categories/devblog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gogo-robot.com</link>
	<description>Independent Games Developer</description>
	<lastBuildDate>Thu, 22 Jul 2010 13:14:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Texture Atlas (Sprite Sheet) Generator</title>
		<link>http://www.gogo-robot.com/2010/03/20/texture-atlas-sprite-sheet-generator/</link>
		<comments>http://www.gogo-robot.com/2010/03/20/texture-atlas-sprite-sheet-generator/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 16:47:50 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[sprite sheet]]></category>
		<category><![CDATA[texture atlas]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=206</guid>
		<description><![CDATA[A texture atlas, or sprite sheet, is a single image containing a number of smaller textures or sprites. This is useful as it is more efficient for the graphics card to process (it doesn&#8217;t have to keep switching textures when drawing different textures, as it can just use the single texture and sample from different [...]]]></description>
			<content:encoded><![CDATA[<p>A texture atlas, or sprite sheet, is a single image containing a number of smaller textures or sprites. This is useful as it is more efficient for the graphics card to process (it doesn&#8217;t have to keep switching textures when drawing different textures, as it can just use the single texture and sample from different parts of it). It is also faster to load into RAM, as you are only loading in one image, as opposed to several smaller ones, so the loading can be done in one stage.</p>
<p><span id="more-206"></span></p>
<p>It is useful to be able to quickly and easily generate texture atlases from a number of source sprites to speed up development. A useful tool when developing using the XNA framework is the <a href="http://creators.xna.com/en-GB/sample/spritesheet">SpriteSheet Sample</a> available on the XNA Creators Club website. One downside of this, however, is that you must still manually generate the XML file list by hand, which can be a pain if you have a lot of textures to place on the sprite sheet. Another is that it is XNA only <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Because we also do development on the iPhone, it was useful to have a similar generator for that platform, as well as for the XNA framework, so we created a tool to easily generate the texture atlases for us. The packing code is based on the SpriteSheet Sample code, though the tool is written in plain .NET, so does not require the XNA framework to be installed (though it does require the .NET framework). The tool can produce a PNG file with the combined textures, as well as an accompanying binary file, called a TXA file, with the information on each of the packed textures. It also supports removing transparent borders from textures to produce a more compact output. It can also optionally produce a C style header with enums in, so you don&#8217;t have to look up the textures by their names.</p>
<p>The help files for the texture atlas generator tool contain information on the format of the TXA files, so you can write your own loaders if you wish.</p>
<p>As well as the PNG + TXA combination, the tool can produce an XNA XML file (and optionally a C# file with enums in) which can be imported into your Content project to build the sprite sheet during compile time. You do need to include the XNA projects in yours to access the Content Processor and runtime types. The runtime library also includes extensions to the SpriteBatch class, to make it easier to draw using the textures.</p>
<p>Finally, there is a runtime library to load the texture atlas on an iPhone. The iPhone code uses the PVRTexture class from Apple&#8217;s sample code, so you can convert the PNG to a PVR if you wish using Apple&#8217;s texturetool (though this can be problematic as the textures may &#8216;bleed&#8217; into each other). The iPhone runtime library also requires <a href="http://www.zlib.net/">zlib</a> and <a href="http://www.libpng.org/pub/png/libpng.html">libpng</a></p>
<p>Each zip contains a Readme.txt file containing license information and other tips. You&#8217;re pretty much free to do what you want with these things, use them however you see fit (commercial or non-commercial), but remember they are provided as-is, and we accept no responsibility for anything <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  We&#8217;ve tried to tidy them up to make them usable by other people, but if you&#8217;ve got any problems or questions, post in the comments and we&#8217;ll try and help. There may be bugs in these, so let us know if you find any!</p>
<ul>
<li><a href="/downloads/textureatlas/Texture%20Atlas%20Generator.zip">Texture Atlas Generator</a></li>
<li><a href="/downloads/textureatlas/Texture%20Atlas%20-%20XNA.zip">XNA Runtime and Pipeline Libraries</a></li>
<li><a href="/downloads/textureatlas/Texture%20Atlas%20Runtime%20-%20iPhone.zip">iPhone Runtime</a> &#8211; Requires zlib and libpng</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2010/03/20/texture-atlas-sprite-sheet-generator/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 Flashlight (With SOS)</title>
		<link>http://www.gogo-robot.com/2010/03/19/windows-phone-7-flashlight-with-sos/</link>
		<comments>http://www.gogo-robot.com/2010/03/19/windows-phone-7-flashlight-with-sos/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 12:02:16 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[windows phone 7]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=199</guid>
		<description><![CDATA[I tried posting this as a comment in reply to this post, but the code got mangled, so I&#8217;m posting it here  

            graphics.GraphicsDevice.Clear&#40;
                /* Check for touch [...]]]></description>
			<content:encoded><![CDATA[<p>I tried posting this as a comment in reply to <a href="http://codecube.net/2010/03/windows-phone-7-flashlight/">this post</a>, but the code got mangled, so I&#8217;m posting it here <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">            graphics.<span style="color: #0000FF;">GraphicsDevice</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span>
                <span style="color: #008080; font-style: italic;">/* Check for touch panel being pressed */</span>
            TouchPanel.<span style="color: #0000FF;">GetState</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">?</span>
                <span style="color: #008080; font-style: italic;">/* Touch panel released, so reset the timer. This is stored in the target elapsed time, so we don't have to create any variables :) We also turn off IsFixedTimeStep, so that TargetElapsedTime isn't used. */</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">IsFixedTimeStep</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span> <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>Math.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span>1.0f, gameTime.<span style="color: #0000FF;">TotalGameTime</span>.<span style="color: #0000FF;">TotalMilliseconds</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> TimeSpan.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">Gray</span> <span style="color: #008000;">:</span> Color.<span style="color: #0000FF;">Gray</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span>
                <span style="color: #008080; font-style: italic;">/* Touch panel pressed, so do SOS */</span>
                <span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">7</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">9</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">12</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">13</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">15</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">17</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">18</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">19</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">21</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>gameTime.<span style="color: #0000FF;">TotalGameTime</span> <span style="color: #008000;">-</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TargetElapsedTime</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Seconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">22</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Color.<span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span>
                Color.<span style="color: #0000FF;">Black</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2010/03/19/windows-phone-7-flashlight-with-sos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Converting a tile map into geometry</title>
		<link>http://www.gogo-robot.com/2010/02/01/converting-a-tile-map-into-geometry/</link>
		<comments>http://www.gogo-robot.com/2010/02/01/converting-a-tile-map-into-geometry/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 13:20:17 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[tile]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=111</guid>
		<description><![CDATA[I recently needed to convert a tile map consisting of squares and triangles into chunks of geometry and after trying a couple of different approaches, I eventually settled on one which seems to work. I didn&#8217;t come across it anywhere else, so I thought I&#8217;d post it here in case anyone else is wanting to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to convert a tile map consisting of squares and triangles into chunks of geometry and after trying a couple of different approaches, I eventually settled on one which seems to work. I didn&#8217;t come across it anywhere else, so I thought I&#8217;d post it here in case anyone else is wanting to do something similar.</p>
<p><span id="more-111"></span></p>
<p>So, the problem is: Given a regular tile map, where each tile is either a square or a triangle taking up half of the square, generate sets of vertices which describe each &#8216;chunk&#8217; of geometry in the map. E.g.:</p>
<p style="text-align: center;"><img src="http://www.gogo-robot.com/wp-content/uploads/2010/01/TilesToGeom1.png" alt="Tiles To Geometry Fig. 1" title="Tiles To Geometry Fig. 1" width="640" height="224" class="alignnone size-full wp-image-114" /></p>
<p>The first method I tried was adapting <a href="http://en.wikipedia.org/wiki/Marching_squares">this method</a>, by representing each tile in a 2&#215;2 way, like so:</p>
<p style="text-align: center;"><img src="http://www.gogo-robot.com/wp-content/uploads/2010/01/TilesToGeom2.png" alt="Tiles To Geometry Fig. 2" title="Tiles To Geometry Fig. 2" width="160" height="112" class="alignnone size-full wp-image-117" /></p>
<p>Then performing the contour tracing algorithm in the link above, and removing every other point in the resulting list of points. At first it looked like this worked, but there were a couple of cases where a triangle was placed next to another triangle where the algorithm failed. <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>After thinking about it for a while, I came up with this method, which I&#8217;m pretty sure works, but if anyone can see any errors with it, please do let me know. I&#8217;ll give you the algorithm, then go through it in detail. The algorithm goes as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Pick a start tile
    Add the start tile to the open list
    Remove the tile from the tile map
    Add each of the tile's vertices to the vertex list
    Add each of the tile's edges to the edge list
    While the open list is not empty
        CurrentTile = Tile at the end of the open list
        NextTile = Tile in the &quot;forward&quot; direction of the current tile
        If NextTile exists and shares a common edge with the current shape
            Add NextTile to the open list
            Remove NextTile from the tile map
            Add NextTile's vertices to the vertex list (without creating duplicates)
            Generate an edge list for NextTile
            Remove edges that overlap from the main edge list and the edge list for NextTile
            Add the remaining edges from NextTile to the main edge list
        Else
            Move on to the next &quot;forward&quot; direction for CurrentTile
            If &quot;forward&quot; direction for CurrentTile = initial &quot;forward&quot; direction
                Remove CurrentTile from the open list
&nbsp;
Traverse the edge list and add the vertices that are used to the vertex list for your shape</pre></div></div>

<p>So, what does that all mean? Well, lets go through it step by step:</p>
<h2>Add the start tile to the open list</h2>
<p>The easiest way to search for a start tile is to do a row by row search. Start at 0,0 and scan along the first row, then the second and so on until you find an occupied tile. This is your start tile. The open list is a list of tiles currently being looked at. The start tile will be the first tile in this list.</p>
<h2>Remove the tile from the tile map</h2>
<p>Removing the tile from the tile map means that the algorithm will no longer visit them. Obviously, if you need your tile map after running the algorithm, create a copy of it <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Add each of the tile&#8217;s vertices to the vertex list</h2>
<p>The vertex list stores each corner point that has been used by each tile. For the first tile, just add the world space corner points to the list.</p>
<h2>Add each of the tile&#8217;s edges to the edge list</h2>
<p>The edge list is an list of, you guessed it, edges! Each edge is a pair of indices into the vertex list. Note that edges should always be specified in the same winding order and should always start at the same corner. I.e. pick clockwise or counter-clockwise and use that for both the square and triangle tiles, like so:</p>
<p style="text-align: center;"><a href="http://www.gogo-robot.com/wp-content/uploads/2010/02/EdgesAndVerts.png"><img src="http://www.gogo-robot.com/wp-content/uploads/2010/02/EdgesAndVerts.png" alt="Edges and Vertices example" title="EdgesAndVerts" width="464" height="132" class="alignnone size-full wp-image-123" /></a></p>
<p>Notice on the last triangle that we started at the top left (as there is no vertex in the bottom left, and this would be the next one round in our clockwise winding order).</p>
<h2>While the open list is not empty</h2>
<p>I.e. While we&#8217;re still examining tiles for our current shape.</p>
<h2>CurrentTile = Tile at the end of the open list</h2>
<p>Set the current tile to the last tile added to the open list</p>
<h2>NextTile = Tile in the &#8220;forward&#8221; direction of the current tile</h2>
<p>Each tile in the open list maintains the direction of the neighbour that it is currently examining. Each time a tile is added to the open list, its &#8220;forward&#8221; direction (i.e. the direction in which to examine) is set to &#8220;up&#8221;. I just use an integer here, with the range 0 &#8211; 3, representing up, right, down and left, in that order. When we&#8217;re on a tile, we examine the neighbour in the tile&#8217;s &#8220;forward&#8221; direction, and pursue that path as far as possible. The next time the algorithm comes back to a tile, it rotates to the next &#8220;forward&#8221; direction and tries that path, until all four directions have been exhausted.</p>
<h2>If NextTile exists and shares a common edge with the current shape</h2>
<p>If there is a neighbour in our current tile&#8217;s &#8220;forward&#8221; direction, then we check to see if the neighbour and the current tile share common edges. One way to do this is to generate a list of edges for NextTile using the current vertex list. Any edges which contain vertices in the current vertex list will use the same vertex indices as the existing edges, but in reverse order (due to each tile having the same winding order), like so:</p>
<p style="text-align: center;"><a href="http://www.gogo-robot.com/wp-content/uploads/2010/02/WindingOrder.png"><img src="http://www.gogo-robot.com/wp-content/uploads/2010/02/WindingOrder.png" alt="Winding Order" title="WindingOrder" width="219" height="132" class="alignnone size-full wp-image-124" /></a></p>
<p>In the above image, the shared edge is V2 -> V3 for the left tile, and V3 -> V2 for the right tile.</p>
<h2>Add NextTile to the open list</h2>
<p>Add NextTile to the end of the open list, initialising its &#8220;forward&#8221; direction to 0 (up)</p>
<h2>Remove NextTile from the tile map</h2>
<p>Remove NextTile from the tile map so it is not revisited by the algorithm (except for when it is returned to by the open list).</p>
<h2>Add NextTile&#8217;s vertices to the vertex list (without creating duplicates)</h2>
<p>Add each world space vertex for the tile to the vertex list, but don&#8217;t create duplicates for vertices that are already in the vertex list.</p>
<h2>Generate an edge list for NextTile</h2>
<p>Generate a list of edges (remember, each edge is a pair of indices into the vertex list), using vertices from the vertex list.</p>
<h2>Remove edges that overlap from the main edge list and the edge list for NextTile</h2>
<p>Remove any edges that overlap from both the edge list that was just generated for NextTile, and the main edge list. Remember, neighbouring tiles with overlapping edges will specify the indices of the overlapping edge in opposite order from each other. I.e. If a tile has an edge V2 -> V3, and the neighbouring tile has an edge that shares these vertices, the neighbouring tile&#8217;s edge will be V3 -> V2. This is due to the winding order, as shown above.</p>
<h2>Add the remaining edges from NextTile to the main edge list</h2>
<p>Any edges that do not overlap can now be added to the main edge list</p>
<h2>Move on to the next &#8220;forward&#8221; direction for CurrentTile</h2>
<p>When you reach here, it means there are no tiles to check in the current direction, so we should rotate to look in the next direction. If you&#8217;re storing your &#8220;forward&#8221; direction as an integer, simply increment it.</p>
<h2>If &#8220;forward&#8221; direction for CurrentTile = initial &#8220;forward&#8221; direction</h2>
<p>This is a check to see if we&#8217;ve exhausted each of the four directions. The initial &#8220;forward&#8221; direction will be 4 if you&#8217;re representing directions as an integer in the range 0 &#8211; 3. If the &#8220;forward&#8221; direction reaches 4, it means we have tested each direction (0 &#8211; 3) and we are done with this tile.</p>
<h2>Remove CurrentTile from the open list</h2>
<p>As we are done with this tile, we can remove it from the open list.</p>
<p></p>
<p>So, after we&#8217;ve generated our edge list, we now need to traverse it. Simply start at the first edge and follow the trial of edges around the shape until you reach the start vertex again. E.g.: First edge: V0 -> V1, so add V0 and V1 to your shape, then search for an edge that starts at V1. Next edge: V1 -> V4, so add V4 to your shape and search for an edge that starts at V4, and so on, until you reach V0 again. You&#8217;ve now got a list of vertices that represent your shape.</p>
<p></p>
<p>After this, two possible further steps are to optimize your final list of vertices (i.e. remove co-linear ones, so you are only left with the corner vertices), and remove any tiles which are inside your shape (unless you want to create shapes within shapes). Note, that the final step will only produce the outline shape of an object, so will not handle holes in closed objects. You may be able to handle holes in closed objects by recursively performing the last step and removing edges from the final edge list as you visit them, until the list is empty. This should give you multiple shapes, the first being the outer shape and the rest being the holes inside the shape, but I haven&#8217;t really looked in to this that much, so it may not work <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here&#8217;s a little animation of the algorithm. The grey edges are overlapping ones that have been removed.</p>
<p style="text-align: center;"><a href="http://www.gogo-robot.com/wp-content/uploads/2010/02/GenerateTileGeomAnim.gif"><img src="http://www.gogo-robot.com/wp-content/uploads/2010/02/GenerateTileGeomAnim.gif" alt="Algorithm Animation" title="GenerateTileGeomAnim" width="208" height="192" class="alignnone size-full wp-image-131" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2010/02/01/converting-a-tile-map-into-geometry/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Facebook Connect Dialog on iPhone</title>
		<link>http://www.gogo-robot.com/2010/01/10/facebook-connect-dialog-on-iphone/</link>
		<comments>http://www.gogo-robot.com/2010/01/10/facebook-connect-dialog-on-iphone/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 15:03:14 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[Facebook Connect]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=75</guid>
		<description><![CDATA[A problem I keep having when adding Facebook Connect to iPhone applications is that sometimes the dialogs appear briefly then disappear. I keep forgetting why it is for a while, so I thought I&#8217;d post this to help anyone who is having a similar problem, and to remind myself about it so that maybe next [...]]]></description>
			<content:encoded><![CDATA[<p>A problem I keep having when adding Facebook Connect to iPhone applications is that sometimes the dialogs appear briefly then disappear. I keep forgetting why it is for a while, so I thought I&#8217;d post this to help anyone who is having a similar problem, and to remind myself about it so that maybe next time I won&#8217;t forget <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-75"></span></p>
<p>Anyway, the problem is in the FBDialog.m file. There are a couple of lines that read:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIWindow<span style="color: #002200;">*</span> window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.keyWindow;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>window<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.windows objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The problem comes when you try and show the Facebook dialog just after something other than the main window is the key window. This could happen if you&#8217;ve popped up a UIAlertView to ask if the user wants to publish to Facebook. The UIAlertView is the application&#8217;s key window and when you display your Facebook dialog, it adds itself as a subview of the UIAlertView (which is currently closing). So, your dialog will start to display, then when the UIAlertView finishes closing, it&#8217;ll disappear. The easy fix is to just change the FBDialog.m file so that it always uses the second line, and doesn&#8217;t try to use the keyWindow property:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIWindow<span style="color: #002200;">*</span> window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.windows objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2010/01/10/facebook-connect-dialog-on-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The App Store Release Date</title>
		<link>http://www.gogo-robot.com/2009/10/05/the-app-store-release-date/</link>
		<comments>http://www.gogo-robot.com/2009/10/05/the-app-store-release-date/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 14:30:10 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=64</guid>
		<description><![CDATA[So I think I’ve finally figured out how you should handle the release date when you’re submitting your app. From what I can see, when your app gets approved, it gets the release date you set when you submitted, even if the date it gets approved is AFTER the release date. Consequently, when your app [...]]]></description>
			<content:encoded><![CDATA[<p>So I think I’ve finally figured out how you should handle the release date when you’re submitting your app. From what I can see, when your app gets approved, it gets the release date you set when you submitted, even if the date it gets approved is AFTER the release date. Consequently, when your app appears on the App Store, it’s already a few pages down when sorted by release date. Not good <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><span id="more-64"></span></p>
<p>So, I’m guessing you have to set your release date way off into the future (because you have no idea how long it’s going to take Apple to approve your app), so that when it’s approved, you can bring it forward to the current date and it’ll be top of the new releases list. I’m going to be trying this with our next iPhone game, so I’ll keep you posted how it turns out. The one potential problem is that if Apple looks at the release date to determine when they’ll test your app, it might be a while before it gets tested.</p>
<p></p>
<p><strong>Update</strong>: Apparently, from reading around, the release date will be listed as the earliest of (i) the date you set when submitting, and (ii) the date it is approved by Apple. So even if you set your availability date to some distant future date, then set it to something sensible when it’s released, it’ll be down in iTunes as the date it was approved. So I guess the moral of the story is this:</p>
<p></p>
<p><em>When submitting your app to the App Store, set the availability date to a month or two ahead (and remember to update it if you have to re-submit or if it takes Apple ages to approve it), so that it’s always ahead of the date it gets approved. Then, once it’s approved, set the release date to the date it was approved (so it actually appears on the App Store). You need to set it as soon as you receive the &#8220;Ready for sale&#8221; e-mail from apple, and remember to set the availability date to the date that Apple sent the e-mail (remember to take into account the time difference between you and Apple in Cupertino). E.g. If you receive the e-mail in the early hours of Tuesday morning, and you&#8217;re in the UK, then Apple will have sent the e-mail on Monday (their time), so your availability date should be Monday.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2009/10/05/the-app-store-release-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two awkward things about iPhone development</title>
		<link>http://www.gogo-robot.com/2009/06/07/two-awkward-things-about-iphone-development/</link>
		<comments>http://www.gogo-robot.com/2009/06/07/two-awkward-things-about-iphone-development/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 14:36:16 +0000</pubDate>
		<dc:creator>Rew</dc:creator>
				<category><![CDATA[Developer Blog]]></category>
		<category><![CDATA[Galactic Aquarium]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://www.gogo-robot.com/?p=69</guid>
		<description><![CDATA[I’m currently in the process of porting The Galactic Aquarium to the iPhone now that the 360 version is done. While doing this, I’ve come across a couple of awkward points to do with UIKit on the iPhone, so I thought I’d share how I got around them.

The first is using a custom font with [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently in the process of porting The <a href="http://www.gogo-robot.com/categories/games/">Galactic Aquarium</a> to the iPhone now that the 360 version is done. While doing this, I’ve come across a couple of awkward points to do with UIKit on the iPhone, so I thought I’d share how I got around them.</p>
<p><span id="more-69"></span></p>
<p>The first is using a custom font with a UILabel. Although you can set the font property of a UILabel, the only fonts you can create are system fonts. What you have to do is subclass the UILabel class and override the <em>– (void)drawTextInRect:(CGRect)rect</em> method with your own implementation.</p>
<p></p>
<p>First things first though, you need to load in your font. <strong>Note: With this method, you need to distribute the .ttf file with your app. Make sure this is ok in the font’s license.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get the path to our custom font and create a data provider.</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>fontPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FontFilename&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ttf&quot;</span><span style="color: #002200;">&#93;</span>;
CGDataProviderRef fontDataProvider <span style="color: #002200;">=</span> CGDataProviderCreateWithFilename<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fontPath UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create the font with the data provider, then release the data provider.</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Note: s_CustomFont is a static variable, not an instance variable</span>
<span style="color: #11740a; font-style: italic;">// so that it can be shared between all instances of the custom font UILabel class</span>
s_CustomFont <span style="color: #002200;">=</span> CGFontCreateWithDataProvider<span style="color: #002200;">&#40;</span>fontDataProvider<span style="color: #002200;">&#41;</span>;
CGDataProviderRelease<span style="color: #002200;">&#40;</span>fontDataProvider<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Now that you’ve got the font loaded in, you need to draw your text with it. Unfortunately, you can’t just set the font then call <em>[super drawTextInRect: rect]</em>. This is because there are actually two different methods of drawing fonts in Quartz. <em>CGContextShowText</em> displays a string of text using the currently <strong>selected</strong> font. You select the font using <em>CGContextSelectFont</em>, but this function is only capable of selecting a built in system font. The other option is to use <em>CGContextShowGlyphs</em> and <em>CGContextSetFont</em>. Notice that’s <strong>Set</strong>Font and not <strong>Select</strong>Font.</p>
<p></p>
<p>The SelectFont / ShowText combination uses the name of the font to look up the font data and the glyph mapping table in the system. The glyph mapping table is used by the font renderering routines to match up glyphs from the font with characters in your string. There’s no way to programatically set the glyph mapping table, which is why there are the two methods of drawing text.</p>
<p></p>
<p>When using the SetFont / ShowGlyphs method, you need to manually convert the characters in your string to glyph indices in the font. Fortunately, this generally works out pretty easily, which we’ll see later.</p>
<p></p>
<p>Now that we have our font loaded in, we can write our custom drawTextInRect function. So, first thing’s first, you need to set the font to use:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextSetFont<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, s_CustomFont<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// We need to tell the system which font size to use too</span>
<span style="color: #a61390;">float</span> FontSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">18</span>; <span style="color: #11740a; font-style: italic;">// 18-pt font size</span>
CGContextSetFontSize<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, FontSize<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Then we need to convert our string to glyph indices. Like I said before, this mostly works out quite easily. Usually, you just need to offset the character index by a “magic number”, and most of the time this number turns out to be -29:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Convert the text to glyphs</span>
<span style="color: #a61390;">int</span> StringLength <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.text length<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Make sure you remember to free these after rendering</span>
CGGlyph<span style="color: #002200;">*</span> Glyphs <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>CGGlyph<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> StringLength<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">char</span><span style="color: #002200;">*</span> Chars <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> <span style="color: #002200;">&#40;</span>StringLength <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Convert characters to glyph indices</span>
<span style="color: #002200;">&#91;</span>self.text getCString<span style="color: #002200;">:</span> Chars maxLength<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>StringLength <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> encoding<span style="color: #002200;">:</span> NSASCIIStringEncoding<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> CurrentChar <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; CurrentChar <span style="color: #002200;">&amp;</span>lt; StringLength; <span style="color: #002200;">++</span>CurrentChar<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    Glyphs<span style="color: #002200;">&#91;</span>CurrentChar<span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> Chars<span style="color: #002200;">&#91;</span>CurrentChar<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> <span style="color: #2400d9;">29</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>One thing to consider here is that any new line escape sequences will end up with some really high glyph index (as CGGlyph is just an unsigned short). But you have to handle new lines manually anyway, so it shouldn’t be too bad.</p>
<p></p>
<p>Finally, we can draw our text. You need to setup your text matrix and text position before drawing the text, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Set the text matrix, otherwise it draws upside down</span>
CGAffineTransform TextMatrix <span style="color: #002200;">=</span> CGAffineTransformIdentity;
&nbsp;
TextMatrix.d <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
CGContextSetTextMatrix<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, TextMatrix<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the text position - Remember, 0, 0 is the top left of the text's draw rectangle, not the screen</span>
CGContextSetTextPosition<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Note that <em>CGContextSetTextPosition</em> sets the same translation values that the translation component of the text matrix sets, so in the above case it’s obsolete. But when you start adding multi-line and alignment support, it’s cleaner to just set the text matrix once and then just use <em>CGContextSetTextPosition</em> to set the position.</p>
<p></p>
<p>Now just draw your glyphs:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextShowGlyphs<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, Glyphs, StringLength<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So there’s our basic custom font drawing routing. If I recall correctly, it’ll crash as soon as you have a newline in your string, because it tries to just use a glyph that isn’t there. What you have to do is, during renderering, scan along your glyphs until there is a new line, draw the glyphs up until the new line, then start again from just after the new line.</p>
<p></p>
<p>You can also implement alignment and word wrapping. To do this, you need to be able to measure the string, so here’s how you do that:</p>
<p></p>
<p>After a call to <em>CGContextShowGlyphs</em>, the current text position is updated and can be retrieved like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGPoint EndPoint <span style="color: #002200;">=</span> CGContextGetTextPosition<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So EndPoint.x is the length of your string (assuming your initial text position was at 0, 0). You can prevent Quartz from actually doing any drawing while you’re measuring the string’s length by setting the text drawing mode to invisible:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextSetTextDrawingMode<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, kCGTextInvisible<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Remember to set your drawing mode and text position back to normal when you want to actually draw your text:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextSetTextDrawingMode<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, kCGTextFill<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// You can also set to the text colour to the UILabel's textColor property</span>
CGContextSetFillColorWithColor<span style="color: #002200;">&#40;</span>UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, self.textColor.CGColor<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Now that you have the text size, you can use the UILabel’s <em>textAlignment</em> property to determine how to align your text when you actually draw it. Word wrapping can be achieved using a standard word wrapping algorithm. Just keep drawing glyphs until EndPoint.x is greater than self.frame.size.width. Then you know you’ve exceeded the frame and need to word wrap. So then you can jump back to the previous word, reset the text position to 0, 0 and continue on. It’s best to store each line length during your measuring stage so that they can be used to align the text during drawing.</p>
<p></p>
<p>One problem I haven’t yet figured out is the height of a line. At the moment I’m just using FontSize * 1.2, which seems to work OK for the font I’ve tried this on. I’m sure there’s probably some better way to do it, but I haven’t looked into it too much.</p>
<p></p>
<p>You can use the measuring technique to automatically size the height of your custom UILabel, to prevent it from being clipped (seeing as how you can’t actually see your custom font in Interface Builder). I added a new function to my custom font label class to calculate the height of the word wrapped text. I then use the height to set the height of the frame. This does mean that I have to set the frame’s width, call the function then set the frame’s height to the returned value. It also means you have to make sure the graphics context is correctly set up during the heigh measuring function (which it probably won’t be if it’s called from outside the drawTextInRect function. The easiest way to do this is to create a temporary graphics context, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGSize ContextSize;
ContextSize.width <span style="color: #002200;">=</span> <span style="color: #2400d9;">480</span>;
ContextSize.height <span style="color: #002200;">=</span> <span style="color: #2400d9;">320</span>;
UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>ContextSize<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Measure the string's height</span>
&nbsp;
.....
&nbsp;
UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So there you have it. That’s how I’ve got my custom font UILabels to be used.</p>
<p></p>
<p>The other awkward thing is a lot simpler. I didn’t realise how long that last one was going to be <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p></p>
<p>The problem arises when you want to display a modal view controller straight after another one has been dismissed. It seems the best place to show the second one is in the <em>viewDidDisappear</em> function of the first UIViewController. However, calling <em>presentModalViewController</em> on the parent UIViewController from this function doesn’t work (nothing seems to happen). I’m guessing it’s because the modal view controller for the parent view controller is still pointing to the one that’s just disappeared, and that this gets cleaned up after <em>viewDidDisappear</em> is called, so any new modal view controller you set in <em>viewDidDisappear</em> is reset straight after the function returns (because the parent view controller is still in the process of cleaning up the first one). The solution is to use the performSelector function on the parent view controller with a delay of 0:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self.parentViewController performSelector<span style="color: #002200;">:</span> <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>myDisplayNextModalViewControllerFunction<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span> afterDelay<span style="color: #002200;">:</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The delay of 0 doesn’t mean that the selector is performed straight away. Because of the order of events in the run loop, the selector will be performed after the parent view controller has fully dismissed its modal view controller, so the next one will display fine.</p>
<p></p>
<p>Hope this helps <img src='http://www.gogo-robot.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gogo-robot.com/2009/06/07/two-awkward-things-about-iphone-development/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
