-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandard2DRenderer.cpp
More file actions
288 lines (244 loc) · 9.5 KB
/
Standard2DRenderer.cpp
File metadata and controls
288 lines (244 loc) · 9.5 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "Standard2DRenderer.h"
#include "DekiRendererRegistry.h"
#include "IClipProvider.h"
#include "ISortableProvider.h"
#include "DekiEngine.h"
#include "PrefabSystem.h"
#include "CameraComponent.h"
#include "RendererComponent.h"
#include "QuadBlit.h"
#include "DekiObject.h"
#include "Prefab.h"
#include "DekiLogSystem.h"
#include <algorithm>
#include <cmath>
// Self-register with the renderer registry
static struct Standard2DRegistrar {
Standard2DRegistrar() {
DekiRendererRegistry::Register("standard2d",
[]() -> DekiRenderer* { return new Standard2DRenderer(); });
}
} s_standard2dRegistrar;
// --- Pass / callback management ---
void Standard2DRenderer::AddPass(RenderPass* pass)
{
if (m_PassCount < MAX_PASSES)
m_Passes[m_PassCount++] = pass;
}
void Standard2DRenderer::RemovePass(RenderPass* pass)
{
for (int i = 0; i < m_PassCount; i++)
{
if (m_Passes[i] == pass)
{
// Shift remaining passes down
for (int j = i; j < m_PassCount - 1; j++)
m_Passes[j] = m_Passes[j + 1];
m_Passes[--m_PassCount] = nullptr;
return;
}
}
}
void Standard2DRenderer::AddSortingCallback(SortingCallback cb)
{
if (m_SortingCallbackCount < MAX_SORTING_CALLBACKS)
m_SortingCallbacks[m_SortingCallbackCount++] = cb;
}
void Standard2DRenderer::RemoveSortingCallback(SortingCallback cb)
{
for (int i = 0; i < m_SortingCallbackCount; i++)
{
if (m_SortingCallbacks[i] == cb)
{
for (int j = i; j < m_SortingCallbackCount - 1; j++)
m_SortingCallbacks[j] = m_SortingCallbacks[j + 1];
m_SortingCallbacks[--m_SortingCallbackCount] = nullptr;
return;
}
}
}
// --- Built-in component handling ---
bool Standard2DRenderer::GetBuiltinSortingOrder(DekiObject* obj, int32_t& outOrder)
{
// Check RendererComponent
auto* renderer = obj->GetComponent<RendererComponent>();
if (renderer)
{
outOrder = renderer->GetSortingOrder();
return true;
}
// Check ISortableProvider (ClipComponent, SortingGroupComponent, etc.)
auto* sortable = obj->FindInterface<ISortableProvider>();
if (sortable)
{
outOrder = sortable->GetSortingOrder();
return true;
}
return false;
}
void Standard2DRenderer::ExecuteBuiltins(DekiObject* obj, RenderContext& ctx)
{
// Clip: push clip rect if IClipProvider is present
auto* clip = obj->FindInterface<IClipProvider>();
if (clip)
{
float fScreenX, fScreenY;
ctx.camera->WorldToScreen(obj->GetWorldX(), obj->GetWorldY(),
ctx.width, ctx.height, fScreenX, fScreenY);
int32_t screenX = static_cast<int32_t>(std::floor(fScreenX));
int32_t screenY = static_cast<int32_t>(std::floor(fScreenY));
const float effective = ctx.camera->GetPixelsPerMeter();
float scaledW = clip->GetClipWidth() * effective * obj->GetWorldScaleX();
float scaledH = clip->GetClipHeight() * effective * obj->GetWorldScaleY();
int32_t left = screenX - static_cast<int32_t>(std::floor(scaledW * 0.5f));
int32_t top = screenY - static_cast<int32_t>(std::floor(scaledH * 0.5f));
QuadBlit::PushClipRect(left, top,
left + static_cast<int32_t>(scaledW),
top + static_cast<int32_t>(scaledH));
}
// Sprite: blit content
auto* renderer = obj->GetComponent<RendererComponent>();
if (renderer)
{
QuadBlit::Source source;
float pivotX, pivotY;
uint8_t tintR, tintG, tintB, tintA;
if (renderer->RenderContent(obj, source, pivotX, pivotY,
tintR, tintG, tintB, tintA))
{
float fScreenX, fScreenY;
ctx.camera->WorldToScreen(obj->GetWorldX(), obj->GetWorldY(),
ctx.width, ctx.height, fScreenX, fScreenY);
// Temporarily disable clipping if renderer has ignore_clip set
bool wasClipEnabled = QuadBlit::IsClipEnabled();
if (renderer->ignore_clip)
QuadBlit::SetClipEnabled(false);
// Apply unit conversion: source pixels -> world meters -> screen pixels.
// screen_px = (source_px / source.pixelsPerMeter) * world_scale * camera.pixelsPerMeter
// QuadBlit applies (source_px * scale), so:
// scale = world_scale * camera.pixelsPerMeter / source.pixelsPerMeter
//
// World coords are always meters; sprite.pixelsPerMeter is always
// honored. Match camera and sprite PPM (and project PPM) for 1:1
// pixel rendering of source art.
const float worldToScreen = ctx.camera->GetPixelsPerMeter();
const float spritePPM = (source.pixelsPerMeter > 0.0f) ? source.pixelsPerMeter : 1.0f;
const float invSourcePPM = 1.0f / spritePPM;
const float drawScaleX = obj->GetWorldScaleX() * worldToScreen * invSourcePPM;
const float drawScaleY = obj->GetWorldScaleY() * worldToScreen * invSourcePPM;
// pixel_snap = true → round to nearest pixel (sharp, sprite-art).
// pixel_snap = false → truncate (sub-pixel motion accumulates;
// visually smoother under continuous movement, no bilinear yet).
const int32_t intScreenX = renderer->pixel_snap
? static_cast<int32_t>(std::lround(fScreenX))
: static_cast<int32_t>(fScreenX);
const int32_t intScreenY = renderer->pixel_snap
? static_cast<int32_t>(std::lround(fScreenY))
: static_cast<int32_t>(fScreenY);
QuadBlit::Blit(
source,
ctx.buffer,
ctx.width,
ctx.height,
ctx.format,
intScreenX,
intScreenY,
drawScaleX,
drawScaleY,
obj->GetWorldRotation(),
pivotX,
pivotY,
tintR,
tintG,
tintB,
tintA
);
// Restore clip state
if (renderer->ignore_clip)
QuadBlit::SetClipEnabled(wasClipEnabled);
// Free intermediate buffer if we own it
if (source.ownsPixels && source.pixels)
{
delete[] source.pixels;
}
}
}
}
void Standard2DRenderer::PostExecuteBuiltins(DekiObject* obj, RenderContext& ctx)
{
// Pop clip rect if IClipProvider is present
if (obj->FindInterface<IClipProvider>())
QuadBlit::PopClipRect();
}
// --- Sortable item collection ---
void Standard2DRenderer::CollectSortableItems(DekiObject* obj,
std::pair<DekiObject*, int>* items, int& count, int maxItems)
{
if (!obj || count >= maxItems) return;
// Check built-in components first
int32_t order;
if (GetBuiltinSortingOrder(obj, order))
{
items[count++] = {obj, order};
return;
}
// Then check custom sorting callbacks
for (int s = 0; s < m_SortingCallbackCount; s++)
{
if (m_SortingCallbacks[s](obj, order))
{
items[count++] = {obj, order};
return;
}
}
// No one claimed it — transparent container, children float up
for (auto* child : obj->GetChildren())
CollectSortableItems(child, items, count, maxItems);
}
// --- Main render loop ---
void Standard2DRenderer::Render(Prefab* prefab, const RenderContext& ctx)
{
if (!prefab || !ctx.camera || !ctx.buffer)
return;
QuadBlit::ClearClipStack();
// Collect and sort root objects
std::pair<DekiObject*, int> sortableItems[64];
int sortableCount = 0;
for (DekiObject* obj : prefab->GetObjects())
CollectSortableItems(obj, sortableItems, sortableCount, 64);
// Also collect persistent objects
const auto& persistentObjects = DekiEngine::GetInstance().GetPrefabSystem().GetPersistentObjects();
for (DekiObject* obj : persistentObjects)
CollectSortableItems(obj, sortableItems, sortableCount, 64);
// Sort by sortingOrder (lower = behind)
std::stable_sort(sortableItems, sortableItems + sortableCount,
[](const auto& a, const auto& b) { return a.second < b.second; });
// Render in sorted order
for (int i = 0; i < sortableCount; i++)
RenderObject(sortableItems[i].first, ctx);
}
void Standard2DRenderer::RenderObject(DekiObject* obj, const RenderContext& ctx)
{
if (!obj || !obj->IsActiveInHierarchy())
return;
RenderContext objCtx = ctx;
// Phase 1: Execute built-in handling (sprite blit)
ExecuteBuiltins(obj, objCtx);
// Phase 2: Execute custom passes (clip push, etc.)
for (int p = 0; p < m_PassCount; p++)
m_Passes[p]->Execute(obj, objCtx);
// Phase 3: Recurse into sorted children
std::pair<DekiObject*, int> childItems[64];
int childCount = 0;
for (auto* child : obj->GetChildren())
CollectSortableItems(child, childItems, childCount, 64);
std::stable_sort(childItems, childItems + childCount,
[](const auto& a, const auto& b) { return a.second < b.second; });
for (int i = 0; i < childCount; i++)
RenderObject(childItems[i].first, ctx);
// Phase 4: Post-execute custom passes (clip pop, reverse order)
for (int p = m_PassCount - 1; p >= 0; p--)
m_Passes[p]->PostExecute(obj, objCtx);
// Phase 5: Post-execute built-ins
PostExecuteBuiltins(obj, objCtx);
}