Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Sprite.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
14#pragma once
15
16// =============================================================================
17// Includes
18// =============================================================================
19
20#include "../../core/Types.hpp"
22#include "../../math/Math.hpp"
24
25namespace esengine::ecs {
26
27// =============================================================================
28// Sprite Component
29// =============================================================================
30
39/* Public because a public component names it: `Sprite.drawMode` is @public, and a
40 frozen field whose TYPE can still change is a promise with a hole in it. The
41 set may still grow — api-surface counts a union gaining a member as a note. */
42ES_ENUM(stability=public)
49
70ES_ENUM(stability=public)
76
77ES_COMPONENT(renderable=enabled, stability=public)
78struct Sprite {
80 ES_PROPERTY(asset = texture)
82
84 ES_PROPERTY(animatable, tooltip="Tint multiplied into the texture (white = unchanged).")
85 glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f};
86
88 ES_PROPERTY(animatable, editor_default={100, 100})
89 glm::vec2 size{1.0f, 1.0f};
90
92 ES_PROPERTY(normalized_of=size, tooltip="Anchor point the sprite rotates and scales about, as a fraction of its size (0.5,0.5 = centre). Values outside 0–1 sit off the sprite.")
93 glm::vec2 pivot{0.5f, 0.5f};
94
96 ES_PROPERTY(advanced)
97 glm::vec2 uvOffset{0.0f, 0.0f};
98
100 ES_PROPERTY(advanced)
101 glm::vec2 uvScale{1.0f, 1.0f};
102
104 ES_PROPERTY(step=1, enum_source=sortingLayers, tooltip="Sorting layer — controls draw order across sprites.")
106
110 ES_PROPERTY(step=1, min=-128, max=127, optional, tooltip="Draw order inside the sorting layer — higher draws on top. Overrides the layer's Y-sort or depth ordering; leave 0 to keep it.")
112
115 ES_PROPERTY(enum=SpriteMaskInteraction, optional,
116 tooltip="Whether a SpriteMask drawn before this sprite clips it, and which side survives.")
118
120 ES_PROPERTY(tooltip="Receive 2D lights: Light entities light this sprite (flat normal). A custom material overrides this.")
121 bool lit{false};
122
125 bool flipX{false};
126
129 bool flipY{false};
130
133 ES_PROPERTY(optional,
134 tooltip="Draw mode: Auto infers from tile size and the texture's 9-slice border; the others override it.")
136
138 ES_PROPERTY(advanced)
139 glm::vec2 tileSize{0.0f, 0.0f};
140
142 ES_PROPERTY(advanced)
143 glm::vec2 tileSpacing{0.0f, 0.0f};
144
149 ES_PROPERTY(advanced, tooltip="Parallax scroll factor (1 = with world, <1 = slower, 0 = locked to camera).")
150 glm::vec2 parallax{1.0f, 1.0f};
151
153 ES_PROPERTY(asset = material, category=Materials, tooltip="The material it is drawn with; none draws it with the built-in sprite look.")
155
157 bool enabled{true};
158
160 Sprite() = default;
161
166 explicit Sprite(resource::TextureHandle tex) : texture(tex) {}
167
173 Sprite(resource::TextureHandle tex, const glm::vec4& col)
174 : texture(tex), color(col) {}
175};
176
177} // namespace esengine::ecs
Type-safe resource handle system.
Math utilities and GLM configuration for ESEngine.
Reflection and metadata system for automatic binding generation.
#define ES_COMPONENT(...)
Mark a struct/class as a component that should be exposed to scripts.
#define ES_ENUM(...)
Mark an enum that should be exposed to scripts.
#define ES_PROPERTY(...)
Mark a field as a property that should be exposed to scripts.
Core type definitions and utilities for ESEngine.
SpriteMaskInteraction
2D sprite component for rendering
Definition Sprite.hpp:71
@ VisibleInside
Drawn only where the mask is — a window.
Definition Sprite.hpp:73
@ VisibleOutside
Drawn only where the mask is NOT — a hole.
Definition Sprite.hpp:74
@ None
Masks reaching this sprite are ignored.
Definition Sprite.hpp:72
SpriteDrawMode
How a Sprite fills its box.
Definition Sprite.hpp:43
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
Definition Handle.hpp:217
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
bool flipY
Flip sprite vertically.
Definition Sprite.hpp:129
glm::vec2 uvOffset
UV coordinate offset for sprite sheets.
Definition Sprite.hpp:97
i32 maskInteraction
What a SpriteMask drawn before this sprite does to it. Opt-in, so a mask added to a scene cannot make...
Definition Sprite.hpp:117
glm::vec2 parallax
Parallax scroll factor per axis. 1 = moves with the world (default, no parallax); <1 = scrolls slower...
Definition Sprite.hpp:150
SpriteDrawMode drawMode
How this sprite fills its box; Auto infers it from tileSize and the texture's slice border.
Definition Sprite.hpp:135
glm::vec2 uvScale
UV coordinate scale for sprite sheets.
Definition Sprite.hpp:101
bool lit
Lit by the scene's 2D lights (Light), no material needed. Off = unlit.
Definition Sprite.hpp:121
bool flipX
Flip sprite horizontally.
Definition Sprite.hpp:125
i32 layer
Sorting layer (higher = rendered on top).
Definition Sprite.hpp:105
Sprite(resource::TextureHandle tex, const glm::vec4 &col)
Constructs sprite with texture and color tint.
Definition Sprite.hpp:173
glm::vec2 size
Sprite size in world units ({1,1} at runtime; authored at 100px).
Definition Sprite.hpp:89
Sprite(resource::TextureHandle tex)
Constructs sprite with texture handle.
Definition Sprite.hpp:166
resource::TextureHandle texture
Texture resource handle (type-safe).
Definition Sprite.hpp:81
i32 order
Order WITHIN the sorting layer; higher draws on top. The explicit answer that overrides whatever the ...
Definition Sprite.hpp:111
u32 material
Custom material ID (0 = use default batch shader).
Definition Sprite.hpp:154
glm::vec2 tileSize
Tile size in world units for tiling mode ({0,0} = no tiling).
Definition Sprite.hpp:139
glm::vec4 color
Color tint (RGBA, 0-1 range).
Definition Sprite.hpp:85
glm::vec2 pivot
Pivot point (0,0 = bottom-left, 0.5,0.5 = center, 1,1 = top-right).
Definition Sprite.hpp:93
Sprite()=default
Default constructor (white, no texture).
glm::vec2 tileSpacing
Spacing between tiles in world units ({0,0} = seamless).
Definition Sprite.hpp:143