/
sgUtils.pas
404 lines (334 loc) · 10.5 KB
/
sgUtils.pas
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//=============================================================================
// sgUtils.pas
//=============================================================================
//
// The Utils unit contains general utility routins that may be useful for games.
//
// Change History:
//
// Version 3:
// - 2010-12-15: Andrew : Created to take general functionality from Core
//=============================================================================
/// SwinGame's Utils contain a number of helper routines that can be useful in
/// general game development.
///
/// @module Utils
/// @static
unit sgUtils;
//=============================================================================
interface
uses sgTypes;
//=============================================================================
//----------------------------------------------------------------------------
// Library Version
//----------------------------------------------------------------------------
/// Retrieves a string representing the version of SwinGame that is executing.
/// This can be used to check that the version supports the features required
/// for your game.
///
/// @lib
function SwinGameVersion(): String;
//----------------------------------------------------------------------------
// Exception Notification/Message
//----------------------------------------------------------------------------
/// This function can be used to retrieve a message containing the details of
/// the last error that occurred in SwinGame.
///
/// @lib
function ExceptionMessage(): String;
/// This function tells you if an error occurred with the last operation in
/// SwinGame.
///
/// @lib
function ExceptionOccured(): Boolean;
//----------------------------------------------------------------------------
// Random
//----------------------------------------------------------------------------
/// Generates a random number between 0 and 1.
///
/// @lib
function Rnd() : Single; overload;
/// Generates a random integer up to (but not including) ubound. Effectively,
/// the ubound value specifies the number of random values to create.
///
/// @lib RndUpto
function Rnd(ubound: Longint): Longint; overload;
//----------------------------------------------------------------------------
// Delay / Framerate
//----------------------------------------------------------------------------
/// Returns the average framerate for the last 10 frames as an integer.
///
/// @returns The current average framerate
///
/// @lib
function GetFramerate(): Longint;
/// Gets the number of milliseconds that have passed. This can be used to
/// determine timing operations, such as updating the game elements.
///
/// @returns The number of milliseconds passed
///
/// @lib
function GetTicks(): Longword;
/// Puts the process to sleep for a specified number of
/// milliseconds. This can be used to add delays into your
/// game.
///
/// @param time - The number of milliseconds to sleep
///
/// Side Effects
/// - Delay before returning
///
/// @lib
procedure Delay(time: Longword);
/// Returns the calculated framerate averages, highest, and lowest values along with
/// the suggested rendering color.
///
/// @lib
/// @sn calculateFramerateAvg:%s high:%s low:%s color:%s
procedure CalculateFramerate(out average, highest, lowest: String; out textColor: Color);
//=============================================================================
implementation
uses
SysUtils, Math, Classes, //System
sgSavePNG, sgShared, sgTrace, sgInputBackend, //SwinGame shared library code
sgResources, sgGeometry, sgImages, sgGraphics, sgDriverTimer, sgInput; //SwinGame
//=============================================================================
type
// Details required for the Frames per second calculations.
FPSData = record
values: Array [0..59] of Single;
pos: Longint;
max, min, avg: Single;
ready: Boolean;
end;
var
_fpsData: FPSData;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//Used to initialise the Frame Per Second data structure.
procedure _InitFPSData();
var
i: Longint;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', '_InitFPSData');
{$ENDIF}
// clear the array of values
for i := Low(_fpsData.values) to High(_fpsData.values) do
_fpsData.values[i] := 0;
// zero the current insert position, and the loop count
_fpsData.pos := 0;
//_fpsData.loops := 0;
// set the moving range and average to sensitble defaults
_fpsData.max := 0;
_fpsData.min := 0;
_fpsData.avg := 0;
_fpsData.ready := false;
{$IFDEF TRACE}
TraceExit('sgUtils', '_InitFPSData');
{$ENDIF}
end;
//----------------------------------------------------------------------------
// Library Version
//----------------------------------------------------------------------------
function SwinGameVersion(): String;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'SwinGameVersion');
{$ENDIF}
result := DLL_VERSION;
{$IFDEF TRACE}
TraceExit('sgUtils', 'SwinGameVersion');
{$ENDIF}
end;
//----------------------------------------------------------------------------
// Exception Notification/Message
//----------------------------------------------------------------------------
function ExceptionMessage(): String;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'ExceptionMessage');
{$ENDIF}
result := ErrorMessage;
{$IFDEF TRACE}
TraceExit('sgUtils', 'ExceptionMessage', result);
{$ENDIF}
end;
function ExceptionOccured(): Boolean;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'ExceptionOccured');
{$ENDIF}
result := HasException;
{$IFDEF TRACE}
TraceExit('sgUtils', 'ExceptionOccured : ' + BoolToStr(result, true));
{$ENDIF}
end;
//----------------------------------------------------------------------------
// Delay / Framerate
//----------------------------------------------------------------------------
procedure Delay(time: Longword);
var
t: Longword;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'Delay');
{$ENDIF}
if time > 0 then
begin
if time < 50 then TimerDriver.Delay(time)
else
begin
for t := 1 to time div 50 do
begin
ProcessEvents();
if WindowCloseRequested() then exit;
TimerDriver.Delay(50);
end;
t := time mod 50;
if t > 0 then TimerDriver.Delay(t);
end;
end;
{$IFDEF TRACE}
TraceExit('sgUtils', 'Delay');
{$ENDIF}
end;
function GetTicks(): Longword;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'GetTicks');
{$ENDIF}
result := TimerDriver.GetTicks();
{$IFDEF TRACE}
TraceExit('sgUtils', 'GetTicks');
{$ENDIF}
end;
function GetFramerate(): Longint;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'GetFramerate');
{$ENDIF}
if _fpsData.avg = 0 then
result := 60
else
result := RoundInt(1000 / _fpsData.avg);
{$IFDEF TRACE}
TraceExit('sgUtils', 'GetFramerate');
{$ENDIF}
end;
procedure CalculateFramerate(out average, highest, lowest: String; out textColor: Color);
var
avg, hi, lo: Single;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'CalculateFramerate');
{$ENDIF}
if not _fpsData.ready then
begin
textColor := ColorBlue;
average :='??.?';
highest :='??.?';
lowest :='??.?';
{$IFDEF TRACE}
TraceExit('sgUtils', 'CalculateFramerate');
{$ENDIF}
exit;
end;
if _fpsData.avg = 0 then
avg := 9999
else
avg := (1000 / _fpsData.avg);
lo := (1000 / _fpsData.max);
hi := (1000 / _fpsData.min);
Str(avg:4:1, average);
Str(hi:4:1, highest);
Str(lo:4:1, lowest);
if avg < 10 then
textColor := ColorRed
else if avg < 50 then
textColor := ColorYellow
else
textColor := ColorGreen;
{$IFDEF TRACE}
TraceExit('sgUtils', 'CalculateFramerate');
{$ENDIF}
end;
procedure _UpdateFPSDataProc(delta: Longword);
function RunningAverage(var values: Array of Single; newValue: Longword; var pos: Longint): Single;
var
i: Longint;
sum: Double;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', '_UpdateFPSData');
{$ENDIF}
// insert the newValue as the position specified
values[pos] := newValue;
// calculate the sum for the average
sum := 0;
for i := Low(values) to High(values) do
sum := sum + values[i];
result := Single(sum / Length(values));
//Inc position index, and wrap-around to start if needed
pos := pos + 1;
if pos > High(values) then
begin
pos := Low(values);
if not _fpsData.ready then
begin
_fpsData.max := _fpsData.avg;
_fpsData.min := _fpsData.avg;
_fpsData.ready := True;
end;
end;
end;
begin
_fpsData.avg := RunningAverage(_fpsData.values, delta, _fpsData.pos);
if _fpsData.avg = 0.0 then _fpsData.avg := 0.01;
//Adjust the min/maxes
if _fpsData.ready then
begin
if _fpsData.avg > _fpsData.max then _fpsData.max := _fpsData.avg
else if _fpsData.avg < _fpsData.min then _fpsData.min := _fpsData.avg;
end;
{$IFDEF TRACE}
TraceExit('sgUtils', '_UpdateFPSData');
{$ENDIF}
end;
//----------------------------------------------------------------------------
// Random
//----------------------------------------------------------------------------
function Rnd() : Single; overload;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'Rnd');
{$ENDIF}
result := Single(System.Random());
{$IFDEF TRACE}
TraceExit('sgUtils', 'Rnd');
{$ENDIF}
end;
function Rnd(ubound: Longint): Longint; overload;
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'Rnd');
{$ENDIF}
result := System.Random(ubound);
{$IFDEF TRACE}
TraceExit('sgUtils', 'Rnd');
{$ENDIF}
end;
//=============================================================================
initialization
begin
{$IFDEF TRACE}
TraceEnter('sgUtils', 'initialization');
{$ENDIF}
InitialiseSwinGame();
_InitFPSData();
_UpdateFPSData := @_UpdateFPSDataProc;
{$IFDEF TRACE}
TraceExit('sgUtils', 'initialization');
{$ENDIF}
end;
end.