PlanetX, multiplatform: iPhone development (Part:2) #iPhone #WP7 #Xbox #XNA #Monotouch #XnaTouch

May be this blue screen does not mean much to you, but any XNA developer will be able to recognize the standard Blue XNA background…

Capture

Basically the same code that works for the WP7, work on the iPhone emulator, on Windows and on the Xbox

image

Tomorrow you’ll see sprites popping out of these small screens Smile.

To achieve this, basically you need to setup monodevelop, monotouch, xnatouch and all that jazz, create a windows Xna game from Visual studio 2010, create a copy of the project for WP7 (right click on the windows project)

image

Then you need to take one of the examples that comes with XnaTouch, delete all the files, and add all the files from the windows xna game.

I suggest you to use linked files in order to have a single codebase, and use svn to share the code between your VS2010 Windows instance and your OSX Monodevelop instance.

I’m using http://www.zennaware.com/cornerstone/ as svn client on OSX. I’ve never used a mac before and the way this svn implementation works is a bit strange to me, but might be more familiar to those coming from the MAC. I prefer a million times Visual Svn, integrated with VS, but there’s noting like that for monodevelop on OSX.

Once you’ve done all this, the project will fail to compile on OSX, because the Microsoft.Xna namespace does not exist, so you have to replace it with

 

#if IPHONE
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using XnaTouch;
using XnaTouch.Framework.Media;
#else
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
#endif

 

Basically, this is my Program.cs file

#region Using Statements
using System;
#if IPHONE
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using XnaTouch;
using XnaTouch.Framework.Media;
#else
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
#endif
#endregion

namespace XPlanets
{
#if IPHONE
    [Register ("AppDelegate")]
class Program : UIApplicationDelegate
{
public override void FinishedLaunching (UIApplication app)
{
// Fun begins..
using (XPlanetsGame game = new XPlanetsGame())
            {
                game.Run();
            }

//MediaLibrary lib = new MediaLibrary();
//object result = lib.Playlists;
}

static void Main (string [] args)
{
UIApplication.Main (args,null,"AppDelegate");
}
}
#elif WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (XPlanetsGame game = new XPlanetsGame())
            {
                game.Run();
            }
        }
    }
#endif
}

and this is my XplanetGame file

using System;
using System.Collections.Generic;
using System.Linq;
#if IPHONE
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using XnaTouch;
using XnaTouch.Framework;
using XnaTouch.Framework.Media;
using XnaTouch.Framework.Graphics;
using XnaTouch.Framework.Input;
#else
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endif

namespace XPlanets
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class XPlanetsGame : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public XPlanetsGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

In this way, you have a single codebase, where 90% of the code is exactly the same on all the devices, and you can optimize or customize for a specific device using C# Preprocessor directives.

You’ll see this block many many times:

 

#if IPHONE
//iPhone
#elif XBOX
//XBox
#elif WINDOWS
//Windows
#elif WINDOWS_PHONE
//Windows Phone
#endif
Advertisements

2 thoughts on “PlanetX, multiplatform: iPhone development (Part:2) #iPhone #WP7 #Xbox #XNA #Monotouch #XnaTouch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s