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.
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.
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. Add the following line in the class field:
Texture2D arrows;
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. Load the following content in the
LoadContent()
method:// Load the arrow content. arrows = Content.Load<Texture2D>("Windows Phone Arrow");
4. In the next step, add the following lines to the
Update()
method:graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft| DisplayOrientation.LandscapeRight;
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. 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:
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.