Saturday, May 9, 2009

Integrating 360 code into a PC project

In the previous post I discussed the code required to make use of a cursor with the Xbox 360.  Today we will discuss specifying the difference between code for the PC and the 360 version of your game.  If you are in the same boat as me and are working on both a PC and 360 version of your game at the same time, you will need to differentiate the two sets of code from each other.

First of all make sure you have two projects – one for PC and one for Xbox.  Depending on which project type you started out with, there will be an option to create the alternative by navigating to Project –> Create Copy of [Project Name] for [Desired Platform].

The commands for splitting up the code are simple:

#if WINDOWS

     //Execute Windows Specific code here

#endif

#if XBOX

     //Execute similar Xbox specific code here

#endif

You will notice one or the other will appear grayed out.  If you opened the file you are currently working on from underneath the Windows project, the Xbox code will be grayed out, and visa versa.  Note: Intellisense will be disabled for the grayed out text as well.

I use the #if statements where clicks from an input device are recognized, and in my cursor class.  Instead of getting the current mouse positions, I had to use the GamePad input gathered with code discussed in the previous post.

Check out a working example in Update(); of the cursor class:

#if WINDOWS
       mouseState = Mouse.GetState();
       this.position = new Vector2(mouseState.X, mouseState.Y);       
#endif

#if XBOX
      this.position = new Vector2(Game.xbCursorX, Game.xbCursorY);       
#endif

As you can see, Update(); will either draw the cursor relative to the mouse pointer’s position if built for Windows, and relative to the input we gathered from the user’s GamePad is built for the Xbox.

Using these #if statements is how I intend to go about solving my graphics dilemma when I begin to tackle that issue.

Try copying some existing code in your project and modifying it a bit.  Add the different #if statements around it, and then build and run the code on both Windows and the Xbox.  You will see that it runs according to the commands you have provided.

Have you used these statements before?  I’m curious…besides input, what other type of code needs to be differentiated between Windows and Xbox? Are there more #if statements to be found?  Leave your  comments below.

No comments:

Post a Comment