Welcome to "Getting started with XNA and the Zune. (Pt. 2)". In "Getting started with XNA and the Zune (Pt. 1)," readers were introduced to the software packages they require to develop game and media applications for the Microsoft Zune media device using Microsoft XNA Game Studio 3.0 CTP.
In this article readers will be exposed to the default "Zune Game (3.0)" project template provided by the XNA Game Studio 3.0 CTP and walked through each of the methods provided by this project template to give developers information as to how these method are used to kick their start game development using XNA Game Studio 3.0 CTP and the XNA Framework.
Description: Introduction to XNA on the Zune (Pt. 2)
Target Audience: Novice developer
Keywords: XNA, Windows, Zune, Game Studio
Requirements: Visual C# Express 2008, XNA Game Studio 3.0 CTP, Zune (Any model)
Getting started with Microsoft Game Studio 3.0 CTP
Before readers can create their first Zune game using the XNA Game Studio 3.0 CTP they will need to prepare their systems first. If you have not already done so, or are unsure if you have installed the required software then readers are encouraged to have a look at the first article in the "Getting Started with XNA and the Zune (Pt. 1)" before continuing.
Now that we have met all the software requirements needed to write games for the Zune it is time to have more detailed look at the "Zune Game (3.0)" project template provided by XNA Game Studio 3.0 CTP. Please not that The topics discussed throughout this article are focused towards the development of game and media applications on the Zune hardware platform specifically and are not directly intended for use with other XNA Game Studio versions even though they may apply.
To get started readers need to create a new Microsoft Game Studio 3.0 CTP project that they will use to build their game or media application. To do so we need start the development environment by clicking, Start | Programs | Microsoft XNA Game Studio 3.0 (CTP) | Microsoft Visual C# 2008 Express Edition.
Once the development environment has successfully started the next step is to create a new Zune game project by clicking, File | New Project and selecting the "Zune Game (3.0)" project template from the list of available templates. Since this is a basic introduction, the default name of ZuneGame1 will be used and left unchanged. To finish the process simply click the Ok button on the New Project dialog to have the new Zune game project created.
What are these files and folders?
Once completed the "Zune Game (3.0)" project template will have automatically created the ZuneGame1 solution and ZuneGame1 game project that are used to hold source and media file specific to the game project. Each new Zune project will contain the following folders and files:
References(Folder)
Used to specify the library objects that will be used throughout the project.
Content(Folder)
Specialized folder used to hold game assets such as graphics, audio, 3D models and any custom content files.
Game1.cs(File)
Main game source file containing the game class and all support methods needed to execute the application.
GameThumbnail.png(File)
Image file used by the Zune hardware when displaying this game on the list of deployed games.
Program.cs(File)
Source file responsible for starting the custom game class defined in the Game1.cs.
What is all this code for and how does it help me?
For readers that are new to game development using XNA Game Studio 3.0 CTP all the new source code that has just been automatically created may be a little overwhelming at first. Not to worry as this code has been generated for you to help simplify the job of creating games..
The Game1.cs source file is where most developers new to XNA Game Studio 3.0 CTP will spend a good portion of their time and is where all the methods that will be discussed throughout this article can be located. Any remaining files will not be required by this article and will not be covered any further.
Since the project file was created using a Visual C# project template any XNA Framework references have already been added to the project and much of the work has been performed for us we will look into the source code contained within the Game1.cs file.
XNA Game Studio 3.0 CTP Assemblies.
Most XNA Game Studio 3.0 CTP projects will require the use of one or more of the following XNA Framework Assemblies if they intend on using any of the features of the XNA Framework from within their Microsoft Visual C# 2008 Express application projects.
using Microsoft.Xna.Framework;
This assembly is required by any application project that will use XNA Game Studio 3.0 CTP Framework features and classes.
using Microsoft.Xna.Framework.Audio;
This assembly is required by any application project that will use the XNA Game Studio 3.0 CTP Framework audio features to render audio and sound effects.
using Microsoft.Xna.Framework.Content;
This assembly is required by any application project that will use XNA Game Studio 3.0 CTP Content Processor features like the built in content pipeline for loading known media file formats or the creation of custom content pipeline processors to handle the loading and processing of custom user formats.
using Microsoft.Xna.Framework.Graphics;
This assembly is required by any application project that will use the XNA Game Studio 3.0 CTP graphics features like Vector2, Vector3 and Texture2D variable types.
using Microsoft.Xna.Framework.Input;
This assembly is required by any application project that will use the XNA Game Studio 3.0 CTP input features like GamePad(s), Mouse, and Keyboard.
using Microsoft.Xna.Framework.Media;
This assembly is required by any application project that will use the XNA Game Studio 3.0 CTP media features specific to handling audio media on the Zune media device.
using Microsoft.Xna.Framework.Storage;
This assembly is required by any application project that will use the XNA Game Studio 3.0 CTP storage features or require access to the underlying file Zune device file system. Please note that very limited access to the Zune devices file system is provided for security reasons.
Breaking down the Game1 Class.
All "Zune Game (3.0)" based projects will have a game class that has been derived from the Microsoft.Xna.Framework.Game namespace and is contained within the Microsoft.Xna.Framework assembly as described above and illustrated by the following code snippet.
public class Game1 : Microsoft.Xna.Framework.Game
{
// Game members and methods.
}
Now that the Game1 class has been created and derived from the Microsoft.Xna.Framework.Game object the Zune Game (3.0) project template will generate two member fields. The first field derived from GraphicsDeviceManager named graphics, which provides access to the graphics device itself and some of the exposed properties allowing changes such as the modification of display resolution settings and changes to other display device operating modes. The second field derived from SpriteBatch named conveniently spriteBatch is responsible for rendering functionality such as the manipulation of render modes, the rendering of text and the rendering of images and textures to the display area. The following code snippet demonstrates the graphics and spriteBatch fields generated automatically when creating a project using the Zune Game (3.0) project template.
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
The Game1 class construction method is where code for such things as Component creation and initialization takes place. We will discuss game components and how they impact XNA game development in a follow-up tutorial. The Zune Game (3.0) project template takes care of initializing the graphics member field for us use by the rest of the project. To make life more simple the Zune Game (3.0) project template will also initialize the root or base content folder for you as demonstrated by the following code snippet.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory("Content");
}
The Initialize method of the Game1 class is responsible for performing any application specific variable and object creation needed by the XNA game application itself. The following code snippet illustrated the basic Initialize method as created by the Zune Game (3.0) project template.
protected override void Intialize()
{
base.Initialize();
}
One of the more important methods created by the Zune Game (3.0) project template is the LoadContent method. This is the method responsible for the importing or loading of audio and graphics data into a Zune Game (3.0) project that are later rendered and played in combination creating a XNA game application.
In the next tutorial in the series we will go into much greater detail in regards to the LoadContent method for now here is a a code snippet of the default LoadContent method as created by the Zune Game (3.0) project template and the initialization of the spriteBatch class member described previously..
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
Opposite to the LoadContent method, the UnloadContent method does as its name implies and gives the XNA developer a location to de-allocate any resources that are currently being manually controlled or managed. The following code snippet illustrates the default UnloadContent method as provided by the Zune Game (3.0) project template.
protected override void UnloadContent()
{
}
The next two methods could be considered the most important methods created by the Zune Game (3.0) game template and are the base methods from which all XNA Game Studio projects or games build from. These two methods are called by the XNA framework in a loop updating and processing game AI, visual, and overall game logic in the Update method, and rendering or drawing any graphics to the display screen using the Draw method.
The first method we will discuss is the Update method. This method will be where the bulk of your game code will reside and is called every cycle (30 times a second on second generation Zune devices, and 60 times a second on older 1st generation devices) to satisfy any game level logic including game play. The following code snippet illustrates the Update method created by the Zune Game (3.0) project template and is populated with code that allows users to exit from the running XNA game application by pressing the Back button on the Zune device as demonstrated.
protected override void Update(GameTime gameTime)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
Last we will discuss the Draw method. This method similar to the Update method will be where the bulk of code that deals specifically with the rendering of images to the display screen on the Zune device. The following code snippet illustrates the Draw method created by the Zune Game (3.0) project template and is populated with code that clears the background of the Zune device to CornFlowerBlue.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
That covers the methods and member variables that are created for you by the display screen Zune Game (3.0) project template for windows as well as for the Zune media device. They have been created to help simplify the game development process helping the developer spend more focus on the game being developed opposed to the wiring underneath.
What's next?
Next in the series we will cover the rendering of sprites to the Zune media device and take a look at how those sprites can be moved around the display screen giving the impression of their bouncing off of the screen edges.
By Matthew Randall. (Exhale Game Studio)