Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. // Game1.cs
  2. using System;
  3. using System.Reflection;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using Myra.Utility;
  8.  
  9. namespace Game3
  10. {
  11. /// <summary>
  12. /// This is the main type for your game.
  13. /// </summary>
  14. public class Game1 : Game
  15. {
  16. private enum Mode
  17. {
  18. BeginEnd,
  19. ReflectionFlush,
  20. Immediate,
  21. OneBatch
  22. }
  23.  
  24. private readonly GraphicsDeviceManager _graphics;
  25. private readonly FPSCounter _fpsCounter = new FPSCounter();
  26. private SpriteBatch _spriteBatch;
  27. private SpriteFont _spriteFont;
  28. private Mode _mode = Mode.BeginEnd;
  29. private KeyboardState _lastState;
  30. private object[] _parameters = new object[] { SpriteSortMode.Deferred, null };
  31.  
  32. public Game1()
  33. {
  34. _graphics = new GraphicsDeviceManager(this);
  35. this.InactiveSleepTime = TimeSpan.FromTicks(1);
  36. this.IsFixedTimeStep = false;
  37. this.TargetElapsedTime = TimeSpan.FromTicks(1);
  38.  
  39. _graphics.SynchronizeWithVerticalRetrace = false;
  40. this._graphics.ApplyChanges();
  41.  
  42. Content.RootDirectory = "Content";
  43.  
  44. IsMouseVisible = true;
  45. }
  46.  
  47. /// <summary>
  48. /// LoadContent will be called once per game and is the place to load
  49. /// all of your content.
  50. /// </summary>
  51. protected override void LoadContent()
  52. {
  53. // Create a new SpriteBatch, which can be used to draw textures.
  54. _spriteBatch = new SpriteBatch(GraphicsDevice);
  55.  
  56. // TODO: use this.Content to load your game content here
  57. _spriteFont = Content.Load<SpriteFont>("MyFont");
  58.  
  59. this.RasterizerState = new RasterizerState
  60. {
  61. ScissorTestEnable = true
  62. };
  63. Setup = _spriteBatch.GetType().GetMethod("Setup", BindingFlags.Instance | BindingFlags.NonPublic);
  64. BatcherField = _spriteBatch.GetType().GetField("_batcher", BindingFlags.Instance | BindingFlags.NonPublic);
  65. Batcher = BatcherField.GetValue(_spriteBatch);
  66. DrawBatch = Batcher.GetType().GetMethod("DrawBatch");
  67. }
  68.  
  69. public object Batcher { get; private set; }
  70.  
  71. public RasterizerState RasterizerState { get; set; }
  72. public MethodInfo Setup { get; private set; }
  73. public FieldInfo BatcherField { get; private set; }
  74. public MethodInfo DrawBatch { get; private set; }
  75.  
  76. /// <summary>
  77. /// UnloadContent will be called once per game and is the place to unload
  78. /// game-specific content.
  79. /// </summary>
  80. protected override void UnloadContent()
  81. {
  82. // TODO: Unload any non ContentManager content here
  83. }
  84.  
  85. /// <summary>
  86. /// Allows the game to run logic such as updating the world,
  87. /// checking for collisions, gathering input, and playing audio.
  88. /// </summary>
  89. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  90. protected override void Update(GameTime gameTime)
  91. {
  92. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  93. Exit();
  94.  
  95. // TODO: Add your update logic here
  96. KeyboardState keyboard = Keyboard.GetState();
  97. if (keyboard.IsKeyDown(Keys.Space) && !_lastState.IsKeyDown(Keys.Space))
  98. {
  99. switch ( _mode )
  100. {
  101.  
  102. case Mode.BeginEnd :
  103. _mode = Mode.ReflectionFlush;
  104. break;
  105. case Mode.ReflectionFlush :
  106. _mode = Mode.Immediate;
  107. break;
  108. case Mode.Immediate :
  109. _mode = Mode.OneBatch;
  110. break;
  111. case Mode.OneBatch :
  112. _mode = Mode.BeginEnd;
  113. break;
  114. default :
  115. throw new ArgumentOutOfRangeException();
  116. }
  117. }
  118.  
  119. _lastState = keyboard;
  120.  
  121. base.Update(gameTime);
  122. }
  123.  
  124. /// <summary>
  125. /// This is called when the game should draw itself.
  126. /// </summary>
  127. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  128. protected override void Draw(GameTime gameTime)
  129. {
  130. _fpsCounter.Update();
  131.  
  132. GraphicsDevice.Clear(Color.CornflowerBlue);
  133.  
  134. // TODO: Add your drawing code here
  135.  
  136. _spriteBatch.Begin(_mode == Mode.Immediate ? SpriteSortMode.Immediate : SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp,
  137. null, RasterizerState);
  138.  
  139. if (_mode == Mode.ReflectionFlush)
  140. {
  141. // We need to explicity call Setup
  142.  
  143. Setup.Invoke(_spriteBatch, null);
  144. }
  145.  
  146. float y = 0;
  147. for (int i = 0; i < 500; i++)
  148. {
  149. _spriteBatch.DrawString(_spriteFont, "Hello, World!", new Vector2(y, y), Color.AliceBlue);
  150.  
  151. switch ( _mode )
  152. {
  153. case Mode.BeginEnd :
  154. _spriteBatch.End();
  155. _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, RasterizerState);
  156. break;
  157. case Mode.ReflectionFlush :
  158.  
  159.  
  160. DrawBatch.Invoke(Batcher, _parameters);
  161. break;
  162. case Mode.OneBatch:
  163. break;
  164. }
  165.  
  166. y += 0.9f;
  167. }
  168.  
  169. _spriteBatch.DrawString(_spriteFont, string.Format("Mode: {0}", _mode), new Vector2(200, 0), Color.White);
  170. _spriteBatch.DrawString(_spriteFont, string.Format("FPS: {0:0.##}", _fpsCounter.FPS), new Vector2(200, 20), Color.White);
  171.  
  172.  
  173. _spriteBatch.End();
  174.  
  175.  
  176. base.Draw(gameTime);
  177. }
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement