OrientationSample.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // OrientationSample.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. using Microsoft.Xna.Framework.Input.Touch;
  15. #endregion
  16. namespace OrientationSample
  17. {
  18. /// <summary>
  19. /// This is the main type for your game
  20. /// </summary>
  21. public class OrientationSample : Microsoft.Xna.Framework.Game
  22. {
  23. #region Fields
  24. GraphicsDeviceManager graphics;
  25. SpriteBatch spriteBatch;
  26. Texture2D directions;
  27. SpriteFont font;
  28. // Is the orientation locked?
  29. bool orientationLocked = false;
  30. // Do we allow dynamically locking/unlocking the orientation?
  31. bool enableOrientationLocking = false;
  32. #endregion
  33. #region Initialization
  34. public OrientationSample()
  35. {
  36. graphics = new GraphicsDeviceManager(this);
  37. Content.RootDirectory = "Content";
  38. // Frame rate is 30 fps by default for Windows Phone.
  39. TargetElapsedTime = TimeSpan.FromTicks(333333);
  40. // DISCLAMER:
  41. // Four different scenarios for initializing orientation support are presented below.
  42. // The first two scenarios are the most common and are recommended for use in most
  43. // cases; the third scenario is a special case to show the hardware scalar work
  44. // result; the fourth scenario is for special cases where games want to dynamically
  45. // support both the landscape and portrait orientations
  46. // Scenario #1 (default):
  47. // SupportedOrientations not changed, so the game will support Landscape orientation only
  48. // Scenario #2:
  49. // SupportedOrientations changed to support the Portrait mode only
  50. // (Uncomment the following two lines)
  51. //graphics.PreferredBackBufferWidth = 480;
  52. //graphics.PreferredBackBufferHeight = 800;
  53. // Scenario #3:
  54. // SupportedOrientations not changed (default), thus game will support the Landscape
  55. // orientations only, but resolution set to half. This makes the hardware scalar work and
  56. // automatically scales the presentation to the device's physical resolution
  57. // (Uncomment the following two lines):
  58. //graphics.PreferredBackBufferWidth = 400;
  59. //graphics.PreferredBackBufferHeight = 240;
  60. // Scenario #4:
  61. // Game supports all possible orientations and enablyes dynamically locking/unlocking the
  62. // orientation.
  63. // (Uncomment the following lines):
  64. graphics.SupportedOrientations = DisplayOrientation.Portrait |
  65. DisplayOrientation.LandscapeLeft |
  66. DisplayOrientation.LandscapeRight;
  67. //enableOrientationLocking = true;
  68. // Switch to full screen mode
  69. graphics.IsFullScreen = true;
  70. }
  71. /// <summary>
  72. /// Allows the game to perform any initialization it needs to before starting to run.
  73. /// This is where it can query for any required services and load any non-graphic
  74. /// related content. Calling base.Initialize will enumerate through any components
  75. /// and initialize them as well.
  76. /// </summary>
  77. protected override void Initialize()
  78. {
  79. // For scenario #4, we handle locking/unlocking the orientation by detecting the tap gesture.
  80. TouchPanel.EnabledGestures = GestureType.Tap;
  81. base.Initialize();
  82. }
  83. #endregion
  84. #region Load and Unload
  85. /// <summary>
  86. /// LoadContent will be called once per game and is the place to load
  87. /// all of your content.
  88. /// </summary>
  89. protected override void LoadContent()
  90. {
  91. // Create a new SpriteBatch, which can be used to draw textures.
  92. spriteBatch = new SpriteBatch(GraphicsDevice);
  93. directions = Content.Load<Texture2D>("directions");
  94. font = Content.Load<SpriteFont>("Font");
  95. }
  96. /// <summary>
  97. /// UnloadContent will be called once per game and is the place to unload
  98. /// all content.
  99. /// </summary>
  100. protected override void UnloadContent()
  101. {
  102. // Nothing to unload in this sample
  103. }
  104. #endregion
  105. #region Update and Render
  106. /// <summary>
  107. /// Allows the game to run logic such as updating the world,
  108. /// checking for collisions, gathering input, and playing audio.
  109. /// </summary>
  110. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  111. protected override void Update(GameTime gameTime)
  112. {
  113. // Allows the game to exit
  114. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  115. this.Exit();
  116. // If we enable locking/unlocking the orientation...
  117. if (enableOrientationLocking)
  118. {
  119. // Read the gestures from the touch panel
  120. while (TouchPanel.IsGestureAvailable)
  121. {
  122. GestureSample gesture = TouchPanel.ReadGesture();
  123. // If the user tapped on the screen...
  124. if (gesture.GestureType == GestureType.Tap)
  125. {
  126. // Toggle the orientation locked state
  127. orientationLocked = !orientationLocked;
  128. if (orientationLocked)
  129. {
  130. // If we're locking the orientation, we want to store the current
  131. // orientation as well as the current viewport size. we have to
  132. // store the viewport size because when we call ApplyChanges(),
  133. // our back buffer may change and we want to make sure that it
  134. // remains at the current size, rather than any previously set
  135. // preferred size.
  136. graphics.SupportedOrientations = Window.CurrentOrientation;
  137. graphics.PreferredBackBufferWidth = GraphicsDevice.Viewport.Width;
  138. graphics.PreferredBackBufferHeight = GraphicsDevice.Viewport.Height;
  139. }
  140. else
  141. {
  142. // If we're unlocking the orientation, we simply set our
  143. // supported orientations back to all orientations
  144. graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft |
  145. DisplayOrientation.LandscapeRight |
  146. DisplayOrientation.Portrait;
  147. }
  148. // ApplyChanges needs to be called if SupportedOrientations was changed
  149. graphics.ApplyChanges();
  150. }
  151. }
  152. }
  153. base.Update(gameTime);
  154. }
  155. /// <summary>
  156. /// This is called when the game should draw itself.
  157. /// </summary>
  158. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  159. protected override void Draw(GameTime gameTime)
  160. {
  161. GraphicsDevice.Clear(Color.CornflowerBlue);
  162. spriteBatch.Begin();
  163. // Draw the directions texture centered on the screen
  164. Vector2 position = new Vector2(
  165. GraphicsDevice.Viewport.Width / 2 - directions.Width / 2,
  166. GraphicsDevice.Viewport.Height / 2 - directions.Height / 2);
  167. spriteBatch.Draw(directions, position, Color.White);
  168. // If we allow locking/unlocking of the orientation, draw the instructions to
  169. // the screen.
  170. if (enableOrientationLocking)
  171. {
  172. // Create a string of our current state
  173. string currentState = orientationLocked
  174. ? "Orientation: Locked"
  175. : "Orientation: Unlocked";
  176. // Create a string for the instructions
  177. string instructions = orientationLocked
  178. ? "Tap to unlock orientation."
  179. : "Tap to lock orientation.";
  180. // Draw the text to the screen
  181. spriteBatch.DrawString(font, currentState, new Vector2(10, 10), Color.White);
  182. spriteBatch.DrawString(font, instructions, new Vector2(10, 25), Color.White);
  183. }
  184. spriteBatch.End();
  185. base.Draw(gameTime);
  186. }
  187. #endregion
  188. }
  189. }