ImplicitOrderingGame.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace TestImplicitOrdering
  6. {
  7. public class ImplicitOrderingGame : Game
  8. {
  9. private SpriteBatch _spriteBatch;
  10. private SpriteFont _font;
  11. public ImplicitOrderingGame()
  12. {
  13. Content.RootDirectory = "Content";
  14. new GraphicsDeviceManager(this);
  15. }
  16. private const int NumberOfBatches = 3;
  17. private const int ItemsPerBatch = 3;
  18. private int _batchNumber = 0;
  19. private List<TestUpdateable> _updateables = new List<TestUpdateable>();
  20. private List<TestUpdateable> _updateablesInUpdateOrder = new List<TestUpdateable>();
  21. private bool _updateablesOrderedCorrectly;
  22. private List<TestDrawable> _drawables = new List<TestDrawable>();
  23. private List<TestDrawable> _drawablesInDrawOrder = new List<TestDrawable>();
  24. private bool _drawablesOrderedCorrectly;
  25. protected override void LoadContent()
  26. {
  27. base.LoadContent();
  28. _spriteBatch = new SpriteBatch(GraphicsDevice);
  29. _font = Content.Load<SpriteFont>("fntStandard");
  30. }
  31. protected override void UnloadContent()
  32. {
  33. base.UnloadContent();
  34. _font = null;
  35. }
  36. protected override void Update(GameTime gameTime)
  37. {
  38. if (_batchNumber < NumberOfBatches &&
  39. gameTime.TotalGameTime >= TimeSpan.FromSeconds(_batchNumber))
  40. {
  41. for (int i = 0; i < ItemsPerBatch; ++i)
  42. {
  43. var updateable = new TestUpdateable(this);
  44. _updateables.Add(updateable);
  45. Components.Add(updateable);
  46. var drawable = new TestDrawable(this);
  47. _drawables.Add(drawable);
  48. Components.Add(drawable);
  49. }
  50. _batchNumber++;
  51. }
  52. base.Update(gameTime);
  53. _updateablesOrderedCorrectly = ListsEqual(_updateables, _updateablesInUpdateOrder);
  54. _updateablesInUpdateOrder.Clear();
  55. }
  56. protected override void Draw(GameTime gameTime)
  57. {
  58. GraphicsDevice.Clear(Color.CornflowerBlue);
  59. base.Draw(gameTime);
  60. _drawablesOrderedCorrectly = ListsEqual(_drawables, _drawablesInDrawOrder);
  61. _drawablesInDrawOrder.Clear();
  62. _spriteBatch.Begin();
  63. DrawStatusString(
  64. string.Format("{0} updateables", _updateables.Count),
  65. 1, _updateablesOrderedCorrectly);
  66. DrawStatusString(
  67. string.Format("{0} drawables", _drawables.Count),
  68. 0, _drawablesOrderedCorrectly);
  69. _spriteBatch.End();
  70. }
  71. private void DrawStatusString(string item, int linesFromBottom, bool isCorrect)
  72. {
  73. var position = new Vector2(
  74. 10, GraphicsDevice.Viewport.Height - ((1 + linesFromBottom) * _font.LineSpacing));
  75. if (isCorrect)
  76. _spriteBatch.DrawString(_font, item + " correctly ordered!", position, Color.Lime);
  77. else
  78. _spriteBatch.DrawString(_font, item + " incorrectly ordered.", position, Color.Red);
  79. }
  80. private bool ListsEqual<T>(IList<T> a, IList<T> b)
  81. {
  82. if (a.Count != b.Count)
  83. return false;
  84. var equalityComparer = EqualityComparer<T>.Default;
  85. for (int i = 0; i < a.Count; ++i)
  86. if (!equalityComparer.Equals(a[i], b[i]))
  87. return false;
  88. return true;
  89. }
  90. private class TestUpdateable : GameComponent
  91. {
  92. public TestUpdateable(Game game) : base(game) { }
  93. public override void Update(GameTime gameTime)
  94. {
  95. base.Update(gameTime);
  96. var game = (ImplicitOrderingGame)Game;
  97. game._updateablesInUpdateOrder.Add(this);
  98. }
  99. }
  100. private class TestDrawable : DrawableGameComponent
  101. {
  102. private static int InstanceCount = 0;
  103. private static readonly Color[] Colors = new Color[]
  104. {
  105. Color.White, Color.Red, Color.Orange, Color.Yellow, Color.Green,
  106. Color.Blue, Color.Indigo, Color.Violet, Color.Black
  107. };
  108. private int _number;
  109. private Color _color;
  110. public TestDrawable(Game game) : base(game)
  111. {
  112. _number = ++InstanceCount;
  113. _color = Colors[_number % Colors.Length];
  114. }
  115. private SpriteBatch _spriteBatch;
  116. protected override void LoadContent()
  117. {
  118. base.LoadContent();
  119. _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
  120. }
  121. protected override void UnloadContent()
  122. {
  123. base.UnloadContent();
  124. _spriteBatch.Dispose();
  125. _spriteBatch = null;
  126. }
  127. public override void Draw(GameTime gameTime)
  128. {
  129. var game = (ImplicitOrderingGame)Game;
  130. game._drawablesInDrawOrder.Add(this);
  131. float halfEx = game._font.MeasureString("x").X / 2;
  132. var position = new Vector2(_number * halfEx, 0);
  133. _spriteBatch.Begin();
  134. _spriteBatch.DrawString(game._font, _number.ToString(), position, _color);
  135. _spriteBatch.End();
  136. }
  137. }
  138. }
  139. }