4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
19 internal class ShapeBatch
21 const int DefaultBufferSize = 512;
23 VertexPositionColor[] vertexBuffer;
39 static readonly SamplerState samplerState =
new SamplerState
41 AddressU = TextureAddressMode.Clamp,
42 AddressW = TextureAddressMode.Clamp,
43 AddressV = TextureAddressMode.Clamp,
46 int iVertexBuffer = 0;
48 bool beginHasBeenCalled =
false;
50 public bool LightingEnabled =
true;
53 public void Initialize()
58 int vertexBufferSize = Math.Min( DefaultBufferSize, 65535 * 3 );
60 vertexBuffer =
new VertexPositionColor[vertexBufferSize];
61 indexBuffer =
new Int16[vertexBufferSize * 2];
64 public void Begin( ref Matrix matrix )
66 Debug.Assert( !beginHasBeenCalled );
67 beginHasBeenCalled =
true;
76 Debug.Assert( beginHasBeenCalled );
78 beginHasBeenCalled =
false;
83 if ( iIndexBuffer != 0 )
85 Game.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
86 Game.GraphicsDevice.SamplerStates[0] = samplerState;
88 effect = Graphics.GetColorEffect(ref matrix, LightingEnabled);
89 effect.CurrentTechnique.Passes[0].Apply();
91 Game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
92 PrimitiveType.TriangleList,
93 vertexBuffer, 0, iVertexBuffer,
102 public void Draw( Vector[] vertices, Int16[] indices, Color color, Vector2 position, Vector2 size,
float angle )
104 if ( ( iVertexBuffer + vertices.Length ) > vertexBuffer.Length ||
105 ( iIndexBuffer + indices.Length ) > indexBuffer.Length )
111 Matrix.CreateScale( size.X, size.Y, 1f )
112 * Matrix.CreateRotationZ( angle )
113 * Matrix.CreateTranslation( position.X, position.Y, 0 )
116 int startIndex = iVertexBuffer;
118 for (
int i = 0; i < vertices.Length; i++ )
120 Vector p = vertices[i];
121 Vector3 p3 =
new Vector3( (
float)p.X, (
float)p.Y, 0f );
122 vertexBuffer[iVertexBuffer].Position = Vector3.Transform( p3, matrix );
123 vertexBuffer[iVertexBuffer].Color = color.AsXnaColor();
127 for (
int i = 0; i < indices.Length; i++ )
129 indexBuffer[iIndexBuffer] = (Int16)( startIndex + indices[i] );