@flare-engine/debug 0.4.8 → 0.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +21 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +21 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +119 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +119 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -129,16 +129,35 @@ interface DebugLine {
|
|
|
129
129
|
}
|
|
130
130
|
/** Union of debug draw primitive commands. */
|
|
131
131
|
type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;
|
|
132
|
+
/** Options for {@link DebugOverlay}. */
|
|
133
|
+
interface DebugOverlayOptions {
|
|
134
|
+
/**
|
|
135
|
+
* When true, `drawRect` / `drawCircle` / `drawLine` reuse pooled command
|
|
136
|
+
* objects, so no command object is created per call once the pools are warm.
|
|
137
|
+
* A reused object changes after `clear()`, so copy a command to keep it past
|
|
138
|
+
* the frame. Default `false`: every call creates a fresh object.
|
|
139
|
+
*/
|
|
140
|
+
reuseCommands?: boolean;
|
|
141
|
+
}
|
|
132
142
|
/**
|
|
133
143
|
* DebugOverlay — collects debug draw commands for the current frame.
|
|
134
144
|
* Commands are generated based on active overlay flags, then consumed
|
|
135
|
-
* by the renderer.
|
|
145
|
+
* by the renderer. Commands are fresh objects by default and pooled with
|
|
146
|
+
* `reuseCommands` (see {@link DebugOverlayOptions}).
|
|
136
147
|
*/
|
|
137
148
|
export declare class DebugOverlay {
|
|
138
149
|
/** Active overlay flags. */
|
|
139
150
|
readonly flags: OverlayFlags;
|
|
140
151
|
/** Queued debug draw commands for the current frame. */
|
|
141
152
|
private _commands;
|
|
153
|
+
private _rects;
|
|
154
|
+
private _circles;
|
|
155
|
+
private _lines;
|
|
156
|
+
private _rectCursor;
|
|
157
|
+
private _circleCursor;
|
|
158
|
+
private _lineCursor;
|
|
159
|
+
private readonly _reuseCommands;
|
|
160
|
+
constructor(options?: DebugOverlayOptions);
|
|
142
161
|
/** Number of queued commands. */
|
|
143
162
|
get commandCount(): number;
|
|
144
163
|
/** Get all commands (read-only view). */
|
|
@@ -231,5 +250,5 @@ export declare class TimeControl {
|
|
|
231
250
|
/** Set slow-motion multiplier (e.g., 0.5 for half-speed). */
|
|
232
251
|
setTimescale(scale: number): void;
|
|
233
252
|
}
|
|
234
|
-
export type { BootEntry, BootTraceHandle, DebugCircle, DebugDrawCommand, DebugLine, DebugRect, DebugStats, FpsWidget, FpsWidgetOptions, FrameBudgetHandle, FrameBudgetLimits, FrameBudgetSnapshot, OverlayFlags, PerfCounterStats, PerfRecorderHandle, PerfReport, PerfSectionStats };
|
|
253
|
+
export type { BootEntry, BootTraceHandle, DebugCircle, DebugDrawCommand, DebugLine, DebugOverlayOptions, DebugRect, DebugStats, FpsWidget, FpsWidgetOptions, FrameBudgetHandle, FrameBudgetLimits, FrameBudgetOpts, FrameBudgetSnapshot, OverlayFlags, PerfCounterStats, PerfRecorderHandle, PerfRecorderOpts, PerfReport, PerfSectionStats };
|
|
235
254
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/boot-trace.ts","../src/stats.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-recorder.ts","../src/perf-output.ts","../src/time-control.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/boot-trace.ts","../src/stats.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-recorder.ts","../src/perf-output.ts","../src/time-control.ts"],"mappings":";KAEY;WACD;WACA;;KAGC;EACV,KAAK;EACL;EACA;EACA,uBAAuB;;wBAGT,gBAAgB,iBAAuB;;UCbtC;;EAEf;;EAEA;;EAEA;;EAEA;;EA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A;;;;;;EAUA;;EAOA,aAAa"}
|
package/dist/index.d.ts
CHANGED
|
@@ -129,16 +129,35 @@ interface DebugLine {
|
|
|
129
129
|
}
|
|
130
130
|
/** Union of debug draw primitive commands. */
|
|
131
131
|
type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;
|
|
132
|
+
/** Options for {@link DebugOverlay}. */
|
|
133
|
+
interface DebugOverlayOptions {
|
|
134
|
+
/**
|
|
135
|
+
* When true, `drawRect` / `drawCircle` / `drawLine` reuse pooled command
|
|
136
|
+
* objects, so no command object is created per call once the pools are warm.
|
|
137
|
+
* A reused object changes after `clear()`, so copy a command to keep it past
|
|
138
|
+
* the frame. Default `false`: every call creates a fresh object.
|
|
139
|
+
*/
|
|
140
|
+
reuseCommands?: boolean;
|
|
141
|
+
}
|
|
132
142
|
/**
|
|
133
143
|
* DebugOverlay — collects debug draw commands for the current frame.
|
|
134
144
|
* Commands are generated based on active overlay flags, then consumed
|
|
135
|
-
* by the renderer.
|
|
145
|
+
* by the renderer. Commands are fresh objects by default and pooled with
|
|
146
|
+
* `reuseCommands` (see {@link DebugOverlayOptions}).
|
|
136
147
|
*/
|
|
137
148
|
export declare class DebugOverlay {
|
|
138
149
|
/** Active overlay flags. */
|
|
139
150
|
readonly flags: OverlayFlags;
|
|
140
151
|
/** Queued debug draw commands for the current frame. */
|
|
141
152
|
private _commands;
|
|
153
|
+
private _rects;
|
|
154
|
+
private _circles;
|
|
155
|
+
private _lines;
|
|
156
|
+
private _rectCursor;
|
|
157
|
+
private _circleCursor;
|
|
158
|
+
private _lineCursor;
|
|
159
|
+
private readonly _reuseCommands;
|
|
160
|
+
constructor(options?: DebugOverlayOptions);
|
|
142
161
|
/** Number of queued commands. */
|
|
143
162
|
get commandCount(): number;
|
|
144
163
|
/** Get all commands (read-only view). */
|
|
@@ -231,5 +250,5 @@ export declare class TimeControl {
|
|
|
231
250
|
/** Set slow-motion multiplier (e.g., 0.5 for half-speed). */
|
|
232
251
|
setTimescale(scale: number): void;
|
|
233
252
|
}
|
|
234
|
-
export type { BootEntry, BootTraceHandle, DebugCircle, DebugDrawCommand, DebugLine, DebugRect, DebugStats, FpsWidget, FpsWidgetOptions, FrameBudgetHandle, FrameBudgetLimits, FrameBudgetSnapshot, OverlayFlags, PerfCounterStats, PerfRecorderHandle, PerfReport, PerfSectionStats };
|
|
253
|
+
export type { BootEntry, BootTraceHandle, DebugCircle, DebugDrawCommand, DebugLine, DebugOverlayOptions, DebugRect, DebugStats, FpsWidget, FpsWidgetOptions, FrameBudgetHandle, FrameBudgetLimits, FrameBudgetOpts, FrameBudgetSnapshot, OverlayFlags, PerfCounterStats, PerfRecorderHandle, PerfRecorderOpts, PerfReport, PerfSectionStats };
|
|
235
254
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/boot-trace.ts","../src/stats.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-recorder.ts","../src/perf-output.ts","../src/time-control.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/boot-trace.ts","../src/stats.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-recorder.ts","../src/perf-output.ts","../src/time-control.ts"],"mappings":";KAEY;WACD;WACA;;KAGC;EACV,KAAK;EACL;EACA;EACA,uBAAuB;;wBAGT,gBAAgB,iBAAuB;;UCbtC;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;qBAOW;;WAEF,OAAO;;EAUhB,gBAAgB,aAAa,eAAe,YAAY;;EAQxD,eAAe;;EAKf,aAAa;;EAKb;;;;;;;;;;;;UCtCe;;EAEf,OAAO,OAAO;;EAEd;;WAES;;;UAIM;;EAEf;;EAEA;;EAEA;;;wBAIc,gBAAgB,UAAS,mBAAwB;KChCrD,oBAAoB;KACpB,sBAAsB;KAEtB;EACV,YAAY;EACZ,eAAe,UAAU;EACzB,eAAe,SAAS;EACxB;EACA;;KAGU;EACV,QAAQ;EACR;EACA;EACA;EACA;;wBAGc,kBAAkB,MAAM,kBAAkB;;;;;;;UCbzC;;EAEf;;EAEA;;EAEA;;EAEA;;;UAIe;WACN;EACT;EACA;EACA;EACA;EACA;;UAGe;WACN;EACT;EACA;EACA;EACA;;UAGe;WACN;EACT;EACA;EACA;EACA;EACA;;;KAIU,mBAAmB,YAAY,cAAc;;UAGxC;;;;;;;EAOf;;;;;;;;qBASW;;WAEF,OAAO;;UAQR;UAEA;UACA;UACA;UACA;UACA;UACA;mBACS;EAEL,YAAA,UAAS;;MAKjB;;MAKA,qBAAqB;;EAKzB,SAAS,WAAW,WAAW,eAAe,gBAAgB;;EAqB9D,WAAW,WAAW,WAAW,gBAAgB;;EAoBjD,SAAS,YAAY,YAAY,YAAY,YAAY;;EAqBzD;;EAQA,WAAW,YAAY;;EAKvB;;EAQA;;KCpLU;WACD;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;KAGC;WACD;WACA;WACA;WACA;WACA;WACA;;KAGC;WACD;WACA;WACA;WACA,mBAAmB;WACnB,mBAAmB;;KAGlB;EACV,aAAa;EACb,WAAW;EACX,YAAY,WAAW;EACvB,SAAS;EACT;;KAoBU;EACV;EACA;;wBAcc,mBAAmB,OAAM,mBAAwB;qBCrEpD;wBAEG,eAAe,QAAQ;;;;;qBCE1B;UACH;UACA;UACA;UACA;EAEI,YAAA,OAAO;;MAKf;;MAKA;;EAKJ;;EAQA;;EAQA;;;;;EAYA;;;;;;EAUA;;EAOA,aAAa"}
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
// src/
|
|
3
|
-
function nowMs
|
|
2
|
+
// src/now.ts
|
|
3
|
+
function nowMs() {
|
|
4
4
|
if (typeof performance !== "undefined" && typeof performance.now === "function") return performance.now();
|
|
5
5
|
return Date.now();
|
|
6
6
|
}
|
|
7
|
+
// src/boot-trace.ts
|
|
7
8
|
function createBootTrace(label = "boot-trace") {
|
|
8
9
|
const entries = [];
|
|
9
10
|
let startT = null;
|
|
10
11
|
function mark(name) {
|
|
11
12
|
if (startT === null) {
|
|
12
|
-
startT = nowMs
|
|
13
|
+
startT = nowMs();
|
|
13
14
|
entries.push({
|
|
14
15
|
name,
|
|
15
16
|
t: 0
|
|
16
17
|
});
|
|
17
18
|
} else entries.push({
|
|
18
19
|
name,
|
|
19
|
-
t: nowMs
|
|
20
|
+
t: nowMs() - startT
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
23
|
function dump() {
|
|
@@ -83,11 +84,18 @@ function createFrameBudget(opts) {
|
|
|
83
84
|
for (const k of limitKeys) _lastSnapshot[k] = 0;
|
|
84
85
|
let head = 0;
|
|
85
86
|
let filled = 0;
|
|
87
|
+
let sum = 0;
|
|
86
88
|
let watchdogFired = false;
|
|
87
89
|
function recordFrame(dtSec) {
|
|
90
|
+
const old = ring[head];
|
|
88
91
|
ring[head] = dtSec;
|
|
92
|
+
sum += ring[head] - old;
|
|
89
93
|
head = (head + 1) % ringSize;
|
|
90
94
|
if (filled < ringSize) filled += 1;
|
|
95
|
+
if (head === 0) {
|
|
96
|
+
sum = 0;
|
|
97
|
+
for (let i = 0; i < ringSize; i++) sum += ring[i];
|
|
98
|
+
}
|
|
91
99
|
if (filled === ringSize && !watchdogFired) {
|
|
92
100
|
if (getAvgFps() < fpsThreshold) {
|
|
93
101
|
watchdogFired = true;
|
|
@@ -108,13 +116,11 @@ function createFrameBudget(opts) {
|
|
|
108
116
|
}
|
|
109
117
|
function getAvgFps() {
|
|
110
118
|
if (filled < ringSize) return 0;
|
|
111
|
-
|
|
112
|
-
for (let i = 0; i < ringSize; i++) sum += ring[i];
|
|
113
|
-
const avgDt = sum / ringSize;
|
|
114
|
-
return avgDt > 0 ? 1 / avgDt : 0;
|
|
119
|
+
return sum > 0 ? ringSize / sum : 0;
|
|
115
120
|
}
|
|
116
121
|
function resetFpsRing() {
|
|
117
122
|
ring.fill(0);
|
|
123
|
+
sum = 0;
|
|
118
124
|
head = 0;
|
|
119
125
|
filled = 0;
|
|
120
126
|
watchdogFired = false;
|
|
@@ -131,7 +137,8 @@ function createFrameBudget(opts) {
|
|
|
131
137
|
/**
|
|
132
138
|
* DebugOverlay — collects debug draw commands for the current frame.
|
|
133
139
|
* Commands are generated based on active overlay flags, then consumed
|
|
134
|
-
* by the renderer.
|
|
140
|
+
* by the renderer. Commands are fresh objects by default and pooled with
|
|
141
|
+
* `reuseCommands` (see {@link DebugOverlayOptions}).
|
|
135
142
|
*/
|
|
136
143
|
var DebugOverlay = class {
|
|
137
144
|
/** Active overlay flags. */
|
|
@@ -143,6 +150,16 @@ var DebugOverlay = class {
|
|
|
143
150
|
};
|
|
144
151
|
/** Queued debug draw commands for the current frame. */
|
|
145
152
|
_commands = [];
|
|
153
|
+
_rects = [];
|
|
154
|
+
_circles = [];
|
|
155
|
+
_lines = [];
|
|
156
|
+
_rectCursor = 0;
|
|
157
|
+
_circleCursor = 0;
|
|
158
|
+
_lineCursor = 0;
|
|
159
|
+
_reuseCommands;
|
|
160
|
+
constructor(options = {}) {
|
|
161
|
+
this._reuseCommands = options.reuseCommands === true;
|
|
162
|
+
}
|
|
146
163
|
/** Number of queued commands. */
|
|
147
164
|
get commandCount() {
|
|
148
165
|
return this._commands.length;
|
|
@@ -153,39 +170,109 @@ var DebugOverlay = class {
|
|
|
153
170
|
}
|
|
154
171
|
/** Add a debug rectangle. */
|
|
155
172
|
drawRect(x, y, width, height, color = "#00ff00") {
|
|
156
|
-
this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
173
|
+
if (!this._reuseCommands) {
|
|
174
|
+
this._commands.push({
|
|
175
|
+
type: "rect",
|
|
176
|
+
x,
|
|
177
|
+
y,
|
|
178
|
+
width,
|
|
179
|
+
height,
|
|
180
|
+
color
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
let cmd = this._rects[this._rectCursor];
|
|
185
|
+
if (cmd) {
|
|
186
|
+
cmd.x = x;
|
|
187
|
+
cmd.y = y;
|
|
188
|
+
cmd.width = width;
|
|
189
|
+
cmd.height = height;
|
|
190
|
+
cmd.color = color;
|
|
191
|
+
} else {
|
|
192
|
+
cmd = {
|
|
193
|
+
type: "rect",
|
|
194
|
+
x,
|
|
195
|
+
y,
|
|
196
|
+
width,
|
|
197
|
+
height,
|
|
198
|
+
color
|
|
199
|
+
};
|
|
200
|
+
this._rects.push(cmd);
|
|
201
|
+
}
|
|
202
|
+
this._rectCursor += 1;
|
|
203
|
+
this._commands.push(cmd);
|
|
164
204
|
}
|
|
165
205
|
/** Add a debug circle. */
|
|
166
206
|
drawCircle(x, y, radius, color = "#00ff00") {
|
|
167
|
-
this.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
207
|
+
if (!this._reuseCommands) {
|
|
208
|
+
this._commands.push({
|
|
209
|
+
type: "circle",
|
|
210
|
+
x,
|
|
211
|
+
y,
|
|
212
|
+
radius,
|
|
213
|
+
color
|
|
214
|
+
});
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let cmd = this._circles[this._circleCursor];
|
|
218
|
+
if (cmd) {
|
|
219
|
+
cmd.x = x;
|
|
220
|
+
cmd.y = y;
|
|
221
|
+
cmd.radius = radius;
|
|
222
|
+
cmd.color = color;
|
|
223
|
+
} else {
|
|
224
|
+
cmd = {
|
|
225
|
+
type: "circle",
|
|
226
|
+
x,
|
|
227
|
+
y,
|
|
228
|
+
radius,
|
|
229
|
+
color
|
|
230
|
+
};
|
|
231
|
+
this._circles.push(cmd);
|
|
232
|
+
}
|
|
233
|
+
this._circleCursor += 1;
|
|
234
|
+
this._commands.push(cmd);
|
|
174
235
|
}
|
|
175
236
|
/** Add a debug line. */
|
|
176
237
|
drawLine(x1, y1, x2, y2, color = "#ffff00") {
|
|
177
|
-
this.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
238
|
+
if (!this._reuseCommands) {
|
|
239
|
+
this._commands.push({
|
|
240
|
+
type: "line",
|
|
241
|
+
x1,
|
|
242
|
+
y1,
|
|
243
|
+
x2,
|
|
244
|
+
y2,
|
|
245
|
+
color
|
|
246
|
+
});
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
let cmd = this._lines[this._lineCursor];
|
|
250
|
+
if (cmd) {
|
|
251
|
+
cmd.x1 = x1;
|
|
252
|
+
cmd.y1 = y1;
|
|
253
|
+
cmd.x2 = x2;
|
|
254
|
+
cmd.y2 = y2;
|
|
255
|
+
cmd.color = color;
|
|
256
|
+
} else {
|
|
257
|
+
cmd = {
|
|
258
|
+
type: "line",
|
|
259
|
+
x1,
|
|
260
|
+
y1,
|
|
261
|
+
x2,
|
|
262
|
+
y2,
|
|
263
|
+
color
|
|
264
|
+
};
|
|
265
|
+
this._lines.push(cmd);
|
|
266
|
+
}
|
|
267
|
+
this._lineCursor += 1;
|
|
268
|
+
this._commands.push(cmd);
|
|
185
269
|
}
|
|
186
270
|
/** Clear all commands. Call at the start of each frame. */
|
|
187
271
|
clear() {
|
|
188
272
|
this._commands.length = 0;
|
|
273
|
+
this._rectCursor = 0;
|
|
274
|
+
this._circleCursor = 0;
|
|
275
|
+
this._lineCursor = 0;
|
|
189
276
|
}
|
|
190
277
|
/** Toggle a specific overlay flag. */
|
|
191
278
|
toggleFlag(flag) {
|
|
@@ -212,10 +299,6 @@ function emitPerfReport(report) {
|
|
|
212
299
|
console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);
|
|
213
300
|
}
|
|
214
301
|
// src/perf-recorder.ts
|
|
215
|
-
function nowMs() {
|
|
216
|
-
if (typeof performance !== "undefined" && typeof performance.now === "function") return performance.now();
|
|
217
|
-
return Date.now();
|
|
218
|
-
}
|
|
219
302
|
function percentile(sorted, n, p) {
|
|
220
303
|
if (n === 0) return 0;
|
|
221
304
|
if (n === 1) return sorted[0];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["nowMs"],"sources":["../src/boot-trace.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-output.ts","../src/perf-recorder.ts","../src/stats.ts","../src/time-control.ts"],"sourcesContent":["export type BootEntry = {\n readonly name: string;\n readonly t: number;\n};\n\nexport type BootTraceHandle = {\n mark(name: string): void;\n dump(): string | null;\n reset(): void;\n getEntries(): readonly BootEntry[];\n};\n\nfunction nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n\nexport function createBootTrace(label = 'boot-trace'): BootTraceHandle {\n const entries: BootEntry[] = [];\n let startT: number | null = null;\n\n function mark(name: string): void {\n if (startT === null) {\n startT = nowMs();\n entries.push({ name, t: 0 });\n } else {\n entries.push({ name, t: nowMs() - startT });\n }\n }\n\n function dump(): string | null {\n if (entries.length === 0) return null;\n const parts: string[] = [];\n for (let i = 1; i < entries.length; i++) {\n const prev = entries[i - 1];\n const cur = entries[i];\n if (!prev || !cur) continue;\n parts.push(`${prev.name}->${cur.name}=${(cur.t - prev.t).toFixed(1)}ms`);\n }\n const last = entries[entries.length - 1];\n const totalMs = last ? last.t.toFixed(1) : '0.0';\n const stepStr = parts.length > 0 ? ` ${parts.join(' ')}` : '';\n return `[${label}] total=${totalMs}ms${stepStr}`;\n }\n\n function reset(): void {\n entries.length = 0;\n startT = null;\n }\n\n function getEntries(): readonly BootEntry[] {\n return entries.slice();\n }\n\n return { mark, dump, reset, getEntries };\n}\n","import type { DebugStats } from './stats.js';\n\n/**\n * Formatter for a compact FPS / stats overlay.\n *\n * Consumes a `DebugStats` snapshot and exposes formatted strings ready\n * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,\n * whatever). The widget itself has no rendering dependency so it stays\n * drop-in across renderers.\n *\n * Wrap in `__DEV__` at the call site for tree-shaking.\n */\nexport interface FpsWidget {\n /** Rebuild `line` / `lines` from the latest stats. */\n update(stats: DebugStats): void;\n /** Compact one-line summary, e.g. \"60 FPS | 16.7ms | e:42 d:8\". */\n line: string;\n /** Multi-line breakdown — one string per stat row. */\n readonly lines: string[];\n}\n\n/** Options for the FPS widget formatter. */\nexport interface FpsWidgetOptions {\n /** Show entity count in the one-line summary. Default true. */\n showEntities?: boolean;\n /** Show draw call count in the one-line summary. Default true. */\n showDrawCalls?: boolean;\n /** Number of decimals for milliseconds. Default 1. */\n msPrecision?: number;\n}\n\n/** Create a new FPS widget formatter. */\nexport function createFpsWidget(options: FpsWidgetOptions = {}): FpsWidget {\n const showEntities = options.showEntities ?? true;\n const showDrawCalls = options.showDrawCalls ?? true;\n const msPrecision = options.msPrecision ?? 1;\n const lines: string[] = [];\n\n const widget: FpsWidget = {\n line: '',\n lines,\n update(stats: DebugStats): void {\n const fps = Math.round(stats.fps);\n const ms = (stats.rawDt * 1000).toFixed(msPrecision);\n\n let summary = `${fps} FPS | ${ms}ms`;\n if (showEntities) summary += ` | e:${stats.entityCount}`;\n if (showDrawCalls) summary += ` d:${stats.drawCalls}`;\n widget.line = summary;\n\n lines.length = 0;\n lines.push(`FPS: ${fps}`);\n lines.push(`dt: ${ms}ms`);\n lines.push(`frame: ${stats.frame}`);\n lines.push(`entities: ${stats.entityCount}`);\n lines.push(`draws: ${stats.drawCalls}`);\n },\n };\n return widget;\n}\n","export type FrameBudgetLimits = Record<string, number>;\nexport type FrameBudgetSnapshot = Record<string, number>;\n\nexport type FrameBudgetHandle = {\n recordFrame(dtSec: number): void;\n recordCounters(snapshot: FrameBudgetSnapshot): void;\n getSnapshot(): Readonly<FrameBudgetSnapshot>;\n getAvgFps(): number;\n resetFpsRing(): void;\n};\n\ntype FrameBudgetOpts = {\n limits: FrameBudgetLimits;\n fpsThreshold?: number;\n ringSize?: number;\n onWatchdog?: () => void;\n debug?: boolean;\n};\n\nexport function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle {\n const { limits, onWatchdog, debug = false } = opts;\n const fpsThreshold = opts.fpsThreshold ?? 55;\n const ringSize = opts.ringSize ?? 180;\n\n const ring = new Float32Array(ringSize);\n const limitKeys = Object.keys(limits);\n const _lastSnapshot: Record<string, number> = {};\n for (const k of limitKeys) {\n _lastSnapshot[k] = 0;\n }\n\n let head = 0;\n let filled = 0;\n let watchdogFired = false;\n\n function recordFrame(dtSec: number): void {\n ring[head] = dtSec;\n head = (head + 1) % ringSize;\n if (filled < ringSize) filled += 1;\n\n if (filled === ringSize && !watchdogFired) {\n if (getAvgFps() < fpsThreshold) {\n watchdogFired = true;\n onWatchdog?.();\n }\n }\n }\n\n function recordCounters(snapshot: FrameBudgetSnapshot): void {\n for (let i = 0; i < limitKeys.length; i++) {\n const k = limitKeys[i] as string;\n const v = snapshot[k] ?? 0;\n _lastSnapshot[k] = v;\n if (debug && v > (limits[k] as number)) {\n console.warn(`[frame-budget] ${k} over limit: ${v} > ${limits[k]}`);\n }\n }\n }\n\n function getSnapshot(): Readonly<FrameBudgetSnapshot> {\n return _lastSnapshot;\n }\n\n function getAvgFps(): number {\n if (filled < ringSize) return 0;\n let sum = 0;\n for (let i = 0; i < ringSize; i++) {\n sum += ring[i] as number;\n }\n const avgDt = sum / ringSize;\n return avgDt > 0 ? 1 / avgDt : 0;\n }\n\n function resetFpsRing(): void {\n ring.fill(0);\n head = 0;\n filled = 0;\n watchdogFired = false;\n }\n\n return { recordFrame, recordCounters, getSnapshot, getAvgFps, resetFpsRing };\n}\n","/**\n * DebugOverlay — configuration for debug visualization overlays.\n * The actual rendering is done by the host (e.g., @flare-engine/react) reading\n * these flags and drawing appropriate debug shapes.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport interface OverlayFlags {\n /** Show collision bounding boxes. */\n showCollisionBounds: boolean;\n /** Show entity origin points. */\n showEntityOrigins: boolean;\n /** Show velocity vectors. */\n showVelocityVectors: boolean;\n /** Show FPS/stats overlay. */\n showStats: boolean;\n}\n\n/** Debug draw command types. */\nexport interface DebugRect {\n readonly type: 'rect';\n x: number;\n y: number;\n width: number;\n height: number;\n color: string;\n}\n\nexport interface DebugCircle {\n readonly type: 'circle';\n x: number;\n y: number;\n radius: number;\n color: string;\n}\n\nexport interface DebugLine {\n readonly type: 'line';\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n color: string;\n}\n\n/** Union of debug draw primitive commands. */\nexport type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;\n\n/**\n * DebugOverlay — collects debug draw commands for the current frame.\n * Commands are generated based on active overlay flags, then consumed\n * by the renderer.\n */\nexport class DebugOverlay {\n /** Active overlay flags. */\n readonly flags: OverlayFlags = {\n showCollisionBounds: false,\n showEntityOrigins: false,\n showVelocityVectors: false,\n showStats: false,\n };\n\n /** Queued debug draw commands for the current frame. */\n private _commands: DebugDrawCommand[] = [];\n\n /** Number of queued commands. */\n get commandCount(): number {\n return this._commands.length;\n }\n\n /** Get all commands (read-only view). */\n get commands(): readonly DebugDrawCommand[] {\n return this._commands;\n }\n\n /** Add a debug rectangle. */\n drawRect(x: number, y: number, width: number, height: number, color = '#00ff00'): void {\n this._commands.push({ type: 'rect', x, y, width, height, color });\n }\n\n /** Add a debug circle. */\n drawCircle(x: number, y: number, radius: number, color = '#00ff00'): void {\n this._commands.push({ type: 'circle', x, y, radius, color });\n }\n\n /** Add a debug line. */\n drawLine(x1: number, y1: number, x2: number, y2: number, color = '#ffff00'): void {\n this._commands.push({ type: 'line', x1, y1, x2, y2, color });\n }\n\n /** Clear all commands. Call at the start of each frame. */\n clear(): void {\n this._commands.length = 0;\n }\n\n /** Toggle a specific overlay flag. */\n toggleFlag(flag: keyof OverlayFlags): void {\n this.flags[flag] = !this.flags[flag];\n }\n\n /** Enable all overlays. */\n enableAll(): void {\n this.flags.showCollisionBounds = true;\n this.flags.showEntityOrigins = true;\n this.flags.showVelocityVectors = true;\n this.flags.showStats = true;\n }\n\n /** Disable all overlays. */\n disableAll(): void {\n this.flags.showCollisionBounds = false;\n this.flags.showEntityOrigins = false;\n this.flags.showVelocityVectors = false;\n this.flags.showStats = false;\n }\n}\n","import type { PerfReport } from './perf-recorder.js';\n\nexport const PERF_BENCH_TAG = '[perf-bench]';\n\nexport function emitPerfReport(report: PerfReport): void {\n console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);\n}\n","export type PerfSectionStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly mean: number;\n readonly p50: number;\n readonly p95: number;\n readonly p99: number;\n};\n\nexport type PerfCounterStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly sum: number;\n readonly mean: number;\n};\n\nexport type PerfReport = {\n readonly label: string;\n readonly durationMs: number;\n readonly frameCount: number;\n readonly sections: readonly PerfSectionStats[];\n readonly counters: readonly PerfCounterStats[];\n};\n\nexport type PerfRecorderHandle = {\n beginSection(name: string): void;\n endSection(name: string): void;\n recordFrame(counters?: Record<string, number>): void;\n flush(): PerfReport;\n reset(): void;\n};\n\ntype SectionState = {\n name: string;\n ring: Float32Array;\n ringSize: number;\n head: number;\n filled: number;\n startT: number;\n};\n\ntype CounterState = {\n name: string;\n count: number;\n min: number;\n max: number;\n sum: number;\n};\n\ntype PerfRecorderOpts = {\n label?: string;\n ringSize?: number;\n};\n\nfunction nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n\nfunction percentile(sorted: Float32Array, n: number, p: number): number {\n if (n === 0) return 0;\n if (n === 1) return sorted[0] as number;\n const idx = (p / 100) * (n - 1);\n const lo = Math.floor(idx);\n const hi = Math.ceil(idx);\n const a = sorted[lo] as number;\n const b = sorted[hi] as number;\n return a + (b - a) * (idx - lo);\n}\n\nexport function createPerfRecorder(opts: PerfRecorderOpts = {}): PerfRecorderHandle {\n const label = opts.label ?? 'perf';\n const ringSize = opts.ringSize ?? 1800;\n\n const sections = new Map<string, SectionState>();\n const counters = new Map<string, CounterState>();\n let frameCount = 0;\n let startT: number | null = null;\n\n // Reusable sort buffer — allocated once on first flush, grown as needed\n let sortBuf = new Float32Array(ringSize);\n\n function getOrCreateSection(name: string): SectionState {\n let s = sections.get(name);\n if (s === undefined) {\n s = {\n name,\n ring: new Float32Array(ringSize),\n ringSize,\n head: 0,\n filled: 0,\n startT: 0,\n };\n sections.set(name, s);\n }\n return s;\n }\n\n function getOrCreateCounter(name: string): CounterState {\n let c = counters.get(name);\n if (c === undefined) {\n c = { name, count: 0, min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY, sum: 0 };\n counters.set(name, c);\n }\n return c;\n }\n\n function beginSection(name: string): void {\n const s = getOrCreateSection(name);\n s.startT = nowMs();\n }\n\n function endSection(name: string): void {\n const s = sections.get(name);\n if (s === undefined) return;\n const elapsed = nowMs() - s.startT;\n s.ring[s.head] = elapsed;\n s.head = (s.head + 1) % s.ringSize;\n if (s.filled < s.ringSize) s.filled += 1;\n }\n\n function recordFrame(frameCounters?: Record<string, number>): void {\n if (startT === null) startT = nowMs();\n frameCount += 1;\n if (frameCounters === undefined) return;\n for (const name in frameCounters) {\n const v = frameCounters[name] as number;\n const c = getOrCreateCounter(name);\n c.count += 1;\n if (v < c.min) c.min = v;\n if (v > c.max) c.max = v;\n c.sum += v;\n }\n }\n\n function flush(): PerfReport {\n const now = nowMs();\n const durationMs = startT !== null ? now - startT : 0;\n\n const sectionStats: PerfSectionStats[] = [];\n for (const s of sections.values()) {\n const n = s.filled;\n if (n === 0) {\n sectionStats.push({\n name: s.name,\n count: 0,\n min: 0,\n max: 0,\n mean: 0,\n p50: 0,\n p95: 0,\n p99: 0,\n });\n continue;\n }\n // Grow sort buffer if needed\n if (sortBuf.length < n) sortBuf = new Float32Array(n);\n let sum = 0;\n let minV = Number.POSITIVE_INFINITY;\n let maxV = Number.NEGATIVE_INFINITY;\n // Copy ring in chronological order\n const start = s.filled < s.ringSize ? 0 : s.head;\n for (let i = 0; i < n; i++) {\n const v = s.ring[(start + i) % s.ringSize] as number;\n sortBuf[i] = v;\n sum += v;\n if (v < minV) minV = v;\n if (v > maxV) maxV = v;\n }\n // Sort only the filled slice\n const slice = sortBuf.subarray(0, n);\n slice.sort();\n sectionStats.push({\n name: s.name,\n count: n,\n min: minV,\n max: maxV,\n mean: sum / n,\n p50: percentile(slice, n, 50),\n p95: percentile(slice, n, 95),\n p99: percentile(slice, n, 99),\n });\n }\n\n const counterStats: PerfCounterStats[] = [];\n for (const c of counters.values()) {\n counterStats.push({\n name: c.name,\n count: c.count,\n min: c.count > 0 ? c.min : 0,\n max: c.count > 0 ? c.max : 0,\n sum: c.sum,\n mean: c.count > 0 ? c.sum / c.count : 0,\n });\n }\n\n return { label, durationMs, frameCount, sections: sectionStats, counters: counterStats };\n }\n\n function reset(): void {\n for (const s of sections.values()) {\n s.ring.fill(0);\n s.head = 0;\n s.filled = 0;\n s.startT = 0;\n }\n for (const c of counters.values()) {\n c.count = 0;\n c.min = Number.POSITIVE_INFINITY;\n c.max = Number.NEGATIVE_INFINITY;\n c.sum = 0;\n }\n frameCount = 0;\n startT = null;\n }\n\n return { beginSection, endSection, recordFrame, flush, reset };\n}\n","/** Snapshot of engine stats for a single frame. */\nexport interface DebugStats {\n /** Frames per second (smoothed). */\n fps: number;\n /** Raw delta time in seconds. */\n rawDt: number;\n /** Scaled delta time in seconds. */\n dt: number;\n /** Current frame number. */\n frame: number;\n /** Total active entities (set by user). */\n entityCount: number;\n /** Draw calls this frame (set by user). */\n drawCalls: number;\n}\n\n/**\n * StatsTracker — collects and exposes per-frame debug statistics.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class StatsTracker {\n /** Current frame stats (read-only snapshot). */\n readonly stats: DebugStats = {\n fps: 0,\n rawDt: 0,\n dt: 0,\n frame: 0,\n entityCount: 0,\n drawCalls: 0,\n };\n\n /** Update timing stats from the clock. */\n updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void {\n this.stats.fps = fps;\n this.stats.rawDt = rawDt;\n this.stats.dt = dt;\n this.stats.frame = frame;\n }\n\n /** Set entity count for this frame. */\n setEntityCount(count: number): void {\n this.stats.entityCount = count;\n }\n\n /** Set draw call count for this frame. */\n setDrawCalls(count: number): void {\n this.stats.drawCalls = count;\n }\n\n /** Reset all counters to zero. */\n reset(): void {\n this.stats.fps = 0;\n this.stats.rawDt = 0;\n this.stats.dt = 0;\n this.stats.frame = 0;\n this.stats.entityCount = 0;\n this.stats.drawCalls = 0;\n }\n}\n","import type { Clock } from '@flare-engine/core';\n\n/**\n * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class TimeControl {\n private _clock: Clock;\n private _savedTimescale = 1;\n private _paused = false;\n private _stepRequested = false;\n\n constructor(clock: Clock) {\n this._clock = clock;\n }\n\n /** Whether the game is currently paused. */\n get paused(): boolean {\n return this._paused;\n }\n\n /** Current time scale. */\n get timescale(): number {\n return this._clock.timescale;\n }\n\n /** Pause the game (sets timescale to 0). */\n pause(): void {\n if (this._paused) return;\n this._savedTimescale = this._clock.timescale;\n this._clock.timescale = 0;\n this._paused = true;\n }\n\n /** Resume the game (restores previous timescale). */\n resume(): void {\n if (!this._paused) return;\n this._clock.timescale = this._savedTimescale;\n this._paused = false;\n this._stepRequested = false;\n }\n\n /** Toggle pause/resume. */\n toggle(): void {\n if (this._paused) {\n this.resume();\n } else {\n this.pause();\n }\n }\n\n /**\n * Request a single-frame step while paused.\n * Call this, then on the next update check `consumeStep()`.\n */\n step(): void {\n if (!this._paused) return;\n this._stepRequested = true;\n }\n\n /**\n * Consume a pending step request. Returns true if a step was requested.\n * When true, the caller should run one frame with the saved timescale,\n * then re-pause.\n */\n consumeStep(): boolean {\n if (!this._stepRequested) return false;\n this._stepRequested = false;\n return true;\n }\n\n /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */\n setTimescale(scale: number): void {\n if (this._paused) {\n this._savedTimescale = scale;\n } else {\n this._clock.timescale = scale;\n }\n }\n}\n"],"mappings":";;AAYA,SAASA,UAAgB;CACvB,IAAI,OAAO,gBAAgB,eAAe,OAAO,YAAY,QAAQ,YACnE,OAAO,YAAY,IAAI;CAEzB,OAAO,KAAK,IAAI;AAClB;AAEA,SAAgB,gBAAgB,QAAQ,cAA+B;CACrE,MAAM,UAAuB,CAAC;CAC9B,IAAI,SAAwB;CAE5B,SAAS,KAAK,MAAoB;EAChC,IAAI,WAAW,MAAM;GACnB,SAASA,QAAM;GACf,QAAQ,KAAK;IAAE;IAAM,GAAG;GAAE,CAAC;EAC7B,OACE,QAAQ,KAAK;GAAE;GAAM,GAAGA,QAAM,IAAI;EAAO,CAAC;CAE9C;CAEA,SAAS,OAAsB;EAC7B,IAAI,QAAQ,WAAW,GAAG,OAAO;EACjC,MAAM,QAAkB,CAAC;EACzB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;GACvC,MAAM,OAAO,QAAQ,IAAI;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,CAAC,QAAQ,CAAC,KAAK;GACnB,MAAM,KAAK,GAAG,KAAK,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,EAAA,CAAG,QAAQ,CAAC,EAAE,GAAG;EACzE;EACA,MAAM,OAAO,QAAQ,QAAQ,SAAS;EAGtC,OAAO,IAAI,MAAM,UAFD,OAAO,KAAK,EAAE,QAAQ,CAAC,IAAI,MAER,IADnB,MAAM,SAAS,IAAI,IAAI,MAAM,KAAK,GAAG,MAAM;CAE7D;CAEA,SAAS,QAAc;EACrB,QAAQ,SAAS;EACjB,SAAS;CACX;CAEA,SAAS,aAAmC;EAC1C,OAAO,QAAQ,MAAM;CACvB;CAEA,OAAO;EAAE;EAAM;EAAM;EAAO;CAAW;AACzC;;;ACzBA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S;EACnB,KAAK,OAAO,YAAY,KAAK;EAC7B,KAAK,UAAU;EACf,KAAK,iBAAiB;CACxB;;CAGA,SAAe;EACb,IAAI,KAAK,SACP,KAAK,OAAO;OAEZ,KAAK,MAAM;CAEf;;;;;CAMA,OAAa;EACX,IAAI,CAAC,KAAK,SAAS;EACnB,KAAK,iBAAiB;CACxB;;;;;;CAOA,cAAuB;EACrB,IAAI,CAAC,KAAK,gBAAgB,OAAO;EACjC,KAAK,iBAAiB;EACtB,OAAO;CACT;;CAGA,aAAa,OAAqB;EAChC,IAAI,KAAK,SACP,KAAK,kBAAkB;OAEvB,KAAK,OAAO,YAAY;CAE5B;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/now.ts","../src/boot-trace.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-output.ts","../src/perf-recorder.ts","../src/stats.ts","../src/time-control.ts"],"sourcesContent":["export function nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n","import { nowMs } from './now.js';\n\nexport type BootEntry = {\n readonly name: string;\n readonly t: number;\n};\n\nexport type BootTraceHandle = {\n mark(name: string): void;\n dump(): string | null;\n reset(): void;\n getEntries(): readonly BootEntry[];\n};\n\nexport function createBootTrace(label = 'boot-trace'): BootTraceHandle {\n const entries: BootEntry[] = [];\n let startT: number | null = null;\n\n function mark(name: string): void {\n if (startT === null) {\n startT = nowMs();\n entries.push({ name, t: 0 });\n } else {\n entries.push({ name, t: nowMs() - startT });\n }\n }\n\n function dump(): string | null {\n if (entries.length === 0) return null;\n const parts: string[] = [];\n for (let i = 1; i < entries.length; i++) {\n const prev = entries[i - 1];\n const cur = entries[i];\n if (!prev || !cur) continue;\n parts.push(`${prev.name}->${cur.name}=${(cur.t - prev.t).toFixed(1)}ms`);\n }\n const last = entries[entries.length - 1];\n const totalMs = last ? last.t.toFixed(1) : '0.0';\n const stepStr = parts.length > 0 ? ` ${parts.join(' ')}` : '';\n return `[${label}] total=${totalMs}ms${stepStr}`;\n }\n\n function reset(): void {\n entries.length = 0;\n startT = null;\n }\n\n function getEntries(): readonly BootEntry[] {\n return entries.slice();\n }\n\n return { mark, dump, reset, getEntries };\n}\n","import type { DebugStats } from './stats.js';\n\n/**\n * Formatter for a compact FPS / stats overlay.\n *\n * Consumes a `DebugStats` snapshot and exposes formatted strings ready\n * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,\n * whatever). The widget itself has no rendering dependency so it stays\n * drop-in across renderers.\n *\n * Wrap in `__DEV__` at the call site for tree-shaking.\n */\nexport interface FpsWidget {\n /** Rebuild `line` / `lines` from the latest stats. */\n update(stats: DebugStats): void;\n /** Compact one-line summary, e.g. \"60 FPS | 16.7ms | e:42 d:8\". */\n line: string;\n /** Multi-line breakdown — one string per stat row. */\n readonly lines: string[];\n}\n\n/** Options for the FPS widget formatter. */\nexport interface FpsWidgetOptions {\n /** Show entity count in the one-line summary. Default true. */\n showEntities?: boolean;\n /** Show draw call count in the one-line summary. Default true. */\n showDrawCalls?: boolean;\n /** Number of decimals for milliseconds. Default 1. */\n msPrecision?: number;\n}\n\n/** Create a new FPS widget formatter. */\nexport function createFpsWidget(options: FpsWidgetOptions = {}): FpsWidget {\n const showEntities = options.showEntities ?? true;\n const showDrawCalls = options.showDrawCalls ?? true;\n const msPrecision = options.msPrecision ?? 1;\n const lines: string[] = [];\n\n const widget: FpsWidget = {\n line: '',\n lines,\n update(stats: DebugStats): void {\n const fps = Math.round(stats.fps);\n const ms = (stats.rawDt * 1000).toFixed(msPrecision);\n\n let summary = `${fps} FPS | ${ms}ms`;\n if (showEntities) summary += ` | e:${stats.entityCount}`;\n if (showDrawCalls) summary += ` d:${stats.drawCalls}`;\n widget.line = summary;\n\n lines.length = 0;\n lines.push(`FPS: ${fps}`);\n lines.push(`dt: ${ms}ms`);\n lines.push(`frame: ${stats.frame}`);\n lines.push(`entities: ${stats.entityCount}`);\n lines.push(`draws: ${stats.drawCalls}`);\n },\n };\n return widget;\n}\n","export type FrameBudgetLimits = Record<string, number>;\nexport type FrameBudgetSnapshot = Record<string, number>;\n\nexport type FrameBudgetHandle = {\n recordFrame(dtSec: number): void;\n recordCounters(snapshot: FrameBudgetSnapshot): void;\n getSnapshot(): Readonly<FrameBudgetSnapshot>;\n getAvgFps(): number;\n resetFpsRing(): void;\n};\n\nexport type FrameBudgetOpts = {\n limits: FrameBudgetLimits;\n fpsThreshold?: number;\n ringSize?: number;\n onWatchdog?: () => void;\n debug?: boolean;\n};\n\nexport function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle {\n const { limits, onWatchdog, debug = false } = opts;\n const fpsThreshold = opts.fpsThreshold ?? 55;\n const ringSize = opts.ringSize ?? 180;\n\n const ring = new Float32Array(ringSize);\n const limitKeys = Object.keys(limits);\n const _lastSnapshot: Record<string, number> = {};\n for (const k of limitKeys) {\n _lastSnapshot[k] = 0;\n }\n\n let head = 0;\n let filled = 0;\n let sum = 0;\n let watchdogFired = false;\n\n function recordFrame(dtSec: number): void {\n const old = ring[head] as number;\n ring[head] = dtSec;\n // Add the f32 value the ring actually stored, so the later subtraction removes that same value.\n sum += (ring[head] as number) - old;\n head = (head + 1) % ringSize;\n if (filled < ringSize) filled += 1;\n\n // Re-sum once per revolution: bounds rounding drift and flushes a non-finite or huge dt\n // that would otherwise stay in the running sum after leaving the ring.\n if (head === 0) {\n sum = 0;\n for (let i = 0; i < ringSize; i++) {\n sum += ring[i] as number;\n }\n }\n\n if (filled === ringSize && !watchdogFired) {\n if (getAvgFps() < fpsThreshold) {\n watchdogFired = true;\n onWatchdog?.();\n }\n }\n }\n\n function recordCounters(snapshot: FrameBudgetSnapshot): void {\n for (let i = 0; i < limitKeys.length; i++) {\n const k = limitKeys[i] as string;\n const v = snapshot[k] ?? 0;\n _lastSnapshot[k] = v;\n if (debug && v > (limits[k] as number)) {\n console.warn(`[frame-budget] ${k} over limit: ${v} > ${limits[k]}`);\n }\n }\n }\n\n function getSnapshot(): Readonly<FrameBudgetSnapshot> {\n return _lastSnapshot;\n }\n\n function getAvgFps(): number {\n if (filled < ringSize) return 0;\n return sum > 0 ? ringSize / sum : 0;\n }\n\n function resetFpsRing(): void {\n ring.fill(0);\n sum = 0;\n head = 0;\n filled = 0;\n watchdogFired = false;\n }\n\n return { recordFrame, recordCounters, getSnapshot, getAvgFps, resetFpsRing };\n}\n","/**\n * DebugOverlay — configuration for debug visualization overlays.\n * The actual rendering is done by the host (e.g., @flare-engine/react) reading\n * these flags and drawing appropriate debug shapes.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport interface OverlayFlags {\n /** Show collision bounding boxes. */\n showCollisionBounds: boolean;\n /** Show entity origin points. */\n showEntityOrigins: boolean;\n /** Show velocity vectors. */\n showVelocityVectors: boolean;\n /** Show FPS/stats overlay. */\n showStats: boolean;\n}\n\n/** Debug draw command types. */\nexport interface DebugRect {\n readonly type: 'rect';\n x: number;\n y: number;\n width: number;\n height: number;\n color: string;\n}\n\nexport interface DebugCircle {\n readonly type: 'circle';\n x: number;\n y: number;\n radius: number;\n color: string;\n}\n\nexport interface DebugLine {\n readonly type: 'line';\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n color: string;\n}\n\n/** Union of debug draw primitive commands. */\nexport type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;\n\n/** Options for {@link DebugOverlay}. */\nexport interface DebugOverlayOptions {\n /**\n * When true, `drawRect` / `drawCircle` / `drawLine` reuse pooled command\n * objects, so no command object is created per call once the pools are warm.\n * A reused object changes after `clear()`, so copy a command to keep it past\n * the frame. Default `false`: every call creates a fresh object.\n */\n reuseCommands?: boolean;\n}\n\n/**\n * DebugOverlay — collects debug draw commands for the current frame.\n * Commands are generated based on active overlay flags, then consumed\n * by the renderer. Commands are fresh objects by default and pooled with\n * `reuseCommands` (see {@link DebugOverlayOptions}).\n */\nexport class DebugOverlay {\n /** Active overlay flags. */\n readonly flags: OverlayFlags = {\n showCollisionBounds: false,\n showEntityOrigins: false,\n showVelocityVectors: false,\n showStats: false,\n };\n\n /** Queued debug draw commands for the current frame. */\n private _commands: DebugDrawCommand[] = [];\n\n private _rects: DebugRect[] = [];\n private _circles: DebugCircle[] = [];\n private _lines: DebugLine[] = [];\n private _rectCursor = 0;\n private _circleCursor = 0;\n private _lineCursor = 0;\n private readonly _reuseCommands: boolean;\n\n constructor(options: DebugOverlayOptions = {}) {\n this._reuseCommands = options.reuseCommands === true;\n }\n\n /** Number of queued commands. */\n get commandCount(): number {\n return this._commands.length;\n }\n\n /** Get all commands (read-only view). */\n get commands(): readonly DebugDrawCommand[] {\n return this._commands;\n }\n\n /** Add a debug rectangle. */\n drawRect(x: number, y: number, width: number, height: number, color = '#00ff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'rect', x, y, width, height, color });\n return;\n }\n let cmd = this._rects[this._rectCursor];\n if (cmd) {\n cmd.x = x;\n cmd.y = y;\n cmd.width = width;\n cmd.height = height;\n cmd.color = color;\n } else {\n cmd = { type: 'rect', x, y, width, height, color };\n this._rects.push(cmd);\n }\n this._rectCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Add a debug circle. */\n drawCircle(x: number, y: number, radius: number, color = '#00ff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'circle', x, y, radius, color });\n return;\n }\n let cmd = this._circles[this._circleCursor];\n if (cmd) {\n cmd.x = x;\n cmd.y = y;\n cmd.radius = radius;\n cmd.color = color;\n } else {\n cmd = { type: 'circle', x, y, radius, color };\n this._circles.push(cmd);\n }\n this._circleCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Add a debug line. */\n drawLine(x1: number, y1: number, x2: number, y2: number, color = '#ffff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'line', x1, y1, x2, y2, color });\n return;\n }\n let cmd = this._lines[this._lineCursor];\n if (cmd) {\n cmd.x1 = x1;\n cmd.y1 = y1;\n cmd.x2 = x2;\n cmd.y2 = y2;\n cmd.color = color;\n } else {\n cmd = { type: 'line', x1, y1, x2, y2, color };\n this._lines.push(cmd);\n }\n this._lineCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Clear all commands. Call at the start of each frame. */\n clear(): void {\n this._commands.length = 0;\n this._rectCursor = 0;\n this._circleCursor = 0;\n this._lineCursor = 0;\n }\n\n /** Toggle a specific overlay flag. */\n toggleFlag(flag: keyof OverlayFlags): void {\n this.flags[flag] = !this.flags[flag];\n }\n\n /** Enable all overlays. */\n enableAll(): void {\n this.flags.showCollisionBounds = true;\n this.flags.showEntityOrigins = true;\n this.flags.showVelocityVectors = true;\n this.flags.showStats = true;\n }\n\n /** Disable all overlays. */\n disableAll(): void {\n this.flags.showCollisionBounds = false;\n this.flags.showEntityOrigins = false;\n this.flags.showVelocityVectors = false;\n this.flags.showStats = false;\n }\n}\n","import type { PerfReport } from './perf-recorder.js';\n\nexport const PERF_BENCH_TAG = '[perf-bench]';\n\nexport function emitPerfReport(report: PerfReport): void {\n console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);\n}\n","import { nowMs } from './now.js';\n\nexport type PerfSectionStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly mean: number;\n readonly p50: number;\n readonly p95: number;\n readonly p99: number;\n};\n\nexport type PerfCounterStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly sum: number;\n readonly mean: number;\n};\n\nexport type PerfReport = {\n readonly label: string;\n readonly durationMs: number;\n readonly frameCount: number;\n readonly sections: readonly PerfSectionStats[];\n readonly counters: readonly PerfCounterStats[];\n};\n\nexport type PerfRecorderHandle = {\n beginSection(name: string): void;\n endSection(name: string): void;\n recordFrame(counters?: Record<string, number>): void;\n flush(): PerfReport;\n reset(): void;\n};\n\ntype SectionState = {\n name: string;\n ring: Float32Array;\n ringSize: number;\n head: number;\n filled: number;\n startT: number;\n};\n\ntype CounterState = {\n name: string;\n count: number;\n min: number;\n max: number;\n sum: number;\n};\n\nexport type PerfRecorderOpts = {\n label?: string;\n ringSize?: number;\n};\n\nfunction percentile(sorted: Float32Array, n: number, p: number): number {\n if (n === 0) return 0;\n if (n === 1) return sorted[0] as number;\n const idx = (p / 100) * (n - 1);\n const lo = Math.floor(idx);\n const hi = Math.ceil(idx);\n const a = sorted[lo] as number;\n const b = sorted[hi] as number;\n return a + (b - a) * (idx - lo);\n}\n\nexport function createPerfRecorder(opts: PerfRecorderOpts = {}): PerfRecorderHandle {\n const label = opts.label ?? 'perf';\n const ringSize = opts.ringSize ?? 1800;\n\n const sections = new Map<string, SectionState>();\n const counters = new Map<string, CounterState>();\n let frameCount = 0;\n let startT: number | null = null;\n\n // Reusable sort buffer — allocated once on first flush, grown as needed\n let sortBuf = new Float32Array(ringSize);\n\n function getOrCreateSection(name: string): SectionState {\n let s = sections.get(name);\n if (s === undefined) {\n s = {\n name,\n ring: new Float32Array(ringSize),\n ringSize,\n head: 0,\n filled: 0,\n startT: 0,\n };\n sections.set(name, s);\n }\n return s;\n }\n\n function getOrCreateCounter(name: string): CounterState {\n let c = counters.get(name);\n if (c === undefined) {\n c = { name, count: 0, min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY, sum: 0 };\n counters.set(name, c);\n }\n return c;\n }\n\n function beginSection(name: string): void {\n const s = getOrCreateSection(name);\n s.startT = nowMs();\n }\n\n function endSection(name: string): void {\n const s = sections.get(name);\n if (s === undefined) return;\n const elapsed = nowMs() - s.startT;\n s.ring[s.head] = elapsed;\n s.head = (s.head + 1) % s.ringSize;\n if (s.filled < s.ringSize) s.filled += 1;\n }\n\n function recordFrame(frameCounters?: Record<string, number>): void {\n if (startT === null) startT = nowMs();\n frameCount += 1;\n if (frameCounters === undefined) return;\n for (const name in frameCounters) {\n const v = frameCounters[name] as number;\n const c = getOrCreateCounter(name);\n c.count += 1;\n if (v < c.min) c.min = v;\n if (v > c.max) c.max = v;\n c.sum += v;\n }\n }\n\n function flush(): PerfReport {\n const now = nowMs();\n const durationMs = startT !== null ? now - startT : 0;\n\n const sectionStats: PerfSectionStats[] = [];\n for (const s of sections.values()) {\n const n = s.filled;\n if (n === 0) {\n sectionStats.push({\n name: s.name,\n count: 0,\n min: 0,\n max: 0,\n mean: 0,\n p50: 0,\n p95: 0,\n p99: 0,\n });\n continue;\n }\n // Grow sort buffer if needed\n if (sortBuf.length < n) sortBuf = new Float32Array(n);\n let sum = 0;\n let minV = Number.POSITIVE_INFINITY;\n let maxV = Number.NEGATIVE_INFINITY;\n // Copy ring in chronological order\n const start = s.filled < s.ringSize ? 0 : s.head;\n for (let i = 0; i < n; i++) {\n const v = s.ring[(start + i) % s.ringSize] as number;\n sortBuf[i] = v;\n sum += v;\n if (v < minV) minV = v;\n if (v > maxV) maxV = v;\n }\n // Sort only the filled slice\n const slice = sortBuf.subarray(0, n);\n slice.sort();\n sectionStats.push({\n name: s.name,\n count: n,\n min: minV,\n max: maxV,\n mean: sum / n,\n p50: percentile(slice, n, 50),\n p95: percentile(slice, n, 95),\n p99: percentile(slice, n, 99),\n });\n }\n\n const counterStats: PerfCounterStats[] = [];\n for (const c of counters.values()) {\n counterStats.push({\n name: c.name,\n count: c.count,\n min: c.count > 0 ? c.min : 0,\n max: c.count > 0 ? c.max : 0,\n sum: c.sum,\n mean: c.count > 0 ? c.sum / c.count : 0,\n });\n }\n\n return { label, durationMs, frameCount, sections: sectionStats, counters: counterStats };\n }\n\n function reset(): void {\n for (const s of sections.values()) {\n s.ring.fill(0);\n s.head = 0;\n s.filled = 0;\n s.startT = 0;\n }\n for (const c of counters.values()) {\n c.count = 0;\n c.min = Number.POSITIVE_INFINITY;\n c.max = Number.NEGATIVE_INFINITY;\n c.sum = 0;\n }\n frameCount = 0;\n startT = null;\n }\n\n return { beginSection, endSection, recordFrame, flush, reset };\n}\n","/** Snapshot of engine stats for a single frame. */\nexport interface DebugStats {\n /** Frames per second (smoothed). */\n fps: number;\n /** Raw delta time in seconds. */\n rawDt: number;\n /** Scaled delta time in seconds. */\n dt: number;\n /** Current frame number. */\n frame: number;\n /** Total active entities (set by user). */\n entityCount: number;\n /** Draw calls this frame (set by user). */\n drawCalls: number;\n}\n\n/**\n * StatsTracker — collects and exposes per-frame debug statistics.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class StatsTracker {\n /** Current frame stats (read-only snapshot). */\n readonly stats: DebugStats = {\n fps: 0,\n rawDt: 0,\n dt: 0,\n frame: 0,\n entityCount: 0,\n drawCalls: 0,\n };\n\n /** Update timing stats from the clock. */\n updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void {\n this.stats.fps = fps;\n this.stats.rawDt = rawDt;\n this.stats.dt = dt;\n this.stats.frame = frame;\n }\n\n /** Set entity count for this frame. */\n setEntityCount(count: number): void {\n this.stats.entityCount = count;\n }\n\n /** Set draw call count for this frame. */\n setDrawCalls(count: number): void {\n this.stats.drawCalls = count;\n }\n\n /** Reset all counters to zero. */\n reset(): void {\n this.stats.fps = 0;\n this.stats.rawDt = 0;\n this.stats.dt = 0;\n this.stats.frame = 0;\n this.stats.entityCount = 0;\n this.stats.drawCalls = 0;\n }\n}\n","import type { Clock } from '@flare-engine/core';\n\n/**\n * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class TimeControl {\n private _clock: Clock;\n private _savedTimescale = 1;\n private _paused = false;\n private _stepRequested = false;\n\n constructor(clock: Clock) {\n this._clock = clock;\n }\n\n /** Whether the game is currently paused. */\n get paused(): boolean {\n return this._paused;\n }\n\n /** Current time scale. */\n get timescale(): number {\n return this._clock.timescale;\n }\n\n /** Pause the game (sets timescale to 0). */\n pause(): void {\n if (this._paused) return;\n this._savedTimescale = this._clock.timescale;\n this._clock.timescale = 0;\n this._paused = true;\n }\n\n /** Resume the game (restores previous timescale). */\n resume(): void {\n if (!this._paused) return;\n this._clock.timescale = this._savedTimescale;\n this._paused = false;\n this._stepRequested = false;\n }\n\n /** Toggle pause/resume. */\n toggle(): void {\n if (this._paused) {\n this.resume();\n } else {\n this.pause();\n }\n }\n\n /**\n * Request a single-frame step while paused.\n * Call this, then on the next update check `consumeStep()`.\n */\n step(): void {\n if (!this._paused) return;\n this._stepRequested = true;\n }\n\n /**\n * Consume a pending step request. Returns true if a step was requested.\n * When true, the caller should run one frame with the saved timescale,\n * then re-pause.\n */\n consumeStep(): boolean {\n if (!this._stepRequested) return false;\n this._stepRequested = false;\n return true;\n }\n\n /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */\n setTimescale(scale: number): void {\n if (this._paused) {\n this._savedTimescale = scale;\n } else {\n this._clock.timescale = scale;\n }\n }\n}\n"],"mappings":";;AAAA,SAAgB,QAAgB;CAC9B,IAAI,OAAO,gBAAgB,eAAe,OAAO,YAAY,QAAQ,YACnE,OAAO,YAAY,IAAI;CAEzB,OAAO,KAAK,IAAI;AAClB;;ACSA,SAAgB,gBAAgB,QAAQ,cAA+B;CACrE,MAAM,UAAuB,CAAC;CAC9B,IAAI,SAAwB;CAE5B,SAAS,KAAK,MAAoB;EAChC,IAAI,WAAW,MAAM;GACnB,SAAS,MAAM;GACf,QAAQ,KAAK;IAAE;IAAM,GAAG;GAAE,CAAC;EAC7B,OACE,QAAQ,KAAK;GAAE;GAAM,GAAG,MAAM,IAAI;EAAO,CAAC;CAE9C;CAEA,SAAS,OAAsB;EAC7B,IAAI,QAAQ,WAAW,GAAG,OAAO;EACjC,MAAM,QAAkB,CAAC;EACzB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;GACvC,MAAM,OAAO,QAAQ,IAAI;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,CAAC,QAAQ,CAAC,KAAK;GACnB,MAAM,KAAK,GAAG,KAAK,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,EAAA,CAAG,QAAQ,CAAC,EAAE,GAAG;EACzE;EACA,MAAM,OAAO,QAAQ,QAAQ,SAAS;EAGtC,OAAO,IAAI,MAAM,UAFD,OAAO,K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ACd;;CAGA,IAAI,YAAoB;EACtB,OAAO,KAAK,OAAO;CACrB;;CAGA,QAAc;EACZ,IAAI,KAAK,SAAS;EAClB,KAAK,kBAAkB,KAAK,OAAO;EACnC,KAAK,OAAO,YAAY;EACxB,KAAK,UAAU;CACjB;;CAGA,SAAe;EACb,IAAI,CAAC,KAAK,SAAS;EACnB,KAAK,OAAO,YAAY,KAAK;EAC7B,KAAK,UAAU;EACf,KAAK,iBAAiB;CACxB;;CAGA,SAAe;EACb,IAAI,KAAK,SACP,KAAK,OAAO;OAEZ,KAAK,MAAM;CAEf;;;;;CAMA,OAAa;EACX,IAAI,CAAC,KAAK,SAAS;EACnB,KAAK,iBAAiB;CACxB;;;;;;CAOA,cAAuB;EACrB,IAAI,CAAC,KAAK,gBAAgB,OAAO;EACjC,KAAK,iBAAiB;EACtB,OAAO;CACT;;CAGA,aAAa,OAAqB;EAChC,IAAI,KAAK,SACP,KAAK,kBAAkB;OAEvB,KAAK,OAAO,YAAY;CAE5B;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
function nowMs
|
|
1
|
+
// src/now.ts
|
|
2
|
+
function nowMs() {
|
|
3
3
|
if (typeof performance !== "undefined" && typeof performance.now === "function") return performance.now();
|
|
4
4
|
return Date.now();
|
|
5
5
|
}
|
|
6
|
+
// src/boot-trace.ts
|
|
6
7
|
function createBootTrace(label = "boot-trace") {
|
|
7
8
|
const entries = [];
|
|
8
9
|
let startT = null;
|
|
9
10
|
function mark(name) {
|
|
10
11
|
if (startT === null) {
|
|
11
|
-
startT = nowMs
|
|
12
|
+
startT = nowMs();
|
|
12
13
|
entries.push({
|
|
13
14
|
name,
|
|
14
15
|
t: 0
|
|
15
16
|
});
|
|
16
17
|
} else entries.push({
|
|
17
18
|
name,
|
|
18
|
-
t: nowMs
|
|
19
|
+
t: nowMs() - startT
|
|
19
20
|
});
|
|
20
21
|
}
|
|
21
22
|
function dump() {
|
|
@@ -82,11 +83,18 @@ function createFrameBudget(opts) {
|
|
|
82
83
|
for (const k of limitKeys) _lastSnapshot[k] = 0;
|
|
83
84
|
let head = 0;
|
|
84
85
|
let filled = 0;
|
|
86
|
+
let sum = 0;
|
|
85
87
|
let watchdogFired = false;
|
|
86
88
|
function recordFrame(dtSec) {
|
|
89
|
+
const old = ring[head];
|
|
87
90
|
ring[head] = dtSec;
|
|
91
|
+
sum += ring[head] - old;
|
|
88
92
|
head = (head + 1) % ringSize;
|
|
89
93
|
if (filled < ringSize) filled += 1;
|
|
94
|
+
if (head === 0) {
|
|
95
|
+
sum = 0;
|
|
96
|
+
for (let i = 0; i < ringSize; i++) sum += ring[i];
|
|
97
|
+
}
|
|
90
98
|
if (filled === ringSize && !watchdogFired) {
|
|
91
99
|
if (getAvgFps() < fpsThreshold) {
|
|
92
100
|
watchdogFired = true;
|
|
@@ -107,13 +115,11 @@ function createFrameBudget(opts) {
|
|
|
107
115
|
}
|
|
108
116
|
function getAvgFps() {
|
|
109
117
|
if (filled < ringSize) return 0;
|
|
110
|
-
|
|
111
|
-
for (let i = 0; i < ringSize; i++) sum += ring[i];
|
|
112
|
-
const avgDt = sum / ringSize;
|
|
113
|
-
return avgDt > 0 ? 1 / avgDt : 0;
|
|
118
|
+
return sum > 0 ? ringSize / sum : 0;
|
|
114
119
|
}
|
|
115
120
|
function resetFpsRing() {
|
|
116
121
|
ring.fill(0);
|
|
122
|
+
sum = 0;
|
|
117
123
|
head = 0;
|
|
118
124
|
filled = 0;
|
|
119
125
|
watchdogFired = false;
|
|
@@ -130,7 +136,8 @@ function createFrameBudget(opts) {
|
|
|
130
136
|
/**
|
|
131
137
|
* DebugOverlay — collects debug draw commands for the current frame.
|
|
132
138
|
* Commands are generated based on active overlay flags, then consumed
|
|
133
|
-
* by the renderer.
|
|
139
|
+
* by the renderer. Commands are fresh objects by default and pooled with
|
|
140
|
+
* `reuseCommands` (see {@link DebugOverlayOptions}).
|
|
134
141
|
*/
|
|
135
142
|
var DebugOverlay = class {
|
|
136
143
|
/** Active overlay flags. */
|
|
@@ -142,6 +149,16 @@ var DebugOverlay = class {
|
|
|
142
149
|
};
|
|
143
150
|
/** Queued debug draw commands for the current frame. */
|
|
144
151
|
_commands = [];
|
|
152
|
+
_rects = [];
|
|
153
|
+
_circles = [];
|
|
154
|
+
_lines = [];
|
|
155
|
+
_rectCursor = 0;
|
|
156
|
+
_circleCursor = 0;
|
|
157
|
+
_lineCursor = 0;
|
|
158
|
+
_reuseCommands;
|
|
159
|
+
constructor(options = {}) {
|
|
160
|
+
this._reuseCommands = options.reuseCommands === true;
|
|
161
|
+
}
|
|
145
162
|
/** Number of queued commands. */
|
|
146
163
|
get commandCount() {
|
|
147
164
|
return this._commands.length;
|
|
@@ -152,39 +169,109 @@ var DebugOverlay = class {
|
|
|
152
169
|
}
|
|
153
170
|
/** Add a debug rectangle. */
|
|
154
171
|
drawRect(x, y, width, height, color = "#00ff00") {
|
|
155
|
-
this.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
172
|
+
if (!this._reuseCommands) {
|
|
173
|
+
this._commands.push({
|
|
174
|
+
type: "rect",
|
|
175
|
+
x,
|
|
176
|
+
y,
|
|
177
|
+
width,
|
|
178
|
+
height,
|
|
179
|
+
color
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
let cmd = this._rects[this._rectCursor];
|
|
184
|
+
if (cmd) {
|
|
185
|
+
cmd.x = x;
|
|
186
|
+
cmd.y = y;
|
|
187
|
+
cmd.width = width;
|
|
188
|
+
cmd.height = height;
|
|
189
|
+
cmd.color = color;
|
|
190
|
+
} else {
|
|
191
|
+
cmd = {
|
|
192
|
+
type: "rect",
|
|
193
|
+
x,
|
|
194
|
+
y,
|
|
195
|
+
width,
|
|
196
|
+
height,
|
|
197
|
+
color
|
|
198
|
+
};
|
|
199
|
+
this._rects.push(cmd);
|
|
200
|
+
}
|
|
201
|
+
this._rectCursor += 1;
|
|
202
|
+
this._commands.push(cmd);
|
|
163
203
|
}
|
|
164
204
|
/** Add a debug circle. */
|
|
165
205
|
drawCircle(x, y, radius, color = "#00ff00") {
|
|
166
|
-
this.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
206
|
+
if (!this._reuseCommands) {
|
|
207
|
+
this._commands.push({
|
|
208
|
+
type: "circle",
|
|
209
|
+
x,
|
|
210
|
+
y,
|
|
211
|
+
radius,
|
|
212
|
+
color
|
|
213
|
+
});
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
let cmd = this._circles[this._circleCursor];
|
|
217
|
+
if (cmd) {
|
|
218
|
+
cmd.x = x;
|
|
219
|
+
cmd.y = y;
|
|
220
|
+
cmd.radius = radius;
|
|
221
|
+
cmd.color = color;
|
|
222
|
+
} else {
|
|
223
|
+
cmd = {
|
|
224
|
+
type: "circle",
|
|
225
|
+
x,
|
|
226
|
+
y,
|
|
227
|
+
radius,
|
|
228
|
+
color
|
|
229
|
+
};
|
|
230
|
+
this._circles.push(cmd);
|
|
231
|
+
}
|
|
232
|
+
this._circleCursor += 1;
|
|
233
|
+
this._commands.push(cmd);
|
|
173
234
|
}
|
|
174
235
|
/** Add a debug line. */
|
|
175
236
|
drawLine(x1, y1, x2, y2, color = "#ffff00") {
|
|
176
|
-
this.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
237
|
+
if (!this._reuseCommands) {
|
|
238
|
+
this._commands.push({
|
|
239
|
+
type: "line",
|
|
240
|
+
x1,
|
|
241
|
+
y1,
|
|
242
|
+
x2,
|
|
243
|
+
y2,
|
|
244
|
+
color
|
|
245
|
+
});
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
let cmd = this._lines[this._lineCursor];
|
|
249
|
+
if (cmd) {
|
|
250
|
+
cmd.x1 = x1;
|
|
251
|
+
cmd.y1 = y1;
|
|
252
|
+
cmd.x2 = x2;
|
|
253
|
+
cmd.y2 = y2;
|
|
254
|
+
cmd.color = color;
|
|
255
|
+
} else {
|
|
256
|
+
cmd = {
|
|
257
|
+
type: "line",
|
|
258
|
+
x1,
|
|
259
|
+
y1,
|
|
260
|
+
x2,
|
|
261
|
+
y2,
|
|
262
|
+
color
|
|
263
|
+
};
|
|
264
|
+
this._lines.push(cmd);
|
|
265
|
+
}
|
|
266
|
+
this._lineCursor += 1;
|
|
267
|
+
this._commands.push(cmd);
|
|
184
268
|
}
|
|
185
269
|
/** Clear all commands. Call at the start of each frame. */
|
|
186
270
|
clear() {
|
|
187
271
|
this._commands.length = 0;
|
|
272
|
+
this._rectCursor = 0;
|
|
273
|
+
this._circleCursor = 0;
|
|
274
|
+
this._lineCursor = 0;
|
|
188
275
|
}
|
|
189
276
|
/** Toggle a specific overlay flag. */
|
|
190
277
|
toggleFlag(flag) {
|
|
@@ -211,10 +298,6 @@ function emitPerfReport(report) {
|
|
|
211
298
|
console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);
|
|
212
299
|
}
|
|
213
300
|
// src/perf-recorder.ts
|
|
214
|
-
function nowMs() {
|
|
215
|
-
if (typeof performance !== "undefined" && typeof performance.now === "function") return performance.now();
|
|
216
|
-
return Date.now();
|
|
217
|
-
}
|
|
218
301
|
function percentile(sorted, n, p) {
|
|
219
302
|
if (n === 0) return 0;
|
|
220
303
|
if (n === 1) return sorted[0];
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["nowMs"],"sources":["../src/boot-trace.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-output.ts","../src/perf-recorder.ts","../src/stats.ts","../src/time-control.ts"],"sourcesContent":["export type BootEntry = {\n readonly name: string;\n readonly t: number;\n};\n\nexport type BootTraceHandle = {\n mark(name: string): void;\n dump(): string | null;\n reset(): void;\n getEntries(): readonly BootEntry[];\n};\n\nfunction nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n\nexport function createBootTrace(label = 'boot-trace'): BootTraceHandle {\n const entries: BootEntry[] = [];\n let startT: number | null = null;\n\n function mark(name: string): void {\n if (startT === null) {\n startT = nowMs();\n entries.push({ name, t: 0 });\n } else {\n entries.push({ name, t: nowMs() - startT });\n }\n }\n\n function dump(): string | null {\n if (entries.length === 0) return null;\n const parts: string[] = [];\n for (let i = 1; i < entries.length; i++) {\n const prev = entries[i - 1];\n const cur = entries[i];\n if (!prev || !cur) continue;\n parts.push(`${prev.name}->${cur.name}=${(cur.t - prev.t).toFixed(1)}ms`);\n }\n const last = entries[entries.length - 1];\n const totalMs = last ? last.t.toFixed(1) : '0.0';\n const stepStr = parts.length > 0 ? ` ${parts.join(' ')}` : '';\n return `[${label}] total=${totalMs}ms${stepStr}`;\n }\n\n function reset(): void {\n entries.length = 0;\n startT = null;\n }\n\n function getEntries(): readonly BootEntry[] {\n return entries.slice();\n }\n\n return { mark, dump, reset, getEntries };\n}\n","import type { DebugStats } from './stats.js';\n\n/**\n * Formatter for a compact FPS / stats overlay.\n *\n * Consumes a `DebugStats` snapshot and exposes formatted strings ready\n * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,\n * whatever). The widget itself has no rendering dependency so it stays\n * drop-in across renderers.\n *\n * Wrap in `__DEV__` at the call site for tree-shaking.\n */\nexport interface FpsWidget {\n /** Rebuild `line` / `lines` from the latest stats. */\n update(stats: DebugStats): void;\n /** Compact one-line summary, e.g. \"60 FPS | 16.7ms | e:42 d:8\". */\n line: string;\n /** Multi-line breakdown — one string per stat row. */\n readonly lines: string[];\n}\n\n/** Options for the FPS widget formatter. */\nexport interface FpsWidgetOptions {\n /** Show entity count in the one-line summary. Default true. */\n showEntities?: boolean;\n /** Show draw call count in the one-line summary. Default true. */\n showDrawCalls?: boolean;\n /** Number of decimals for milliseconds. Default 1. */\n msPrecision?: number;\n}\n\n/** Create a new FPS widget formatter. */\nexport function createFpsWidget(options: FpsWidgetOptions = {}): FpsWidget {\n const showEntities = options.showEntities ?? true;\n const showDrawCalls = options.showDrawCalls ?? true;\n const msPrecision = options.msPrecision ?? 1;\n const lines: string[] = [];\n\n const widget: FpsWidget = {\n line: '',\n lines,\n update(stats: DebugStats): void {\n const fps = Math.round(stats.fps);\n const ms = (stats.rawDt * 1000).toFixed(msPrecision);\n\n let summary = `${fps} FPS | ${ms}ms`;\n if (showEntities) summary += ` | e:${stats.entityCount}`;\n if (showDrawCalls) summary += ` d:${stats.drawCalls}`;\n widget.line = summary;\n\n lines.length = 0;\n lines.push(`FPS: ${fps}`);\n lines.push(`dt: ${ms}ms`);\n lines.push(`frame: ${stats.frame}`);\n lines.push(`entities: ${stats.entityCount}`);\n lines.push(`draws: ${stats.drawCalls}`);\n },\n };\n return widget;\n}\n","export type FrameBudgetLimits = Record<string, number>;\nexport type FrameBudgetSnapshot = Record<string, number>;\n\nexport type FrameBudgetHandle = {\n recordFrame(dtSec: number): void;\n recordCounters(snapshot: FrameBudgetSnapshot): void;\n getSnapshot(): Readonly<FrameBudgetSnapshot>;\n getAvgFps(): number;\n resetFpsRing(): void;\n};\n\ntype FrameBudgetOpts = {\n limits: FrameBudgetLimits;\n fpsThreshold?: number;\n ringSize?: number;\n onWatchdog?: () => void;\n debug?: boolean;\n};\n\nexport function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle {\n const { limits, onWatchdog, debug = false } = opts;\n const fpsThreshold = opts.fpsThreshold ?? 55;\n const ringSize = opts.ringSize ?? 180;\n\n const ring = new Float32Array(ringSize);\n const limitKeys = Object.keys(limits);\n const _lastSnapshot: Record<string, number> = {};\n for (const k of limitKeys) {\n _lastSnapshot[k] = 0;\n }\n\n let head = 0;\n let filled = 0;\n let watchdogFired = false;\n\n function recordFrame(dtSec: number): void {\n ring[head] = dtSec;\n head = (head + 1) % ringSize;\n if (filled < ringSize) filled += 1;\n\n if (filled === ringSize && !watchdogFired) {\n if (getAvgFps() < fpsThreshold) {\n watchdogFired = true;\n onWatchdog?.();\n }\n }\n }\n\n function recordCounters(snapshot: FrameBudgetSnapshot): void {\n for (let i = 0; i < limitKeys.length; i++) {\n const k = limitKeys[i] as string;\n const v = snapshot[k] ?? 0;\n _lastSnapshot[k] = v;\n if (debug && v > (limits[k] as number)) {\n console.warn(`[frame-budget] ${k} over limit: ${v} > ${limits[k]}`);\n }\n }\n }\n\n function getSnapshot(): Readonly<FrameBudgetSnapshot> {\n return _lastSnapshot;\n }\n\n function getAvgFps(): number {\n if (filled < ringSize) return 0;\n let sum = 0;\n for (let i = 0; i < ringSize; i++) {\n sum += ring[i] as number;\n }\n const avgDt = sum / ringSize;\n return avgDt > 0 ? 1 / avgDt : 0;\n }\n\n function resetFpsRing(): void {\n ring.fill(0);\n head = 0;\n filled = 0;\n watchdogFired = false;\n }\n\n return { recordFrame, recordCounters, getSnapshot, getAvgFps, resetFpsRing };\n}\n","/**\n * DebugOverlay — configuration for debug visualization overlays.\n * The actual rendering is done by the host (e.g., @flare-engine/react) reading\n * these flags and drawing appropriate debug shapes.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport interface OverlayFlags {\n /** Show collision bounding boxes. */\n showCollisionBounds: boolean;\n /** Show entity origin points. */\n showEntityOrigins: boolean;\n /** Show velocity vectors. */\n showVelocityVectors: boolean;\n /** Show FPS/stats overlay. */\n showStats: boolean;\n}\n\n/** Debug draw command types. */\nexport interface DebugRect {\n readonly type: 'rect';\n x: number;\n y: number;\n width: number;\n height: number;\n color: string;\n}\n\nexport interface DebugCircle {\n readonly type: 'circle';\n x: number;\n y: number;\n radius: number;\n color: string;\n}\n\nexport interface DebugLine {\n readonly type: 'line';\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n color: string;\n}\n\n/** Union of debug draw primitive commands. */\nexport type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;\n\n/**\n * DebugOverlay — collects debug draw commands for the current frame.\n * Commands are generated based on active overlay flags, then consumed\n * by the renderer.\n */\nexport class DebugOverlay {\n /** Active overlay flags. */\n readonly flags: OverlayFlags = {\n showCollisionBounds: false,\n showEntityOrigins: false,\n showVelocityVectors: false,\n showStats: false,\n };\n\n /** Queued debug draw commands for the current frame. */\n private _commands: DebugDrawCommand[] = [];\n\n /** Number of queued commands. */\n get commandCount(): number {\n return this._commands.length;\n }\n\n /** Get all commands (read-only view). */\n get commands(): readonly DebugDrawCommand[] {\n return this._commands;\n }\n\n /** Add a debug rectangle. */\n drawRect(x: number, y: number, width: number, height: number, color = '#00ff00'): void {\n this._commands.push({ type: 'rect', x, y, width, height, color });\n }\n\n /** Add a debug circle. */\n drawCircle(x: number, y: number, radius: number, color = '#00ff00'): void {\n this._commands.push({ type: 'circle', x, y, radius, color });\n }\n\n /** Add a debug line. */\n drawLine(x1: number, y1: number, x2: number, y2: number, color = '#ffff00'): void {\n this._commands.push({ type: 'line', x1, y1, x2, y2, color });\n }\n\n /** Clear all commands. Call at the start of each frame. */\n clear(): void {\n this._commands.length = 0;\n }\n\n /** Toggle a specific overlay flag. */\n toggleFlag(flag: keyof OverlayFlags): void {\n this.flags[flag] = !this.flags[flag];\n }\n\n /** Enable all overlays. */\n enableAll(): void {\n this.flags.showCollisionBounds = true;\n this.flags.showEntityOrigins = true;\n this.flags.showVelocityVectors = true;\n this.flags.showStats = true;\n }\n\n /** Disable all overlays. */\n disableAll(): void {\n this.flags.showCollisionBounds = false;\n this.flags.showEntityOrigins = false;\n this.flags.showVelocityVectors = false;\n this.flags.showStats = false;\n }\n}\n","import type { PerfReport } from './perf-recorder.js';\n\nexport const PERF_BENCH_TAG = '[perf-bench]';\n\nexport function emitPerfReport(report: PerfReport): void {\n console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);\n}\n","export type PerfSectionStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly mean: number;\n readonly p50: number;\n readonly p95: number;\n readonly p99: number;\n};\n\nexport type PerfCounterStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly sum: number;\n readonly mean: number;\n};\n\nexport type PerfReport = {\n readonly label: string;\n readonly durationMs: number;\n readonly frameCount: number;\n readonly sections: readonly PerfSectionStats[];\n readonly counters: readonly PerfCounterStats[];\n};\n\nexport type PerfRecorderHandle = {\n beginSection(name: string): void;\n endSection(name: string): void;\n recordFrame(counters?: Record<string, number>): void;\n flush(): PerfReport;\n reset(): void;\n};\n\ntype SectionState = {\n name: string;\n ring: Float32Array;\n ringSize: number;\n head: number;\n filled: number;\n startT: number;\n};\n\ntype CounterState = {\n name: string;\n count: number;\n min: number;\n max: number;\n sum: number;\n};\n\ntype PerfRecorderOpts = {\n label?: string;\n ringSize?: number;\n};\n\nfunction nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n\nfunction percentile(sorted: Float32Array, n: number, p: number): number {\n if (n === 0) return 0;\n if (n === 1) return sorted[0] as number;\n const idx = (p / 100) * (n - 1);\n const lo = Math.floor(idx);\n const hi = Math.ceil(idx);\n const a = sorted[lo] as number;\n const b = sorted[hi] as number;\n return a + (b - a) * (idx - lo);\n}\n\nexport function createPerfRecorder(opts: PerfRecorderOpts = {}): PerfRecorderHandle {\n const label = opts.label ?? 'perf';\n const ringSize = opts.ringSize ?? 1800;\n\n const sections = new Map<string, SectionState>();\n const counters = new Map<string, CounterState>();\n let frameCount = 0;\n let startT: number | null = null;\n\n // Reusable sort buffer — allocated once on first flush, grown as needed\n let sortBuf = new Float32Array(ringSize);\n\n function getOrCreateSection(name: string): SectionState {\n let s = sections.get(name);\n if (s === undefined) {\n s = {\n name,\n ring: new Float32Array(ringSize),\n ringSize,\n head: 0,\n filled: 0,\n startT: 0,\n };\n sections.set(name, s);\n }\n return s;\n }\n\n function getOrCreateCounter(name: string): CounterState {\n let c = counters.get(name);\n if (c === undefined) {\n c = { name, count: 0, min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY, sum: 0 };\n counters.set(name, c);\n }\n return c;\n }\n\n function beginSection(name: string): void {\n const s = getOrCreateSection(name);\n s.startT = nowMs();\n }\n\n function endSection(name: string): void {\n const s = sections.get(name);\n if (s === undefined) return;\n const elapsed = nowMs() - s.startT;\n s.ring[s.head] = elapsed;\n s.head = (s.head + 1) % s.ringSize;\n if (s.filled < s.ringSize) s.filled += 1;\n }\n\n function recordFrame(frameCounters?: Record<string, number>): void {\n if (startT === null) startT = nowMs();\n frameCount += 1;\n if (frameCounters === undefined) return;\n for (const name in frameCounters) {\n const v = frameCounters[name] as number;\n const c = getOrCreateCounter(name);\n c.count += 1;\n if (v < c.min) c.min = v;\n if (v > c.max) c.max = v;\n c.sum += v;\n }\n }\n\n function flush(): PerfReport {\n const now = nowMs();\n const durationMs = startT !== null ? now - startT : 0;\n\n const sectionStats: PerfSectionStats[] = [];\n for (const s of sections.values()) {\n const n = s.filled;\n if (n === 0) {\n sectionStats.push({\n name: s.name,\n count: 0,\n min: 0,\n max: 0,\n mean: 0,\n p50: 0,\n p95: 0,\n p99: 0,\n });\n continue;\n }\n // Grow sort buffer if needed\n if (sortBuf.length < n) sortBuf = new Float32Array(n);\n let sum = 0;\n let minV = Number.POSITIVE_INFINITY;\n let maxV = Number.NEGATIVE_INFINITY;\n // Copy ring in chronological order\n const start = s.filled < s.ringSize ? 0 : s.head;\n for (let i = 0; i < n; i++) {\n const v = s.ring[(start + i) % s.ringSize] as number;\n sortBuf[i] = v;\n sum += v;\n if (v < minV) minV = v;\n if (v > maxV) maxV = v;\n }\n // Sort only the filled slice\n const slice = sortBuf.subarray(0, n);\n slice.sort();\n sectionStats.push({\n name: s.name,\n count: n,\n min: minV,\n max: maxV,\n mean: sum / n,\n p50: percentile(slice, n, 50),\n p95: percentile(slice, n, 95),\n p99: percentile(slice, n, 99),\n });\n }\n\n const counterStats: PerfCounterStats[] = [];\n for (const c of counters.values()) {\n counterStats.push({\n name: c.name,\n count: c.count,\n min: c.count > 0 ? c.min : 0,\n max: c.count > 0 ? c.max : 0,\n sum: c.sum,\n mean: c.count > 0 ? c.sum / c.count : 0,\n });\n }\n\n return { label, durationMs, frameCount, sections: sectionStats, counters: counterStats };\n }\n\n function reset(): void {\n for (const s of sections.values()) {\n s.ring.fill(0);\n s.head = 0;\n s.filled = 0;\n s.startT = 0;\n }\n for (const c of counters.values()) {\n c.count = 0;\n c.min = Number.POSITIVE_INFINITY;\n c.max = Number.NEGATIVE_INFINITY;\n c.sum = 0;\n }\n frameCount = 0;\n startT = null;\n }\n\n return { beginSection, endSection, recordFrame, flush, reset };\n}\n","/** Snapshot of engine stats for a single frame. */\nexport interface DebugStats {\n /** Frames per second (smoothed). */\n fps: number;\n /** Raw delta time in seconds. */\n rawDt: number;\n /** Scaled delta time in seconds. */\n dt: number;\n /** Current frame number. */\n frame: number;\n /** Total active entities (set by user). */\n entityCount: number;\n /** Draw calls this frame (set by user). */\n drawCalls: number;\n}\n\n/**\n * StatsTracker — collects and exposes per-frame debug statistics.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class StatsTracker {\n /** Current frame stats (read-only snapshot). */\n readonly stats: DebugStats = {\n fps: 0,\n rawDt: 0,\n dt: 0,\n frame: 0,\n entityCount: 0,\n drawCalls: 0,\n };\n\n /** Update timing stats from the clock. */\n updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void {\n this.stats.fps = fps;\n this.stats.rawDt = rawDt;\n this.stats.dt = dt;\n this.stats.frame = frame;\n }\n\n /** Set entity count for this frame. */\n setEntityCount(count: number): void {\n this.stats.entityCount = count;\n }\n\n /** Set draw call count for this frame. */\n setDrawCalls(count: number): void {\n this.stats.drawCalls = count;\n }\n\n /** Reset all counters to zero. */\n reset(): void {\n this.stats.fps = 0;\n this.stats.rawDt = 0;\n this.stats.dt = 0;\n this.stats.frame = 0;\n this.stats.entityCount = 0;\n this.stats.drawCalls = 0;\n }\n}\n","import type { Clock } from '@flare-engine/core';\n\n/**\n * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class TimeControl {\n private _clock: Clock;\n private _savedTimescale = 1;\n private _paused = false;\n private _stepRequested = false;\n\n constructor(clock: Clock) {\n this._clock = clock;\n }\n\n /** Whether the game is currently paused. */\n get paused(): boolean {\n return this._paused;\n }\n\n /** Current time scale. */\n get timescale(): number {\n return this._clock.timescale;\n }\n\n /** Pause the game (sets timescale to 0). */\n pause(): void {\n if (this._paused) return;\n this._savedTimescale = this._clock.timescale;\n this._clock.timescale = 0;\n this._paused = true;\n }\n\n /** Resume the game (restores previous timescale). */\n resume(): void {\n if (!this._paused) return;\n this._clock.timescale = this._savedTimescale;\n this._paused = false;\n this._stepRequested = false;\n }\n\n /** Toggle pause/resume. */\n toggle(): void {\n if (this._paused) {\n this.resume();\n } else {\n this.pause();\n }\n }\n\n /**\n * Request a single-frame step while paused.\n * Call this, then on the next update check `consumeStep()`.\n */\n step(): void {\n if (!this._paused) return;\n this._stepRequested = true;\n }\n\n /**\n * Consume a pending step request. Returns true if a step was requested.\n * When true, the caller should run one frame with the saved timescale,\n * then re-pause.\n */\n consumeStep(): boolean {\n if (!this._stepRequested) return false;\n this._stepRequested = false;\n return true;\n }\n\n /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */\n setTimescale(scale: number): void {\n if (this._paused) {\n this._savedTimescale = scale;\n } else {\n this._clock.timescale = scale;\n }\n }\n}\n"],"mappings":";AAYA,SAASA,UAAgB;CACvB,IAAI,OAAO,gBAAgB,eAAe,OAAO,YAAY,QAAQ,YACnE,OAAO,YAAY,IAAI;CAEzB,OAAO,KAAK,IAAI;AAClB;AAEA,SAAgB,gBAAgB,QAAQ,cAA+B;CACrE,MAAM,UAAuB,CAAC;CAC9B,IAAI,SAAwB;CAE5B,SAAS,KAAK,MAAoB;EAChC,IAAI,WAAW,MAAM;GACnB,SAASA,Q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|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/now.ts","../src/boot-trace.ts","../src/fps-widget.ts","../src/frame-budget.ts","../src/overlay.ts","../src/perf-output.ts","../src/perf-recorder.ts","../src/stats.ts","../src/time-control.ts"],"sourcesContent":["export function nowMs(): number {\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now();\n }\n return Date.now();\n}\n","import { nowMs } from './now.js';\n\nexport type BootEntry = {\n readonly name: string;\n readonly t: number;\n};\n\nexport type BootTraceHandle = {\n mark(name: string): void;\n dump(): string | null;\n reset(): void;\n getEntries(): readonly BootEntry[];\n};\n\nexport function createBootTrace(label = 'boot-trace'): BootTraceHandle {\n const entries: BootEntry[] = [];\n let startT: number | null = null;\n\n function mark(name: string): void {\n if (startT === null) {\n startT = nowMs();\n entries.push({ name, t: 0 });\n } else {\n entries.push({ name, t: nowMs() - startT });\n }\n }\n\n function dump(): string | null {\n if (entries.length === 0) return null;\n const parts: string[] = [];\n for (let i = 1; i < entries.length; i++) {\n const prev = entries[i - 1];\n const cur = entries[i];\n if (!prev || !cur) continue;\n parts.push(`${prev.name}->${cur.name}=${(cur.t - prev.t).toFixed(1)}ms`);\n }\n const last = entries[entries.length - 1];\n const totalMs = last ? last.t.toFixed(1) : '0.0';\n const stepStr = parts.length > 0 ? ` ${parts.join(' ')}` : '';\n return `[${label}] total=${totalMs}ms${stepStr}`;\n }\n\n function reset(): void {\n entries.length = 0;\n startT = null;\n }\n\n function getEntries(): readonly BootEntry[] {\n return entries.slice();\n }\n\n return { mark, dump, reset, getEntries };\n}\n","import type { DebugStats } from './stats.js';\n\n/**\n * Formatter for a compact FPS / stats overlay.\n *\n * Consumes a `DebugStats` snapshot and exposes formatted strings ready\n * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,\n * whatever). The widget itself has no rendering dependency so it stays\n * drop-in across renderers.\n *\n * Wrap in `__DEV__` at the call site for tree-shaking.\n */\nexport interface FpsWidget {\n /** Rebuild `line` / `lines` from the latest stats. */\n update(stats: DebugStats): void;\n /** Compact one-line summary, e.g. \"60 FPS | 16.7ms | e:42 d:8\". */\n line: string;\n /** Multi-line breakdown — one string per stat row. */\n readonly lines: string[];\n}\n\n/** Options for the FPS widget formatter. */\nexport interface FpsWidgetOptions {\n /** Show entity count in the one-line summary. Default true. */\n showEntities?: boolean;\n /** Show draw call count in the one-line summary. Default true. */\n showDrawCalls?: boolean;\n /** Number of decimals for milliseconds. Default 1. */\n msPrecision?: number;\n}\n\n/** Create a new FPS widget formatter. */\nexport function createFpsWidget(options: FpsWidgetOptions = {}): FpsWidget {\n const showEntities = options.showEntities ?? true;\n const showDrawCalls = options.showDrawCalls ?? true;\n const msPrecision = options.msPrecision ?? 1;\n const lines: string[] = [];\n\n const widget: FpsWidget = {\n line: '',\n lines,\n update(stats: DebugStats): void {\n const fps = Math.round(stats.fps);\n const ms = (stats.rawDt * 1000).toFixed(msPrecision);\n\n let summary = `${fps} FPS | ${ms}ms`;\n if (showEntities) summary += ` | e:${stats.entityCount}`;\n if (showDrawCalls) summary += ` d:${stats.drawCalls}`;\n widget.line = summary;\n\n lines.length = 0;\n lines.push(`FPS: ${fps}`);\n lines.push(`dt: ${ms}ms`);\n lines.push(`frame: ${stats.frame}`);\n lines.push(`entities: ${stats.entityCount}`);\n lines.push(`draws: ${stats.drawCalls}`);\n },\n };\n return widget;\n}\n","export type FrameBudgetLimits = Record<string, number>;\nexport type FrameBudgetSnapshot = Record<string, number>;\n\nexport type FrameBudgetHandle = {\n recordFrame(dtSec: number): void;\n recordCounters(snapshot: FrameBudgetSnapshot): void;\n getSnapshot(): Readonly<FrameBudgetSnapshot>;\n getAvgFps(): number;\n resetFpsRing(): void;\n};\n\nexport type FrameBudgetOpts = {\n limits: FrameBudgetLimits;\n fpsThreshold?: number;\n ringSize?: number;\n onWatchdog?: () => void;\n debug?: boolean;\n};\n\nexport function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle {\n const { limits, onWatchdog, debug = false } = opts;\n const fpsThreshold = opts.fpsThreshold ?? 55;\n const ringSize = opts.ringSize ?? 180;\n\n const ring = new Float32Array(ringSize);\n const limitKeys = Object.keys(limits);\n const _lastSnapshot: Record<string, number> = {};\n for (const k of limitKeys) {\n _lastSnapshot[k] = 0;\n }\n\n let head = 0;\n let filled = 0;\n let sum = 0;\n let watchdogFired = false;\n\n function recordFrame(dtSec: number): void {\n const old = ring[head] as number;\n ring[head] = dtSec;\n // Add the f32 value the ring actually stored, so the later subtraction removes that same value.\n sum += (ring[head] as number) - old;\n head = (head + 1) % ringSize;\n if (filled < ringSize) filled += 1;\n\n // Re-sum once per revolution: bounds rounding drift and flushes a non-finite or huge dt\n // that would otherwise stay in the running sum after leaving the ring.\n if (head === 0) {\n sum = 0;\n for (let i = 0; i < ringSize; i++) {\n sum += ring[i] as number;\n }\n }\n\n if (filled === ringSize && !watchdogFired) {\n if (getAvgFps() < fpsThreshold) {\n watchdogFired = true;\n onWatchdog?.();\n }\n }\n }\n\n function recordCounters(snapshot: FrameBudgetSnapshot): void {\n for (let i = 0; i < limitKeys.length; i++) {\n const k = limitKeys[i] as string;\n const v = snapshot[k] ?? 0;\n _lastSnapshot[k] = v;\n if (debug && v > (limits[k] as number)) {\n console.warn(`[frame-budget] ${k} over limit: ${v} > ${limits[k]}`);\n }\n }\n }\n\n function getSnapshot(): Readonly<FrameBudgetSnapshot> {\n return _lastSnapshot;\n }\n\n function getAvgFps(): number {\n if (filled < ringSize) return 0;\n return sum > 0 ? ringSize / sum : 0;\n }\n\n function resetFpsRing(): void {\n ring.fill(0);\n sum = 0;\n head = 0;\n filled = 0;\n watchdogFired = false;\n }\n\n return { recordFrame, recordCounters, getSnapshot, getAvgFps, resetFpsRing };\n}\n","/**\n * DebugOverlay — configuration for debug visualization overlays.\n * The actual rendering is done by the host (e.g., @flare-engine/react) reading\n * these flags and drawing appropriate debug shapes.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport interface OverlayFlags {\n /** Show collision bounding boxes. */\n showCollisionBounds: boolean;\n /** Show entity origin points. */\n showEntityOrigins: boolean;\n /** Show velocity vectors. */\n showVelocityVectors: boolean;\n /** Show FPS/stats overlay. */\n showStats: boolean;\n}\n\n/** Debug draw command types. */\nexport interface DebugRect {\n readonly type: 'rect';\n x: number;\n y: number;\n width: number;\n height: number;\n color: string;\n}\n\nexport interface DebugCircle {\n readonly type: 'circle';\n x: number;\n y: number;\n radius: number;\n color: string;\n}\n\nexport interface DebugLine {\n readonly type: 'line';\n x1: number;\n y1: number;\n x2: number;\n y2: number;\n color: string;\n}\n\n/** Union of debug draw primitive commands. */\nexport type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;\n\n/** Options for {@link DebugOverlay}. */\nexport interface DebugOverlayOptions {\n /**\n * When true, `drawRect` / `drawCircle` / `drawLine` reuse pooled command\n * objects, so no command object is created per call once the pools are warm.\n * A reused object changes after `clear()`, so copy a command to keep it past\n * the frame. Default `false`: every call creates a fresh object.\n */\n reuseCommands?: boolean;\n}\n\n/**\n * DebugOverlay — collects debug draw commands for the current frame.\n * Commands are generated based on active overlay flags, then consumed\n * by the renderer. Commands are fresh objects by default and pooled with\n * `reuseCommands` (see {@link DebugOverlayOptions}).\n */\nexport class DebugOverlay {\n /** Active overlay flags. */\n readonly flags: OverlayFlags = {\n showCollisionBounds: false,\n showEntityOrigins: false,\n showVelocityVectors: false,\n showStats: false,\n };\n\n /** Queued debug draw commands for the current frame. */\n private _commands: DebugDrawCommand[] = [];\n\n private _rects: DebugRect[] = [];\n private _circles: DebugCircle[] = [];\n private _lines: DebugLine[] = [];\n private _rectCursor = 0;\n private _circleCursor = 0;\n private _lineCursor = 0;\n private readonly _reuseCommands: boolean;\n\n constructor(options: DebugOverlayOptions = {}) {\n this._reuseCommands = options.reuseCommands === true;\n }\n\n /** Number of queued commands. */\n get commandCount(): number {\n return this._commands.length;\n }\n\n /** Get all commands (read-only view). */\n get commands(): readonly DebugDrawCommand[] {\n return this._commands;\n }\n\n /** Add a debug rectangle. */\n drawRect(x: number, y: number, width: number, height: number, color = '#00ff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'rect', x, y, width, height, color });\n return;\n }\n let cmd = this._rects[this._rectCursor];\n if (cmd) {\n cmd.x = x;\n cmd.y = y;\n cmd.width = width;\n cmd.height = height;\n cmd.color = color;\n } else {\n cmd = { type: 'rect', x, y, width, height, color };\n this._rects.push(cmd);\n }\n this._rectCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Add a debug circle. */\n drawCircle(x: number, y: number, radius: number, color = '#00ff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'circle', x, y, radius, color });\n return;\n }\n let cmd = this._circles[this._circleCursor];\n if (cmd) {\n cmd.x = x;\n cmd.y = y;\n cmd.radius = radius;\n cmd.color = color;\n } else {\n cmd = { type: 'circle', x, y, radius, color };\n this._circles.push(cmd);\n }\n this._circleCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Add a debug line. */\n drawLine(x1: number, y1: number, x2: number, y2: number, color = '#ffff00'): void {\n if (!this._reuseCommands) {\n this._commands.push({ type: 'line', x1, y1, x2, y2, color });\n return;\n }\n let cmd = this._lines[this._lineCursor];\n if (cmd) {\n cmd.x1 = x1;\n cmd.y1 = y1;\n cmd.x2 = x2;\n cmd.y2 = y2;\n cmd.color = color;\n } else {\n cmd = { type: 'line', x1, y1, x2, y2, color };\n this._lines.push(cmd);\n }\n this._lineCursor += 1;\n this._commands.push(cmd);\n }\n\n /** Clear all commands. Call at the start of each frame. */\n clear(): void {\n this._commands.length = 0;\n this._rectCursor = 0;\n this._circleCursor = 0;\n this._lineCursor = 0;\n }\n\n /** Toggle a specific overlay flag. */\n toggleFlag(flag: keyof OverlayFlags): void {\n this.flags[flag] = !this.flags[flag];\n }\n\n /** Enable all overlays. */\n enableAll(): void {\n this.flags.showCollisionBounds = true;\n this.flags.showEntityOrigins = true;\n this.flags.showVelocityVectors = true;\n this.flags.showStats = true;\n }\n\n /** Disable all overlays. */\n disableAll(): void {\n this.flags.showCollisionBounds = false;\n this.flags.showEntityOrigins = false;\n this.flags.showVelocityVectors = false;\n this.flags.showStats = false;\n }\n}\n","import type { PerfReport } from './perf-recorder.js';\n\nexport const PERF_BENCH_TAG = '[perf-bench]';\n\nexport function emitPerfReport(report: PerfReport): void {\n console.log(`${PERF_BENCH_TAG} ${JSON.stringify(report)}`);\n}\n","import { nowMs } from './now.js';\n\nexport type PerfSectionStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly mean: number;\n readonly p50: number;\n readonly p95: number;\n readonly p99: number;\n};\n\nexport type PerfCounterStats = {\n readonly name: string;\n readonly count: number;\n readonly min: number;\n readonly max: number;\n readonly sum: number;\n readonly mean: number;\n};\n\nexport type PerfReport = {\n readonly label: string;\n readonly durationMs: number;\n readonly frameCount: number;\n readonly sections: readonly PerfSectionStats[];\n readonly counters: readonly PerfCounterStats[];\n};\n\nexport type PerfRecorderHandle = {\n beginSection(name: string): void;\n endSection(name: string): void;\n recordFrame(counters?: Record<string, number>): void;\n flush(): PerfReport;\n reset(): void;\n};\n\ntype SectionState = {\n name: string;\n ring: Float32Array;\n ringSize: number;\n head: number;\n filled: number;\n startT: number;\n};\n\ntype CounterState = {\n name: string;\n count: number;\n min: number;\n max: number;\n sum: number;\n};\n\nexport type PerfRecorderOpts = {\n label?: string;\n ringSize?: number;\n};\n\nfunction percentile(sorted: Float32Array, n: number, p: number): number {\n if (n === 0) return 0;\n if (n === 1) return sorted[0] as number;\n const idx = (p / 100) * (n - 1);\n const lo = Math.floor(idx);\n const hi = Math.ceil(idx);\n const a = sorted[lo] as number;\n const b = sorted[hi] as number;\n return a + (b - a) * (idx - lo);\n}\n\nexport function createPerfRecorder(opts: PerfRecorderOpts = {}): PerfRecorderHandle {\n const label = opts.label ?? 'perf';\n const ringSize = opts.ringSize ?? 1800;\n\n const sections = new Map<string, SectionState>();\n const counters = new Map<string, CounterState>();\n let frameCount = 0;\n let startT: number | null = null;\n\n // Reusable sort buffer — allocated once on first flush, grown as needed\n let sortBuf = new Float32Array(ringSize);\n\n function getOrCreateSection(name: string): SectionState {\n let s = sections.get(name);\n if (s === undefined) {\n s = {\n name,\n ring: new Float32Array(ringSize),\n ringSize,\n head: 0,\n filled: 0,\n startT: 0,\n };\n sections.set(name, s);\n }\n return s;\n }\n\n function getOrCreateCounter(name: string): CounterState {\n let c = counters.get(name);\n if (c === undefined) {\n c = { name, count: 0, min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY, sum: 0 };\n counters.set(name, c);\n }\n return c;\n }\n\n function beginSection(name: string): void {\n const s = getOrCreateSection(name);\n s.startT = nowMs();\n }\n\n function endSection(name: string): void {\n const s = sections.get(name);\n if (s === undefined) return;\n const elapsed = nowMs() - s.startT;\n s.ring[s.head] = elapsed;\n s.head = (s.head + 1) % s.ringSize;\n if (s.filled < s.ringSize) s.filled += 1;\n }\n\n function recordFrame(frameCounters?: Record<string, number>): void {\n if (startT === null) startT = nowMs();\n frameCount += 1;\n if (frameCounters === undefined) return;\n for (const name in frameCounters) {\n const v = frameCounters[name] as number;\n const c = getOrCreateCounter(name);\n c.count += 1;\n if (v < c.min) c.min = v;\n if (v > c.max) c.max = v;\n c.sum += v;\n }\n }\n\n function flush(): PerfReport {\n const now = nowMs();\n const durationMs = startT !== null ? now - startT : 0;\n\n const sectionStats: PerfSectionStats[] = [];\n for (const s of sections.values()) {\n const n = s.filled;\n if (n === 0) {\n sectionStats.push({\n name: s.name,\n count: 0,\n min: 0,\n max: 0,\n mean: 0,\n p50: 0,\n p95: 0,\n p99: 0,\n });\n continue;\n }\n // Grow sort buffer if needed\n if (sortBuf.length < n) sortBuf = new Float32Array(n);\n let sum = 0;\n let minV = Number.POSITIVE_INFINITY;\n let maxV = Number.NEGATIVE_INFINITY;\n // Copy ring in chronological order\n const start = s.filled < s.ringSize ? 0 : s.head;\n for (let i = 0; i < n; i++) {\n const v = s.ring[(start + i) % s.ringSize] as number;\n sortBuf[i] = v;\n sum += v;\n if (v < minV) minV = v;\n if (v > maxV) maxV = v;\n }\n // Sort only the filled slice\n const slice = sortBuf.subarray(0, n);\n slice.sort();\n sectionStats.push({\n name: s.name,\n count: n,\n min: minV,\n max: maxV,\n mean: sum / n,\n p50: percentile(slice, n, 50),\n p95: percentile(slice, n, 95),\n p99: percentile(slice, n, 99),\n });\n }\n\n const counterStats: PerfCounterStats[] = [];\n for (const c of counters.values()) {\n counterStats.push({\n name: c.name,\n count: c.count,\n min: c.count > 0 ? c.min : 0,\n max: c.count > 0 ? c.max : 0,\n sum: c.sum,\n mean: c.count > 0 ? c.sum / c.count : 0,\n });\n }\n\n return { label, durationMs, frameCount, sections: sectionStats, counters: counterStats };\n }\n\n function reset(): void {\n for (const s of sections.values()) {\n s.ring.fill(0);\n s.head = 0;\n s.filled = 0;\n s.startT = 0;\n }\n for (const c of counters.values()) {\n c.count = 0;\n c.min = Number.POSITIVE_INFINITY;\n c.max = Number.NEGATIVE_INFINITY;\n c.sum = 0;\n }\n frameCount = 0;\n startT = null;\n }\n\n return { beginSection, endSection, recordFrame, flush, reset };\n}\n","/** Snapshot of engine stats for a single frame. */\nexport interface DebugStats {\n /** Frames per second (smoothed). */\n fps: number;\n /** Raw delta time in seconds. */\n rawDt: number;\n /** Scaled delta time in seconds. */\n dt: number;\n /** Current frame number. */\n frame: number;\n /** Total active entities (set by user). */\n entityCount: number;\n /** Draw calls this frame (set by user). */\n drawCalls: number;\n}\n\n/**\n * StatsTracker — collects and exposes per-frame debug statistics.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class StatsTracker {\n /** Current frame stats (read-only snapshot). */\n readonly stats: DebugStats = {\n fps: 0,\n rawDt: 0,\n dt: 0,\n frame: 0,\n entityCount: 0,\n drawCalls: 0,\n };\n\n /** Update timing stats from the clock. */\n updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void {\n this.stats.fps = fps;\n this.stats.rawDt = rawDt;\n this.stats.dt = dt;\n this.stats.frame = frame;\n }\n\n /** Set entity count for this frame. */\n setEntityCount(count: number): void {\n this.stats.entityCount = count;\n }\n\n /** Set draw call count for this frame. */\n setDrawCalls(count: number): void {\n this.stats.drawCalls = count;\n }\n\n /** Reset all counters to zero. */\n reset(): void {\n this.stats.fps = 0;\n this.stats.rawDt = 0;\n this.stats.dt = 0;\n this.stats.frame = 0;\n this.stats.entityCount = 0;\n this.stats.drawCalls = 0;\n }\n}\n","import type { Clock } from '@flare-engine/core';\n\n/**\n * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.\n * Wrap in `__DEV__` checks at the call site for tree-shaking.\n */\nexport class TimeControl {\n private _clock: Clock;\n private _savedTimescale = 1;\n private _paused = false;\n private _stepRequested = false;\n\n constructor(clock: Clock) {\n this._clock = clock;\n }\n\n /** Whether the game is currently paused. */\n get paused(): boolean {\n return this._paused;\n }\n\n /** Current time scale. */\n get timescale(): number {\n return this._clock.timescale;\n }\n\n /** Pause the game (sets timescale to 0). */\n pause(): void {\n if (this._paused) return;\n this._savedTimescale = this._clock.timescale;\n this._clock.timescale = 0;\n this._paused = true;\n }\n\n /** Resume the game (restores previous timescale). */\n resume(): void {\n if (!this._paused) return;\n this._clock.timescale = this._savedTimescale;\n this._paused = false;\n this._stepRequested = false;\n }\n\n /** Toggle pause/resume. */\n toggle(): void {\n if (this._paused) {\n this.resume();\n } else {\n this.pause();\n }\n }\n\n /**\n * Request a single-frame step while paused.\n * Call this, then on the next update check `consumeStep()`.\n */\n step(): void {\n if (!this._paused) return;\n this._stepRequested = true;\n }\n\n /**\n * Consume a pending step request. Returns true if a step was requested.\n * When true, the caller should run one frame with the saved timescale,\n * then re-pause.\n */\n consumeStep(): boolean {\n if (!this._stepRequested) return false;\n this._stepRequested = false;\n return true;\n }\n\n /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */\n setTimescale(scale: number): void {\n if (this._paused) {\n this._savedTimescale = scale;\n } else {\n this._clock.timescale = scale;\n }\n }\n}\n"],"mappings":";AAAA,SAAgB,Q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KAAK,iBAAiB;CACxB;;;;;;CAOA,cAAuB;EACrB,IAAI,CAAC,KAAK,gBAAgB,OAAO;EACjC,KAAK,iBAAiB;EACtB,OAAO;CACT;;CAGA,aAAa,OAAqB;EAChC,IAAI,KAAK,SACP,KAAK,kBAAkB;OAEvB,KAAK,OAAO,YAAY;CAE5B;AACF"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/debug",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
4
4
|
"description": "Dev overlay, stats, entity inspector, time controls",
|
|
5
|
-
"keywords": ["flare-engine", "debug", "overlay", "stats"],
|
|
5
|
+
"keywords": ["flare-engine", "debug", "overlay", "stats", "react-native", "typescript"],
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"typecheck": "tsc --noEmit"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@flare-engine/core": "^0.4.
|
|
40
|
+
"@flare-engine/core": "^0.4.11"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=20"
|