5 using System.Runtime.InteropServices;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Paradox.Effects.Modules;
10 namespace SiliconStudio.
Paradox.Graphics
15 public partial class SpriteBatch : BatchBase<SpriteBatch.SpriteDrawInfo>
17 private static readonly
Vector2[] CornerOffsets = { Vector2.Zero, Vector2.UnitX, Vector2.One, Vector2.UnitY };
18 private static Vector2 vector2Zero = Vector2.Zero;
21 private Matrix userViewMatrix;
22 private Matrix userProjectionMatrix;
24 private readonly
Matrix defaultViewMatrix = Matrix.Identity;
25 private Matrix defaultProjectionMatrix;
31 public float DefaultDepth {
get; set; }
36 public Vector3? VirtualResolution {
get; set; }
45 : base(graphicsDevice, Bytecode, StaticQuadBufferInfo.CreateQuadBufferInfo(
"SpriteBatch.VertexIndexBuffer", bufferElementCount, batchCapacity),
VertexPositionColorTextureSwizzle.Layout)
60 CalculateDefaultProjection(ref virtualResolution, out matrix);
70 var xRatio = 1f / virtualResolution.X;
71 var yRatio = -1f / virtualResolution.Y;
72 var zRatio = -1f / virtualResolution.Z;
74 projection =
new Matrix { M11 = 2f * xRatio, M22 = 2f * yRatio, M33 = zRatio, M44 = 1f, M41 = -1f, M42 = 1f, M43 = 0.5f };
77 private Vector3 GetCurrentResolution()
82 private void UpdateDefaultProjectionMatrix()
84 var resolution = GetCurrentResolution();
85 CalculateDefaultProjection(ref resolution, out defaultProjectionMatrix);
95 UpdateDefaultProjectionMatrix();
96 Begin(defaultViewMatrix, defaultProjectionMatrix, sortMode, null, null, null, null, effect);
111 UpdateDefaultProjectionMatrix();
112 Begin(defaultViewMatrix, defaultProjectionMatrix, sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, stencilValue);
128 UpdateDefaultProjectionMatrix();
129 Begin(viewMatrix, defaultProjectionMatrix, sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, stencilValue);
146 CheckEndHasBeenCalled(
"begin");
148 userViewMatrix = viewMatrix;
149 userProjectionMatrix = projectionMatrix;
151 Begin(effect, sortMode, blendState, samplerState, depthStencilState, rasterizerState, stencilValue);
165 DrawSprite(texture, ref destinationRectangle,
false, ref nullRectangle, color, 0f, ref vector2Zero,
SpriteEffects.None,
ImageOrientation.AsIs, 0f);
186 var destination =
new RectangleF(position.
X, position.
Y, 1f, 1f);
205 DrawSprite(texture, ref destinationRectangle,
false, ref sourceRectangle, color, rotation, ref origin, effects, orientation, layerDepth);
224 Draw(texture, position, null, color, rotation, origin, scale, effects, orientation, layerDepth);
242 Draw(texture, position, null, color, rotation, origin, scale, effects, orientation, layerDepth);
254 var destination =
new RectangleF(position.
X, position.
Y, 1f, 1f);
274 var destination =
new RectangleF(position.
X, position.
Y, scale, scale);
275 DrawSprite(texture, ref destination,
true, ref sourceRectangle, color, rotation, ref origin, effects, orientation, layerDepth);
294 var destination =
new RectangleF(position.
X, position.
Y, scale.
X, scale.
Y);
295 DrawSprite(texture, ref destination,
true, ref sourceRectangle, color, rotation, ref origin, effects, orientation, layerDepth);
308 if (spriteFont == null)
throw new ArgumentNullException(
"spriteFont");
310 return MeasureString(spriteFont, text, spriteFont.
Size, targetSize);
324 if (spriteFont == null)
throw new ArgumentNullException(
"spriteFont");
326 if (
string.IsNullOrEmpty(text))
330 var virtualResolution = VirtualResolution.HasValue? VirtualResolution.Value:
new Vector3(targetSize, DefaultDepth);
331 var ratio =
new Vector2(targetSize.
X / virtualResolution.X, targetSize.
Y / virtualResolution.Y);
333 var realSize = spriteFont.MeasureString(text, fontSize * ratio);
336 var virtualSize = realSize;
337 virtualSize.X /= ratio.X;
338 virtualSize.Y /= ratio.Y;
351 var proxy =
new SpriteFont.StringProxy(text);
352 DrawString(spriteFont, ref proxy, -1, ref position, ref color, 0,
Vector2.
Zero,
Vector2.
One,
SpriteEffects.None, 0f, alignment);
363 var proxy =
new SpriteFont.StringProxy(text);
364 DrawString(spriteFont, ref proxy, -1, ref position, ref color, 0,
Vector2.
Zero,
Vector2.
One,
SpriteEffects.None, 0f, alignment);
376 var proxy =
new SpriteFont.StringProxy(text);
377 DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, 0,
Vector2.
Zero,
Vector2.
One,
SpriteEffects.None, 0f, alignment);
389 var proxy =
new SpriteFont.StringProxy(text);
390 DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, 0,
Vector2.
Zero,
Vector2.
One,
SpriteEffects.None, 0f, alignment);
406 var proxy =
new SpriteFont.StringProxy(text);
407 DrawString(spriteFont, ref proxy, -1, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
423 var proxy =
new SpriteFont.StringProxy(text);
424 DrawString(spriteFont, ref proxy, -1, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
439 public void DrawString(
SpriteFont spriteFont,
string text,
float fontSize,
Vector2 position,
Color color,
float rotation,
Vector2 origin,
Vector2 scale,
SpriteEffects effects,
float layerDepth,
TextAlignment alignment)
441 var proxy =
new SpriteFont.StringProxy(text);
442 DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
457 public void DrawString(
SpriteFont spriteFont, StringBuilder text,
float fontSize,
Vector2 position,
Color color,
float rotation,
Vector2 origin,
Vector2 scale,
SpriteEffects effects,
float layerDepth,
TextAlignment alignment)
459 var proxy =
new SpriteFont.StringProxy(text);
460 DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
463 private void DrawString(
SpriteFont spriteFont, ref
SpriteFont.StringProxy text,
float fontSize, ref
Vector2 position, ref
Color color,
float rotation,
Vector2 origin,
Vector2 scale,
SpriteEffects effects,
float layerDepth,
TextAlignment alignment)
465 DrawString(spriteFont, ref text, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
468 private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text,
float fontSize, ref
Vector2 position, ref
Color color,
float rotation, ref
Vector2 origin, ref
Vector2 scale,
SpriteEffects effects,
float layerDepth,
TextAlignment alignment)
470 if (spriteFont == null)
472 throw new ArgumentNullException(
"spriteFont");
476 throw new ArgumentNullException(
"text");
479 fontSize = spriteFont.Size;
482 var viewportSize = GraphicsDevice.Viewport;
483 var virtualResolution = GetCurrentResolution();
484 var resolutionRatio =
new Vector2(viewportSize.Width / virtualResolution.X, viewportSize.Height / virtualResolution.Y);
485 scale.X = scale.X / resolutionRatio.X;
486 scale.Y = scale.Y / resolutionRatio.Y;
488 var fontSize2 = fontSize * (spriteFont.IsDynamic ? resolutionRatio : Vector2.One);
489 var drawCommand =
new SpriteFont.InternalDrawCommand(
this, ref fontSize2, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth);
492 Vector2.Modulate(ref drawCommand.Position, ref resolutionRatio, out drawCommand.Position);
493 drawCommand.Position.X = (float)Math.Round(drawCommand.Position.X);
494 drawCommand.Position.Y = (float)Math.Round(drawCommand.Position.Y);
495 drawCommand.Position.X /= resolutionRatio.X;
496 drawCommand.Position.Y /= resolutionRatio.Y;
498 spriteFont.InternalDraw(ref text, ref drawCommand, alignment);
501 internal unsafe
void DrawSprite(Texture texture, ref
RectangleF destination,
bool scaleDestination, ref
RectangleF? sourceRectangle,
Color color,
507 throw new ArgumentNullException(
"texture");
511 var elementInfo =
new ElementInfo();
512 var spriteInfo = &elementInfo.DrawInfo;
518 if (sourceRectangle.HasValue)
520 var rectangle = sourceRectangle.Value;
521 spriteInfo->Source.X = rectangle.X;
522 spriteInfo->Source.Y = rectangle.Y;
523 width = rectangle.Width;
524 height = rectangle.Height;
529 spriteInfo->Source.X = 0.0f;
530 spriteInfo->Source.Y = 0.0f;
531 width = texture.Width;
532 height = texture.Height;
536 spriteInfo->Source.Width = width;
537 spriteInfo->Source.Height = height;
540 if (scaleDestination)
544 destination.Width *= height;
545 destination.Height *= width;
549 destination.Width *= width;
550 destination.Height *= height;
555 spriteInfo->Destination = destination;
558 spriteInfo->Origin.X = origin.X;
559 spriteInfo->Origin.Y = origin.Y;
560 spriteInfo->Rotation = rotation;
561 spriteInfo->Depth = depth;
562 spriteInfo->SpriteEffects = effects;
563 spriteInfo->Color = color;
564 spriteInfo->Swizzle = swizzle;
565 spriteInfo->TextureSize.X = texture.Width;
566 spriteInfo->TextureSize.Y = texture.Height;
567 spriteInfo->Orientation = orientation;
569 elementInfo.VertexCount = StaticQuadBufferInfo.VertexByElement;
570 elementInfo.IndexCount = StaticQuadBufferInfo.IndicesByElement;
571 elementInfo.Depth = depth;
573 Draw(texture, null, ref elementInfo);
581 var deltaX = 1f / drawInfo->TextureSize.X;
582 var deltaY = 1f / drawInfo->TextureSize.Y;
584 var rotation = Math.Abs(drawInfo->Rotation) >
MathUtil.
ZeroTolerance ?
new Vector2((
float)Math.Cos(drawInfo->Rotation), (
float)Math.Sin(drawInfo->Rotation)) : Vector2.UnitX;
587 var origin = drawInfo->Origin;
588 origin.X /= Math.Max(MathUtil.ZeroTolerance, drawInfo->Source.Width);
589 origin.Y /= Math.Max(MathUtil.ZeroTolerance, drawInfo->Source.Height);
591 for (
int j = 0; j < 4; j++)
594 var corner = CornerOffsets[j];
596 var position =
new Vector2((corner.X - origin.X) * drawInfo->Destination.Width, (corner.Y - origin.Y) * drawInfo->Destination.Height);
599 vertex->Position.X = drawInfo->Destination.X + (position.X * rotation.X) - (position.Y * rotation.Y);
600 vertex->Position.Y = drawInfo->Destination.Y + (position.X * rotation.Y) + (position.Y * rotation.X);
601 vertex->Position.Z = drawInfo->Depth;
602 vertex->Position.W = 1f;
603 vertex->Color = drawInfo->Color;
605 corner = CornerOffsets[((j ^ (int)drawInfo->SpriteEffects) + (int)drawInfo->Orientation) % 4];
606 vertex->TextureCoordinate.X = (drawInfo->Source.X + corner.X * drawInfo->Source.Width) * deltaX;
607 vertex->TextureCoordinate.Y = (drawInfo->Source.Y + corner.Y * drawInfo->Source.Height) * deltaY;
609 vertex->Swizzle = (int)drawInfo->Swizzle;
619 Matrix.MultiplyTo(ref userViewMatrix, ref userProjectionMatrix, out viewProjection);
623 Effect.Parameters.Set(SpriteBaseKeys.MatrixTransform, viewProjection);
625 base.PrepareForRendering();
628 [StructLayout(LayoutKind.Sequential)]
void Begin(SpriteSortMode sortMode=SpriteSortMode.Deferred, BlendState blendState=null, SamplerState samplerState=null, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, Effect effect=null, int stencilValue=0)
Begins a sprite batch rendering using the specified sorting mode and blend state, sampler...
float Size
Gets the font size (resp. the default font size) for static fonts (resp. for dynamic fonts) in pixels...
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
static readonly Vector2 Zero
A SiliconStudio.Core.Mathematics.Vector2 with all of its components set to zero.
ImageOrientation Orientation
Represents a two dimensional mathematical vector.
void Draw(Texture texture, RectangleF destinationRectangle, Color color)
Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle...
void Begin(SpriteSortMode sortMode, Effect effect)
Begins a sprite batch operation using deferred sort and default state objects (BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise).
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
Contains depth-stencil state for the device.
void DrawString(SpriteFont spriteFont, string text, float fontSize, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
Describes a custom vertex format structure that contains position, color, texture and swizzle informa...
override unsafe void UpdateBufferValuesFromElementInfo(ref ElementInfo elementInfo, IntPtr vertexPtr, IntPtr indexPtr, int vertexOffset)
Update the mapped vertex and index buffer values using the provided element info. ...
TextAlignment
Specify the available text alignment when rendering text.
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Represents a three dimensional mathematical vector.
SpriteEffects
Defines sprite mirroring options.
static Matrix CalculateDefaultProjection(Vector3 virtualResolution)
Calculate the default projection matrix for the provided virtual resolution.
void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color color, TextAlignment alignment=TextAlignment.Left)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
static readonly Vector2 One
A SiliconStudio.Core.Mathematics.Vector2 with all of its components set to one.
Define a RectangleF. This structure is slightly different from System.Drawing.RectangleF as it is int...
Vector2 MeasureString(SpriteFont spriteFont, string text, Vector2 targetSize)
Returns the size of the given text in virtual pixels.
void Draw(Texture texture, Vector2 position, Color color, float rotation, Vector2 origin, float scale=1.0f, SpriteEffects effects=SpriteEffects.None, ImageOrientation orientation=ImageOrientation.AsIs, float layerDepth=0)
Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer.
SpriteSortMode
Defines sprite sort-rendering options.
void Draw(Texture texture, Vector2 position, RectangleF?sourceRectangle, Color color, float rotation, Vector2 origin, float scale=1f, SpriteEffects effects=SpriteEffects.None, ImageOrientation orientation=ImageOrientation.AsIs, float layerDepth=0)
Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer.
void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, TextAlignment alignment=TextAlignment.Left)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
SpriteFont to use with SpriteBatch. See SpriteFont to learn how to use it.
float Y
The Y component of the vector.
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color, TextAlignment alignment=TextAlignment.Left)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
void Draw(Texture texture, Vector2 position, RectangleF?sourceRectangle, Color color)
Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, and color.
void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
Vector2 MeasureString(SpriteFont spriteFont, string text, float fontSize, Vector2 targetSize)
Returns the size of the given text in virtual pixels.
void Draw(Texture texture, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects=SpriteEffects.None, ImageOrientation orientation=ImageOrientation.AsIs, float layerDepth=0)
Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer.
static void CalculateDefaultProjection(ref Vector3 virtualResolution, out Matrix projection)
Calculate the default projection matrix for the provided virtual resolution.
void Draw(Texture texture, Vector2 position, Color color)
Adds a sprite to a batch of sprites for rendering using the specified texture, position and color...
SpriteEffects SpriteEffects
SwizzleMode
Specify how to swizzle a vector.
float X
The X component of the vector.
void Draw(Texture texture, RectangleF destinationRectangle, RectangleF?sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects=SpriteEffects.None, ImageOrientation orientation=ImageOrientation.AsIs, float layerDepth=0f)
Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle...
void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
override void PrepareForRendering()
SiliconStudio.Core.Mathematics.Vector3 Vector3
static readonly Color White
White color.
void DrawString(SpriteFont spriteFont, string text, float fontSize, Vector2 position, Color color, TextAlignment alignment=TextAlignment.Left)
Adds a string to a batch of sprites for rendering using the specified font, text, position...
void Draw(Texture texture, Vector2 position)
Adds a sprite to a batch of sprites for rendering using the specified texture, position and color...
void Draw(Texture texture, Vector2 position, RectangleF?sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects=SpriteEffects.None, ImageOrientation orientation=ImageOrientation.AsIs, float layerDepth=0)
Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer.
SiliconStudio.Core.Mathematics.RectangleF RectangleF
SpriteBatch(GraphicsDevice graphicsDevice, int bufferElementCount=1024, int batchCapacity=64)
Initializes a new instance of the SpriteBatch class.
void Begin(Matrix viewMatrix, Matrix projectionMatrix, SpriteSortMode sortMode=SpriteSortMode.Deferred, BlendState blendState=null, SamplerState samplerState=null, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, Effect effect=null, int stencilValue=0)
Begins a sprite batch rendering using the specified sorting mode and blend state, sampler...
void Begin(Matrix viewMatrix, SpriteSortMode sortMode=SpriteSortMode.Deferred, BlendState blendState=null, SamplerState samplerState=null, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, Effect effect=null, int stencilValue=0)
Begins a sprite batch rendering using the specified sorting mode and blend state, sampler...
ImageOrientation
Defines the possible rotations to apply on image regions.
Base class for texture resources.
Represents a 4x4 mathematical matrix.