A blog post about iPhone game programming by a .NET developer might seem odd at first sight. However, when I´m not working with .NET I usually work with the some of my game projects. And one of my favourite platforms for developing games is of course the iPhone. But it´s not the only preferred platform, I also have some experience with Microsoft´s XNA. I just haven´t really tested it on the Windows Phone yet. But one of the things I really liked with XNA was how easy it was for me to get started. Since I already had a .NET background and knowledge of the basics behind game programming it didn´t take me very long before I had a simple game up and running. Unfortunately my first games on the iphone didn´t turn out that well. In fact, they never got finished. And there where two majors factors involved, learning a new language and my (lack of) experience with OpenGL. The latter was solved by using the Sparrow Framework, an open source game engine for iOS. But when it comes to learning a new language there are no shortcuts. It took me some time, and a couple of unfinished games, but these days I really feel comfortable with Objective C. So if you´re in a similar situation and having problems with your first game, is there a way to skip all the initial problems? The answer is maybe. I really don´t know, but I can try to find out. Duane Wandless recently released a Sparrow C# Wrapper for MonoTouch. Simply put this means that you could use all the advantages you get from the Sparrow Framework, but still write your games with C#. The rest of my post will deal with setting up MonoTouch from scratch and getting the Sparrow test project running. Everything I try out will be done on a MacBook Pro with OS X Snow Leopard.
We´ll start by downloading everything needed. Download and install the following four components in order:
Only registered Apple developers have access to the iOS SDK. So in order to download from the first link you have to join the iOS Developer Program. Another note is about the MonoTouch SDK. I downloaded the trial version while writing this post. The evaluation version of MonoTouch does not expire, but only enables development and testing against the iPhone Simulator.
Once everything is installed, it´s time to download the Sparrow C# Wrapper. A test project is included in the package, so start by opening the solution file with MonoDevelop and compile.
Building the test project
My first attempt to compile the project failed with the following error:
mtouch failed with no output
After a quick search with Google I found a solution to my first problem. My project had the compiler target set to x86 which resulted in an error. Setting it back to Any CPU solved the error but the build still failed. This time the error was:
The type or namespace name `SPView’ could not be found. Are you missing a using directive or an assembly reference?
The second error doesn´t seem as cryptic as the first one. It´s also an error warned about in the instructions.
Whenever MonoDevelop regenerates the MainWindowIPhone.xib.designer.cs file you will get a compile error: MainWindowIPhone.xib.designer.cs(25,25): Error CS0246: The type or namespace name `SPView’ could not be found. Are you missing a using directive or an assembly reference? (CS0246) (SparrowTest) To fix this do a compile, manually edit that file, add a using Sparrow, compile again. How the XIB is definied MD cannot resolve the reference to the SPView class.
So I just added
in the auto generated MainWindowIPhone.xib.designer.cs file and hit the compile button again. This time the build was successful! Once the first compilation was finished a second compile automatically started in order to compile to native code. Now it´s time to try the test app on the iPhone Simulator.
Since I use the evaluation version of MonoTouch the only option for testing against is the iPhone Simulator. Choosing the Debug|iPhoneSimulator option and pressing Run starts the simulator and loads the app. Unfortunately, a successful build does not guarantee a working app. The third error I encountered was a run time error:
System.MissingMethodException has been thrown “Method not found:
Another quick google search resulted with a thread at Stackoverflow. This seemed to be a common error after updating MonoTouch to version 4.0 (I downloaded the latest version). The proposed solution was to regenerate sparrow.dll with btouch for MonoTouch 4. This was easier said then done. Luckily, the original poster of the thread solved the problem and described the answer in two steps. Open the terminal and enter the following:
/Developer/MonoTouch/usr/bin/btouch sparrow.cs -s:enum.cs –outdir=gen -ns=Sparrow –unsafe –sourceonly=genfiles
Do the second step once the first build is complete:
/Developer/MonoTouch/usr/bin/smcs -out:sparrow.dll `cat genfiles` extensions.cs -unsafe -r:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll enum.cs -target:library
We should now have a new sparrow.dll that works with MonoTouch 4. Let´s try running the app again.
And there you have it! Once it´s finished loading it should look something like the picture above. The setup is now complete and we can start working on are first iPhone game written entirely in C#.
The setup didn´t go as smoothly as I first expected and it took me a while before I had the test application running on the simulator. But this post will hopefully provide help for anyone else who is experiencing the same problems. All there is left to do now is the actual game programming. So try it out and see what you can accomplish. If you need any further information, please visit MonoTouch and Sparrow.