Book Image

Windows Phone 7 XNA Cookbook

By : Zheng Yang
Book Image

Windows Phone 7 XNA Cookbook

By: Zheng Yang

Overview of this book

Developing games for Windows Phone 7, a new mobile platform, is your big chance to impact the world of mobile games. The XNA 4.0 for Windows Phone 7 integrates a lot of capabilities from software and hardware for you to create incredible games. The next generation of mobile games will be built by you. Windows Phone 7 XNA Cookbook is the best choice for you to make a game on Windows Phone 7. The book helps you to master the indispensable techniques to create your games using XNA 4.0. From the basics such as animating a 2D sprite and interacting with the customized graphical user interface to the more challenging such as 3D graphic rendering and collision detection. This comprehensive cookbook covers all the essential areas of XNA game development for Windows Phone 7, such as approaches to control the sensors, gestures and typical kinds of cameras. We also have recipes for sprite animation, texture rendering, and graphical user interface development that will give you a powerful tool to work with 2D effects. After this we move onto the more juicy stuff with recipes covering 3D graphic rendering and collision detection, and major ways to improve your loading efficiency. You will also work with Xbox live so you can take your game global. Finally, no mobile game development book would be complete without a look at performance optimization to make your games run faster. Windows Phone 7 XNA Cookbook will equip you with the firepower to rock the game world.
Table of Contents (17 chapters)
Windows Phone 7 XNA Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Getting familiar with orientation


The orientation for Windows Phone 7 stands for different directions, landscape or portrait. By default, XNA programs for Windows Phone 7 run in landscape mode. This recipe discusses how to transcend those defaults and explores other issues involving screen size and events.

Getting ready

The following two screenshots demonstrate the different modes of Windows Phone 7. The image on the left indicates the landscape mode with a resolution of 800 * 480 and the screenshot to the right shows the portrait mode with a resolution of 480 * 800. By default, XNA for Windows Phone is set up for a landscape orientation. If you prefer designing your game for a portrait display, it's easy to do.

How to do it...

Now, let's create a new Windows Phone Project named WindowsPhoneOrientation. Find the class Game1 in Game1.cs. You can also rename the filename and class name to any other name you like:

  1. 1. Add the following line in the class field:

    Texture2D arrows;
    
  2. 2. Next, find the Game1() constructor and insert the following lines:

    graphics.SupportedOrientations =
    DisplayOrientation.Portrait |
    DisplayOrientation.LandscapeLeft|
    DisplayOrientation.LandscapeRight;
    // Switch to full screen mode
    graphics.IsFullScreen = true;
    

    Note

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  3. 3. Load the following content in the LoadContent() method:

    // Load the arrow content.
    arrows = Content.Load<Texture2D>("Windows Phone Arrow");
    
  4. 4. In the next step, add the following lines to the Update() method:

    graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft| DisplayOrientation.LandscapeRight;
    
  5. 5. Find the Draw() method and insert the following code:

    spriteBatch.Begin();
    // Draw the directions texture centered on the screen
    Vector2 position = new Vector2(
    GraphicsDevice.Viewport.Width / 2 - arrows.Width / 2,
    GraphicsDevice.Viewport.Height / 2 - arrows.Height / 2);
    spriteBatch.Draw(arrows, position, Color.White);
    spriteBatch.End();
    
  6. 6. All done! Build the project and run it in the emulator. Change the orientation by clicking on the change buttons located at the top-right of the emulator. If the application runs well, you will see three different presentations, as shown in the following screenshots:

How it works...

In step 1, Texture2D represents the images, SpriteFont represents text, showing the different directions.

In step 2, in class constructor, set the SupportedOrientations attribute of GraphicsDeviceManager (here is graphics) to DisplayOrientation options: Portrait, LandscapeLeft, and LandscapeRight enables the Windows Phone to adjust the resolution according to the different orientations.

In step 4, you may be curious about the code. Why do we have to set SupportedOrientation in a similar way as we have in step 2? The reason is that the responsibility of the Update() method is to update the game status, game logic, input, and so on. Here, for Windows Phone Orientation, the Update() method needs to listen to the changes in the device orientation. The method should react to any change between Landscape or Portrait mode. Once the orientation meets the settings of the DisplayOrientation, the GraphicsDeviceManager(graphics) will apply the change to the default device. Again, when the Update() method completes its work in every cycle, by default, the XNA will call the Draw() method to render the new face of the game.

In step 5, the snippet provides a way for placing the image or texture in the middle of the screen. Half of the viewport just makes the top-left of the image locate at the center of the screen. We need the center of the image to replace the image top-left point, therefore, we should subtract half of the width and length of the image. Then the SpriteBatch.Draw() method renders the image on the Windows Phone screen. Notice that all the position manipulations and sprite drawing methods must be placed between the SpriteBatch.Begin() and SpriteBatch.End() methods.