-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SpriteBatch.cs
469 lines (402 loc) · 15.8 KB
/
SpriteBatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Text;
namespace Microsoft.Xna.Framework.Graphics
{
public class SpriteBatch : GraphicsResource
{
readonly SpriteBatcher _batcher;
SpriteSortMode _sortMode;
BlendState _blendState;
SamplerState _samplerState;
DepthStencilState _depthStencilState;
RasterizerState _rasterizerState;
Effect _effect;
bool _beginCalled;
Effect _spriteEffect;
readonly EffectParameter _matrixTransform;
readonly EffectPass _spritePass;
Matrix _matrix;
Rectangle _tempRect = new Rectangle (0,0,0,0);
Vector2 _texCoordTL = new Vector2 (0,0);
Vector2 _texCoordBR = new Vector2 (0,0);
public SpriteBatch (GraphicsDevice graphicsDevice)
{
if (graphicsDevice == null) {
throw new ArgumentException ("graphicsDevice");
}
this.GraphicsDevice = graphicsDevice;
// Use a custom SpriteEffect so we can control the transformation matrix
_spriteEffect = new Effect(graphicsDevice, SpriteEffect.Bytecode);
_matrixTransform = _spriteEffect.Parameters["MatrixTransform"];
_spritePass = _spriteEffect.CurrentTechnique.Passes[0];
_batcher = new SpriteBatcher(graphicsDevice);
_beginCalled = false;
}
public void Begin ()
{
Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Matrix.Identity);
}
public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix)
{
if (_beginCalled)
throw new InvalidOperationException("Begin cannot be called again until End has been successfully called.");
// defaults
_sortMode = sortMode;
_blendState = blendState ?? BlendState.AlphaBlend;
_samplerState = samplerState ?? SamplerState.LinearClamp;
_depthStencilState = depthStencilState ?? DepthStencilState.None;
_rasterizerState = rasterizerState ?? RasterizerState.CullCounterClockwise;
_effect = effect;
_matrix = transformMatrix;
// Setup things now so a user can chage them.
if (sortMode == SpriteSortMode.Immediate)
Setup();
_beginCalled = true;
}
public void Begin (SpriteSortMode sortMode, BlendState blendState)
{
Begin (sortMode, blendState, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Matrix.Identity);
}
public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState)
{
Begin (sortMode, blendState, samplerState, depthStencilState, rasterizerState, null, Matrix.Identity);
}
public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect)
{
Begin (sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, Matrix.Identity);
}
public void End ()
{
_beginCalled = false;
if (_sortMode != SpriteSortMode.Immediate)
Setup();
#if PSM
GraphicsDevice.BlendState = _blendState;
_blendState.ApplyState(GraphicsDevice);
#endif
_batcher.DrawBatch(_sortMode);
}
void Setup()
{
var gd = GraphicsDevice;
gd.BlendState = _blendState;
gd.DepthStencilState = _depthStencilState;
gd.RasterizerState = _rasterizerState;
gd.SamplerStates[0] = _samplerState;
// Setup the default sprite effect.
var vp = gd.Viewport;
Matrix projection;
#if PSM || DIRECTX
Matrix.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, -1, 0, out projection);
#else
// GL requires a half pixel offset to match DX.
Matrix.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, 0, 1, out projection);
projection.M41 += -0.5f * projection.M11;
projection.M42 += -0.5f * projection.M22;
#endif
Matrix.Multiply(ref _matrix, ref projection, out projection);
_matrixTransform.SetValue(projection);
_spritePass.Apply();
// If the user supplied a custom effect then apply
// it now to override the sprite effect.
if (_effect != null)
_effect.CurrentTechnique.Passes[0].Apply();
}
void CheckValid(Texture2D texture)
{
if (texture == null)
throw new ArgumentNullException("texture");
if (!_beginCalled)
throw new InvalidOperationException("Draw was called, but Begin has not yet been called. Begin must be called successfully before you can call Draw.");
}
void CheckValid(SpriteFont spriteFont, string text)
{
if (spriteFont == null)
throw new ArgumentNullException("spriteFont");
if (text == null)
throw new ArgumentNullException("text");
if (!_beginCalled)
throw new InvalidOperationException("DrawString was called, but Begin has not yet been called. Begin must be called successfully before you can call DrawString.");
}
void CheckValid(SpriteFont spriteFont, StringBuilder text)
{
if (spriteFont == null)
throw new ArgumentNullException("spriteFont");
if (text == null)
throw new ArgumentNullException("text");
if (!_beginCalled)
throw new InvalidOperationException("DrawString was called, but Begin has not yet been called. Begin must be called successfully before you can call DrawString.");
}
// Overload for calling Draw() with named parameters
/// <summary>
/// This is a MonoGame Extension method for calling Draw() using named parameters. It is not available in the standard XNA Framework.
/// </summary>
/// <param name='texture'>
/// The Texture2D to draw. Required.
/// </param>
/// <param name='position'>
/// The position to draw at. If left empty, the method will draw at drawRectangle instead.
/// </param>
/// <param name='drawRectangle'>
/// The rectangle to draw at. If left empty, the method will draw at position instead.
/// </param>
/// <param name='sourceRectangle'>
/// The source rectangle of the texture. Default is null
/// </param>
/// <param name='origin'>
/// Origin of the texture. Default is Vector2.Zero
/// </param>
/// <param name='rotation'>
/// Rotation of the texture. Default is 0f
/// </param>
/// <param name='scale'>
/// The scale of the texture as a Vector2. Default is Vector2.One
/// </param>
/// <param name='color'>
/// Color of the texture. Default is Color.White
/// </param>
/// <param name='effect'>
/// SpriteEffect to draw with. Default is SpriteEffects.None
/// </param>
/// <param name='depth'>
/// Draw depth. Default is 0f.
/// </param>
public void Draw (Texture2D texture,
Vector2? position = null,
Rectangle? drawRectangle = null,
Rectangle? sourceRectangle = null,
Vector2? origin = null,
float rotation = 0f,
Vector2? scale = null,
Color? color = null,
SpriteEffects effect = SpriteEffects.None,
float depth = 0f)
{
// Assign default values to null parameters here, as they are not compile-time constants
if(!color.HasValue)
color = Color.White;
if(!origin.HasValue)
origin = Vector2.Zero;
if(!scale.HasValue)
scale = Vector2.One;
// If both drawRectangle and position are null, or if both have been assigned a value, raise an error
if((drawRectangle.HasValue) == (position.HasValue))
{
throw new InvalidOperationException("Expected drawRectangle or position, but received neither or both.");
}
else if(position != null)
{
// Call Draw() using position
Draw(texture, (Vector2)position, sourceRectangle, (Color)color, rotation, (Vector2)origin, (Vector2)scale, effect, depth);
}
else
{
// Call Draw() using drawRectangle
Draw(texture, (Rectangle)drawRectangle, sourceRectangle, (Color)color, rotation, (Vector2)origin, effect, depth);
}
}
public void Draw (Texture2D texture,
Vector2 position,
Rectangle? sourceRectangle,
Color color,
float rotation,
Vector2 origin,
Vector2 scale,
SpriteEffects effect,
float depth)
{
CheckValid(texture);
var w = texture.Width * scale.X;
var h = texture.Height * scale.Y;
if (sourceRectangle.HasValue)
{
w = sourceRectangle.Value.Width*scale.X;
h = sourceRectangle.Value.Height*scale.Y;
}
DrawInternal(texture,
new Vector4(position.X, position.Y, w, h),
sourceRectangle,
color,
rotation,
origin * scale,
effect,
depth);
}
public void Draw (Texture2D texture,
Vector2 position,
Rectangle? sourceRectangle,
Color color,
float rotation,
Vector2 origin,
float scale,
SpriteEffects effect,
float depth)
{
CheckValid(texture);
var w = texture.Width * scale;
var h = texture.Height * scale;
if (sourceRectangle.HasValue)
{
w = sourceRectangle.Value.Width * scale;
h = sourceRectangle.Value.Height * scale;
}
DrawInternal(texture,
new Vector4(position.X, position.Y, w, h),
sourceRectangle,
color,
rotation,
origin * scale,
effect,
depth);
}
public void Draw (Texture2D texture,
Rectangle destinationRectangle,
Rectangle? sourceRectangle,
Color color,
float rotation,
Vector2 origin,
SpriteEffects effect,
float depth)
{
CheckValid(texture);
DrawInternal(texture,
new Vector4(destinationRectangle.X,
destinationRectangle.Y,
destinationRectangle.Width,
destinationRectangle.Height),
sourceRectangle,
color,
rotation,
new Vector2(origin.X * ((float)destinationRectangle.Width / (float)( (sourceRectangle.HasValue && sourceRectangle.Value.Width != 0) ? sourceRectangle.Value.Width : texture.Width)),
origin.Y * ((float)destinationRectangle.Height) / (float)( (sourceRectangle.HasValue && sourceRectangle.Value.Height != 0) ? sourceRectangle.Value.Height : texture.Height)),
effect,
depth);
}
internal void DrawInternal (Texture2D texture,
Vector4 destinationRectangle,
Rectangle? sourceRectangle,
Color color,
float rotation,
Vector2 origin,
SpriteEffects effect,
float depth)
{
var item = _batcher.CreateBatchItem();
item.Depth = depth;
item.Texture = texture;
if (sourceRectangle.HasValue) {
_tempRect = sourceRectangle.Value;
} else {
_tempRect.X = 0;
_tempRect.Y = 0;
_tempRect.Width = texture.Width;
_tempRect.Height = texture.Height;
}
_texCoordTL.X = _tempRect.X / (float)texture.Width;
_texCoordTL.Y = _tempRect.Y / (float)texture.Height;
_texCoordBR.X = (_tempRect.X + _tempRect.Width) / (float)texture.Width;
_texCoordBR.Y = (_tempRect.Y + _tempRect.Height) / (float)texture.Height;
if ((effect & SpriteEffects.FlipVertically) != 0) {
var temp = _texCoordBR.Y;
_texCoordBR.Y = _texCoordTL.Y;
_texCoordTL.Y = temp;
}
if ((effect & SpriteEffects.FlipHorizontally) != 0) {
var temp = _texCoordBR.X;
_texCoordBR.X = _texCoordTL.X;
_texCoordTL.X = temp;
}
item.Set (destinationRectangle.X,
destinationRectangle.Y,
-origin.X,
-origin.Y,
destinationRectangle.Z,
destinationRectangle.W,
(float)Math.Sin (rotation),
(float)Math.Cos (rotation),
color,
_texCoordTL,
_texCoordBR);
if (_sortMode == SpriteSortMode.Immediate)
_batcher.DrawBatch(_sortMode);
}
public void Draw (Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color)
{
Draw (texture, position, sourceRectangle, color, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
}
public void Draw (Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color)
{
Draw (texture, destinationRectangle, sourceRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0f);
}
public void Draw (Texture2D texture, Vector2 position, Color color)
{
Draw (texture, position, null, color);
}
public void Draw (Texture2D texture, Rectangle rectangle, Color color)
{
Draw (texture, rectangle, null, color);
}
public void DrawString (SpriteFont spriteFont, string text, Vector2 position, Color color)
{
CheckValid(spriteFont, text);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto (
this, ref source, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
}
public void DrawString (
SpriteFont spriteFont, string text, Vector2 position, Color color,
float rotation, Vector2 origin, float scale, SpriteEffects effects, float depth)
{
CheckValid(spriteFont, text);
var scaleVec = new Vector2(scale, scale);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto(this, ref source, position, color, rotation, origin, scaleVec, effects, depth);
}
public void DrawString (
SpriteFont spriteFont, string text, Vector2 position, Color color,
float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
{
CheckValid(spriteFont, text);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto(this, ref source, position, color, rotation, origin, scale, effect, depth);
}
public void DrawString (SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color)
{
CheckValid(spriteFont, text);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto(this, ref source, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
}
public void DrawString (
SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color,
float rotation, Vector2 origin, float scale, SpriteEffects effects, float depth)
{
CheckValid(spriteFont, text);
var scaleVec = new Vector2 (scale, scale);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto(this, ref source, position, color, rotation, origin, scaleVec, effects, depth);
}
public void DrawString (
SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color,
float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
{
CheckValid(spriteFont, text);
var source = new SpriteFont.CharacterSource(text);
spriteFont.DrawInto(this, ref source, position, color, rotation, origin, scale, effect, depth);
}
protected override void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
if (_spriteEffect != null)
{
_spriteEffect.Dispose();
_spriteEffect = null;
}
}
}
base.Dispose(disposing);
}
}
}