class Sprite

Generates geometry and contains information needed for rendering a two dimensional element.

Public

Constructors

Sprite

Sprite() = default

Methods

~Sprite

virtual ~Sprite() noexcept = default

GetBounds

Area2I GetBounds(const Vector2I &offset, const Area2I &clipRect) const

Returns clipped bounds of the sprite.

offset
Offset that will be added to the returned bounds.
clipRect
Local clip rect that is used for clipping the sprite bounds. (Clipping is done before the offset is applied). If clip rect width or height is zero, no clipping is done.

Returns: Clipped sprite bounds.

GetRenderElementCount

u32 GetRenderElementCount() const

Returns the number of separate render elements in the sprite.

Normally this is 1, but some sprites may consist of multiple materials, in which case each will require its own mesh (render element)

Returns: The number render elements.

GetRenderElement

void GetRenderElement(u32 index, SpriteRenderElement &info) const

Copies the internal render element information into the provided object.

Note the pointers to vertex/index buffers continue to be owned by the Sprite. You must not manually free them. They will be valid until the sprite is destroyed, or until sprite is updated with new data or its mesh data is explicitly cleared, at which point you must no longer use them.

FillBuffer

u32 FillBuffer(u8 *outVertices, u8 *outUv, u32 *outIndices, u32 vertexOffset, u32 indexOffset, u32 maxVertexCount, u32 maxIndexCount, u32 vertexStride, u32 indexStride, u32 renderElementIndex, const Vector2I &offset, const Area2I &clipRect, bool clip = true) const

Fill the pre-allocated vertex, uv and index buffers with the mesh data for the specified render element.

outVertices
Previously allocated buffer where to store the vertices.
outUv
Previously allocated buffer where to store the uv coordinates.
outIndices
Previously allocated buffer where to store the indices.
vertexOffset
At which vertex should the method start filling the buffer.
indexOffset
At which index should the method start filling the buffer.
maxVertexCount
Total number of vertices the buffers were allocated for. Used only for memory safety.
maxIndexCount
Total number of indices the buffers were allocated for. Used only for memory safety.
vertexStride
Number of bytes between of vertices in the provided vertex and uv data.
indexStride
Number of bytes between two indexes in the provided index data.
renderElementIndex
Zero-based index of the render element.
offset
Position offset to apply to all vertices, after clipping.
clipRect
Rectangle to clip the vertices to.
clip
Should the vertices be clipped to the provided .

staticClipQuadsToRect

static void ClipQuadsToRect(u8 *outVertices, u8 *outUv, u32 quadCount, u32 vertexStride, const Area2I &clipRect)

Clips the provided 2D vertices to the provided clip rectangle.

The vertices must form axis aligned quads.

outVertices
Pointer to the start of the buffer containing vertex positions.
outUv
Pointer to the start of the buffer containing UV coordinates.
quadCount
Number of quads in the provided buffer pointers.
vertexStride
Number of bytes to skip when going to the next vertex. This assumes both position and uv coordinates have the same stride (as they are likely pointing to the same buffer).
clipRect
Rectangle to clip the geometry to.

staticClipQuadsToRectangle

static void ClipQuadsToRectangle(DataRange &vertices, DataRange &uv, u32 quadCount, u32 startVertexIndex, const Area2 &clipRectangle)

Clips the provided 2D vertices to the provided clip rectangle.

The vertices must form axis aligned quads.

vertices
Buffer containing vertex positions to clip.
uv
Buffer containing UV positions to clip.
quadCount
Number of quads to clip.
startVertexIndex
Offset into vertex/uv buffers at which to start clipping.
clipRectangle
Rectangle to clip the geometry to.

staticClipTrianglesToRect

static void ClipTrianglesToRect(u8 *vertices, u8 *uv, u32 triangleCount, u32 vertexStride, const Area2I &clipRect, const std::function<void (Vector2 *, Vector2 *, u32)> &writeCallback)

Clips the provided 2D vertices to the provided clip rectangle.

The vertices can be arbitrary triangles.

vertices
Pointer to the start of the buffer containing vertex positions.
uv
Pointer to the start of the buffer containing UV coordinates. Can be null if UV is not needed.
triangleCount
Number of triangles in the provided buffer pointers.
vertexStride
Number of bytes to skip when going to the next vertex. This assumes both position and uv coordinates have the same stride (as they are likely pointing to the same buffer).
clipRect
Rectangle to clip the geometry to.
writeCallback
Callback that will be triggered when clipped vertices and UV coordinates are generated and need to be stored. Vertices are always generate in tuples of three, forming a single triangle.

Protected

Methods

staticGetAnchorOffset

static Vector2I GetAnchorOffset(SpriteAnchor anchor, u32 width, u32 height)

Returns the offset needed to move the sprite in order for it to respect the provided anchor.

UpdateBounds

void UpdateBounds() const

Calculates the bounds of all sprite vertices.

Fields

mBounds

Area2I mBounds

mCachedRenderElements

TInlineArray<RenderElementData, 2> mCachedRenderElements