Tuesday, February 21, 2012

MonoGame: Getting Started 2/3

Next we set up the iOS project. Inside the iOS directory create a file called AppDelegate.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

using Microsoft.Xna.Framework;

namespace MyNamespace
{
 // The UIApplicationDelegate for the application. This class is responsible for launching the 
 // User Interface of the application, as well as listening (and optionally responding) to 
 // application events from iOS.
 [Register ("AppDelegate")]
 class Program : MonoGameProgram 
 {
  private MyGame game;

  public override bool FinishedLaunching (UIApplication app, NSDictionary options)
  {
   // Fun begins..
   game = new MyGame();
   MonoGameGame = game;
   game.Run();
   return true;
  }

  static void Main (string [] args)
  {
   UIApplication.Main (args,null,"AppDelegate");
  }
 }
}

Change MyNamespace to your namespace and MyGame to your game class name.
The second file we need is Info.plist. If you are new to Apple, a plist file is a configuration file for the application. Create the file like this:


 MinimumOSVersion
 4.1
 UIDeviceFamily
 
  2
 
 UISupportedInterfaceOrientations
 
  UIInterfaceOrientationPortrait
 



Now we only need a solution and a project file. For the project file, create a text file with the name of your game dot ios dot csproj, e.g. MyGame.ios.csproj and copy this content:


  
    Debug
    iPhoneSimulator
    10.0.0
    2.0
    {6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
    Exe
    MyGame.ios
    MyGameios
  
  
    true
    full
    false
    bin\iPhoneSimulator\Debug
    DEBUG; IOS;
    prompt
    4
    false
    None
    true
  
  
    none
    false
    bin\iPhoneSimulator\Release
    prompt
    4
    false
    None
  
  
    true
    full
    false
    bin\iPhone\Debug
    DEBUG;
    prompt
    4
    false
    true
    iPhone Developer
    
    
  
  
    none
    false
    bin\iPhone\Release
    prompt
    4
    false
    iPhone Developer
  
  
    
    
    
    
    
  
  
    
  
  
    
  
  
  
    
      {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}
      MonoGame.Framework.iOS
    
  
  
    
  

Change the tag RootNamespace and AssemblyName to meet your game name and check the tag ProjectReference for the path to MonoGame project file to point on your disk.
Finally create a solution file e.g. MyGame.ios.sln with this content:
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network.iOS", "..\..\MonoGame\ThirdParty\Lidgren.Network\Lidgren.Network.iOS.csproj", "{734EAA48-F1CA-481A-B391-0285BC0E8B40}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Framework.iOS", "..\..\MonoGame\MonoGame.Framework\MonoGame.Framework.iOS.csproj", "{DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}"
EndProject
Global
 GlobalSection(SolutionConfigurationPlatforms) = preSolution
  Debug|iPhoneSimulator = Debug|iPhoneSimulator
  Release|iPhoneSimulator = Release|iPhoneSimulator
  Debug|iPhone = Debug|iPhone
  Release|iPhone = Release|iPhone
 EndGlobalSection
 GlobalSection(ProjectConfigurationPlatforms) = postSolution
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Debug|iPhone.ActiveCfg = Debug|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Debug|iPhone.Build.0 = Debug|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Release|iPhone.ActiveCfg = Release|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Release|iPhone.Build.0 = Release|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
  {734EAA48-F1CA-481A-B391-0285BC0E8B40}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Debug|iPhone.ActiveCfg = Debug|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Debug|iPhone.Build.0 = Debug|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Release|iPhone.ActiveCfg = Release|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Release|iPhone.Build.0 = Release|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
  {77FF5112-1227-4929-A04C-5B36E87A93D5}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Debug|iPhone.ActiveCfg = Debug|iPhone
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Debug|iPhone.Build.0 = Debug|iPhone
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Release|iPhone.ActiveCfg = Release|iPhone
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Release|iPhone.Build.0 = Release|iPhone
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
  {9D1AD899-582E-407D-A504-AB36062BCA1F}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Debug|iPhone.ActiveCfg = Debug|iPhone
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Debug|iPhone.Build.0 = Debug|iPhone
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Release|iPhone.ActiveCfg = Release|iPhone
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Release|iPhone.Build.0 = Release|iPhone
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
  {DB8508BB-9849-4CC2-BC0F-8EB5DACB3C47}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
 EndGlobalSection
EndGlobal

Again change the path of the MonoGame project and the Lidgren.Network project to match your local installation.
Next open the solution with MonoDevelop. If you edited the path correctly you should have the MonoGame and Lidgren project there. If you entered the path wrong than just add them with add project.
Now use add project to add the project file of your game that you created before. Here also check that the reference to MonoGame is correct. So far you should only have two files in the project. The AppDelegate.cs and the Info.plist.
Now add the source code of your game to the project. Add each of your source files, except for the Program.cs. Add the source files as link, so you will use the same files for all platforms and not copies.
If everything worked fine, you should now be able to compile the source code. If you run across code that won't compile with MonoGame, you should use defines for each platform as work around.
The last step that remains is adding your content to the project. Obviously you don't have the content pipeline from the original XNA in MonoGame. But that is no problem as most content types can be read by MonoGame directly without being pre-processed. If you created the project file as described before, you already should have a Content folder inside your project. Now add all your content from the WP7 content folder there. As with the source code, you should add them as link to prevent them from being copied.
The only content file I ran across so far that is not supported, is .spritefont. To add this type of content, you have to take the compiled file from the bin/Content directory of your WP7 folder. The compiled file has the ending .xnb. Just copy the fontname.xnb to your MyGame.ios/Content folder and add this in the project instead of the original spritefont file.
For each content file in your project you have to set the option "Build action Content". Also be aware, that iOS, unlike Windows, is case sensitive to your content file names. For example, if your Content folder is upper case but in your source code you reference to lower case, you will end up in an exception at runtime.
Now you should be able to compile your game and test it on the emulator. On my MacMini the emulator runs rather slow. With an actual device, deploying, debugging and running is much better.


No comments:

Post a Comment