-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteComponent.h
More file actions
129 lines (108 loc) · 4.22 KB
/
Copy pathSpriteComponent.h
File metadata and controls
129 lines (108 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include <stdint.h>
#include "deki-rendering/RendererComponent.h"
#include "Sprite.h"
#include "Color.h"
#include "assets/AssetRef.h"
#include "reflection/DekiProperty.h"
/**
* @brief How a SpriteComponent draws its sprite.
*
* Normal - Single quad at the sprite's native size (default).
* Tiled - Repeat the sprite to fill render_width x render_height.
* NineSlice - 9-slice scale to render_width x render_height. Requires the
* sprite to have hasNineSlice = true with valid borders.
*/
enum class SpriteRenderMode : uint8_t
{
Normal = 0,
Tiled = 1,
NineSlice = 2,
};
/**
* @brief Sprite component for entities that have visual representation
*/
class SpriteComponent : public RendererComponent
{
public:
DEKI_COMPONENT(SpriteComponent, RendererComponent, "2D", "ca17ba76-f46d-484d-97ff-6e8e5497ef8d", "DEKI_FEATURE_SPRITE")
DEKI_DESCRIPTION("Draws a sprite, with tint, flip, and tiled or 9-slice modes.")
// Sprite asset - uses AssetRef for automatic GUID storage and loading
DEKI_EXPORT
Deki::AssetRef<Sprite> sprite;
// Frame rectangle (for sprite atlas/sub-texture rendering)
// Controlled by AnimationComponent - NOT exported
int32_t frameX;
int32_t frameY;
int32_t frameWidth;
int32_t frameHeight;
// Tint color (white = no tint)
DEKI_EXPORT
deki::Color tintColor;
// Render mode: single quad / tiled / 9-slice
DEKI_EXPORT
SpriteRenderMode renderMode = SpriteRenderMode::Normal;
// Rendered size in meters, used by Tiled and NineSlice modes (0 = sprite
// native size). The sprite is baked into a pixel buffer sized by
// round(width * ppm) so the on-screen quad expands while the transform's
// scale stays the same.
DEKI_VISIBLE_WHEN(renderMode, Tiled, NineSlice)
DEKI_EXPORT
DEKI_UNIT(Distance)
float width;
DEKI_VISIBLE_WHEN(renderMode, Tiled, NineSlice)
DEKI_EXPORT
DEKI_UNIT(Distance)
float height;
SpriteComponent(Sprite* spr = nullptr);
/**
* @brief Set the tint color for the sprite
* @param color Tint color (white = no tint)
*/
void SetTint(const deki::Color& color);
/**
* @brief Set the tint color for the sprite
* @param r Red component (0-255)
* @param g Green component (0-255)
* @param b Blue component (0-255)
*/
void SetTint(uint8_t r, uint8_t g, uint8_t b);
/**
* @brief Clear tint (set to white)
*/
void ClearTint();
/**
* @brief Set the animation frame rectangle for sub-texture rendering
* @param x X offset within sprite texture
* @param y Y offset within sprite texture
* @param w Width of the frame (or 0 to use full sprite width)
* @param h Height of the frame (or 0 to use full sprite height)
*/
void SetFrameRect(int32_t x, int32_t y, int32_t w, int32_t h);
// Unified rendering - returns sprite data for QuadBlit
bool RenderContent(const DekiObject* owner,
QuadBlit::Source& outSource,
float& outPivotX,
float& outPivotY,
uint8_t& outTintR,
uint8_t& outTintG,
uint8_t& outTintB,
uint8_t& outTintA) override;
// Called after sprite AssetRef is resolved - handles SubAsset frame lookup
void OnAssetRefResolved(const char* propertyName, void* asset, const char* guid) override;
// Clear cached sprite pointer to force reload (used when assets are modified in editor)
void UnloadAssets() override;
private:
// Cached frame buffer to avoid per-frame allocation for spritesheet frames
uint8_t* m_cachedFrameBuffer = nullptr;
size_t m_cachedFrameSize = 0;
// Cached pre-baked buffer for Tiled / NineSlice modes (re-baked on size/source/mode change)
uint8_t* m_cachedRenderBuffer = nullptr;
size_t m_cachedRenderSize = 0;
int32_t m_cachedRenderW = 0;
int32_t m_cachedRenderH = 0;
const Sprite* m_cachedRenderSrc = nullptr;
SpriteRenderMode m_cachedRenderMode = SpriteRenderMode::Normal;
};
// Generated property metadata (after class definition for offsetof)
#include "generated/SpriteComponent.gen.h"