@flare-engine/debug 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Grzegorz Otto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @flare-engine/debug
2
+
3
+ Dev-only stats + overlays. `StatsTracker` records frame timings; `DebugStats` is a snapshot; `createFpsWidget` formats a one-line `"60 FPS | 16.7ms | e:42 d:8"` summary plus a multi-line breakdown — rendering-agnostic so hosts can draw via Skia or RN `<Text>`. `TimeControl` exposes pause / step-frame / slow-motion via `timeScale`.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add @flare-engine/debug
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { StatsTracker, createFpsWidget, TimeControl } from '@flare-engine/debug';
15
+
16
+ const stats = new StatsTracker();
17
+ const fpsWidget = createFpsWidget();
18
+ const time = new TimeControl();
19
+
20
+ // per frame:
21
+ stats.beginFrame();
22
+ // ...sim...
23
+ stats.endFrame(drawCalls, entityCount);
24
+ const line = fpsWidget.formatLine(stats.snapshot());
25
+ ```
26
+
27
+ Tree-shaken in production via `__DEV__` guard at the call site.
28
+
29
+ See [API spec](../../docs/design/api-specs.md#flare-enginedebug--3kb-gzip-tree-shaken-in-prod) and [skill reference](../../.github/skills/flare-debug/SKILL.md).
30
+
31
+ ## License
32
+
33
+ MIT
@@ -0,0 +1,243 @@
1
+ import { Clock } from '@flare-engine/core';
2
+
3
+ /** Snapshot of engine stats for a single frame. */
4
+ interface DebugStats {
5
+ /** Frames per second (smoothed). */
6
+ fps: number;
7
+ /** Raw delta time in seconds. */
8
+ rawDt: number;
9
+ /** Scaled delta time in seconds. */
10
+ dt: number;
11
+ /** Current frame number. */
12
+ frame: number;
13
+ /** Total active entities (set by user). */
14
+ entityCount: number;
15
+ /** Draw calls this frame (set by user). */
16
+ drawCalls: number;
17
+ }
18
+ /**
19
+ * StatsTracker — collects and exposes per-frame debug statistics.
20
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
21
+ */
22
+ declare class StatsTracker {
23
+ /** Current frame stats (read-only snapshot). */
24
+ readonly stats: DebugStats;
25
+ /** Update timing stats from the clock. */
26
+ updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void;
27
+ /** Set entity count for this frame. */
28
+ setEntityCount(count: number): void;
29
+ /** Set draw call count for this frame. */
30
+ setDrawCalls(count: number): void;
31
+ /** Reset all counters to zero. */
32
+ reset(): void;
33
+ }
34
+
35
+ /**
36
+ * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.
37
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
38
+ */
39
+ declare class TimeControl {
40
+ private _clock;
41
+ private _savedTimescale;
42
+ private _paused;
43
+ private _stepRequested;
44
+ constructor(clock: Clock);
45
+ /** Whether the game is currently paused. */
46
+ get paused(): boolean;
47
+ /** Current time scale. */
48
+ get timescale(): number;
49
+ /** Pause the game (sets timescale to 0). */
50
+ pause(): void;
51
+ /** Resume the game (restores previous timescale). */
52
+ resume(): void;
53
+ /** Toggle pause/resume. */
54
+ toggle(): void;
55
+ /**
56
+ * Request a single-frame step while paused.
57
+ * Call this, then on the next update check `consumeStep()`.
58
+ */
59
+ step(): void;
60
+ /**
61
+ * Consume a pending step request. Returns true if a step was requested.
62
+ * When true, the caller should run one frame with the saved timescale,
63
+ * then re-pause.
64
+ */
65
+ consumeStep(): boolean;
66
+ /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */
67
+ setTimescale(scale: number): void;
68
+ }
69
+
70
+ /**
71
+ * DebugOverlay — configuration for debug visualization overlays.
72
+ * The actual rendering is done by the host (e.g., @flare-engine/react) reading
73
+ * these flags and drawing appropriate debug shapes.
74
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
75
+ */
76
+ interface OverlayFlags {
77
+ /** Show collision bounding boxes. */
78
+ showCollisionBounds: boolean;
79
+ /** Show entity origin points. */
80
+ showEntityOrigins: boolean;
81
+ /** Show velocity vectors. */
82
+ showVelocityVectors: boolean;
83
+ /** Show FPS/stats overlay. */
84
+ showStats: boolean;
85
+ }
86
+ /** Debug draw command types. */
87
+ interface DebugRect {
88
+ readonly type: 'rect';
89
+ x: number;
90
+ y: number;
91
+ width: number;
92
+ height: number;
93
+ color: string;
94
+ }
95
+ interface DebugCircle {
96
+ readonly type: 'circle';
97
+ x: number;
98
+ y: number;
99
+ radius: number;
100
+ color: string;
101
+ }
102
+ interface DebugLine {
103
+ readonly type: 'line';
104
+ x1: number;
105
+ y1: number;
106
+ x2: number;
107
+ y2: number;
108
+ color: string;
109
+ }
110
+ /** Union of debug draw primitive commands. */
111
+ type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;
112
+ /**
113
+ * DebugOverlay — collects debug draw commands for the current frame.
114
+ * Commands are generated based on active overlay flags, then consumed
115
+ * by the renderer.
116
+ */
117
+ declare class DebugOverlay {
118
+ /** Active overlay flags. */
119
+ readonly flags: OverlayFlags;
120
+ /** Queued debug draw commands for the current frame. */
121
+ private _commands;
122
+ /** Number of queued commands. */
123
+ get commandCount(): number;
124
+ /** Get all commands (read-only view). */
125
+ get commands(): readonly DebugDrawCommand[];
126
+ /** Add a debug rectangle. */
127
+ drawRect(x: number, y: number, width: number, height: number, color?: string): void;
128
+ /** Add a debug circle. */
129
+ drawCircle(x: number, y: number, radius: number, color?: string): void;
130
+ /** Add a debug line. */
131
+ drawLine(x1: number, y1: number, x2: number, y2: number, color?: string): void;
132
+ /** Clear all commands. Call at the start of each frame. */
133
+ clear(): void;
134
+ /** Toggle a specific overlay flag. */
135
+ toggleFlag(flag: keyof OverlayFlags): void;
136
+ /** Enable all overlays. */
137
+ enableAll(): void;
138
+ /** Disable all overlays. */
139
+ disableAll(): void;
140
+ }
141
+
142
+ /**
143
+ * Formatter for a compact FPS / stats overlay.
144
+ *
145
+ * Consumes a `DebugStats` snapshot and exposes formatted strings ready
146
+ * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,
147
+ * whatever). The widget itself has no rendering dependency so it stays
148
+ * drop-in across renderers.
149
+ *
150
+ * Wrap in `__DEV__` at the call site for tree-shaking.
151
+ */
152
+ interface FpsWidget {
153
+ /** Rebuild `line` / `lines` from the latest stats. */
154
+ update(stats: DebugStats): void;
155
+ /** Compact one-line summary, e.g. "60 FPS | 16.7ms | e:42 d:8". */
156
+ line: string;
157
+ /** Multi-line breakdown — one string per stat row. */
158
+ readonly lines: string[];
159
+ }
160
+ /** Options for the FPS widget formatter. */
161
+ interface FpsWidgetOptions {
162
+ /** Show entity count in the one-line summary. Default true. */
163
+ showEntities?: boolean;
164
+ /** Show draw call count in the one-line summary. Default true. */
165
+ showDrawCalls?: boolean;
166
+ /** Number of decimals for milliseconds. Default 1. */
167
+ msPrecision?: number;
168
+ }
169
+ /** Create a new FPS widget formatter. */
170
+ declare function createFpsWidget(options?: FpsWidgetOptions): FpsWidget;
171
+
172
+ type FrameBudgetLimits = Record<string, number>;
173
+ type FrameBudgetSnapshot = Record<string, number>;
174
+ type FrameBudgetHandle = {
175
+ recordFrame(dtSec: number): void;
176
+ recordCounters(snapshot: FrameBudgetSnapshot): void;
177
+ getSnapshot(): Readonly<FrameBudgetSnapshot>;
178
+ getAvgFps(): number;
179
+ resetFpsRing(): void;
180
+ };
181
+ type FrameBudgetOpts = {
182
+ limits: FrameBudgetLimits;
183
+ fpsThreshold?: number;
184
+ ringSize?: number;
185
+ onWatchdog?: () => void;
186
+ debug?: boolean;
187
+ };
188
+ declare function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle;
189
+
190
+ type BootEntry = {
191
+ readonly name: string;
192
+ readonly t: number;
193
+ };
194
+ type BootTraceHandle = {
195
+ mark(name: string): void;
196
+ dump(): string | null;
197
+ reset(): void;
198
+ getEntries(): readonly BootEntry[];
199
+ };
200
+ declare function createBootTrace(label?: string): BootTraceHandle;
201
+
202
+ type PerfSectionStats = {
203
+ readonly name: string;
204
+ readonly count: number;
205
+ readonly min: number;
206
+ readonly max: number;
207
+ readonly mean: number;
208
+ readonly p50: number;
209
+ readonly p95: number;
210
+ readonly p99: number;
211
+ };
212
+ type PerfCounterStats = {
213
+ readonly name: string;
214
+ readonly count: number;
215
+ readonly min: number;
216
+ readonly max: number;
217
+ readonly sum: number;
218
+ readonly mean: number;
219
+ };
220
+ type PerfReport = {
221
+ readonly label: string;
222
+ readonly durationMs: number;
223
+ readonly frameCount: number;
224
+ readonly sections: readonly PerfSectionStats[];
225
+ readonly counters: readonly PerfCounterStats[];
226
+ };
227
+ type PerfRecorderHandle = {
228
+ beginSection(name: string): void;
229
+ endSection(name: string): void;
230
+ recordFrame(counters?: Record<string, number>): void;
231
+ flush(): PerfReport;
232
+ reset(): void;
233
+ };
234
+ type PerfRecorderOpts = {
235
+ label?: string;
236
+ ringSize?: number;
237
+ };
238
+ declare function createPerfRecorder(opts?: PerfRecorderOpts): PerfRecorderHandle;
239
+
240
+ declare const PERF_BENCH_TAG = "[perf-bench]";
241
+ declare function emitPerfReport(report: PerfReport): void;
242
+
243
+ export { type BootEntry, type BootTraceHandle, type DebugCircle, type DebugDrawCommand, type DebugLine, DebugOverlay, type DebugRect, type DebugStats, type FpsWidget, type FpsWidgetOptions, type FrameBudgetHandle, type FrameBudgetLimits, type FrameBudgetSnapshot, type OverlayFlags, PERF_BENCH_TAG, type PerfCounterStats, type PerfRecorderHandle, type PerfReport, type PerfSectionStats, StatsTracker, TimeControl, createBootTrace, createFpsWidget, createFrameBudget, createPerfRecorder, emitPerfReport };
@@ -0,0 +1,243 @@
1
+ import { Clock } from '@flare-engine/core';
2
+
3
+ /** Snapshot of engine stats for a single frame. */
4
+ interface DebugStats {
5
+ /** Frames per second (smoothed). */
6
+ fps: number;
7
+ /** Raw delta time in seconds. */
8
+ rawDt: number;
9
+ /** Scaled delta time in seconds. */
10
+ dt: number;
11
+ /** Current frame number. */
12
+ frame: number;
13
+ /** Total active entities (set by user). */
14
+ entityCount: number;
15
+ /** Draw calls this frame (set by user). */
16
+ drawCalls: number;
17
+ }
18
+ /**
19
+ * StatsTracker — collects and exposes per-frame debug statistics.
20
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
21
+ */
22
+ declare class StatsTracker {
23
+ /** Current frame stats (read-only snapshot). */
24
+ readonly stats: DebugStats;
25
+ /** Update timing stats from the clock. */
26
+ updateFromClock(fps: number, rawDt: number, dt: number, frame: number): void;
27
+ /** Set entity count for this frame. */
28
+ setEntityCount(count: number): void;
29
+ /** Set draw call count for this frame. */
30
+ setDrawCalls(count: number): void;
31
+ /** Reset all counters to zero. */
32
+ reset(): void;
33
+ }
34
+
35
+ /**
36
+ * TimeControl — pause, step-frame, and slow-motion via Clock.timescale.
37
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
38
+ */
39
+ declare class TimeControl {
40
+ private _clock;
41
+ private _savedTimescale;
42
+ private _paused;
43
+ private _stepRequested;
44
+ constructor(clock: Clock);
45
+ /** Whether the game is currently paused. */
46
+ get paused(): boolean;
47
+ /** Current time scale. */
48
+ get timescale(): number;
49
+ /** Pause the game (sets timescale to 0). */
50
+ pause(): void;
51
+ /** Resume the game (restores previous timescale). */
52
+ resume(): void;
53
+ /** Toggle pause/resume. */
54
+ toggle(): void;
55
+ /**
56
+ * Request a single-frame step while paused.
57
+ * Call this, then on the next update check `consumeStep()`.
58
+ */
59
+ step(): void;
60
+ /**
61
+ * Consume a pending step request. Returns true if a step was requested.
62
+ * When true, the caller should run one frame with the saved timescale,
63
+ * then re-pause.
64
+ */
65
+ consumeStep(): boolean;
66
+ /** Set slow-motion multiplier (e.g., 0.5 for half-speed). */
67
+ setTimescale(scale: number): void;
68
+ }
69
+
70
+ /**
71
+ * DebugOverlay — configuration for debug visualization overlays.
72
+ * The actual rendering is done by the host (e.g., @flare-engine/react) reading
73
+ * these flags and drawing appropriate debug shapes.
74
+ * Wrap in `__DEV__` checks at the call site for tree-shaking.
75
+ */
76
+ interface OverlayFlags {
77
+ /** Show collision bounding boxes. */
78
+ showCollisionBounds: boolean;
79
+ /** Show entity origin points. */
80
+ showEntityOrigins: boolean;
81
+ /** Show velocity vectors. */
82
+ showVelocityVectors: boolean;
83
+ /** Show FPS/stats overlay. */
84
+ showStats: boolean;
85
+ }
86
+ /** Debug draw command types. */
87
+ interface DebugRect {
88
+ readonly type: 'rect';
89
+ x: number;
90
+ y: number;
91
+ width: number;
92
+ height: number;
93
+ color: string;
94
+ }
95
+ interface DebugCircle {
96
+ readonly type: 'circle';
97
+ x: number;
98
+ y: number;
99
+ radius: number;
100
+ color: string;
101
+ }
102
+ interface DebugLine {
103
+ readonly type: 'line';
104
+ x1: number;
105
+ y1: number;
106
+ x2: number;
107
+ y2: number;
108
+ color: string;
109
+ }
110
+ /** Union of debug draw primitive commands. */
111
+ type DebugDrawCommand = DebugRect | DebugCircle | DebugLine;
112
+ /**
113
+ * DebugOverlay — collects debug draw commands for the current frame.
114
+ * Commands are generated based on active overlay flags, then consumed
115
+ * by the renderer.
116
+ */
117
+ declare class DebugOverlay {
118
+ /** Active overlay flags. */
119
+ readonly flags: OverlayFlags;
120
+ /** Queued debug draw commands for the current frame. */
121
+ private _commands;
122
+ /** Number of queued commands. */
123
+ get commandCount(): number;
124
+ /** Get all commands (read-only view). */
125
+ get commands(): readonly DebugDrawCommand[];
126
+ /** Add a debug rectangle. */
127
+ drawRect(x: number, y: number, width: number, height: number, color?: string): void;
128
+ /** Add a debug circle. */
129
+ drawCircle(x: number, y: number, radius: number, color?: string): void;
130
+ /** Add a debug line. */
131
+ drawLine(x1: number, y1: number, x2: number, y2: number, color?: string): void;
132
+ /** Clear all commands. Call at the start of each frame. */
133
+ clear(): void;
134
+ /** Toggle a specific overlay flag. */
135
+ toggleFlag(flag: keyof OverlayFlags): void;
136
+ /** Enable all overlays. */
137
+ enableAll(): void;
138
+ /** Disable all overlays. */
139
+ disableAll(): void;
140
+ }
141
+
142
+ /**
143
+ * Formatter for a compact FPS / stats overlay.
144
+ *
145
+ * Consumes a `DebugStats` snapshot and exposes formatted strings ready
146
+ * for the host to draw (Skia `canvas.drawText`, an RN `<Text>` overlay,
147
+ * whatever). The widget itself has no rendering dependency so it stays
148
+ * drop-in across renderers.
149
+ *
150
+ * Wrap in `__DEV__` at the call site for tree-shaking.
151
+ */
152
+ interface FpsWidget {
153
+ /** Rebuild `line` / `lines` from the latest stats. */
154
+ update(stats: DebugStats): void;
155
+ /** Compact one-line summary, e.g. "60 FPS | 16.7ms | e:42 d:8". */
156
+ line: string;
157
+ /** Multi-line breakdown — one string per stat row. */
158
+ readonly lines: string[];
159
+ }
160
+ /** Options for the FPS widget formatter. */
161
+ interface FpsWidgetOptions {
162
+ /** Show entity count in the one-line summary. Default true. */
163
+ showEntities?: boolean;
164
+ /** Show draw call count in the one-line summary. Default true. */
165
+ showDrawCalls?: boolean;
166
+ /** Number of decimals for milliseconds. Default 1. */
167
+ msPrecision?: number;
168
+ }
169
+ /** Create a new FPS widget formatter. */
170
+ declare function createFpsWidget(options?: FpsWidgetOptions): FpsWidget;
171
+
172
+ type FrameBudgetLimits = Record<string, number>;
173
+ type FrameBudgetSnapshot = Record<string, number>;
174
+ type FrameBudgetHandle = {
175
+ recordFrame(dtSec: number): void;
176
+ recordCounters(snapshot: FrameBudgetSnapshot): void;
177
+ getSnapshot(): Readonly<FrameBudgetSnapshot>;
178
+ getAvgFps(): number;
179
+ resetFpsRing(): void;
180
+ };
181
+ type FrameBudgetOpts = {
182
+ limits: FrameBudgetLimits;
183
+ fpsThreshold?: number;
184
+ ringSize?: number;
185
+ onWatchdog?: () => void;
186
+ debug?: boolean;
187
+ };
188
+ declare function createFrameBudget(opts: FrameBudgetOpts): FrameBudgetHandle;
189
+
190
+ type BootEntry = {
191
+ readonly name: string;
192
+ readonly t: number;
193
+ };
194
+ type BootTraceHandle = {
195
+ mark(name: string): void;
196
+ dump(): string | null;
197
+ reset(): void;
198
+ getEntries(): readonly BootEntry[];
199
+ };
200
+ declare function createBootTrace(label?: string): BootTraceHandle;
201
+
202
+ type PerfSectionStats = {
203
+ readonly name: string;
204
+ readonly count: number;
205
+ readonly min: number;
206
+ readonly max: number;
207
+ readonly mean: number;
208
+ readonly p50: number;
209
+ readonly p95: number;
210
+ readonly p99: number;
211
+ };
212
+ type PerfCounterStats = {
213
+ readonly name: string;
214
+ readonly count: number;
215
+ readonly min: number;
216
+ readonly max: number;
217
+ readonly sum: number;
218
+ readonly mean: number;
219
+ };
220
+ type PerfReport = {
221
+ readonly label: string;
222
+ readonly durationMs: number;
223
+ readonly frameCount: number;
224
+ readonly sections: readonly PerfSectionStats[];
225
+ readonly counters: readonly PerfCounterStats[];
226
+ };
227
+ type PerfRecorderHandle = {
228
+ beginSection(name: string): void;
229
+ endSection(name: string): void;
230
+ recordFrame(counters?: Record<string, number>): void;
231
+ flush(): PerfReport;
232
+ reset(): void;
233
+ };
234
+ type PerfRecorderOpts = {
235
+ label?: string;
236
+ ringSize?: number;
237
+ };
238
+ declare function createPerfRecorder(opts?: PerfRecorderOpts): PerfRecorderHandle;
239
+
240
+ declare const PERF_BENCH_TAG = "[perf-bench]";
241
+ declare function emitPerfReport(report: PerfReport): void;
242
+
243
+ export { type BootEntry, type BootTraceHandle, type DebugCircle, type DebugDrawCommand, type DebugLine, DebugOverlay, type DebugRect, type DebugStats, type FpsWidget, type FpsWidgetOptions, type FrameBudgetHandle, type FrameBudgetLimits, type FrameBudgetSnapshot, type OverlayFlags, PERF_BENCH_TAG, type PerfCounterStats, type PerfRecorderHandle, type PerfReport, type PerfSectionStats, StatsTracker, TimeControl, createBootTrace, createFpsWidget, createFrameBudget, createPerfRecorder, emitPerfReport };