Game1.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SplitScreenGame.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. #endregion
  15. namespace Samples.FXAA
  16. {
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. SpriteFont font;
  22. KeyboardState previousKeyboardState;
  23. bool useFXAA = true;
  24. bool rotate = true;
  25. Vector3 cameraPosition;
  26. Matrix world;
  27. Matrix projection;
  28. Matrix view;
  29. Spaceship spaceship;
  30. Vector3 spaceshipPos = Vector3.Zero;
  31. float time = 0;
  32. AntiAliasing _antiAliasing;
  33. public Game1()
  34. {
  35. graphics = new GraphicsDeviceManager(this);
  36. Content.RootDirectory = "Content";
  37. graphics.GraphicsProfile = GraphicsProfile.HiDef;
  38. #if WINDOWS_PHONE
  39. graphics.IsFullScreen = true;
  40. TargetElapsedTime = TimeSpan.FromTicks(333333);
  41. #endif
  42. }
  43. protected override void LoadContent()
  44. {
  45. // Create and load our tank
  46. spaceship = new Spaceship();
  47. spaceship.Load(Content);
  48. spriteBatch = new SpriteBatch(GraphicsDevice);
  49. font = Content.Load<SpriteFont>("font");
  50. projection = Matrix.CreatePerspectiveFieldOfView(
  51. MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 10f, 10000f);
  52. _antiAliasing = new AntiAliasing(GraphicsDevice);
  53. }
  54. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  55. protected override void Update(GameTime gameTime)
  56. {
  57. KeyboardState keyState = Keyboard.GetState();
  58. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  59. if (keyState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
  60. Exit();
  61. if (keyState.IsKeyDown(Keys.F1) && !previousKeyboardState.IsKeyDown(Keys.F1))
  62. useFXAA = !useFXAA;
  63. if (keyState.IsKeyDown(Keys.F2) && !previousKeyboardState.IsKeyDown(Keys.F2))
  64. rotate = !rotate;
  65. if (rotate)
  66. time += (float)gameTime.ElapsedGameTime.TotalSeconds;
  67. world = Matrix.CreateFromAxisAngle(Vector3.Up, time);
  68. cameraPosition = new Vector3(0, 2800f, 2800f);
  69. view = Matrix.CreateLookAt(
  70. cameraPosition,
  71. Vector3.Zero,
  72. Vector3.Up);
  73. previousKeyboardState = keyState;
  74. base.Update(gameTime);
  75. }
  76. protected override void Draw(GameTime gameTime)
  77. {
  78. GraphicsDevice.BlendState = BlendState.Opaque;
  79. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  80. _antiAliasing.SetRenderTarget(GraphicsDevice.Viewport, Color.Black);
  81. spaceship.Draw(world, view, projection);
  82. GraphicsDevice.SetRenderTarget(null);
  83. _antiAliasing.DrawRenderTarget( (useFXAA ? 3 : 0), GraphicsDevice.Viewport, false);
  84. spriteBatch.Begin();
  85. spriteBatch.DrawString(font, String.Format("[F1] FXAA - ({0})", useFXAA ? "ON" : "OFF"), new Vector2(20, 20), Color.White);
  86. spriteBatch.DrawString(font, String.Format("[F2] Rotate - ({0})", rotate ? "ON" : "OFF"), new Vector2(20, 40), Color.White);
  87. spriteBatch.End();
  88. base.Draw(gameTime);
  89. }
  90. }
  91. }