Game.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. using System;
  15. #endregion
  16. namespace DistortionSample
  17. {
  18. /// <summary>
  19. /// This sample demonstrates a variety of image-distorting
  20. /// post-processing techniques.
  21. /// </summary>
  22. public class DistortionSampleGame : Microsoft.Xna.Framework.Game
  23. {
  24. #region Fields
  25. GraphicsDeviceManager graphics;
  26. const float initialViewAngle = MathHelper.Pi / 2f;
  27. float viewAngle = initialViewAngle;
  28. const float CameraRotationSpeed = 0.1f;
  29. const float ViewDistance = 750.0f;
  30. DistortionComponent distortionComponent;
  31. Distorter[] distorters;
  32. int currentDistorter;
  33. SpriteBatch spriteBatch;
  34. SpriteFont spriteFont;
  35. Vector2 overlayTextLocation;
  36. Texture2D background;
  37. KeyboardState lastKeyboardState = new KeyboardState();
  38. GamePadState lastGamePadState = new GamePadState();
  39. KeyboardState currentKeyboardState = new KeyboardState();
  40. GamePadState currentGamePadState = new GamePadState();
  41. #endregion
  42. #region Initialization
  43. public DistortionSampleGame()
  44. {
  45. graphics = new GraphicsDeviceManager(this);
  46. Content.RootDirectory = "Content";
  47. distortionComponent = new DistortionComponent(this);
  48. Components.Add(distortionComponent);
  49. }
  50. protected override void Initialize()
  51. {
  52. distorters = new Distorter[3];
  53. currentDistorter = 0;
  54. base.Initialize();
  55. }
  56. /// <summary>
  57. /// Load your graphics content.
  58. /// </summary>
  59. protected override void LoadContent()
  60. {
  61. spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  62. spriteFont = Content.Load<SpriteFont>("hudFont");
  63. background = Content.Load<Texture2D>("Sunset");
  64. distorters[0] = new Distorter();
  65. distorters[0].ModelName = "Dude";
  66. distorters[0].World = Matrix.CreateTranslation(0, -40, 0) *
  67. Matrix.CreateScale(8);
  68. distorters[0].Model = Content.Load<Model>("Dude");
  69. distorters[0].Technique =
  70. DistortionComponent.DistortionTechnique.PullIn;
  71. distorters[0].DistortionScale = 0.0003f;
  72. distorters[0].DistortionBlur = true;
  73. distorters[1] = new Distorter();
  74. distorters[1].ModelName = "Cylinder";
  75. distorters[1].World = Matrix.CreateScale(200);
  76. distorters[1].Model = Content.Load<Model>("Cylinder");
  77. distorters[1].Technique =
  78. DistortionComponent.DistortionTechnique.HeatHaze;
  79. distorters[1].DistortionScale = 0.025f;
  80. distorters[1].DistortionBlur = true;
  81. distorters[2] = new Distorter();
  82. distorters[2].ModelName = "Window";
  83. distorters[2].World = Matrix.CreateScale(500);
  84. distorters[2].Model = Content.Load<Model>("Window");
  85. distorters[2].Technique =
  86. DistortionComponent.DistortionTechnique.DisplacementMapped;
  87. distorters[2].DistortionScale = 0.025f;
  88. distorters[2].DistortionBlur = false;
  89. overlayTextLocation = new Vector2(
  90. (float)graphics.GraphicsDevice.Viewport.X +
  91. (float)graphics.GraphicsDevice.Viewport.Width * 0.1f,
  92. (float)graphics.GraphicsDevice.Viewport.Y +
  93. (float)graphics.GraphicsDevice.Viewport.Height * 0.1f
  94. );
  95. }
  96. #endregion
  97. #region Update and Draw
  98. /// <summary>
  99. /// Allows the game to run logic.
  100. /// </summary>
  101. protected override void Update(GameTime gameTime)
  102. {
  103. HandleInput();
  104. // update the distortion component
  105. distortionComponent.Distorter = distorters[currentDistorter];
  106. base.Update(gameTime);
  107. }
  108. /// <summary>
  109. /// This is called when the game should draw itself.
  110. /// </summary>
  111. protected override void Draw(GameTime gameTime)
  112. {
  113. distortionComponent.BeginDraw();
  114. GraphicsDevice.Clear(Color.Black);
  115. // Draw the background image.
  116. spriteBatch.Begin(0, BlendState.Opaque);
  117. spriteBatch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
  118. spriteBatch.End();
  119. // Draw other components (which includes the distortion).
  120. base.Draw(gameTime);
  121. // Display some text over the top. Note how we draw this after distortion
  122. // because we don't want the text to be affected by the postprocessing.
  123. DrawOverlayText();
  124. }
  125. /// <summary>
  126. /// Displays an overlay showing what the controls are,
  127. /// and which settings are currently selected.
  128. /// </summary>
  129. void DrawOverlayText()
  130. {
  131. string text = distorters[currentDistorter].ToString() + "\n\n" +
  132. "A: Cycle Distorter\n" +
  133. "X: " + (distorters[currentDistorter].DistortionBlur ? "Disable" :
  134. "Enable") + " Distorter Blur\n" +
  135. "B: " + (distortionComponent.ShowDistortionMap ? "Hide" : "Show") +
  136. " Distortion Map\n";
  137. spriteBatch.Begin();
  138. // Draw the string twice to create a drop shadow, first colored black
  139. // and offset one pixel to the bottom right, then again in white at the
  140. // intended position. this makes text easier to read over the background.
  141. spriteBatch.DrawString(spriteFont, text, overlayTextLocation + Vector2.One,
  142. Color.Black);
  143. spriteBatch.DrawString(spriteFont, text, overlayTextLocation, Color.White);
  144. spriteBatch.End();
  145. }
  146. #endregion
  147. #region Handle Input
  148. /// <summary>
  149. /// Handles input for quitting or changing the sample settings.
  150. /// </summary>
  151. private void HandleInput()
  152. {
  153. lastKeyboardState = currentKeyboardState;
  154. lastGamePadState = currentGamePadState;
  155. currentKeyboardState = Keyboard.GetState();
  156. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  157. // Check for exit.
  158. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  159. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  160. {
  161. Exit();
  162. }
  163. // Cycle mode
  164. if ((currentGamePadState.Buttons.A == ButtonState.Pressed &&
  165. lastGamePadState.Buttons.A != ButtonState.Pressed) ||
  166. (currentKeyboardState.IsKeyDown(Keys.Space) &&
  167. lastKeyboardState.IsKeyUp(Keys.Space)) ||
  168. (currentKeyboardState.IsKeyDown(Keys.A) &&
  169. lastKeyboardState.IsKeyUp(Keys.A)))
  170. {
  171. currentDistorter = (currentDistorter + 1) % distorters.Length;
  172. viewAngle = initialViewAngle;
  173. }
  174. // Toggle showing the distortion map on or off?
  175. if ((currentGamePadState.Buttons.B == ButtonState.Pressed &&
  176. lastGamePadState.Buttons.B != ButtonState.Pressed) ||
  177. (currentKeyboardState.IsKeyDown(Keys.Tab) &&
  178. lastKeyboardState.IsKeyUp(Keys.Tab)) ||
  179. (currentKeyboardState.IsKeyDown(Keys.B) &&
  180. lastKeyboardState.IsKeyUp(Keys.B)))
  181. {
  182. distortionComponent.ShowDistortionMap =
  183. !distortionComponent.ShowDistortionMap;
  184. }
  185. // Toggle showing the distortion map on or off?
  186. if ((currentGamePadState.Buttons.X == ButtonState.Pressed &&
  187. lastGamePadState.Buttons.X != ButtonState.Pressed) ||
  188. (currentKeyboardState.IsKeyDown(Keys.LeftControl) &&
  189. lastKeyboardState.IsKeyUp(Keys.LeftControl)) ||
  190. (currentKeyboardState.IsKeyDown(Keys.X) &&
  191. lastKeyboardState.IsKeyUp(Keys.X)))
  192. {
  193. distorters[currentDistorter].DistortionBlur =
  194. !distorters[currentDistorter].DistortionBlur;
  195. }
  196. // rotate the camera, using the left thumbstick and arrow keys
  197. float viewAngleChange = currentGamePadState.ThumbSticks.Left.X;
  198. if (currentKeyboardState.IsKeyDown(Keys.Left))
  199. {
  200. viewAngleChange = -1;
  201. }
  202. else if (currentKeyboardState.IsKeyDown(Keys.Right))
  203. {
  204. viewAngleChange = 1;
  205. }
  206. viewAngle += viewAngleChange * CameraRotationSpeed;
  207. distortionComponent.View = Matrix.CreateLookAt(ViewDistance *
  208. new Vector3((float)Math.Cos(viewAngle), 0, (float)Math.Sin(viewAngle)),
  209. Vector3.Zero, Vector3.Up);
  210. }
  211. #endregion
  212. }
  213. #region Entry Point
  214. /// <summary>
  215. /// The main entry point for the application.
  216. /// </summary>
  217. static class Program
  218. {
  219. static void Main()
  220. {
  221. using (DistortionSampleGame game = new DistortionSampleGame())
  222. {
  223. game.Run();
  224. }
  225. }
  226. }
  227. #endregion
  228. }