3 using System.Collections.Generic;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
10 internal class TextureCoordinates
12 public Vector2 TopLeft;
13 public Vector2 TopRight;
14 public Vector2 BottomLeft;
15 public Vector2 BottomRight;
28 internal class ImageBatch
30 private const int DefaultBufferSize = 512;
32 static readonly Vector3[] Vertices =
new Vector3[]
35 new Vector3(-0.5f, 0.5f, 0),
36 new Vector3(-0.5f, -0.5f, 0),
37 new Vector3(0.5f, 0.5f, 0),
40 new Vector3(-0.5f, -0.5f, 0),
41 new Vector3(0.5f, -0.5f, 0),
42 new Vector3(0.5f, 0.5f, 0),
45 static readonly
int VerticesPerTexture = Vertices.Length;
50 VertexPositionTexture[] vertexBuffer;
53 bool beginHasBeenCalled =
false;
55 public bool LightingEnabled =
true;
62 public void Initialize()
67 this.BufferSize = Math.Min( DefaultBufferSize, 65535 / 2 );
68 vertexBuffer =
new VertexPositionTexture[BufferSize * VerticesPerTexture];
71 public void Begin( ref Matrix matrix, Texture2D texture )
73 Debug.Assert( !beginHasBeenCalled );
74 beginHasBeenCalled =
true;
77 this.texture = texture;
83 Debug.Assert( beginHasBeenCalled );
87 int textureCount = iTexture;
88 beginHasBeenCalled =
false;
95 var device = Game.GraphicsDevice;
96 device.RasterizerState = RasterizerState.CullClockwise;
97 device.BlendState = BlendState.AlphaBlend;
99 effect = Graphics.GetTextureEffect(ref matrix, texture, LightingEnabled);
100 effect.CurrentTechnique.Passes[0].Apply();
105 device.SamplerStates[0] = Graphics.GetDefaultSamplerState();
107 device.DrawUserPrimitives<VertexPositionTexture>(
108 PrimitiveType.TriangleList,
116 public void Draw( TextureCoordinates c, Vector2 position, Vector2 size,
float angle )
118 Debug.Assert( beginHasBeenCalled );
120 if ( iTexture >= BufferSize )
124 Matrix.CreateScale( size.X, size.Y, 1f )
125 * Matrix.CreateRotationZ( angle )
126 * Matrix.CreateTranslation( position.X, position.Y, 0 )
128 Vector3[] transformedPoints =
new Vector3[VerticesPerTexture];
129 Vector3.Transform( Vertices, ref matrix, transformedPoints );
131 int startIndex = ( iTexture * VerticesPerTexture );
133 for (
int i = 0; i < VerticesPerTexture; i++ )
135 int bi = ( iTexture * VerticesPerTexture ) + i;
136 vertexBuffer[bi].Position = transformedPoints[i];
140 vertexBuffer[startIndex + 0].TextureCoordinate = c.TopLeft;
141 vertexBuffer[startIndex + 1].TextureCoordinate = c.BottomLeft;
142 vertexBuffer[startIndex + 2].TextureCoordinate = c.TopRight;
145 vertexBuffer[startIndex + 3].TextureCoordinate = c.BottomLeft;
146 vertexBuffer[startIndex + 4].TextureCoordinate = c.BottomRight;
147 vertexBuffer[startIndex + 5].TextureCoordinate = c.TopRight;