Thursday, February 23, 2012

MonoGame: Getting Started 3/3

Now let us create an Android project for the game. To start the project the following files are need:
  • Activity class file
  • Manifest file
  • Project file
  • Solution file
  • Icon file
In Android each app needs to have at least one Activity class. Therefore create a file inside your Android directory with the name Activity1.cs and the following content:
using Android.App;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Content.PM;

namespace MyNamespace
    [Activity(Label = "MyGame Label", 
       MainLauncher = true, 
       Icon = "@drawable/icon", 
       ScreenOrientation = ScreenOrientation.Portrait,
       ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden, 
    public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
        protected override void OnCreate (Bundle bundle)
            base.OnCreate (bundle);
            MyGame.Activity = this;
            var game = new MyGame ();
            FrameLayout fl = new FrameLayout(this);
            SetContentView (fl);            

            game.Run ();            
Inside this source change the MyNamespace and MyGame to fit your project. Next we need a manifest file. For that create a directory Properties and inside create the file AndroidManifest.xml with this content:

Before saving it you have to change the package tag to your game name and application label. The manifest file is the configuration file for your Android app. E.g. the minSdkVersion 8 means the Android API 8, which is Android 2.2. So this manifest file supports devices from version 2.2 upwards. Also the permissions of what features your application uses are defined in this file. Please read the Android docs to find out more about the manifest file:

Now go back to your Android source directory and create the project file with the following content:
You have to change the RootNamespace and AssemblyName tag to match your project. Also check if the ProjectReference tag of MonoGame and Lidgren point to your local installation path.
Now create the solution file for this project MyGame.sln and copy this content:
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network.Android", "..\..\MonoGame\ThirdParty\Lidgren.Network\Lidgren.Network.Android.csproj", "{565129E0-4EE5-4F6F-B403-C3484C9740BE}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Framework.Android", "..\..\MonoGame\MonoGame.Framework\MonoGame.Framework.Android.csproj", "{BA9476CF-99BA-4D03-92F2-73D2C5E58883}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9C06E528-B9C7-4115-81C1-0E572103B242}"
 GlobalSection(SolutionConfigurationPlatforms) = preSolution
  Debug|Any CPU = Debug|Any CPU
  Release|Any CPU = Release|Any CPU
 GlobalSection(ProjectConfigurationPlatforms) = postSolution
  {565129E0-4EE5-4F6F-B403-C3484C9740BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  {565129E0-4EE5-4F6F-B403-C3484C9740BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
  {565129E0-4EE5-4F6F-B403-C3484C9740BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
  {565129E0-4EE5-4F6F-B403-C3484C9740BE}.Release|Any CPU.Build.0 = Release|Any CPU
  {BA9476CF-99BA-4D03-92F2-73D2C5E58883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  {BA9476CF-99BA-4D03-92F2-73D2C5E58883}.Debug|Any CPU.Build.0 = Debug|Any CPU
  {BA9476CF-99BA-4D03-92F2-73D2C5E58883}.Release|Any CPU.ActiveCfg = Release|Any CPU
  {BA9476CF-99BA-4D03-92F2-73D2C5E58883}.Release|Any CPU.Build.0 = Release|Any CPU
  {C04E8EB0-46A8-431F-9C16-E5DA58C1D705}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  {C04E8EB0-46A8-431F-9C16-E5DA58C1D705}.Debug|Any CPU.Build.0 = Debug|Any CPU
  {C04E8EB0-46A8-431F-9C16-E5DA58C1D705}.Release|Any CPU.ActiveCfg = Release|Any CPU
  {C04E8EB0-46A8-431F-9C16-E5DA58C1D705}.Release|Any CPU.Build.0 = Release|Any CPU
  {F10C1F14-64AF-4957-847E-E60490371E84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  {F10C1F14-64AF-4957-847E-E60490371E84}.Debug|Any CPU.Build.0 = Debug|Any CPU
  {F10C1F14-64AF-4957-847E-E60490371E84}.Release|Any CPU.ActiveCfg = Release|Any CPU
  {F10C1F14-64AF-4957-847E-E60490371E84}.Release|Any CPU.Build.0 = Release|Any CPU
  {1C3FA898-56B3-4135-8A92-C876CECB5C60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  {1C3FA898-56B3-4135-8A92-C876CECB5C60}.Debug|Any CPU.Build.0 = Debug|Any CPU
  {1C3FA898-56B3-4135-8A92-C876CECB5C60}.Release|Any CPU.ActiveCfg = Release|Any CPU
  {1C3FA898-56B3-4135-8A92-C876CECB5C60}.Release|Any CPU.Build.0 = Release|Any CPU
 GlobalSection(NestedProjects) = preSolution
 GlobalSection(MonoDevelopProperties) = preSolution
  StartupItem = BulletBabs.Android.csproj
  Policies = $0
  $0.DotNetNamingPolicy = $1
  $1.DirectoryNamespaceAssociation = None
  $1.ResourceNamePolicy = FileFormatDefault
Here again check if the path to the MonoGame and Lidgren project files point to your local MonoGame installation.
The one file remaining now is an icon for your game. For that you should create the folders Resources/drawable and place a png image file with the name Icon.png inside. Make the resolution of this file 96x96 pixel.

Now you are ready to open the solution file in either Visual Studio or MonoGame, depending on your installation option of Mono for Android. First check if the references to the MonoGame and Lidgren project are correct. Then add your game by using add project to add the project file we created before.
If you created and placed all files correctly you should now see all files inside your project map. Again please the if the references to MonoGame are correct.
Now you have to proceed similar as with the iOS project. First you add all your game source files as link to your project. At this point you should then already be able to compile your game. If not, try to find and fix the errors.
Once your game compiles you are ready to add the content files. If you haven't, please read the remarks to that in the iOS part before. On Android all content goes into an Assets folder. Therefore you need to add your content files in a folder Assets/Content. Change the options of each file to "Build Type: AndroidAsset".

Now you are ready to deploy and run your game on the Android emulator. However, the Android emulator is so slow - at least on my computer - that running and debugging is cumbersome. For a first test it should be fine though.
Unfortunately, if you are seriously porting your game to Android, you are not finished her yet. Unlike WP7 and iOS, Android does not take care about your application when it gets suspended and resumed. On Android you have to handle it yourself. The magic word here is Lifecycle Management, you can start getting the idea be reading the official Android docs:
To make things worse, your application's OpenGL context will get invalid once your app is resumed, resulting in broken textures. Hopefully in later versions Mono for Android or MonoGame will take care of that. By the time of writing however, you will have to take care about this yourself. You should check the discussion board of MonoGame on how you could handle it. I am not very satisfied with the hack I am using for my games so far myself.

I hope this blog post is useful to show how fast you can get your XNA game running on iOS and Android. However, if you seriously planning to publish your game on these platforms, you also need to read about and understand their special technical differences and issues.

No comments:

Post a Comment