Class to handle efficient rendering of lines, points, and triangles to the screen. More...
Public Member Functions | |
| PrimitiveBatch (GraphicsDevice graphicsDevice) | |
| Creates a new PrimitiveBatch instance. | |
| void | Dispose () |
| Dispose interface. | |
| void | Begin (PrimitiveType primitiveType) |
| Begin is called to tell the PrimitiveBatch what kind of primitives will be drawn, and to prepare the graphics card to render those primitives. | |
| void | AddVertex (Vector2 vertex, Color color) |
| AddVertex is called to add another vertex to be rendered. To draw a point, AddVertex must be called once. for lines, twice, and for triangles 3 times. this function can only be called once begin has been called. if there is not enough room in the vertices buffer, Flush is called automatically. | |
| void | End () |
| End is called once all the primitives have been drawn using AddVertex. it will call Flush to actually submit the draw call to the graphics card, and then tell the basic effect to end. | |
Protected Member Functions | |
| virtual void | Dispose (bool disposing) |
| Dispose interface with flag. | |
Class to handle efficient rendering of lines, points, and triangles to the screen.
Definition at line 16 of file PrimitiveBatch.cs.
| NewGamePhysics.Utilities.PrimitiveBatch.PrimitiveBatch | ( | GraphicsDevice | graphicsDevice | ) |
Creates a new PrimitiveBatch instance.
Definition at line 87 of file PrimitiveBatch.cs.
00088 { 00089 if (graphicsDevice == null) 00090 { 00091 throw new ArgumentNullException("graphicsDevice"); 00092 } 00093 00094 device = graphicsDevice; 00095 00096 // create a vertex declaration, which tells the graphics card what kind of 00097 // data to expect during a draw call. We're drawing using 00098 // VertexPositionColors, so we'll use those vertex elements. 00099 vertexDeclaration = new VertexDeclaration(graphicsDevice, 00100 VertexPositionColor.VertexElements); 00101 00102 // set up a new basic effect, and enable vertex colors. 00103 basicEffect = new BasicEffect(graphicsDevice, null); 00104 basicEffect.VertexColorEnabled = true; 00105 00106 // projection uses CreateOrthographicOffCenter to create 2d projection 00107 // matrix with 0,0 in the upper left. 00108 basicEffect.Projection = Matrix.CreateOrthographicOffCenter 00109 (0, graphicsDevice.Viewport.Width, 00110 graphicsDevice.Viewport.Height, 0, 00111 0, 1); 00112 }
| void NewGamePhysics.Utilities.PrimitiveBatch.AddVertex | ( | Vector2 | vertex, | |
| Color | color | |||
| ) |
AddVertex is called to add another vertex to be rendered. To draw a point, AddVertex must be called once. for lines, twice, and for triangles 3 times. this function can only be called once begin has been called. if there is not enough room in the vertices buffer, Flush is called automatically.
| vertex | The vertex to add. | |
| color | The color of this vertex. |
Definition at line 193 of file PrimitiveBatch.cs.
00194 { 00195 if (!hasBegun) 00196 { 00197 throw new InvalidOperationException 00198 ("Begin must be called before AddVertex can be called."); 00199 } 00200 00201 // are we starting a new primitive? if so, and there will not be enough room 00202 // for a whole primitive, flush. 00203 bool newPrimitive = ((positionInBuffer % numVertsPerPrimitive) == 0); 00204 00205 if (newPrimitive && 00206 (positionInBuffer + numVertsPerPrimitive) >= vertices.Length) 00207 { 00208 Flush(); 00209 } 00210 00211 // once we know there's enough room, set the vertex in the buffer, 00212 // and increase position. 00213 vertices[positionInBuffer].Position = new Vector3(vertex, 0); 00214 vertices[positionInBuffer].Color = color; 00215 00216 positionInBuffer++; 00217 }
| void NewGamePhysics.Utilities.PrimitiveBatch.Begin | ( | PrimitiveType | primitiveType | ) |
Begin is called to tell the PrimitiveBatch what kind of primitives will be drawn, and to prepare the graphics card to render those primitives.
| primitiveType | The primitive types to draw. |
Definition at line 150 of file PrimitiveBatch.cs.
00151 { 00152 if (hasBegun) 00153 { 00154 throw new InvalidOperationException 00155 ("End must be called before Begin can be called again."); 00156 } 00157 00158 // these three types reuse vertices, so we can't flush properly without more 00159 // complex logic. 00160 if (primitiveType == PrimitiveType.LineStrip || 00161 primitiveType == PrimitiveType.TriangleFan || 00162 primitiveType == PrimitiveType.TriangleStrip) 00163 { 00164 throw new NotSupportedException 00165 ("The specified primitiveType is not supported by PrimitiveBatch."); 00166 } 00167 00168 this.primitiveType = primitiveType; 00169 00170 // how many verts will each of these primitives require? 00171 this.numVertsPerPrimitive = NumVertsPerPrimitive(primitiveType); 00172 00173 // prepare the graphics device for drawing by setting the vertex declaration 00174 // and telling our basic effect to begin. 00175 device.VertexDeclaration = vertexDeclaration; 00176 basicEffect.Begin(); 00177 basicEffect.CurrentTechnique.Passes[0].Begin(); 00178 00179 // flip the error checking boolean. It's now ok to call AddVertex, Flush, 00180 // and End. 00181 hasBegun = true; 00182 }
| virtual void NewGamePhysics.Utilities.PrimitiveBatch.Dispose | ( | bool | disposing | ) | [protected, virtual] |
Dispose interface with flag.
| disposing | Flag indicating if we are disposing the object. |
Definition at line 127 of file PrimitiveBatch.cs.
| void NewGamePhysics.Utilities.PrimitiveBatch.Dispose | ( | ) |
Dispose interface.
Definition at line 117 of file PrimitiveBatch.cs.
00118 { 00119 this.Dispose(true); 00120 GC.SuppressFinalize(this); 00121 }
| void NewGamePhysics.Utilities.PrimitiveBatch.End | ( | ) |
End is called once all the primitives have been drawn using AddVertex. it will call Flush to actually submit the draw call to the graphics card, and then tell the basic effect to end.
Definition at line 224 of file PrimitiveBatch.cs.
00225 { 00226 if (!hasBegun) 00227 { 00228 throw new InvalidOperationException 00229 ("Begin must be called before End can be called."); 00230 } 00231 00232 // Draw whatever the user wanted us to draw 00233 Flush(); 00234 00235 // and then tell basic effect that we're done. 00236 basicEffect.CurrentTechnique.Passes[0].End(); 00237 basicEffect.End(); 00238 hasBegun = false; 00239 }
1.6.2