Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SpriteDrawBuffer.cs
Go to the documentation of this file.
1using System;
5
7
8public class SpriteDrawBuffer
9{
11
13
15
17
18 private Texture[] textures = new Texture[0];
19
20 private int maxSprites;
21
22 private int vertexCount;
23
25
27
29 {
30 this.graphicsDevice = graphicsDevice;
31 maxSprites = defaultSize;
33 }
34
36 {
37 if (this.graphicsDevice != graphicsDevice)
38 {
39 this.graphicsDevice = graphicsDevice;
41 }
42 }
43
44 private void CreateBuffers()
45 {
46 if (vertexBuffer != null)
47 {
49 }
51 if (indexBuffer != null)
52 {
54 }
55 indexBuffer = new IndexBuffer(graphicsDevice, typeof(ushort), maxSprites * 6, BufferUsage.WriteOnly);
57 Array.Resize(ref vertices, maxSprites * 6);
58 Array.Resize(ref textures, maxSprites);
59 }
60
61 private static ushort[] GenIndexBuffer(int maxSprites)
62 {
63 ushort[] array = new ushort[maxSprites * 6];
64 int num = 0;
65 ushort num2 = 0;
66 while (num < maxSprites)
67 {
68 array[num++] = num2;
69 array[num++] = (ushort)(num2 + 1);
70 array[num++] = (ushort)(num2 + 2);
71 array[num++] = (ushort)(num2 + 3);
72 array[num++] = (ushort)(num2 + 2);
73 array[num++] = (ushort)(num2 + 1);
74 num2 += 4;
75 }
76 return array;
77 }
78
79 public void UploadAndBind()
80 {
81 if (vertexCount > 0)
82 {
83 vertexBuffer.SetData(vertices, 0, vertexCount, SetDataOptions.Discard);
84 }
85 vertexCount = 0;
86 Bind();
87 }
88
96
97 public void Unbind()
98 {
100 graphicsDevice.Indices = preBindIndexBuffer;
102 preBindIndexBuffer = null;
103 }
104
105 public void DrawRange(int index, int count)
106 {
108 graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, index * 4, 0, count * 4, 0, count * 2);
109 }
110
111 public void DrawSingle(int index)
112 {
113 DrawRange(index, 1);
114 }
115
116 public void Draw(Texture2D texture, Vector2 position, VertexColors colors)
117 {
118 Draw(texture, position, null, colors, 0f, Vector2.Zero, 1f, SpriteEffects.None);
119 }
120
121 public void Draw(Texture2D texture, Rectangle destination, VertexColors colors)
122 {
123 Draw(texture, destination, null, colors);
124 }
125
126 public void Draw(Texture2D texture, Rectangle destination, Rectangle? sourceRectangle, VertexColors colors)
127 {
128 Draw(texture, destination, sourceRectangle, colors, 0f, Vector2.Zero, SpriteEffects.None);
129 }
130
131 public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, VertexColors color, float rotation, Vector2 origin, float scale, SpriteEffects effects)
132 {
133 Draw(texture, position, sourceRectangle, color, rotation, origin, new Vector2(scale, scale), effects);
134 }
135
136 public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects)
137 {
138 float z;
139 float w;
140 if (sourceRectangle.HasValue)
141 {
142 z = (float)sourceRectangle.Value.Width * scale.X;
143 w = (float)sourceRectangle.Value.Height * scale.Y;
144 }
145 else
146 {
147 z = (float)texture.Width * scale.X;
148 w = (float)texture.Height * scale.Y;
149 }
150 Draw(texture, new Vector4(position.X, position.Y, z, w), sourceRectangle, colors, rotation, origin, effects, 0f);
151 }
152
153 public void Draw(Texture2D texture, Rectangle destination, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, SpriteEffects effects)
154 {
155 Draw(texture, new Vector4(destination.X, destination.Y, destination.Width, destination.Height), sourceRectangle, colors, rotation, origin, effects, 0f);
156 }
157
158 public void Draw(Texture2D texture, Vector4 destinationRectangle, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, SpriteEffects effect, float depth)
159 {
160 Vector4 sourceRectangle2 = default(Vector4);
161 if (sourceRectangle.HasValue)
162 {
163 sourceRectangle2.X = sourceRectangle.Value.X;
164 sourceRectangle2.Y = sourceRectangle.Value.Y;
165 sourceRectangle2.Z = sourceRectangle.Value.Width;
166 sourceRectangle2.W = sourceRectangle.Value.Height;
167 }
168 else
169 {
170 sourceRectangle2.X = 0f;
171 sourceRectangle2.Y = 0f;
172 sourceRectangle2.Z = texture.Width;
173 sourceRectangle2.W = texture.Height;
174 }
175 Vector2 texCoordTL = default(Vector2);
176 texCoordTL.X = sourceRectangle2.X / (float)texture.Width;
177 texCoordTL.Y = sourceRectangle2.Y / (float)texture.Height;
178 Vector2 texCoordBR = default(Vector2);
179 texCoordBR.X = (sourceRectangle2.X + sourceRectangle2.Z) / (float)texture.Width;
180 texCoordBR.Y = (sourceRectangle2.Y + sourceRectangle2.W) / (float)texture.Height;
181 if ((effect & SpriteEffects.FlipVertically) != 0)
182 {
183 float y = texCoordBR.Y;
184 texCoordBR.Y = texCoordTL.Y;
185 texCoordTL.Y = y;
186 }
187 if ((effect & SpriteEffects.FlipHorizontally) != 0)
188 {
189 float x = texCoordBR.X;
190 texCoordBR.X = texCoordTL.X;
191 texCoordTL.X = x;
192 }
193 QueueSprite(destinationRectangle, -origin, colors, sourceRectangle2, texCoordTL, texCoordBR, texture, depth, rotation);
194 }
195
196 private void QueueSprite(Vector4 destinationRect, Vector2 origin, VertexColors colors, Vector4 sourceRectangle, Vector2 texCoordTL, Vector2 texCoordBR, Texture2D texture, float depth, float rotation)
197 {
198 float num = origin.X / sourceRectangle.Z;
199 float num2 = origin.Y / sourceRectangle.W;
200 float x = destinationRect.X;
201 float y = destinationRect.Y;
202 float z = destinationRect.Z;
203 float w = destinationRect.W;
204 float num3 = num * z;
205 float num4 = num2 * w;
206 float num5;
207 float num6;
208 if (rotation != 0f)
209 {
210 num5 = (float)Math.Cos(rotation);
211 num6 = (float)Math.Sin(rotation);
212 }
213 else
214 {
215 num5 = 1f;
216 num6 = 0f;
217 }
218 if (vertexCount + 4 >= maxSprites * 4)
219 {
220 maxSprites *= 2;
222 }
223 textures[vertexCount / 4] = texture;
224 PushVertex(new Vector3(x + num3 * num5 - num4 * num6, y + num3 * num6 + num4 * num5, depth), colors.TopLeftColor, texCoordTL);
225 PushVertex(new Vector3(x + (num3 + z) * num5 - num4 * num6, y + (num3 + z) * num6 + num4 * num5, depth), colors.TopRightColor, new Vector2(texCoordBR.X, texCoordTL.Y));
226 PushVertex(new Vector3(x + num3 * num5 - (num4 + w) * num6, y + num3 * num6 + (num4 + w) * num5, depth), colors.BottomLeftColor, new Vector2(texCoordTL.X, texCoordBR.Y));
227 PushVertex(new Vector3(x + (num3 + z) * num5 - (num4 + w) * num6, y + (num3 + z) * num6 + (num4 + w) * num5, depth), colors.BottomRightColor, texCoordBR);
228 }
229
230 private void PushVertex(Vector3 pos, Color color, Vector2 texCoord)
231 {
232 SetVertex(ref vertices[vertexCount++], pos, color, texCoord);
233 }
234
235 private static void SetVertex(ref VertexPositionColorTexture vertex, Vector3 pos, Color color, Vector2 texCoord)
236 {
237 vertex.Position = pos;
238 vertex.Color = color;
239 vertex.TextureCoordinate = texCoord;
240 }
241}
unsafe void SetVertexBuffers(VertexBufferBinding *pBindings, int vertexBufferCount)
unsafe void SetVertexBuffer(VertexBuffer vertexBuffer, int vertexOffset)
unsafe void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount)
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
static double Cos(double d)
static double Sin(double a)
SpriteDrawBuffer(GraphicsDevice graphicsDevice, int defaultSize)
void CheckGraphicsDevice(GraphicsDevice graphicsDevice)
void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, VertexColors color, float rotation, Vector2 origin, float scale, SpriteEffects effects)
void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects)
void Draw(Texture2D texture, Vector2 position, VertexColors colors)
void QueueSprite(Vector4 destinationRect, Vector2 origin, VertexColors colors, Vector4 sourceRectangle, Vector2 texCoordTL, Vector2 texCoordBR, Texture2D texture, float depth, float rotation)
void Draw(Texture2D texture, Rectangle destination, Rectangle? sourceRectangle, VertexColors colors)
void PushVertex(Vector3 pos, Color color, Vector2 texCoord)
void Draw(Texture2D texture, Rectangle destination, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, SpriteEffects effects)
static void SetVertex(ref VertexPositionColorTexture vertex, Vector3 pos, Color color, Vector2 texCoord)
void Draw(Texture2D texture, Rectangle destination, VertexColors colors)
void Draw(Texture2D texture, Vector4 destinationRectangle, Rectangle? sourceRectangle, VertexColors colors, float rotation, Vector2 origin, SpriteEffects effect, float depth)
static ushort[] GenIndexBuffer(int maxSprites)
VertexPositionColorTexture[] vertices