Skip to content

Sprite ​

Textured quad drawn in canvas space.

Sprite draws a Texture2D or a rectangular region of it at the node's position. The sprite's size in canvas units matches the texture or region size in pixels so a sprite draws at its natural size until scaled through transform. The anchor selects which point of the quad sits at the node's position. The default is the top-left corner.

Sprites are composited after tone mapping in display space. Load their textures with ColorSpace::Linear so the pixel values are used as authored.

cpp
using enum vglx::Texture::ColorSpace;

auto texture = vglx::LoadTexture("assets/icon.png", Linear);
if (texture.has_value()) {
  auto sprite = canvas.Add(vglx::Sprite::Create(texture.value()));
  sprite->transform.SetPosition({20.0f, 20.0f});
}

INFO

Derives from Renderable2D and inherits all public properties and methods.

Construction ​

Constructors ​


Sprite() ​

Constructs a sprite.

cpp
Sprite::Sprite(std::shared_ptr<Texture2D> texture);
ParameterDescription
textureTexture to draw or nullptr to assign one later.

Factories preferred ​


Sprite::Create() std::unique_ptr<Sprite> ​

Creates an instance of Sprite.

cpp
static std::unique_ptr<Sprite> Sprite::Create(std::shared_ptr<Texture2D> texture);
ParameterDescription
textureTexture to draw or nullptr to assign one later.

Properties ​

region std::optional<Rect> ​

Optional sub-region of the texture to draw in pixels.

Selects a rectangle of the texture with a top-left origin which is how a frame is picked out of a sprite sheet. When unset the whole texture is drawn.

cpp
std::optional<Rect> region {};

texture std::shared_ptr<Texture2D> ​

Texture drawn by the sprite. A null texture draws nothing.

cpp
std::shared_ptr<Texture2D> texture {};

Functions ​

GetGeometry() std::shared_ptr<Geometry> virtual ​

Returns the unit quad shared by all sprites.

cpp
std::shared_ptr<Geometry> Sprite::GetGeometry() override;

GetGeometryTransform() Matrix3 virtual ​

Scales the unit quad to the sprite's size and shifts it by the anchor.

cpp
Matrix3 Sprite::GetGeometryTransform() const override;

GetNodeType() Node2D::Type virtual ​

Identifies this node as Node2D::Type::Sprite.

cpp
Type vglx::Sprite::GetNodeType() const override;

GetSize() Vector2 virtual ​

Returns the sprite's size in canvas units before scaling.

Equals the region size when a region is set, otherwise the texture's dimensions in pixels. Returns zero when there is no texture.

cpp
Vector2 Sprite::GetSize() const override;

GetTexture() std::shared_ptr<Texture2D> virtual ​

Returns the sprite's texture.

cpp
std::shared_ptr<Texture2D> Sprite::GetTexture() const override;

GetTextureTransform() Matrix3 virtual ​

Maps the unit quad's texture coordinates onto the region.

cpp
Matrix3 Sprite::GetTextureTransform() const override;

Released under the MIT License.