PrimitiveBatch.cs 8.8 KB

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