USC: Background Image and Orienting a background image

With some software considerations, you can easily move the background image around to fit the orientation of the phone.

To get to the attached zip file, you will need to scroll down to the bottom of the page.

Code Snippet

  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. namespace OrientingYourDarnSelf
  6. {
  7.     /************************************************************
  8.      * ##Intro
  9.      * Comments that look like this one are the author's
  10.      * In production, you wouldn't use this type of comment
  11.      * as you would have to maintain or update them over the life
  12.      * of the software.
  13.      * I use this style so you can tell that I am talking to YOU!
  14.      * As you can see too much commenting makes your code seem
  15.      * clunky, but it is also useful if you are helping others
  16.      * such as students, hobbyists and others to learn how
  17.      * to create games.
  18.      ************************************************************/
  19.  
  20.     public class Game1 : Microsoft.Xna.Framework.Game
  21.     {
  22.         GraphicsDeviceManager graphics;
  23.         SpriteBatch spriteBatch;
  24.          /****************************************************
  25.           * SpriteFont
  26.           *
  27.           * 1. Add a SpriteFont here to use in your code
  28.           * 2. Add a Vector to locate your Sprite
  29.           * 3. Add a String variable to store your string
  30.           *
  31.           * Then add the triple whack '///' to add comment
  32.           * to your intellisense code for each variable.  
  33.           * Which makes sense if the project gets BIG!
  34.           ***************************************************/
  35.     
  36.         /// <summary>
  37.         /// Font used to demo the font and orientation
  38.         /// </summary>
  39.         SpriteFont WeirdoFont;
  40.         Color color = Color.Blue;
  41.         Color font_Color = Color.White;
  42.         /********************************************************************
  43.          * Adding the Texture2D memory location for the background and the
  44.          * rectangle to hold it
  45.          *******************************************************************/
  46.         Texture2D background;
  47.         Rectangle mainBackground_Portrait;
  48.         Rectangle mainBackground_Landscape;
  49.  
  50.  
  51.         public Game1()
  52.         {
  53.             graphics = new GraphicsDeviceManager(this);
  54.             Content.RootDirectory = "Content";
  55.            
  56.             // Frame rate is 30 fps by default for Windows Phone.
  57.             TargetElapsedTime = TimeSpan.FromTicks(333333);
  58.  
  59.             // Extend battery life under lock.
  60.             InactiveSleepTime = TimeSpan.FromSeconds(1);
  61.  
  62.             /********************************************************************
  63.              * The following extended but singele line of code (a line of code
  64.              * is shown by the semi-colon), detects that type of orientation
  65.              * supported by the program
  66.              *******************************************************************/
  67.             graphics.SupportedOrientations =
  68.                                  DisplayOrientation.Portrait |
  69.                                  DisplayOrientation.LandscapeLeft |
  70.                                  DisplayOrientation.LandscapeRight;
  71.             graphics.IsFullScreen = false;
  72.         }
  73.  
  74.  
  75.         protected override void Initialize()
  76.         {
  77.             base.Initialize();
  78.         }
  79.  
  80.         protected override void LoadContent()
  81.         {
  82.             /********************************************************************
  83.              * Create a new SpriteBatch, which can be used to draw textures.
  84.              * Inserted by the default XNA Template
  85.              *******************************************************************/
  86.             spriteBatch = new SpriteBatch(GraphicsDevice);
  87.             /********************************************************************
  88.              * Load the Background Image, the image is drawn for landscape
  89.              * but I use the program to implement the switch
  90.              *******************************************************************/
  91.             background = Content.Load<Texture2D>("GameScreenBkgd");
  92.             /********************************************************************
  93.              * Load the font, I set the size larger inside of the spritefont
  94.              * XML file (In this program it is named:
  95.              * WeirdoFont.spritefont
  96.              *******************************************************************/
  97.             WeirdoFont = Content.Load<SpriteFont>("WeirdoFont");
  98.             /********************************************************************
  99.              * Set the rectangle parameters for the Portrait and Landscape (I use
  100.              * the same one for both the LandScapeLeft and LandScapeRight
  101.              * Note that Width and Height are reversed, this works because the
  102.              * background is relatively symteric, but it isn't really
  103.                    *******************************************************************/
  104.             mainBackground_Portrait =
  105.                 new Rectangle(0, 0, GraphicsDevice.Viewport.Width,
  106.                                     GraphicsDevice.Viewport.Height);
  107.             mainBackground_Landscape =
  108.                 new Rectangle(0, 0, GraphicsDevice.Viewport.Height,
  109.                                     GraphicsDevice.Viewport.Width);
  110.   
  111.         }
  112.  
  113.         protected override void Update(GameTime gameTime)
  114.         {
  115.             base.Update(gameTime);
  116.         }
  117.  
  118.         protected override void Draw(GameTime gameTime)
  119.         {
  120.             GraphicsDevice.Clear(Color.White);
  121.  
  122.             /*****************************************************************************************
  123.              * Here there are series of code that allows you to experiment with the way to locate
  124.              * your sprite fonts
  125.              * **************************************************************************************/
  126.  
  127.             spriteBatch.Begin();
  128.             /********************************************************************
  129.              * Switch tests the Orientation
  130.              *******************************************************************/
  131.             switch (Window.CurrentOrientation.ToString())
  132.             {
  133.                 case ("LandscapeLeft"):
  134.                         spriteBatch.Draw(background, mainBackground_Landscape, color);
  135.                         break;
  136.                 case ("LandscapeRight"):
  137.                         spriteBatch.Draw(background, mainBackground_Landscape, color);
  138.                         break;
  139.                 case ("Portrait"):
  140.                         spriteBatch.Draw(background, mainBackground_Portrait, color);
  141.                         break;
  142.                 default:
  143.                         break;
  144.             }
  145.             /****************************************************************************************
  146.              * The output here is automatic via the Window.CurrentOrientation.ToString()
  147.              ***************************************************************************************/
  148.                 spriteBatch.DrawString(
  149.                         WeirdoFont,
  150.                         Window.CurrentOrientation.ToString(),
  151.                         new Vector2(70, 75), //Here I create a "new" vector on the fly
  152.                         font_Color);     
  153.             /********************************************************************
  154.              * The required end to the spriteBatch
  155.              *******************************************************************/
  156.             spriteBatch.End();
  157.             /********************************************************************
  158.              * base.Draw(gameTime) is entered by the default XNA Template
  159.              *******************************************************************/
  160.             base.Draw(gameTime);
  161.         }        
  162.     }
  163. }

ImageOrientation.zip