LayoutSample.cs 8.6 KB

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