USC: Screen Orientation
Here is a simple example that shows how to determine the Screen Orientation on Windows Phone 7
The example project is zipped at the bottom of the code. You may need to scroll down to see it.
Leave a comment if you do download it!
Orientation
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- namespace OrientingYourDarnSelf
- {
- /************************************************************
- * ##Intro
- * Comments that look like this one are the author's
- * In production, you wouldn't use this type of comment
- * as you would have to maintain or update them over the life
- * of the software.
- * I use this style so you can tell that I am talking to YOU!
- * As you can see too much commenting makes your code seem
- * clunky, but it is also useful if you are helping others
- * such as students, hobbyists and others to learn how
- * to create games.
- ************************************************************/
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- /****************************************************
- * SpriteFont
- *
- * 1. Add a SpriteFont here to use in your code
- * 2. Add a Vector to locate your Sprite
- * 3. Add a String variable to store your string
- *
- * Then add the triple whack '///' to add comment
- * to your intellisense code for each variable.
- * Which makes sense if the project gets BIG!
- ***************************************************/
- /// <summary>
- /// Font used to demo the font and orientation
- /// </summary>
- SpriteFont WeirdoFont;
- SpriteFont spriteFont;
- Vector2 messagePos = Vector2.Zero;
- Color color = Color.Black;
- /****************************************************
- * Vector to hold the position of the SpriteFont
- * In this case: WeirdoFont
- * **************************************************/
- /// <summary>
- /// Vector to hold the position of the SpriteFont
- /// </summary>
- Vector2 loc_WeirdoFont_top;
- Vector2 loc_WeirdoFont_bot;
- Vector2 loc_WeirdoFont_mid;
- /*****************************************************
- * Now you need a string memory location to hold the
- * String that you will use to write the WeirdoFont
- ****************************************************/
- /// <summary>
- /// String that you will use to write the WeirdoFont
- /// </summary>
- String text_WeirdoFont_top = "top oriented";
- String text_WeirdoFont_mid = "middle oriented";
- String text_WeirdoFont_bot = "bottom oriented";
- int viewportx, viewporty;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- // Frame rate is 30 fps by default for Windows Phone.
- TargetElapsedTime = TimeSpan.FromTicks(333333);
- // Extend battery life under lock.
- InactiveSleepTime = TimeSpan.FromSeconds(1);
- graphics.SupportedOrientations =
- DisplayOrientation.Portrait |
- DisplayOrientation.LandscapeLeft |
- DisplayOrientation.LandscapeRight;
- graphics.IsFullScreen = false;
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- WeirdoFont = this.Content.Load<SpriteFont>("WeirdoFont");
- Vector2 textSize = WeirdoFont.MeasureString(text_WeirdoFont_mid);
- Viewport viewport = this.GraphicsDevice.Viewport;
- viewportx = viewport.Width;
- viewporty = viewport.Height;
- loc_WeirdoFont_top = new Vector2((viewport.Width - textSize.X)/2, 0);
- loc_WeirdoFont_mid = new Vector2((viewport.Width - textSize.X) / 2,
- (viewport.Height - textSize.Y)/2);
- loc_WeirdoFont_bot = new Vector2((viewport.Width - textSize.X) / 2,
- ( viewport.Height-textSize.Y));
- }
- protected override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- /*****************************************************************************************
- * Here there are series of code that allows you to experiment with the way to locate
- * your sprite fonts
- * **************************************************************************************/
- spriteBatch.Begin();
- /****************************************************************************************
- * In C#, you can add new lines inside of aline of code, like I have in this sample
- * that allows it to be shown more easily on a blog like mine. You can also
- * add the double slash // comments after each comma, which is handy for some of the
- * large overrides
- ****************************************************************************************/
- spriteBatch.DrawString(WeirdoFont, //this refers to the asset name for the spritefont
- text_WeirdoFont_top, //This is the message for the top
- loc_WeirdoFont_top, //This is the vector for the top
- Color.White); //This is the color of the font
- /****************************************************************************************
- * The following is a one line example of code, and is the "normal" way of presenting
- * code. In this one you can't add comments after each comma
- ***************************************************************************************/
- spriteBatch.DrawString(WeirdoFont, "Orientation ", new Vector2(50, 50), Color.White);
- /****************************************************************************************
- * The output here is automatic via the Window.CurrentOrientation.ToString()
- ***************************************************************************************/
- spriteBatch.DrawString(WeirdoFont,
- Window.CurrentOrientation.ToString(),
- new Vector2(50, 75), //Here I create a "new" vector on the fly
- Color.White);
- /****************************************************************************************
- * The OrientationMethod was created by selected the code inside of the OrientationMethod
- * currently, right clicking on the selection, selecting the "refactor" and then extract
- * method. The Extract method was given the name OrientationMethod.
- ***************************************************************************************/
- OrientationMethod();
- /***************************************************************************************/
- spriteBatch.DrawString(WeirdoFont,
- text_WeirdoFont_bot,
- loc_WeirdoFont_bot,
- Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void OrientationMethod()
- {
- switch (Window.CurrentOrientation.ToString())
- {
- case "LandscapeLeft" :
- spriteBatch.DrawString(WeirdoFont, "Sidewise Left",
- new Vector2(100, 200), Color.White);
- spriteBatch.DrawString(WeirdoFont,
- text_WeirdoFont_bot,
- new Vector2(100, 450),
- Color.White);
- break;
- case "LandscapeRight":
- spriteBatch.DrawString(WeirdoFont, "Sidewise Right",
- new Vector2(100, 200), Color.White);
- spriteBatch.DrawString(WeirdoFont,
- text_WeirdoFont_bot,
- new Vector2(100, 450),
- Color.White);
- break;
- case "Portrait":
- spriteBatch.DrawString(WeirdoFont, "Portrait",
- new Vector2(100, 200), Color.White);
- spriteBatch.DrawString(WeirdoFont,
- text_WeirdoFont_bot,
- loc_WeirdoFont_bot,
- Color.White);
- break;
- default:
- break;
- }
- }
- }
- }