XBOX 360 E User Guide
Have a look at the manual XBOX 360 E User Guide online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 11 XBOX manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
The Complete Example 166 Xbox 360™Han dbook #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion public class Game1 : Microsoft.Xna.Framework.Game {GraphicsDeviceManager graphics; ContentManager content; public Game1() {graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { base.Initialize(); } //this is a texture we can render Texture2D myTexture; //coordinates to draw the sprite at Vector2 spritePosition = Vector2.Zero; //this is the object that will draw the sprites SpriteBatch spriteBatch; protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
167 Welcome to XNA™ {myTexture = content.Load(“mytexture”); spriteBatch = new SpriteBatch(graphics.GraphicsDevice); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) {content.Unload(); } } //store some info about the sprite’s motion Vector2 spriteSpeed = new Vector2(50.0f, 50.0f); protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit(); //move the sprite around UpdateSprite(gameTime); base.Update(gameTime); } void UpdateSprite(GameTime gameTime) { //move the sprite by speed, scaled by elapsed time spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; int MaxX = graphics.GraphicsDevice.Viewport.Width—myTexture.Width; int MinX = 0; int MaxY = graphics.GraphicsDevice.Viewport.Height—myTexture.Height; int MinY = 0; //check for bounce if (spritePosition.X > MaxX) C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
168 Xbox 360™Han dbook {spriteSpeed.X *= -1; spritePosition.X = MaxX; } else if (spritePosition.X < MinX) { spriteSpeed.X *= -1; spritePosition.X = MinX; } if (spritePosition.Y > MaxY) { spriteSpeed.Y *= -1; spritePosition.Y = MaxY; } else if (spritePosition.Y < MinY) { spriteSpeed.Y *= -1; spritePosition.Y = MinY; } } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //draw our sprite spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.Draw(myTexture, spritePosition, Color.White); spriteBatch.End(); base.Draw(gameTime); } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
GOING BEYOND—3-D MODELS Introduction In the “Your First Game: Microsoft XNA Game Studio Express in 2-D” section, you saw a simple example that used the XNA Framework Content Pipeline to load a sprite (represented by a Texture2D object) and that used the XNA Framework APIs to draw it on the screen. This tutorial goes beyond that simple sample and introduces you to many concepts that XNA Game Studio Express makes easy, so you can focus on creating fun, interactive games.This first tutorial introduces you to the Content Pipeline in a little more detail and some of the XNA Framework API calls you’ll use to draw 3-D objects on the screen. When you complete this tutorial, you’ll have a 3-D model with textures and lighting. Let’s get started! Step 1: Create a Spacewar Project Before you start coding, you’ll need some art assets to play around with. In this case, we want a 3-D model and an associated texture file so that the model has some detail. These assets are loaded into your game using the XNA Framework Content Pipeline, which is a feature built right into the Visual C# 2005 Express Edition user interface. With XNA Game Studio Express, there is a rich source of art right at your fingertips, in the Spacewar Starter Kit. We want only the art, not all the prebuilt code for the Spacewar game, so your first step is to isolate the art. Let’s begin by creating a Spacewar Starter Kit project. > On your Windows computer, open the Start menu and navigate to All Programs, then Microsoft XNA Game Studio Express folder, and then XNA Game Studio Express. > Visual C# 2005 Express Edition launches. When it appears, click the File menu, then click New Project. > From the list of templates that appears, click on either Spacewar Windows Starter Kit or Spacewar Xbox 360 Starter Kit, depending on whether you’re developing on the Xbox 360 or Windows. Either way, the art assets are the same. > Type a path that you can remember into the Location field—you’ll be copying art from this location. > Leave the rest of the fields at their default values. The dialog box should look similar to the screen below. > Click OK. The Spacewar project is populated at the path you typed into the Location field. Once the project code opens on the screen, click the File menu, and then click Close Solution. We don’t need the Spacewar solution anymore. 169 Welcome to XNA™ Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Step 2: Create a New Project and Use the Spacewar Model Now that you have the art assets, let’s create the actual code project that you’ll be writing:> Click the File menu, then click New Project... to create a new project. > From the list of templates that appears, click either Windows Game or Xbox 360 Game, depending on whether you’re developing on the Xbox 360 or Windows. If you develop for Xbox 360, be sure you have a subscription to the XNA Creators Club; otherwise, you will not be able to play your game! > Type a name for your game into the Name field, and in the Location field, type the path to where you want the game files stored. > Click OK. The code for your new game appears. The project already contains many of the methods that are needed to start and run a game. Right now, however, we need to make sure our art assets are being loaded; then we can modify the game to display them on the screen. Let’s get some art into our project: > Make sure you can see the Solution Explorer for your project on the window’s right side. If you cannot see it, click the View menu, and then click Solution Explorer. When it appears, you see files associated with your project in a tree structure. > In the Solution Explorer, right-click on the Project icon (one level below the Solution icon), click Add, and then click New Folder. Name this folder “Content.” This is the root folder for our art. We must add two more folders underneath this one. > Right-click on the Content folder you just created, click Add, and then click New Folder. This creates a new folder underneath Content. Name this folder “Models.” > Repeat the last step, creating a new folder under Content. This time, call the folder “Textures.” Your project structure should look similar to the screen below. We need two pieces of art. The first is the 3-D model that goes into this new Content\Models folder; the second is a texture that is drawn on the 3-D model and is in the Content\Textures folder. The files we need are back in the path you placed the Spacewar project in. Let’s go get them: > Right-click on the Models folder in the Solution Explorer, click Add, and then click Existing Item. Using the dialog box that appears, browse to the path you placed the Spacewar project in, and find the Contents\Models folder. Select p1_wedge.fbx. If you can’t see any files, change the Files of Type Selection field to read Content Pipeline Files. Click OK. > Now, copy the textures associated with the model into the Textures folder. To do this, open Windows Explorer and browse to the Spacewar project path and the Content\Textures folder. Copy wedge_p1_diff_v1.tga, and then browse to your project folder, and then into the Content\Textures folder. Paste in the .tga you just copied. Your project structure should now look similar to the screen at the top of the next page. 170 Xbox 360™Han dbook Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Note that you don’t see the texture you added in the Solution Explorer. When you add a model, the textures that the model uses do not need to be added to the Content Pipeline. If you need to add textures that you will access manually (such as textures used for 2-D sprite drawing), do this via the Solution Explorer. Otherwise, you can simply copy the texture files to the appropriate folder. When the files are added to the project, the Content Pipeline automatically identifies them as content files and set the appropriate processors to run when you build your project. At this point, we’re ready to code! Step 3: Load the Model Using the Content Pipeline Look at the code for Game1.cs; it should still be on your screen from opening your project. You’ll see a lot already done for you. Each of the methods already in the code is waiting for you to drop in your own calls to the XNA Framework. For now, we’ll start by modifying the LoadGraphicsContent method: > Double-click the Game1.cs file in Solution Explorer to bring up the code for your game. > In the code, find the LoadGraphicsContent method. > Modify the code (including adding the lines shown above the method) to look like what’s in the box to the right: In that step, you told the Content Pipeline to load your model into your game when LoadGraphicsContent is called at your game’s start. Note how you have to pass in the path to the asset relative to your project folder. Also note that there is no longer an extension on the asset. The asset’s name can be anything you want, but by default is the name of the asset file minus its extension. The code now loads the model. Let’s get it showing on the screen! 171 Welcome to XNA™ Model files contain path information for the textures they use. All models in the Spacewar StarterKit find their textures in a “Textures” folder that exists alongside the folder the models are in. For this tutorial, and any time you intend to use Spacewar content, you must replicate the folder structure in the Solution Explorer noted in this tutorial. Models you create or retrieve from other sources may have different path requirements and therefore may require different folder setups. You can determine the correct texture paths by examining the model files, or by compiling themodel as part of your game using XNA Game Studio Express and noting any Content Pipeline pathing errors that are returned. NOTE //3d model to draw Model myModel; protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) {myModel = content.Load(“Content\\Models\\p1_ wedge”); } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Step 4: Display the Model Onscreen (and Make It Rotate) We’ll want to modify two of the methods in our Game1.cs file: > In the Draw method, we will draw the model on the screen with texture and lighting. > In the Update method, we will make the model change its orientation based on time, so it appears to rotate over time. Let’s do the harder work first: drawing the model. We have to use some XNA Framework methods to set up the model’s position and lighting to draw the model on the screen: > In the code, find the Draw method. > Modify the code (including adding the lines shown above the method) to look like this: 172 Xbox 360™Han dbook //Position of the model in world space, and rotation Vector3 modelPosition = Vector3.Zero; float modelRotation = 0.0f; //Position of the camera in world space, for our view matrix Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f); //Aspect ratio to use for the projection matrix float aspectRatio = 640.0f / 480.0f; protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //Copy any parent transforms Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); //Draw the model; a model can have multiple meshes, so loop foreach (ModelMesh mesh in myModel.Meshes) {//This is where the mesh orientation is set, as well as our camera and projection foreach (BasicEffect effect in mesh.Effects) {effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation)* Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } //Draw the mesh; will use the effects set above. mesh.Draw(); } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
This code uses helper methods provided by the XNA Framework to set up the necessary 3-D math and lighting to display the model on the screen. Use the World matrix to change the position of the model in the world, the View matrix to change the position and direction of the camera (your eye), and the Projection matrix to control how the view of the 3-D world is turned into a 2-D image (projected) on your screen. The call to CopyAbsoluteBoneTransformsTo and associated code inside the setup of the World matrix is not strictly necessary for this model, but when using more complicated models—which often use hierarchical structure (where mesh positions, scales, and rotations are controlled by “bones”)—this code ensures that any mesh is first transformed by the bone that controls it, if such a bone exists. The mesh is then transformed relative to the bone transformation. If you compile and run your code now, you see your model onscreen! It is a spaceship with detail texture. But if you can resist the urge to compile your project and run, we can very easily make the model rotate in real-time so you can see all of it: > In the code, find the Update method. > Modify the code (including adding the lines shown above the method) to look like this: And that’s it. Compile and run your project by pressing 5or by clicking the Debug menu, and then clicking Start Debugging. Congratulations! You did it. There’s a lot to making games, but you’ve taken the first step—a 3-D model with lighting and movement in real time. From here, there’s no limit to where you can go! For simplicity’s sake, we took some shortcuts that can be optimized for better performance. An obvious improvement is to precalculate the View and Projection matrices instead of calculating them every time Draw is called, since they do not change. Try out this optimization as a first step. 173 Welcome to XNA™ protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit(); modelRotation += (float)gameTime.ElapsedGameTime.TotalSeconds; base.Update(gameTime); } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Ideas to Expand Got the urge to tinker with the project a bit? Try these ideas:> Modify the lighting parameters in the Draw call. Look at BasicEffect for an idea of what you can modify. > Instead of looking at a blue background, try adding an image as your background. (Hint: Make sure you use a call to SpriteBatch.Draw that allows you to specify a layerDepth parameter, and set that depth to 1.0f.) The Complete Example 174 Xbox 360™Han dbook #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; public Game1() {graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { base.Initialize(); } //3d model to draw Model myModel; protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) {myModel = content.Load(“Content\\Models\\p1_wedge”); } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
175 Welcome to XNA™ } protected override void UnloadGraphicsContent(bool unloadAllContent) {if (unloadAllContent == true) {content.Unload(); } } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit(); modelRotation += (float)gameTime.ElapsedGameTime.TotalSeconds; base.Update(gameTime); } //Position of the model in world space, and rotation Vector3 modelPosition = Vector3.Zero; float modelRotation = 0.0f; //Position of the camera in world space, for our view matrix Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f); //Aspect ratio to use for the projection matrix float aspectRatio = 640.0f / 480.0f; protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //Copy any parent transforms Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); //Draw the model; a model can have multiple meshes, so loop foreach (ModelMesh mesh in myModel.Meshes) {//This is where the mesh orientation is set, as well as our camera and projection foreach (BasicEffect effect in mesh.Effects) {effect.EnableDefaultLighting(); C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.