PrimitiveBatch.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace StarWarrior.Primitives
  9. {
  10. // PrimitiveBatch is a class that handles efficient rendering automatically for its
  11. // users, in a similar way to SpriteBatch. PrimitiveBatch can render lines, points,
  12. // and triangles to the screen. In this sample, it is used to draw a spacewars
  13. // retro scene.
  14. public class PrimitiveBatch : IDisposable
  15. {
  16. #region Constants and Fields
  17. // this constant controls how large the vertices buffer is. Larger buffers will
  18. // require flushing less often, which can increase performance. However, having
  19. // buffer that is unnecessarily large will waste memory.
  20. const int DefaultBufferSize = 500;
  21. // a block of vertices that calling AddVertex will fill. Flush will draw using
  22. // this array, and will determine how many primitives to draw from
  23. // positionInBuffer.
  24. VertexPositionColor[] vertices = new VertexPositionColor[DefaultBufferSize];
  25. // keeps track of how many vertices have been added. this value increases until
  26. // we run out of space in the buffer, at which time Flush is automatically
  27. // called.
  28. int positionInBuffer = 0;
  29. // a basic effect, which contains the shaders that we will use to draw our
  30. // primitives.
  31. BasicEffect basicEffect;
  32. // the device that we will issue draw calls to.
  33. GraphicsDevice device;
  34. // this value is set by Begin, and is the type of primitives that we are
  35. // drawing.
  36. PrimitiveType primitiveType;
  37. // how many verts does each of these primitives take up? points are 1,
  38. // lines are 2, and triangles are 3.
  39. int numVertsPerPrimitive;
  40. // hasBegun is flipped to true once Begin is called, and is used to make
  41. // sure users don't call End before Begin is called.
  42. bool hasBegun = false;
  43. bool isDisposed = false;
  44. #endregion
  45. // the constructor creates a new PrimitiveBatch and sets up all of the internals
  46. // that PrimitiveBatch will need.
  47. public PrimitiveBatch(GraphicsDevice graphicsDevice)
  48. {
  49. if (graphicsDevice == null)
  50. {
  51. throw new ArgumentNullException("graphicsDevice");
  52. }
  53. device = graphicsDevice;
  54. // set up a new basic effect, and enable vertex colors.
  55. basicEffect = new BasicEffect(graphicsDevice);
  56. basicEffect.VertexColorEnabled = true;
  57. // projection uses CreateOrthographicOffCenter to create 2d projection
  58. // matrix with 0,0 in the upper left.
  59. basicEffect.Projection = Matrix.CreateOrthographicOffCenter
  60. (0, graphicsDevice.Viewport.Width,
  61. graphicsDevice.Viewport.Height, 0,
  62. 0, 1);
  63. }
  64. public void Dispose()
  65. {
  66. this.Dispose(true);
  67. GC.SuppressFinalize(this);
  68. }
  69. protected virtual void Dispose(bool disposing)
  70. {
  71. if (disposing && !isDisposed)
  72. {
  73. if (basicEffect != null)
  74. basicEffect.Dispose();
  75. isDisposed = true;
  76. }
  77. }
  78. // Begin is called to tell the PrimitiveBatch what kind of primitives will be
  79. // drawn, and to prepare the graphics card to render those primitives.
  80. public void Begin(PrimitiveType primitiveType)
  81. {
  82. if (hasBegun)
  83. {
  84. throw new InvalidOperationException
  85. ("End must be called before Begin can be called again.");
  86. }
  87. // these three types reuse vertices, so we can't flush properly without more
  88. // complex logic. Since that's a bit too complicated for this sample, we'll
  89. // simply disallow them.
  90. if (primitiveType == PrimitiveType.LineStrip ||
  91. primitiveType == PrimitiveType.TriangleStrip)
  92. {
  93. throw new NotSupportedException
  94. ("The specified primitiveType is not supported by PrimitiveBatch.");
  95. }
  96. this.primitiveType = primitiveType;
  97. // how many verts will each of these primitives require?
  98. this.numVertsPerPrimitive = NumVertsPerPrimitive(primitiveType);
  99. //tell our basic effect to begin.
  100. basicEffect.CurrentTechnique.Passes[0].Apply();
  101. // flip the error checking boolean. It's now ok to call AddVertex, Flush,
  102. // and End.
  103. hasBegun = true;
  104. }
  105. // AddVertex is called to add another vertex to be rendered. To draw a point,
  106. // AddVertex must be called once. for lines, twice, and for triangles 3 times.
  107. // this function can only be called once begin has been called.
  108. // if there is not enough room in the vertices buffer, Flush is called
  109. // automatically.
  110. public void AddVertex(Vector2 vertex, Color color)
  111. {
  112. if (!hasBegun)
  113. {
  114. throw new InvalidOperationException
  115. ("Begin must be called before AddVertex can be called.");
  116. }
  117. // are we starting a new primitive? if so, and there will not be enough room
  118. // for a whole primitive, flush.
  119. bool newPrimitive = ((positionInBuffer % numVertsPerPrimitive) == 0);
  120. if (newPrimitive &&
  121. (positionInBuffer + numVertsPerPrimitive) >= vertices.Length)
  122. {
  123. Flush();
  124. }
  125. // once we know there's enough room, set the vertex in the buffer,
  126. // and increase position.
  127. vertices[positionInBuffer].Position = new Vector3(vertex, 0);
  128. vertices[positionInBuffer].Color = color;
  129. positionInBuffer++;
  130. }
  131. // End is called once all the primitives have been drawn using AddVertex.
  132. // it will call Flush to actually submit the draw call to the graphics card, and
  133. // then tell the basic effect to end.
  134. public void End()
  135. {
  136. if (!hasBegun)
  137. {
  138. throw new InvalidOperationException
  139. ("Begin must be called before End can be called.");
  140. }
  141. // Draw whatever the user wanted us to draw
  142. Flush();
  143. hasBegun = false;
  144. }
  145. // Flush is called to issue the draw call to the graphics card. Once the draw
  146. // call is made, positionInBuffer is reset, so that AddVertex can start over
  147. // at the beginning. End will call this to draw the primitives that the user
  148. // requested, and AddVertex will call this if there is not enough room in the
  149. // buffer.
  150. private void Flush()
  151. {
  152. if (!hasBegun)
  153. {
  154. throw new InvalidOperationException
  155. ("Begin must be called before Flush can be called.");
  156. }
  157. // no work to do
  158. if (positionInBuffer == 0)
  159. {
  160. return;
  161. }
  162. // how many primitives will we draw?
  163. int primitiveCount = positionInBuffer / numVertsPerPrimitive;
  164. // submit the draw call to the graphics card
  165. device.DrawUserPrimitives<VertexPositionColor>(primitiveType, vertices, 0,
  166. primitiveCount);
  167. // now that we've drawn, it's ok to reset positionInBuffer back to zero,
  168. // and write over any vertices that may have been set previously.
  169. positionInBuffer = 0;
  170. }
  171. #region Helper functions
  172. // NumVertsPerPrimitive is a boring helper function that tells how many vertices
  173. // it will take to draw each kind of primitive.
  174. static private int NumVertsPerPrimitive(PrimitiveType primitive)
  175. {
  176. int numVertsPerPrimitive;
  177. switch (primitive)
  178. {
  179. case PrimitiveType.LineList:
  180. numVertsPerPrimitive = 2;
  181. break;
  182. case PrimitiveType.TriangleList:
  183. numVertsPerPrimitive = 3;
  184. break;
  185. default:
  186. throw new InvalidOperationException("primitive is not valid");
  187. }
  188. return numVertsPerPrimitive;
  189. }
  190. #endregion
  191. }
  192. }