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
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- 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;
- Color color = Color.Blue;
- Color font_Color = Color.White;
- /********************************************************************
- * Adding the Texture2D memory location for the background and the
- * rectangle to hold it
- *******************************************************************/
- Texture2D background;
- Rectangle mainBackground_Portrait;
- Rectangle mainBackground_Landscape;
- 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);
- /********************************************************************
- * The following extended but singele line of code (a line of code
- * is shown by the semi-colon), detects that type of orientation
- * supported by the program
- *******************************************************************/
- 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.
- * Inserted by the default XNA Template
- *******************************************************************/
- spriteBatch = new SpriteBatch(GraphicsDevice);
- /********************************************************************
- * Load the Background Image, the image is drawn for landscape
- * but I use the program to implement the switch
- *******************************************************************/
- background = Content.Load<Texture2D>("GameScreenBkgd");
- /********************************************************************
- * Load the font, I set the size larger inside of the spritefont
- * XML file (In this program it is named:
- * WeirdoFont.spritefont
- *******************************************************************/
- WeirdoFont = Content.Load<SpriteFont>("WeirdoFont");
- /********************************************************************
- * Set the rectangle parameters for the Portrait and Landscape (I use
- * the same one for both the LandScapeLeft and LandScapeRight
- * Note that Width and Height are reversed, this works because the
- * background is relatively symteric, but it isn't really
- *******************************************************************/
- mainBackground_Portrait =
- new Rectangle(0, 0, GraphicsDevice.Viewport.Width,
- GraphicsDevice.Viewport.Height);
- mainBackground_Landscape =
- new Rectangle(0, 0, GraphicsDevice.Viewport.Height,
- GraphicsDevice.Viewport.Width);
- }
- protected override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.White);
- /*****************************************************************************************
- * Here there are series of code that allows you to experiment with the way to locate
- * your sprite fonts
- * **************************************************************************************/
- spriteBatch.Begin();
- /********************************************************************
- * Switch tests the Orientation
- *******************************************************************/
- switch (Window.CurrentOrientation.ToString())
- {
- case ("LandscapeLeft"):
- spriteBatch.Draw(background, mainBackground_Landscape, color);
- break;
- case ("LandscapeRight"):
- spriteBatch.Draw(background, mainBackground_Landscape, color);
- break;
- case ("Portrait"):
- spriteBatch.Draw(background, mainBackground_Portrait, color);
- break;
- default:
- break;
- }
- /****************************************************************************************
- * The output here is automatic via the Window.CurrentOrientation.ToString()
- ***************************************************************************************/
- spriteBatch.DrawString(
- WeirdoFont,
- Window.CurrentOrientation.ToString(),
- new Vector2(70, 75), //Here I create a "new" vector on the fly
- font_Color);
- /********************************************************************
- * The required end to the spriteBatch
- *******************************************************************/
- spriteBatch.End();
- /********************************************************************
- * base.Draw(gameTime) is entered by the default XNA Template
- *******************************************************************/
- base.Draw(gameTime);
- }
- }
- }