Repository navigation
Expand file tree
/
Copy pathFMXAnimation.Stats.Hud.pas
More file actions
311 lines (283 loc) · 8.02 KB
/
Copy pathFMXAnimation.Stats.Hud.pas
File metadata and controls
311 lines (283 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/// <summary>
/// FMXAnimation.Stats.Hud
/// Formats and draws the demo stats overlay (Skia). Uses SystemInfo for data.
/// </summary>
///
/// <remarks>
/// Presentation only. Adapts font size, line count, and bar height to surface
/// width (phone-friendly). System diagnostics live in FMXAnimation.SystemInfo.
/// </remarks>
///
/// <copyright>
/// Copyright © 2026 Olaf Monien
/// Licensed under MIT
/// </copyright>
unit FMXAnimation.Stats.Hud;
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Diagnostics,
System.Math,
System.Skia,
FMXAnimation.SystemInfo;
/// <summary>
/// Height of the stats bar for a given surface width (responsive).
/// </summary>
function GetStatsHudHeight(const ASurfaceWidth: Single): Single;
type
/// <summary>
/// Rolling FPS / frame-time / CPU samples and multi-line HUD text.
/// </summary>
TStatsHudModel = record
private
FStopwatch: TStopwatch;
FFrameCount: Integer;
FWindowStart: Double;
FLastRenderTime: Double;
FFps: Integer;
FFrameMs: Double;
FRefreshTimer: Double;
FPlatformLabel: string;
FBackendLabel: string;
FCpuPercentOfOneCore: Double;
FLogicalCpuCount: Integer;
FCpuSampler: TProcessCpuSampler;
FText: string;
FSurfaceWidth: Single;
public
procedure Reset;
procedure CaptureEnvironment;
procedure OnFrameRendered;
function ShouldRefresh(const ADeltaTime: Double): Boolean;
/// <summary>Rebuild text; ASurfaceWidth selects compact vs wide layout.</summary>
procedure RefreshText(
const AParticleCount, AOrbCount: Integer;
const ASimTime: Double;
const ASurfaceWidth: Single;
const AParticleLabel: string = 'Particles';
const AOrbLabel: string = 'Orbs');
property Text: string read FText;
property Fps: Integer read FFps;
property FrameMs: Double read FFrameMs;
property SurfaceWidth: Single read FSurfaceWidth;
end;
/// <summary>
/// Draws the stats bar + text into a Skia canvas (bottom strip of ADest).
/// </summary>
TStatsHudPainter = class sealed
public
class procedure Draw(
const ACanvas: ISkCanvas;
const ADest: TRectF;
const AText: string); static;
end;
implementation
const
cStatsRefreshInterval = 0.25;
cStatsBarColor = $FF0B1220;
cStatsTextColor = $FFE8EEF8;
cStatsTextPadX = 10;
cStatsPadTop = 12;
cStatsLineStep = 14;
cStatsPadBottom = 8;
cNarrowWidth = 480;
cMediumWidth = 720;
function IsNarrow(const AWidth: Single): Boolean;
begin
Result := AWidth < cNarrowWidth;
end;
function IsMedium(const AWidth: Single): Boolean;
begin
Result := (AWidth >= cNarrowWidth) and (AWidth < cMediumWidth);
end;
function FontSizeForWidth(const AWidth: Single): Single;
begin
if IsNarrow(AWidth) then
Result := 10
else if IsMedium(AWidth) then
Result := 11
else
Result := 12;
end;
function GetStatsHudHeight(const ASurfaceWidth: Single): Single;
begin
// Narrow phones: 4 lines; medium: 3; wide: 2
if IsNarrow(ASurfaceWidth) then
Result := cStatsPadTop + 4 * cStatsLineStep + cStatsPadBottom
else if IsMedium(ASurfaceWidth) then
Result := cStatsPadTop + 3 * cStatsLineStep + cStatsPadBottom
else
Result := cStatsPadTop + 2 * cStatsLineStep + cStatsPadBottom;
end;
{ TStatsHudModel }
procedure TStatsHudModel.Reset;
begin
FStopwatch := TStopwatch.StartNew;
FFrameCount := 0;
FWindowStart := 0;
FLastRenderTime := 0;
FFps := 0;
FFrameMs := 0;
FRefreshTimer := 0;
FPlatformLabel := '';
FBackendLabel := '';
FCpuPercentOfOneCore := 0;
FLogicalCpuCount := GetLogicalProcessorCount;
FCpuSampler.Reset;
FSurfaceWidth := 800;
FText := 'FPS: -';
end;
procedure TStatsHudModel.CaptureEnvironment;
begin
FPlatformLabel := GetHostPlatformLabel;
FBackendLabel := GetActiveRenderBackendLabel;
end;
procedure TStatsHudModel.OnFrameRendered;
var
LNow: Double;
begin
LNow := FStopwatch.Elapsed.TotalSeconds;
if FLastRenderTime > 0 then
begin
FFrameMs := (LNow - FLastRenderTime) * 1000.0;
end;
FLastRenderTime := LNow;
Inc(FFrameCount);
if (LNow - FWindowStart) >= 1.0 then
begin
FFps := FFrameCount;
FFrameCount := 0;
FWindowStart := LNow;
end;
end;
function TStatsHudModel.ShouldRefresh(const ADeltaTime: Double): Boolean;
begin
FRefreshTimer := FRefreshTimer + ADeltaTime;
Result := FRefreshTimer >= cStatsRefreshInterval;
if Result then
begin
FRefreshTimer := 0;
end;
end;
procedure TStatsHudModel.RefreshText(
const AParticleCount, AOrbCount: Integer;
const ASimTime: Double;
const ASurfaceWidth: Single;
const AParticleLabel: string;
const AOrbLabel: string);
var
LOfMachine: Double;
LLines: TStringList;
LParticleLabel: string;
LOrbLabel: string;
begin
FSurfaceWidth := ASurfaceWidth;
FCpuPercentOfOneCore := FCpuSampler.Sample;
if FLogicalCpuCount < 1 then
begin
FLogicalCpuCount := 1;
end;
LOfMachine := FCpuPercentOfOneCore / FLogicalCpuCount;
LParticleLabel := AParticleLabel;
if LParticleLabel = '' then
begin
LParticleLabel := 'Particles';
end;
LOrbLabel := AOrbLabel;
if LOrbLabel = '' then
begin
LOrbLabel := 'Orbs';
end;
LLines := TStringList.Create;
try
// Preferred is read live (radio can change it without re-CaptureEnvironment).
if IsNarrow(ASurfaceWidth) then
begin
// Phone: short tokens, one topic per line
LLines.Add(Format('FPS: %d | Frame: %.0f ms | Pref: %d',
[FFps, FFrameMs, GetPreferredFramesPerSecond]));
LLines.Add(Format('CPU: %.0f%%/core (%.0f%% of %d) | %s: %d | %s: %d',
[FCpuPercentOfOneCore, LOfMachine, FLogicalCpuCount,
LParticleLabel, AParticleCount, LOrbLabel, AOrbCount]));
LLines.Add(Format('Sim: %.1fs | %s', [ASimTime, FPlatformLabel]));
LLines.Add('Backend: ' + FBackendLabel);
end
else if IsMedium(ASurfaceWidth) then
begin
LLines.Add(Format('FPS: %d | Frame: %.1f ms | Preferred: %d | CPU: %.0f%% of 1 core (%.0f%% of %d)',
[FFps, FFrameMs, GetPreferredFramesPerSecond, FCpuPercentOfOneCore, LOfMachine,
FLogicalCpuCount]));
LLines.Add(Format('%s: %d | %s: %d | Sim: %.1f s',
[LParticleLabel, AParticleCount, LOrbLabel, AOrbCount, ASimTime]));
LLines.Add(Format('Platform: %s | Backend: %s',
[FPlatformLabel, FBackendLabel]));
end
else
begin
LLines.Add(Format(
'FPS: %d | Frame: %.1f ms | Preferred: %d | CPU: %.0f%% of 1 core (%.0f%% of %d) | %s: %d | %s: %d | Sim: %.1f s',
[FFps, FFrameMs, GetPreferredFramesPerSecond, FCpuPercentOfOneCore, LOfMachine,
FLogicalCpuCount, LParticleLabel, AParticleCount, LOrbLabel, AOrbCount, ASimTime]));
LLines.Add(Format('Platform: %s | Backend: %s',
[FPlatformLabel, FBackendLabel]));
end;
FText := LLines.Text.Trim;
finally
LLines.Free;
end;
end;
{ TStatsHudPainter }
class procedure TStatsHudPainter.Draw(
const ACanvas: ISkCanvas;
const ADest: TRectF;
const AText: string);
var
LBar: TRectF;
LPaint: ISkPaint;
LFont: ISkFont;
LLines: TStringList;
LHeight: Single;
LFontSize: Single;
LY: Single;
i: Integer;
begin
if ACanvas = nil then
begin
Exit;
end;
LHeight := GetStatsHudHeight(ADest.Width);
LFontSize := FontSizeForWidth(ADest.Width);
LBar := TRectF.Create(ADest.Left, ADest.Bottom - LHeight, ADest.Right, ADest.Bottom);
LPaint := TSkPaint.Create;
LPaint.AntiAlias := True;
LPaint.Style := TSkPaintStyle.Fill;
LPaint.Color := cStatsBarColor;
ACanvas.DrawRect(LBar, LPaint);
LLines := TStringList.Create;
try
LLines.Text := AText;
LFont := TSkFont.Create(TSkTypeface.MakeDefault, LFontSize);
LPaint.Color := cStatsTextColor;
for i := 0 to LLines.Count - 1 do
begin
if LLines[i] = '' then
begin
Continue;
end;
LY := LBar.Top + cStatsPadTop + i * cStatsLineStep;
LPaint.Color := cStatsTextColor;
ACanvas.DrawSimpleText(
LLines[i],
LBar.Left + cStatsTextPadX,
LY,
LFont,
LPaint);
end;
finally
LLines.Free;
end;
end;
end.