ImplicitOrderingGame.cs 5.6 KB

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