Sprite
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.
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.
Sprite::Sprite(std::shared_ptr<Texture2D> texture);| Parameter | Description |
|---|---|
| texture | Texture to draw or nullptr to assign one later. |
Factories preferred
Creates an instance of Sprite.
static std::unique_ptr<Sprite> Sprite::Create(std::shared_ptr<Texture2D> texture);| Parameter | Description |
|---|---|
| texture | Texture to draw or nullptr to assign one later. |
Properties
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.
std::optional<Rect> region {};Texture drawn by the sprite. A null texture draws nothing.
std::shared_ptr<Texture2D> texture {};Functions
Returns the unit quad shared by all sprites.
std::shared_ptr<Geometry> Sprite::GetGeometry() override;Scales the unit quad to the sprite's size and shifts it by the anchor.
Matrix3 Sprite::GetGeometryTransform() const override;GetNodeType() Node2D::Type virtual
Identifies this node as Node2D::Type::Sprite.
Type vglx::Sprite::GetNodeType() const override;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.
Vector2 Sprite::GetSize() const override;Returns the sprite's texture.
std::shared_ptr<Texture2D> Sprite::GetTexture() const override;