The Windows Phone version of the XNA Framework includes an automatic rotation feature. This allows XNA games to choose whether they want to run in landscape or portrait mode, without having to roll their own rotation solution like we used to on Zune.
Note: rotation was not implemented in the CTP release of the Windows Phone Emulator, but is fully supported in our recent beta version.
Things you should know about rotation
- Rotation is implemented by special driver magic, so costs literally nothing
- In contrast, rolling your own rotation via a rendertarget costs lots of GPU time and battery
- So use the built in rotation, don't roll your own!
- Touch input is automatically rotated to match your chosen orientation
- If you use other sensor inputs such as accelerometer, you must rotate them yourself
Making a landscape game is easy: just don't do anything at all. The Game class defaults to 800x480 landscape resolution.
If you turn the phone the other way up, landscape XNA games automatically flip between LandscapeLeft and LandscapeRight orientation. You don't need to do anything special to enable this. Graphics rendering and touch input are automatically rotated so everything 'just works' ™.
To make a portrait game, add this to your Game constructor:
graphics.PreferredBackBufferWidth = 480; graphics.PreferredBackBufferHeight = 800;
Now you have a portrait mode game, which will not rotate as you turn the phone.
Thanks to the scaler feature, the width and height do not have to be exactly 800x480 or 480x800. If width is greater than height, XNA will choose landscape orientation, otherwise it will choose portrait.
If you want to lock your game to just one landscape orientation, so it will not automatically flip between LandscapeLeft and LandscapeRight, add this to your Game constructor:
graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft;
If you want to change orientation sometime after the game has been initialized:
graphics.SupportedOrientations = DisplayOrientation.LandscapeRight; graphics.ApplyChanges();
If you want to automatically switch between both landscape and portrait orientations as the phone is rotated:
graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
Switching between LandscapeLeft and LandscapeRight can be handled automatically with no special help from the game, and is therefore enabled by default. But switching between landscape and portrait alters the backbuffer dimensions (short-and-wide vs. tall-and-thin), which will most likely require you to adjust your screen layout. Not all games will be able to handle this (and some designs only make sense one way up), so dynamic switching between landscape and portrait is only enabled for games that explicitly opt-in by setting SupportedOrientations.
When you turn the phone from one orientation to another, two things happen:
- The GameWindow.OrientationChanged event is raised
- If the new orientation is included in your SupportedOrientations settings, the graphics device is reset accordingly, which raises the various device reset events
To check the current orientation:
- GameWindow.CurrentOrientation tells you which way up the phone is currently being held
- GraphicsDevice.PresentationParameters.DisplayOrientation tells you which way up your game is currently displayed
If the current phone orientation is not one of your SupportedOrientations, these two values may not be the same.
Similarly, if your game is using the scaler, the current backbuffer resolution (which is scaled, and can be queried using GraphicsDevice.PresentationParameters.BackBufferWidth and BackBufferHeight) will not be the same as the screen resolution (which is not scaled, and can be queried using DisplayMode or GameWindow.ClientBounds).