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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace OrientingYourDarnSelf
  14. {
  15.     /************************************************************
  16.      * ##Intro
  17.      * Comments that look like this one are the author's
  18.      * In production, you wouldn't use this type of comment
  19.      * as you would have to maintain or update them over the life
  20.      * of the software.
  21.      * I use this style so you can tell that I am talking to YOU!
  22.      * As you can see too much commenting makes your code seem
  23.      * clunky, but it is also useful if you are helping others
  24.      * such as students, hobbyists and others to learn how
  25.      * to create games.
  26.      ************************************************************/
  27.  
  28.     public class Game1 : Microsoft.Xna.Framework.Game
  29.     {
  30.         GraphicsDeviceManager graphics;
  31.         SpriteBatch spriteBatch;
  32.          /****************************************************
  33.           * SpriteFont
  34.           *
  35.           * 1. Add a SpriteFont here to use in your code
  36.           * 2. Add a Vector to locate your Sprite
  37.           * 3. Add a String variable to store your string
  38.           *
  39.           * Then add the triple whack '///' to add comment
  40.           * to your intellisense code for each variable.  
  41.           * Which makes sense if the project gets BIG!
  42.           ***************************************************/
  43.     
  44.         /// <summary>
  45.         /// Font used to demo the font and orientation
  46.         /// </summary>
  47.         SpriteFont WeirdoFont;
  48.         SpriteFont spriteFont;
  49.         Vector2 messagePos = Vector2.Zero;
  50.         Color color = Color.Black;
  51.         /****************************************************
  52.          * Vector to hold the position of the SpriteFont
  53.          * In this case: WeirdoFont
  54.          * **************************************************/
  55.         /// <summary>
  56.         /// Vector to hold the position of the SpriteFont
  57.         /// </summary>
  58.         Vector2 loc_WeirdoFont_top;
  59.         Vector2 loc_WeirdoFont_bot;
  60.         Vector2 loc_WeirdoFont_mid;
  61.  
  62.         /*****************************************************
  63.          * Now you need a string memory location to hold the
  64.          * String that you will use to write the WeirdoFont
  65.          ****************************************************/
  66.         /// <summary>
  67.         /// String that you will use to write the WeirdoFont
  68.         /// </summary>
  69.         String text_WeirdoFont_top = "top oriented";
  70.         String text_WeirdoFont_mid = "middle oriented";
  71.         String text_WeirdoFont_bot = "bottom oriented";
  72.         int viewportx, viewporty;
  73.  
  74.         public Game1()
  75.         {
  76.             graphics = new GraphicsDeviceManager(this);
  77.             Content.RootDirectory = "Content";
  78.            
  79.             // Frame rate is 30 fps by default for Windows Phone.
  80.             TargetElapsedTime = TimeSpan.FromTicks(333333);
  81.  
  82.             // Extend battery life under lock.
  83.             InactiveSleepTime = TimeSpan.FromSeconds(1);
  84.  
  85.  
  86.             graphics.SupportedOrientations =
  87.                                  DisplayOrientation.Portrait |
  88.                                  DisplayOrientation.LandscapeLeft |
  89.                                  DisplayOrientation.LandscapeRight;
  90.  
  91.  
  92.             graphics.IsFullScreen = false;
  93.         }
  94.  
  95.  
  96.         protected override void Initialize()
  97.         {
  98.             base.Initialize();
  99.         }
  100.  
  101.         protected override void LoadContent()
  102.         {
  103.             // Create a new SpriteBatch, which can be used to draw textures.
  104.             spriteBatch = new SpriteBatch(GraphicsDevice);
  105.   
  106.             WeirdoFont = this.Content.Load<SpriteFont>("WeirdoFont");
  107.             Vector2 textSize = WeirdoFont.MeasureString(text_WeirdoFont_mid);
  108.             Viewport viewport = this.GraphicsDevice.Viewport;
  109.             viewportx = viewport.Width;
  110.             viewporty = viewport.Height;
  111.             loc_WeirdoFont_top = new Vector2((viewport.Width - textSize.X)/2, 0);           
  112.             loc_WeirdoFont_mid = new Vector2((viewport.Width - textSize.X) / 2,
  113.                                            (viewport.Height - textSize.Y)/2);
  114.  
  115.             loc_WeirdoFont_bot = new Vector2((viewport.Width - textSize.X) / 2,
  116.                                           ( viewport.Height-textSize.Y));
  117.  
  118.         }
  119.  
  120.         protected override void Update(GameTime gameTime)
  121.         {
  122.             base.Update(gameTime);
  123.         }
  124.  
  125.         protected override void Draw(GameTime gameTime)
  126.         {
  127.             GraphicsDevice.Clear(Color.CornflowerBlue);
  128.  
  129.             /*****************************************************************************************
  130.              * Here there are series of code that allows you to experiment with the way to locate
  131.              * your sprite fonts
  132.              * **************************************************************************************/
  133.  
  134.             spriteBatch.Begin();
  135.             /****************************************************************************************
  136.              * In C#, you can add new lines inside of aline of code, like I have in this sample
  137.              * that allows it to be shown more easily on a blog like mine. You can also
  138.              * add the double slash // comments after each comma, which is handy for some of the
  139.              * large overrides
  140.              ****************************************************************************************/
  141.                 spriteBatch.DrawString(WeirdoFont,              //this refers to the asset name for the spritefont
  142.                                         text_WeirdoFont_top,    //This is the message for the top
  143.                                         loc_WeirdoFont_top,     //This is the vector for the top
  144.                                         Color.White);           //This is the color of the font
  145.             /****************************************************************************************
  146.              * The following is a one line example of code, and is the "normal" way of presenting
  147.              * code. In this one you can't add comments after each comma
  148.              ***************************************************************************************/
  149.                 spriteBatch.DrawString(WeirdoFont, "Orientation ", new Vector2(50, 50), Color.White);
  150.             /****************************************************************************************
  151.              * The output here is automatic via the Window.CurrentOrientation.ToString()
  152.              ***************************************************************************************/
  153.                 spriteBatch.DrawString(WeirdoFont,
  154.                                         Window.CurrentOrientation.ToString(),
  155.                                         new Vector2(50, 75), //Here I create a "new" vector on the fly
  156.                                         Color.White);
  157.             /****************************************************************************************
  158.              * The OrientationMethod was created by selected the code inside of the OrientationMethod
  159.              * currently, right clicking on the selection, selecting the "refactor" and then extract
  160.              * method. The Extract method was given the name OrientationMethod.
  161.              ***************************************************************************************/
  162.                 OrientationMethod();
  163.             /***************************************************************************************/
  164.                 spriteBatch.DrawString(WeirdoFont,
  165.                                         text_WeirdoFont_bot,
  166.                                         loc_WeirdoFont_bot,
  167.                                         Color.White);
  168.  
  169.  
  170.             spriteBatch.End();
  171.             base.Draw(gameTime);
  172.         }
  173.  
  174.         private void OrientationMethod()
  175.         {
  176.             switch (Window.CurrentOrientation.ToString())
  177.             {
  178.                 case "LandscapeLeft" :
  179.                     spriteBatch.DrawString(WeirdoFont, "Sidewise Left",
  180.                                             new Vector2(100, 200), Color.White);
  181.                     spriteBatch.DrawString(WeirdoFont,
  182.                         text_WeirdoFont_bot,
  183.                         new Vector2(100, 450),
  184.                         Color.White);
  185.                     break;
  186.                 case "LandscapeRight":
  187.                     spriteBatch.DrawString(WeirdoFont, "Sidewise Right",
  188.                                             new Vector2(100, 200), Color.White);
  189.                     spriteBatch.DrawString(WeirdoFont,
  190.                                             text_WeirdoFont_bot,
  191.                                             new Vector2(100, 450),
  192.                                             Color.White);
  193.                     break;
  194.                 case "Portrait":
  195.                     spriteBatch.DrawString(WeirdoFont, "Portrait",
  196.                                             new Vector2(100, 200), Color.White);
  197.                     spriteBatch.DrawString(WeirdoFont,
  198.                                             text_WeirdoFont_bot,
  199.                                             loc_WeirdoFont_bot,
  200.                                             Color.White);
  201.  
  202.                     break;
  203.  
  204.                 default:
  205.                     break;
  206.             }
  207.         }
  208.     }
  209. }

OrientingYourDarnSelf.zip