Game.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //-----------------------------------------------------------------------------
  2. // Game.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. namespace BloomSample.Core
  12. {
  13. /// <summary>
  14. /// Sample showing how to implement a bloom postprocess,
  15. /// adding a glowing effect over the top of an existing scene.
  16. /// </summary>
  17. public class Game1 : Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. BloomComponent bloom;
  21. int bloomSettingsIndex = 0;
  22. SpriteBatch spriteBatch;
  23. SpriteFont spriteFont;
  24. Texture2D background;
  25. Model model;
  26. KeyboardState lastKeyboardState = new KeyboardState();
  27. GamePadState lastGamePadState = new GamePadState();
  28. KeyboardState currentKeyboardState = new KeyboardState();
  29. GamePadState currentGamePadState = new GamePadState();
  30. public Game1()
  31. {
  32. Content.RootDirectory = "Content";
  33. graphics = new GraphicsDeviceManager(this);
  34. bloom = new BloomComponent(this);
  35. Components.Add(bloom);
  36. }
  37. /// <summary>
  38. /// Load your graphics content.
  39. /// </summary>
  40. protected override void LoadContent()
  41. {
  42. spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  43. spriteFont = Content.Load<SpriteFont>("hudFont");
  44. background = Content.Load<Texture2D>("sunset");
  45. model = Content.Load<Model>("tank");
  46. }
  47. /// <summary>
  48. /// Allows the game to run logic.
  49. /// </summary>
  50. protected override void Update(GameTime gameTime)
  51. {
  52. HandleInput();
  53. base.Update(gameTime);
  54. }
  55. /// <summary>
  56. /// This is called when the game should draw itself.
  57. /// </summary>
  58. protected override void Draw(GameTime gameTime)
  59. {
  60. GraphicsDevice device = graphics.GraphicsDevice;
  61. Viewport viewport = device.Viewport;
  62. bloom.BeginDraw();
  63. device.Clear(Color.Black);
  64. // Draw the background image.
  65. spriteBatch.Begin(0, BlendState.Opaque);
  66. spriteBatch.Draw(background,
  67. new Rectangle(0, 0, viewport.Width, viewport.Height),
  68. Color.White);
  69. spriteBatch.End();
  70. // Draw the spinning model.
  71. device.DepthStencilState = DepthStencilState.Default;
  72. DrawModel(gameTime);
  73. // Draw other components (which includes the bloom).
  74. base.Draw(gameTime);
  75. // Display some text over the top. Note how we draw this after the bloom,
  76. // because we don't want the text to be affected by the postprocessing.
  77. DrawOverlayText();
  78. }
  79. /// <summary>
  80. /// Helper for drawing the spinning 3D model.
  81. /// </summary>
  82. void DrawModel(GameTime gameTime)
  83. {
  84. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  85. Viewport viewport = graphics.GraphicsDevice.Viewport;
  86. float aspectRatio = (float)viewport.Width / (float)viewport.Height;
  87. // Create camera matrices.
  88. Matrix world = Matrix.CreateRotationY(time * 0.42f);
  89. Matrix view = Matrix.CreateLookAt(new Vector3(750, 100, 0),
  90. new Vector3(0, 300, 0),
  91. Vector3.Up);
  92. Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspectRatio,
  93. 1, 10000);
  94. // Look up the bone transform matrices.
  95. Matrix[] transforms = new Matrix[model.Bones.Count];
  96. model.CopyAbsoluteBoneTransformsTo(transforms);
  97. // Draw the model.
  98. foreach (ModelMesh mesh in model.Meshes)
  99. {
  100. foreach (BasicEffect effect in mesh.Effects)
  101. {
  102. effect.World = transforms[mesh.ParentBone.Index] * world;
  103. effect.View = view;
  104. effect.Projection = projection;
  105. effect.EnableDefaultLighting();
  106. // Override the default specular color to make it nice and bright,
  107. // so we'll get some decent glints that the bloom can key off.
  108. effect.SpecularColor = Vector3.One;
  109. }
  110. mesh.Draw();
  111. }
  112. }
  113. /// <summary>
  114. /// Displays an overlay showing what the controls are,
  115. /// and which settings are currently selected.
  116. /// </summary>
  117. void DrawOverlayText()
  118. {
  119. string text = "A = settings (" + bloom.Settings.Name + ")\n" +
  120. "B = toggle bloom (" + (bloom.Visible ? "on" : "off") + ")\n" +
  121. "X = show buffer (" + bloom.ShowBuffer.ToString() + ")";
  122. spriteBatch.Begin();
  123. // Draw the string twice to create a drop shadow, first colored black
  124. // and offset one pixel to the bottom right, then again in white at the
  125. // intended position. This makes text easier to read over the background.
  126. spriteBatch.DrawString(spriteFont, text, new Vector2(65, 65), Color.Black);
  127. spriteBatch.DrawString(spriteFont, text, new Vector2(64, 64), Color.White);
  128. spriteBatch.End();
  129. }
  130. /// <summary>
  131. /// Handles input for quitting or changing the bloom settings.
  132. /// </summary>
  133. private void HandleInput()
  134. {
  135. lastKeyboardState = currentKeyboardState;
  136. lastGamePadState = currentGamePadState;
  137. currentKeyboardState = Keyboard.GetState();
  138. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  139. // Check for exit.
  140. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  141. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  142. {
  143. Exit();
  144. }
  145. // Switch to the next bloom settings preset?
  146. if ((currentGamePadState.Buttons.A == ButtonState.Pressed &&
  147. lastGamePadState.Buttons.A != ButtonState.Pressed) ||
  148. (currentKeyboardState.IsKeyDown(Keys.A) &&
  149. lastKeyboardState.IsKeyUp(Keys.A)))
  150. {
  151. bloomSettingsIndex = (bloomSettingsIndex + 1) %
  152. BloomSettings.PresetSettings.Length;
  153. bloom.Settings = BloomSettings.PresetSettings[bloomSettingsIndex];
  154. bloom.Visible = true;
  155. }
  156. // Toggle bloom on or off?
  157. if ((currentGamePadState.Buttons.B == ButtonState.Pressed &&
  158. lastGamePadState.Buttons.B != ButtonState.Pressed) ||
  159. (currentKeyboardState.IsKeyDown(Keys.B) &&
  160. lastKeyboardState.IsKeyUp(Keys.B)))
  161. {
  162. bloom.Visible = !bloom.Visible;
  163. }
  164. // Cycle through the intermediate buffer debug display modes?
  165. if ((currentGamePadState.Buttons.X == ButtonState.Pressed &&
  166. lastGamePadState.Buttons.X != ButtonState.Pressed) ||
  167. (currentKeyboardState.IsKeyDown(Keys.X) &&
  168. lastKeyboardState.IsKeyUp(Keys.X)))
  169. {
  170. bloom.Visible = true;
  171. bloom.ShowBuffer++;
  172. if (bloom.ShowBuffer > BloomComponent.IntermediateBuffer.FinalResult)
  173. bloom.ShowBuffer= 0;
  174. }
  175. }
  176. }
  177. }