<?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>[Glitch toString];</title>
	<atom:link href="http://richard.giliam.net/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://richard.giliam.net</link>
	<description>Ramblings of a software developer.</description>
	<lastBuildDate>Fri, 15 Jan 2010 05:10:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A change of venue.</title>
		<link>http://richard.giliam.net/?p=293</link>
		<comments>http://richard.giliam.net/?p=293#comments</comments>
		<pubDate>Fri, 15 Jan 2010 05:10:45 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=293</guid>
		<description><![CDATA[In the event that anyone is still monitoring this blog (I suck I know&#8230;) I&#8217;ve decided to move to a new site.
The new blog (which will contain much the same awesomeness [in higher frequency] as the old blog) is located at http://anti-alias.me and will focus primarily on computer graphics, and my university stuffs, seeing as [...]]]></description>
			<content:encoded><![CDATA[<p>In the event that anyone is still monitoring this blog (I suck I know&#8230;) I&#8217;ve decided to move to a new site.</p>
<p>The new blog (which will contain much the same awesomeness [in higher frequency] as the old blog) is located at <a href="http://anti-alias.me">http://anti-alias.me</a> and will focus primarily on computer graphics, and my university stuffs, seeing as how I&#8217;m back in school full-time, and working on an honors thesis.</p>
<p>I figure, it will save me a few keystrokes to get there.. it&#8217;s a catchy, theme-based name to inspire topics, and I REALLY mean it this time, I&#8217;m going to be actively blogging.</p>
<p>I&#8217;m currently getting ready to transfer the unfinished posts I have stored up here, over to there.. in order to prime the content pump.. and feel free, if someone is actually reading this, to give me a hard time for not posting. :) Comments are open over there, and discussion is always welcome.</p>
<p>This site, along with the .info version, will become a static page that will represent me, as a CV of sorts.</p>
<p>Time to get this party started. :D</p>
<p>-glitch</p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=293</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LuaEngine: Using Lua With Objective-C</title>
		<link>http://richard.giliam.net/?p=78</link>
		<comments>http://richard.giliam.net/?p=78#comments</comments>
		<pubDate>Tue, 11 Aug 2009 03:16:29 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[C/C++/Obj-C]]></category>
		<category><![CDATA[Game Development]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=78</guid>
		<description><![CDATA[In this series of posts, I want to explore the, already well covered, topic of using Lua for game scripting.  My direction will include Objective-C as the host language, though the beginning code will stick to C.  The end goal of these posts, is to implement a bridge between Lua and Objective-C that [...]]]></description>
			<content:encoded><![CDATA[<p>In this series of posts, I want to explore the, already well covered, topic of using Lua for game scripting.  My direction will include Objective-C as the host language, though the beginning code will stick to C.  The end goal of these posts, is to implement a bridge between Lua and Objective-C that will allow us to expose obj-c objects to Lua script, without a large amount of &#8220;setup&#8221; code.</p>
<p>I&#8217;m going to make the assumption that anyone reading this has already gotten Lua compiled for their target platform, and can jump right into embedding Lua into a simple application.</p>
<h2>The Basics</h2>
<p>The first step to creating a generic Lua engine in Objective-C is introducing yourself to how Lua&#8217;s C API works and how we can use it to share data between script and native code. So, here is the bare minimum we need to bring Lua into our app:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">char</span> <span style="color: #002200;">**</span>argv<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>argc <span style="color: #002200;">!=</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Usage: %s <span style="color: #2400d9;">\n</span>&quot;</span>, argv<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
   <span style="color: #002200;">&#125;</span>
   lua_State <span style="color: #002200;">*</span>lua <span style="color: #002200;">=</span> lua_open<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
   luaL_openlib<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Open standard libs (math, io, os, etc)</span>
   luaL_dofile<span style="color: #002200;">&#40;</span>lua, argv<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Run the script supplied by the user</span>
   lua_close<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Shut down our lua vm.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The above code will:
<ul>
<li>Start a Lua VM (<code>lua_open</code>)</li>
<li>Initialize standard libs (<code>luaL_openlib</code>)</li>
<li>Run the Lua script supplied by the user. (<code>luaL_dofile</code>)</li>
<li>And finally, terminate the Lua VM. (<code>lua_close</code>)
</ul>
<p>This can be tested by providing a Lua script on the command line, like:</p>
<blockquote><p>./luatest test.lua</p></blockquote>
<p>So, lets pretend that we wanted to expose functionality to Lua that would allow us to write Lua script that would manage the location of a game object.  For simplicity, lets call that object a Box.  The Box is a simple quad that we&#8217;ll render in our game.  The goal, will be to have the game code remain static, while we implement various movement algorithms for our Box.</p>
<h3>Sharing Data</h3>
<p>Our first phase will be to expose global values that represent the X &amp; Y coordinates of the Box. Through those global variables we can just set the coordinates that we want, and the render loop of our game can read them in for translation. So, building onto the above code we could add:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">float</span> box_x <span style="color: #002200;">=</span> 0.0f;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">float</span> box_y <span style="color: #002200;">=</span> 0.0f;
<span style="color: #a61390;">int</span> initializeGlobals<span style="color: #002200;">&#40;</span>lua_State <span style="color: #002200;">*</span>l<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, box_x<span style="color: #002200;">&#41;</span>;
   lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxX&quot;</span><span style="color: #002200;">&#41;</span>;
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, box_y<span style="color: #002200;">&#41;</span>;
   lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxY&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I&#8217;ve added two global variables (box_x &#038; box_y). Please note, I am in no way saying this is the best way to implement this, but it certainly is the easiest for this small example.</p>
<p>With those two global variables defined, the function <code>initializeGlobals</code>, when called, will take the supplied Lua VM and push the values of both global variables into the Lua environment.  It does this by first pushing <code>box_x</code>&#8217;s value onto the Lua stack. Then, it calls <code>lua_setglobal</code> and supplies the name &#8220;BoxX&#8221;. What this does is, pops the value, of <code>box_x</code> that we pushed onto the stack, off and adds it to the global registry under the name &#8220;BoxX&#8221;. This will allow any Lua script which runs from now on, to access a global variable named, <code>BoxX</code>. Initially, the value will be our initial value of <code>box_x</code>, but the Lua script will be able to change that as it wishes. We&#8217;ll also have to implement code later on to read back the current value of that global variable if we ever want to do anything else with it.</p>
<p>Now, all we have to do is make a call to <code>initializeGlobals</code> inside our <code>main</code> function from above.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">luaL_openlib<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Begin new code</span>
initializeGlobals<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// End new code</span>
luaL_dofile<span style="color: #002200;">&#40;</span>lua, argv<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>To test it out we can write a quick Lua script like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Testing global variables</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;X: &quot;</span>, BoxX, <span style="color: #ff0000;">&quot; Y: &quot;</span>, BoxY<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which should give an output like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">X:      0        Y:     0</pre></div></div>

<p>Ok, so now we can initialize our <code>BoxX</code> and <code>BoxY</code> variables so our code can use them. But, like I said before, we now need to write a little more code so we can read back their values.  The code uses a very similar pattern to how we set the values, but as expected, a bit in reverse:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> readGlobals<span style="color: #002200;">&#40;</span>lua_State <span style="color: #002200;">*</span>l<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   lua_getglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxX&quot;</span><span style="color: #002200;">&#41;</span>;
   box_x <span style="color: #002200;">=</span> lua_tonumber<span style="color: #002200;">&#40;</span>l, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
   lua_getglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxY&quot;</span><span style="color: #002200;">&#41;</span>;
   box_y <span style="color: #002200;">=</span> lua_tonumber<span style="color: #002200;">&#40;</span>l, <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
   lua_pop<span style="color: #002200;">&#40;</span>l, <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
   <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;x = %.3f  y = %.3f<span style="color: #2400d9;">\n</span>&quot;</span>, box_x, box_y<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So, instead of pushing our values on, and telling Lua that we want to set new globals, we now tell Lua what globals we want to <b>get</b> and Lua pushes their values onto the stack. So, we start off by getting the value of <code>BoxX</code> by calling <code>lua_getglobal</code>. Once that&#8217;s done, the value of <code>BoxX</code> will be on the top of the stack for us. Once the value is on the stack, we can actually use it to do anything we want, like use it for a Lua function&#8217;s parameter, or any number of things. But instead, we&#8217;re simply going to copy the value from the stack, using <code>lua_tonumber</code>, and specifying stack index <b>1</b>. Something to take note of, is the fact that <code>lua_tonumber</code> does not pop the value off the stack. So we&#8217;re left to clean up after we&#8217;re done.</p>
<p>After doing the same for <code>BoxY</code> a call to <code>lua_pop</code> telling it to remove the top <b>2</b> elements from the stack cleans up after everything we&#8217;ve done, and we can rest assured that we&#8217;re being good stack using citizens.</p>
<p>Just like before, we need to add a call to <code>readGlobals</code> to our <code>main</code> function. Since we want to get the values of our globals <b>after</b> the script has had a chance to modify them, we need to place the call after our call to <code>luaL_dofile</code>. Like so:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">luaL_dofile<span style="color: #002200;">&#40;</span>lua, argv<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Begin new code</span>
readGlobals<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// End new code</span>
lua_close<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Accompanying Lua script to test our changes out:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;X: &quot;</span>, BoxX, <span style="color: #ff0000;">&quot; Y: &quot;</span>, BoxY<span style="color: #66cc66;">&#41;</span>
BoxX <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1.5</span>
BoxY <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1.0</span></pre></div></div>

<p>The output should look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">X:      0        Y:     0
x = 1.500  y = 1.000</pre></div></div>

<p>Excellent. Now we can share data with our Lua scripts. But that still keeps us rather limited in what we can do in Lua. So, our next step will be to expose native code, so we can make use of highly optimized routines, or just already implemented functionality of the game engine.</p>
<h3>Sharing Functionality</h3>
<p>Now we have the ability to update our Box&#8217;s position. Since we want to have the logic for moving our box each frame implemented in Lua, we might want to start working on implementing a movement algorithm. So lets implement a very basic movement scheme which will bounce the box around the screen. Very simple.  But first, we need a random velocity for our box.  We can do this very simply in Lua, but for lack of a better example, I&#8217;m going to use this as a reason for exposing native code to Lua.</p>
<p>First, we&#8217;re going to want a function that can provide us with the x and y components to a randomly generated velocity.  I&#8217;d also like to allow the Lua script to specify what the magnitude of that velocity should be, that way changing the Box&#8217;s speed doesn&#8217;t require a recompile.</p>
<p>In order to do this, we first need to define a native function that Lua can call.  Since Lua is going to be calling this function directly, it has to match Lua&#8217;s C function prototype. This means the function has to return an <code>int</code>, and must take a single parameter of type <code>lua_State*</code>.  The <code>lua_State</code> pointer is a pointer to the Lua VM that the function is being called from, and the return value of the function is the number of values the function returned on the stack.</p>
<p>Here&#8217;s a function that would generate a random 2D velocity (x &#038; y components) that can be called directly from Lua:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> genRandomVelocity<span style="color: #002200;">&#40;</span>lua_State <span style="color: #002200;">*</span>l<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #a61390;">float</span> speed <span style="color: #002200;">=</span> luaL_checknumber<span style="color: #002200;">&#40;</span>l, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
   <span style="color: #a61390;">srand</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">time</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
   <span style="color: #a61390;">float</span> x <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">rand</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">RAND_MAX</span>;
   <span style="color: #a61390;">float</span> y <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">rand</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">RAND_MAX</span>;
   <span style="color: #a61390;">float</span> len <span style="color: #002200;">=</span> <span style="color: #a61390;">sqrt</span><span style="color: #002200;">&#40;</span>x<span style="color: #002200;">*</span>x <span style="color: #002200;">+</span> y<span style="color: #002200;">*</span>y<span style="color: #002200;">&#41;</span>;
   x <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>x<span style="color: #002200;">/</span>len<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> speed;
   y <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>y<span style="color: #002200;">/</span>len<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> speed;
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, x<span style="color: #002200;">&#41;</span>;
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, y<span style="color: #002200;">&#41;</span>;
   <span style="color: #a61390;">return</span> <span style="color: #2400d9;">2</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><code>genRandomVelocity</code> starts out by checking to make sure the first argument (stack item #1) is a number. If it is, the number is copied to our local variable <code>speed</code>. Once it has the value for speed, it generates a random 2D vector, normalizes it, and then scales it to the Lua supplied magnitude (speed).</p>
<p>Once the vector is calculated it pushes the component values onto the stack using <code>lua_pushnumber</code>. The values should be pushed in the order that you want the user to retrieve them. Finally, the function returns 2, telling Lua that it has 2 elements on the stack that it is returning.</p>
<p>So now that we have a function to generate our velocity we can expose it to Lua from inside our <code>initializeGlobals</code> function with a quick call to <code>lua_register</code> like so:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> initializeGlobals<span style="color: #002200;">&#40;</span>lua_State <span style="color: #002200;">*</span>l<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, box_x<span style="color: #002200;">&#41;</span>;
   lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxX&quot;</span><span style="color: #002200;">&#41;</span>;
   lua_pushnumber<span style="color: #002200;">&#40;</span>l, box_y<span style="color: #002200;">&#41;</span>;
   lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxY&quot;</span><span style="color: #002200;">&#41;</span>;
   <span style="color: #11740a; font-style: italic;">// Begin New Code</span>
   lua_register<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;randvelocity&quot;</span>, <span style="color: #002200;">&amp;</span>genRandomVelocity<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Tell Lua about our function</span>
   <span style="color: #11740a; font-style: italic;">// End New Code</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Which allow us to use the Lua function <code>randvelocity</code> inside our script:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Generate a new random velocity</span>
x, y <span style="color: #66cc66;">=</span> randvelocity<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;vx: &quot;</span>, x, <span style="color: #ff0000;">&quot; vy: &quot;</span>, y<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which should generate an output like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vx:     0.00092774751828983      vy:    0.012465523555875</pre></div></div>

<p>Which will naturally give us different values each time we run it.</p>
<p>Ok, so now our Lua script can utilize data, and functionality, implemented in our native code. But, unless we plan on storing every piece of Lua functionality in its own file, we need a way to call Lua functions from native code.</p>
<p>As we&#8217;ve seen, by using <code>luaL_dofile</code> our script will be read, and evaluated, by the Lua VM. So, we&#8217;ve been relying on the evaluation in order to execute our previous test code. What we can do now, is modify our script by adding a function called &#8216;update&#8217;, and moving our Box position code into it. By leaving our velocity generation code outside of the function we can still take advantage of the evaluation to initialize our Box&#8217;s velocity.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Generate a new random velocity</span>
x, y <span style="color: #66cc66;">=</span> randvelocity<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0125</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;vx: &quot;</span>, x, <span style="color: #ff0000;">&quot; vy: &quot;</span>, y<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">function</span> update <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;X: &quot;</span>, BoxX, <span style="color: #ff0000;">&quot; Y: &quot;</span>, BoxY<span style="color: #66cc66;">&#41;</span>
   BoxX <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1.5</span>
   BoxY <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1.0</span>
<span style="color: #b1b100;">end</span></pre></div></div>

<p>Now, if we run the same version of our native code that we had prior to making this change, all the output we should see is something like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vx: 	0.011539350263774	 vy: 	0.0048055569641292
x = 1.000  y = 0.000</pre></div></div>

<p>Which is great, because that means it is still running the code that we&#8217;re using for our velocity when it loads the Lua file, and not executing our update function at all.  So, whenever we want to actually call that update function, we need to add some native code which, for now, can be added in right before our call to <code>readGlobals</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">luaL_dofile<span style="color: #002200;">&#40;</span>lua, argc<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// New code...</span>
lua_getglobal<span style="color: #002200;">&#40;</span>lua, <span style="color: #bf1d1a;">&quot;update&quot;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Push the 'update' function onto the stack</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>lua_pcall<span style="color: #002200;">&#40;</span>lua, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// Call the function that's on the stack</span>
   <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;An error has occured running 'update': %s<span style="color: #2400d9;">\n</span>&quot;</span>, lua_tostring<span style="color: #002200;">&#40;</span>lua, <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// End new code</span>
readGlobals<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Calling a Lua function from C follows a very similar pattern to getting values from our globals.  First we need to tell Lua that we want the global &#8220;update&#8221; to be pushed on the stack. It doesn&#8217;t matter that &#8216;update&#8217; is a function, Lua will treat it just like it treats any other global value.  So our call to <code>lua_getglobal</code> will push <code>update</code> onto the stack for us, so we can use it however we want.  In order to execute it though, we need to call <code>lua_pcall</code> (or one of the other <code>lua_call</code>-like functions) which will execute the function currently on the stack, and return 0 if it executed successfully. <code>lua_pcall</code> will also pop the function off of the stack, so we don&#8217;t have to worry about cleaning it up.</p>
<p>If the call to <code>lua_pcall</code> fails, Lua will push the error string onto stack index -1. Which is why I&#8217;ve printed out that stack element, as a string, if an error occurs.  The strings generated will be what you would expect a Lua interpreter to tell you if you have syntax errors, etc.</p>
<p>Now that we can call our Lua function from native code, we can move towards simulating an actual game loop.  By looping over <code>lua_getglobal</code>, <code>lua_pcall</code>, and <code>readGlobals</code> we can use the <code>update</code> function like it would be used on a per-frame position update.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #b1b100;">function</span> update <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   BoxX <span style="color: #66cc66;">=</span> BoxX + x
   BoxY <span style="color: #66cc66;">=</span> BoxY + y
<span style="color: #b1b100;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">   <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> frame<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; frame&lt;<span style="color: #2400d9;">10</span>; <span style="color: #002200;">++</span>frame<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      lua_getglobal<span style="color: #002200;">&#40;</span>lua, <span style="color: #bf1d1a;">&quot;update&quot;</span><span style="color: #002200;">&#41;</span>;
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>lua_pcall<span style="color: #002200;">&#40;</span>lua, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
         <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;An error has occured running 'update': %s<span style="color: #2400d9;">\n</span>&quot;</span>, lua_tostring<span style="color: #002200;">&#40;</span>lua, <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
      readGlobals<span style="color: #002200;">&#40;</span>lua<span style="color: #002200;">&#41;</span>;
   <span style="color: #002200;">&#125;</span></pre></div></div>

<p>Giving us an output like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vx: 	0.010487142950296	 vy: 	0.0068021933548152
x = 1.000  y = 0.000
x = 1.010  y = 0.007
x = 1.021  y = 0.014
x = 1.031  y = 0.020
x = 1.042  y = 0.027
x = 1.052  y = 0.034
x = 1.063  y = 0.041
x = 1.073  y = 0.048
x = 1.084  y = 0.054
x = 1.094  y = 0.061
x = 1.105  y = 0.068</pre></div></div>

<p>If we actually had a render loop, we&#8217;d see our box move across the screen in a random direction.  But it would still leave the view, and we&#8217;d never see it again.  So if we go back to our <code>initializeGlobals</code> we can put a couple more globals in to expose our viewport width and height.  Lets assume we&#8217;re using an orthogonal projection with a view of x=[-1.0, 1.0], and y=[-1.5, 1.5].</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;BoxY&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Begin New Code</span>
lua_pushnumber<span style="color: #002200;">&#40;</span>l, <span style="color: #002200;">-</span>1.5f<span style="color: #002200;">&#41;</span>;
lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;MIN_HEIGHT&quot;</span><span style="color: #002200;">&#41;</span>;
lua_pushnumber<span style="color: #002200;">&#40;</span>l, 1.5f<span style="color: #002200;">&#41;</span>;
lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;MAX_HEIGHT&quot;</span><span style="color: #002200;">&#41;</span>;
lua_pushnumber<span style="color: #002200;">&#40;</span>l, <span style="color: #002200;">-</span>1.0f<span style="color: #002200;">&#41;</span>;
lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;MIN_WIDTH&quot;</span><span style="color: #002200;">&#41;</span>;
lua_pushnumber<span style="color: #002200;">&#40;</span>l, 1.0f<span style="color: #002200;">&#41;</span>;
lua_setglobal<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;MAX_WIDTH&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// End New Code</span>
lua_register<span style="color: #002200;">&#40;</span>l, <span style="color: #bf1d1a;">&quot;randvelocity&quot;</span>, <span style="color: #002200;">&amp;</span>genRandomVelocity<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>This probably isn&#8217;t the most elegant solution, but it will work for this example, and we can now implement our <code>update</code> function like so:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- x and y are defined outside the function as the x and y components of</span>
<span style="color: #808080; font-style: italic;">-- our velocity vector.</span>
<span style="color: #b1b100;">function</span> update <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   nx <span style="color: #66cc66;">=</span> BoxX + x
   ny <span style="color: #66cc66;">=</span> BoxY + y
   <span style="color: #b1b100;">if</span> nx <span style="color: #66cc66;">&lt;</span> MIN_WIDTH <span style="color: #b1b100;">or</span> nx <span style="color: #66cc66;">&gt;</span> MAX_WIDTH <span style="color: #b1b100;">then</span>
      nx <span style="color: #66cc66;">=</span> nx - x <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2.0</span>
      x <span style="color: #66cc66;">=</span> x <span style="color: #66cc66;">*</span> -<span style="color: #cc66cc;">1.0</span>
   <span style="color: #b1b100;">end</span>
   <span style="color: #b1b100;">if</span> ny <span style="color: #66cc66;">&lt;</span> MIN_HEIGHT <span style="color: #b1b100;">or</span> ny <span style="color: #66cc66;">&gt;</span> MAX_HEIGHT <span style="color: #b1b100;">then</span>
      ny <span style="color: #66cc66;">=</span> ny - y <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2.0</span>
      y <span style="color: #66cc66;">=</span> y <span style="color: #66cc66;">*</span> -<span style="color: #cc66cc;">1.0</span>
   <span style="color: #b1b100;">end</span>
   BoxX <span style="color: #66cc66;">=</span> nx
   BoxY <span style="color: #66cc66;">=</span> ny
<span style="color: #b1b100;">end</span></pre></div></div>

<p>Running our code now, and modifying our velocity magnitude to 0.5, should give us an output like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vx:     0.47514313459396         vy:    0.15568877756596
x = 1.000  y = 0.000
x = 0.525  y = 0.156
x = 0.050  y = 0.311
x = -0.425  y = 0.467
x = -0.901  y = 0.623
x = -0.425  y = 0.778
x = 0.050  y = 0.934
x = 0.525  y = 1.090
x = 1.000  y = 1.246
x = 0.525  y = 1.401
x = 0.050  y = 1.246</pre></div></div>

<p>A nice, and bouncy, moving quad. Updated entirely from our Lua script.</p>
<h3>The next step..</h3>
<p>Now we have some basic Lua support capabilities that can help us to move some of our game logic into script. This is great, if all we wanted to do was expose some globals, and share some functions. So, our next challenge, is to generalize our code so we don&#8217;t have to keep using the boilerplate Lua C API to add globals, and expose functions.</p>
<p><a href="/?page_id=134">Code for this post.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=78</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What I&#8217;ve been up to..</title>
		<link>http://richard.giliam.net/?p=212</link>
		<comments>http://richard.giliam.net/?p=212#comments</comments>
		<pubDate>Fri, 24 Jul 2009 23:59:10 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=212</guid>
		<description><![CDATA[Another long stretch of inactivity.. So, I figured I&#8217;d throw out a post about what I&#8217;ve been up to, and what I have coming down the pipeline.
I&#8217;ve been spending a lot of time over the past year working with the iPhone, as my post about ARM VFP might suggest. In doing so, I&#8217;ve been putting [...]]]></description>
			<content:encoded><![CDATA[<p>Another long stretch of inactivity.. So, I figured I&#8217;d throw out a post about what I&#8217;ve been up to, and what I have coming down the pipeline.</p>
<p>I&#8217;ve been spending a lot of time over the past year working with the iPhone, as my post about ARM VFP might suggest. In doing so, I&#8217;ve been putting together a fairly basic 2D game engine that I&#8217;m hoping to use for some game ideas I have. I&#8217;ve also been polishing my Cocoa skills so that I can bring a decent set of design tools to the table for creating the content for my games.</p>
<p>During all of this, I&#8217;ve been creating new blog posts describing a few of the things that I&#8217;m exploring, and developing. Unfortunately, all of these posts have remained drafts since I haven&#8217;t taken the time to polish them and commit them to the interwebs.  So, I&#8217;ll be taking a little time over the next week or so, to cross my &#8216;T&#8217;s and dot my &#8216;i&#8217;s in those posts and hopefully bring some interesting content to this site, for once. ;)</p>
<p>Here are some of the topics I&#8217;m writing about:</p>
<ul>
<li>Dynamic Shadowing in 2D</li>
<li>Integrating Lua into an Objective-C based Game Engine.</li>
<li>More ARM VFP Code.</li>
<li>Grid-based Mesh Decimation &#8211; A fun one that I worked on over a year ago, for very simple 3D terrain optimization.</li>
</ul>
<p>There&#8217;s more on the plate as well.. but that will come in time. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=212</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ARM VFP: Part 1 &#8211; GCC Inline Assembler</title>
		<link>http://richard.giliam.net/?p=20</link>
		<comments>http://richard.giliam.net/?p=20#comments</comments>
		<pubDate>Wed, 28 Jan 2009 19:19:58 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[Asm]]></category>
		<category><![CDATA[C/C++/Obj-C]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[VFP]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=20</guid>
		<description><![CDATA[So you&#8217;ve gotten your feet wet developing for the iPhone, learned XCode if you didn&#8217;t already know it, and now you&#8217;re looking for a way to speed up your matrix and vector operations so you can write some wicked OpenGL ES apps.  Luckily, the iPhone and iPod Touch have a Vector Floating-point (VFP) co-processor, which [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve gotten your feet wet developing for the iPhone, learned XCode if you didn&#8217;t already know it, and now you&#8217;re looking for a way to speed up your matrix and vector operations so you can write some wicked OpenGL ES apps.  Luckily, the iPhone and iPod Touch have a Vector Floating-point (VFP) co-processor, which allows you to perform multiple (up to 8) simultaneous floating point operations with one instruction.</p>
<p>Chances are however, that you already knew that, why else would you be reading a blog post about the ARM VFP? So, I might as well get to the point eh?</p>
<h3>GCC Inline Assembly</h3>
<p>As you, more than likely, know GCC is the compiler used by XCode when developing for the iPhone. As such, it is GCC&#8217;s syntax we must abide by in order to combine the powers of either Objective-C or C++, with the assembly we&#8217;ll need to utilize the VFP co-processor.</p>
<p>Telling GCC that we&#8217;d like to provide some assembly to be mixed in with our C/C++ code is pretty straight forward. Utilizing the <strong>asm</strong> statement we can provide GCC with a string representing all of the instructions that we&#8217;d like to inject into the compiler&#8217;s output. So, we might do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">asm <span style="color: #0000ff;">volatile</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;bkpt 0x0001<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Well, we probably wouldn&#8217;t, but that&#8217;s a quick and easy example.  Notice the <strong>&#8220;\n\t&#8221;</strong> at the end of the instruction.  Since GCC is going to output our asm right into the asm that it is already generating when it compiles our C/C++ code, we need the asm instruction to play nice with the surrounding assembly.  So, we clear the end of line, and tab in to put it inline with the other instructions.  Keep that in mind if you ever see an error such as:</p>
<blockquote><p>garbage following instruction &#8212; `mov r0, r1mov r2, r1&#8242;</p></blockquote>
<p>The other thing to notice, is the keyword <em><strong>volatile</strong></em>, which tells GCC to leave your asm block right where it is, and don&#8217;t attempt any optimizations.</p>
<p>In other compilers that I&#8217;ve used, inline assembly is treated by the compiler as a &#8220;You&#8217;re on your own&#8221; block of code, requiring you to sanitize all of the registers so you don&#8217;t accidently step on the compiler&#8217;s toes.  GCC however, has what is called &#8220;extended inline assembly&#8221; which allows you to tell GCC what registers you&#8217;re going to use, and even provide GCC with enough information that it can choose registers for you and incorporate your assembly into it&#8217;s optimizations.</p>
<p>For example, if I wanted to write a quick assembly routine to add two integer values using ARM assembly I might do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">int</span> add2f<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
   asm <span style="color: #0000ff;">volatile</span> <span style="color: #008000;">&#40;</span>
      <span style="color: #FF0000;">&quot;add  %0, %1, %2     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>
      <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;=r&quot;</span><span style="color: #008000;">&#40;</span>ret<span style="color: #008000;">&#41;</span>       <span style="color: #666666;">// Output registers</span>
      <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span>  <span style="color: #666666;">// Input registers</span>
      <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;r0&quot;</span>, <span style="color: #FF0000;">&quot;r1&quot;</span>      <span style="color: #666666;">// Clobber List</span>
   <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">return</span> ret<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see from the above code, there are 3 trailing statements after the actual assembly instruction template.  These trailing statements (labeled: Output registers, Input registers, and clobber list) are what tell the compiler your intentions for register usage.</p>
<p>The output registers section allows us to tell GCC what our requirements are for using registers to bring values from the assembly, back into our C/C++ code.  In the above example I&#8217;m telling GCC that I need one register, and the value in the register needs to be accessible from the local variable <strong>ret</strong> after the assembly block is done.  You&#8217;ll notice, the syntax &#8212; <strong>&#8220;=r&#8221;(ret)</strong> &#8212; the <strong>&#8220;=&#8221;</strong> tells GCC that the register is going to be written to.  The &#8220;r&#8221; tells GCC that we need a general purpose register.  There are a few different options that can be used here, such as &#8220;f&#8221; for a floating point register, but for our purposes the majority of the time &#8220;r&#8221; is all we need.</p>
<p>Likewise, the input section allows us to tell GCC what our register requirements are for bringing values from our C/C++ code into the assembly block.  So in the above example again, I&#8217;m telling GCC that I need two registers, one to hold the value of &#8220;<strong>a</strong>&#8220;, and the other for &#8220;<strong>b</strong>&#8220;, both being general registers.</p>
<p>These input and output statements are known as constraints, and when you define them it allows GCC to provide you with registers that won&#8217;t interfere with what GCC is doing with the rest of your code.  As such, you&#8217;ll need a method for GCC to tell you what registers it has chosen, and this is done via the assembly template.</p>
<p>The assembly template is the actual assembly code string that precedes the constraint statements. You&#8217;ll notice in the above example I have the statement, <code>mov r0, %1          \n\t</code> The %1 signifies the second requested register in the constraint list.  So, using the above example, %0 represents the output register, %1 represents &#8216;a&#8217;, and %2 represents &#8216;b&#8217;.</p>
<p>Finally, after the input/output constraint lists, is what is known as the clobber list.  This is a list of registers that your assembly simply, clobbers, without worrying about GCC&#8217;s usage for them.  By telling GCC that you plan to destroy the values in these registers, GCC can make preparations for saving them, or simply not using their values after your assembly block.</p>
<p>Along with registers if your assembly also changes values in memory you can specify &#8220;memory&#8221; in the clobber list, so that GCC knows to re-fetch any values from memory before proceeding.</p>
<hr width="50%" />
That is GCC&#8217;s extended inline assembly in a nutshell.  The next part of this series will begin working with the ARM VFP co-processor. Feel free to provide any feedback via comments or twitter (nirosys), this was a pretty basic intro so I may not have gone into enough detail with some things.</p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=20</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iPhone: Accessing the Simulator&#8217;s File System</title>
		<link>http://richard.giliam.net/?p=13</link>
		<comments>http://richard.giliam.net/?p=13#comments</comments>
		<pubDate>Tue, 16 Dec 2008 05:12:12 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=13</guid>
		<description><![CDATA[Anyone working with the iPhone SDK has probably used the iPhone Simulator. While generally it is pretty speedy when pushing new applications to it, or making minor changes to code, and re-pushing your application for testing, there are some times that you may wish to update something non-code related. Like a simple text file, that [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone working with the iPhone SDK has probably used the iPhone Simulator. While generally it is pretty speedy when pushing new applications to it, or making minor changes to code, and re-pushing your application for testing, there are some times that you may wish to update something non-code related. Like a simple text file, that your application might read occasionally.</p>
<p>Something like this might not even require an app restart. Such as modifying a database table that is re-read every time a certain screen is accessed.</p>
<p>For tasks like this, it may be easier to just modify the file in question on the simulator&#8217;s &#8220;File system&#8221; for real-time changes.  Luckily, such things are possible.</p>
<p>When you run the simulator, it creates a directory under your home directory located at:</p>
<blockquote><p>~/Library/Application Support/iPhone Simulator/</p></blockquote>
<p>When you install an application onto the simulator, it creates a sandbox for the application in a path that looks something like this:</p>
<blockquote><p>~/Library/Application Support/iPhone Simulator/user/Applications/&lt;GUID&gt;</p></blockquote>
<p>Where &lt;GUID&gt; is a directory with the name being a GUID assigned to the application during the installation process.  The GUID isn&#8217;t particularly necessary to find for you application, and the path to your application&#8217;s sandbox can be found with a quick display of command-line foo. From the <em>~/Library/Application Support/iPhone Simulator/User/Applications/</em> directory, run:</p>
<blockquote><p>neuron:Applications glitch$ <strong>find ./ -name &#8220;SQLiteBooks.app&#8221;</strong><br />
.//9A08D2DD-58CE-4878-9D75-BE4F0B5E511D/SQLiteBooks.app</p></blockquote>
<div>And, voila. The returned path is the path is the path to the application&#8217;s bundle, and while you can access the &#8220;private&#8221; data for your app inside that directory generally you&#8217;ll want to go to the <em>Documents</em> directory inside the GUID directory.  So, if we go to:</div>
<blockquote>
<div>
<div>neuron:Applications glitch$ <strong>cd 9A08D2DD-58CE-4878-9D75-BE4F0B5E511D/Documents/</strong></div>
<div>neuron:Documents glitch$ ls</div>
<div>bookdb.sql</div>
<div>neuron:Documents glitch$ </div>
</div>
</blockquote>
<p>A well behaved app will copy any data that it will be modifying into the <em>Documents</em> directory so that it can be modified without invalidating the application&#8217;s digital signature by writing to a file in the bundle&#8217;s directory. So, from here we can freely modify any data that your app will modify and test the changes either while the app is running, or with a quick restart from the iPhone Simulator.</p>
<p>Another use for accessing the files directly, is to test user supplied data for tracking down bugs. Or, in the case of the SQLiteBooks demo, copying a new database that contains many many books, to test performance. Having access to the files directly, lets you swap out new files easily, without having to play around with your XCode project.</p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=13</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Obligatory &#8220;starting over&#8221; post.</title>
		<link>http://richard.giliam.net/?p=4</link>
		<comments>http://richard.giliam.net/?p=4#comments</comments>
		<pubDate>Wed, 12 Nov 2008 02:32:52 +0000</pubDate>
		<dc:creator>Glitch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://richard.giliam.net/?p=4</guid>
		<description><![CDATA[I tend to do this with my blog every so often (once a year it seems).  This time was not exactly due to me falling out of interest with it, and then attempting to rekindle that interest.  Instead, this &#8220;do over&#8221; was triggered by me wanting to use WordPress, and being too disinterested in my [...]]]></description>
			<content:encoded><![CDATA[<p>I tend to do this with my blog every so often (once a year it seems).  This time was not exactly due to me falling out of interest with it, and then attempting to rekindle that interest.  Instead, this &#8220;do over&#8221; was triggered by me wanting to use WordPress, and being too disinterested in my old posts to bring them over to this new WordPress install.</p>
<p>I&#8217;ve been a <a href="http://www.b2evolution.net">b2evolution</a> user since the early beginnings, and am now starting to lean towards WordPress after using it in another site I&#8217;m getting ready to unleash onto the world.</p>
<p>So my apologies to the 2 or 3 people out there who subscribed to my RSS feed. ;) It won&#8217;t happen again.</p>
]]></content:encoded>
			<wfw:commentRss>http://richard.giliam.net/?feed=rss2&amp;p=4</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
