-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmUtil.bas
446 lines (341 loc) · 12.8 KB
/
mUtil.bas
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
Attribute VB_Name = "mUtil"
'-----------------------------------------
'--- Utils Module
'-----------------------------------------
Rem *** Purpose: Some math, calculations, system info retrieving procs., _
Log File and etc.
Enum CONST_PRIORITIES
P_HIGH = THREAD_PRIORITY_ABOVE_NORMAL
P_NORMAL = THREAD_PRIORITY_NORMAL
End Enum
' logfile constants
Public Const LOG_FILENAME = "dd2log.txt" ' logfile name
Public Const LOG_FILEHANDLE = 1 ' logfile_handle
Public Const LOG_DASH As String = "-------------------------------------------------" & vbCrLf
' math constantss
Public Const PI = 3.1415926
Public Const PI360 = 6.2831853
Public Const DEGTORAD = PI / 180
Public Const RADTODEG = 180 / PI
Public Const MAX_INT = 32767
Public Const MIN_INT = -32768
' ////////////////////////////////////////////////////////////////
' //// Set program priority
' ////////////////////////////////////////////////////////////////
Public Function _
SetProgramPriority(lprior As CONST_PRIORITIES) As Boolean
Dim curproc As Long
curproc = GetCurrentThread()
If (SetThreadPriority(curproc, lprior)) Then
SetPriority = True
AppendToLog ("Setting priority " & lprior & " successful.")
Else
SetPriority = False
AppendToLog ("Setting priority " & lprior & " failed.")
End If
'curproc = GetCurrentProcess()
'If (SetPriorityClass(curproc, lprior)) Then _
' SetPriority = True Else _
' SetPriority = False
End Function
' ////////////////////////////////////////////////////////////////
' //// Open up the Log File
' ////////////////////////////////////////////////////////////////
Public Sub _
OpenLog(lpPath As String)
On Local Error GoTo OPENLOG_ERROR:
Open lpPath & LOG_FILENAME For Append Access Write Lock Read As #LOG_FILEHANDLE
Exit Sub
OPENLOG_ERROR:
Call ErrorMsg("Could not open Log file!")
End Sub
' ////////////////////////////////////////////////////////////////
' //// Stream data to log file
' ////////////////////////////////////////////////////////////////
Public Sub _
AppendToLog(lpStr As String)
On Local Error Resume Next
Print #LOG_FILEHANDLE, chGetTime & ": " & lpStr ' Format(Time$, "hh:mm:ss")
End Sub
' ////////////////////////////////////////////////////////////////
' //// Close the log file
' ////////////////////////////////////////////////////////////////
Public Sub _
CloseLog()
On Local Error Resume Next
AppendToLog (LOG_DASH)
Print #LOG_FILEHANDLE, "Game Closed at: " & chGetTime
Print #LOG_FILEHANDLE, LOG_DASH
Close #LOG_FILEHANDLE
End Sub
' ////////////////////////////////////////////////////////////////
' //// Get velocity frictions (I used this before I get
' //// the GetAngle function)
' ////////////////////////////////////////////////////////////////
Public Sub CalcVelocityBound(ByVal x As Single, ByVal y As Single, _
ByVal dx As Single, ByVal dy As Single, _
xVel_Bound As Single, yVel_Bound As Single)
' Desc: procedure that returns velocity fixing values, so
' an object will move at specific vector
Dim fVar1 As Single, fVar2 As Single
xVel_Bound = 1 ' reset vars to 1 so division will give the same value
yVel_Bound = 1
fVar1 = Abs(x - dx)
fVar2 = Abs(y - dy)
If fVar1 <= 0 Then fVar1 = 1
If fVar2 <= 0 Then fVar2 = 1
'Call GetDist2P(x, dx, fVar1) ' get x distance
'Call GetDist2P(y, dy, fVar2) ' get y distacne
If fVar1 > fVar2 Then ' if xdist>ydist then
yVel_Bound = fVar1 / fVar2 ' assign xd/yd divison to y-velocity-boundary
ElseIf fVar1 < fVar2 Then
xVel_Bound = fVar2 / fVar1 ' assign yd/xd divison to x-velocity-boundary
End If
End Sub
' ////////////////////////////////////////////////////////////////
' //// Get The Angle Between Two Points
' ////////////////////////////////////////////////////////////////
Public Function _
GetAngle(ByVal x1, ByVal y1, ByVal x2, ByVal y2) As Single
Dim val1 As Single, val2 As Single
val1 = (x2 - x1)
val2 = (y2 - y1)
If (val1 > 0) Then
GetAngle = Atn(val2 / val1)
ElseIf (val1 < 0) Then
GetAngle = Atn(val2 / val1) + PI
Else
GetAngle = 2 * Atn(Sgn(val2))
End If
End Function
' ////////////////////////////////////////////////////////////////
' //// Checks if 2 rectangles interpolate
' ////////////////////////////////////////////////////////////////
Public Function _
Collide(rObject1 As RECT, rObject2 As RECT) As Boolean
Dim rRect As RECT
If (IntersectRect(rRect, rObject1, rObject2)) Then
Collide = True
Else
Collide = False
End If
End Function
Public Function _
InRange(ByVal SrcVar As Integer, ByVal MinVar As Integer, ByVal MaxVar As Integer) As Boolean
If (SrcVar >= MinVar And SrcVar <= MaxVar) Then
InRange = True
Else
InRange = False
End If
End Function
' ////////////////////////////////////////////////////////////////
' //// checks if var 1 is bigger than var2 ( for both INT and
' //// FLOAT data types )
' ////////////////////////////////////////////////////////////////
Public Function _
max(ByVal Var1, ByVal Var2) As Boolean
If Var1 >= Var2 Then
max = True
Else
max = False
End If
End Function
' ////////////////////////////////////////////////////////////////
' //// Get 1D distance ;-)
' ////////////////////////////////////////////////////////////////
Public Sub _
GetDist2P(ByVal Var1 As Single, ByVal Var2 As Single, ByVal Var3 As Single)
' Desc: Get the distance between 2 points
'Var3 = Sqr((Var1 - Var2) ^ 2)
Var3 = Abs(Var1 - Var2)
End Sub
' ////////////////////////////////////////////////////////////////
' //// Get the Distance between two points (Phytagorous)
' ////////////////////////////////////////////////////////////////
Public Function _
nGetDist2D(sx As Integer, sy As Integer, _
dx As Integer, dy As Integer) As Integer
nGetDist2D = Sqr(((sx - dx) ^ 2) + ((sy - dy) ^ 2))
End Function
Public Function _
fGetDist2D(sx As Single, sy As Single, _
dx As Single, dy As Single) As Single
fGetDist2D = Sqr(((sx - dx) ^ 2) + ((sy - dy) ^ 2))
End Function
' ////////////////////////////////////////////////////////////////
' //// get random INT value
' ////////////////////////////////////////////////////////////////
Public Function _
nGetRnd(nMin As Integer, nMax As Integer) As Integer
nGetRnd = ((nMax - nMin) * Rnd) + nMin
End Function
' ////////////////////////////////////////////////////////////////
' //// get random FLOAT value
' ////////////////////////////////////////////////////////////////
Public Function _
fGetRnd(nMin As Single, nMax As Single) As Single
fGetRnd = ((nMax - nMin) * Rnd) + nMin
End Function
'////////////////////////////////////////////////////////////////
'//// Convert unsigned to signed value
'////////////////////////////////////////////////////////////////
Public Function _
CSigned(lNum As Long) As Integer
If (lNum < 32768) Then
CSigned = CInt(lNum)
Else
CSigned = CInt(lNum - 65535)
End If
End Function
'////////////////////////////////////////////////////////////////
'//// Convert signed to unsigned value
'////////////////////////////////////////////////////////////////
Public Function _
CUnSigned(nNum As Integer) As Long
If (nNum >= 0) Then
CUnSigned = nNum
Else
CUnSigned = (nNum + 65535)
End If
End Function
'////////////////////////////////////////////////////////////////
'//// Return Windows tickcount
'////////////////////////////////////////////////////////////////
Public Function _
GetTicks() As Long
GetTicks = GetTickCount()
End Function
'////////////////////////////////////////////////////////////////
'//// Return current local time (h:m:s:ms)
'////////////////////////////////////////////////////////////////
Public Function _
chGetTime() As String
Dim lpSysTime As SYSTEMTIME
Dim chs As String
' get local time settings
GetLocalTime lpSysTime
chs = Format$(lpSysTime.wSecond) ', "##")
chGetTime = lpSysTime.wHour & ":" & lpSysTime.wMinute & ":" & _
chs & "." & lpSysTime.wMilliseconds
End Function
'////////////////////////////////////////////////////////////////
'//// Calculates the Frame Rate per Second ( in 2 ways )
'////////////////////////////////////////////////////////////////
Public Function _
CalcFrameRate(npFPS As Integer) As Integer
Static lTime As Long
Static lFrameTime As Long
Static nFrameCount As Long
Static Frames As Long
Static szAveFPS As String
' Method 1
' nFrameCount = nFrameCount + 1
' lTime = GetTicks - lFrameTime ' see if substraction is bigger then 1 sec.
' If lTime > 1000 Then ' 1 sec. elapsed
' Frames = (nFrameCount * 1000) / lTime ' calc FPS
' lFrameTime = GetTicks
' nFrameCount = 0 ' zero raw frame counter
' CalcFrameRate = Frames
' 'szAveFPS = CStr(Frames) ' copy average FPS to a string
' End If
'Method 2
If GetTicks - lTime >= 1000 Then
lTime = GetTicks
'szAveFPS = CStr(Frames)
npFPS = CInt(Frames)
Frames = 0
End If
Frames = Frames + 1
' blit FPS info to backbuffer
'lpBack.SetForeColor RGB(45, 255, 35)
'lpBack.DrawText 10, 70, szAveFPS & " FPS", False
End Function
'////////////////////////////////////////////////////////////////
'//// Gets key value
'//// LONG lKeyRoot - registry root (HKEY_LOCAL_MACHINE)
'//// LPCSTR strKeyName - main key name (SOFTWARE\DD2\PLAYERS)
'//// LPCSTR strKeyName - key name (NAME)
'//// LPCSTR strKeyVal - where the data goes to
'////////////////////////////////////////////////////////////////
Public Function _
RegLoadKey(lKeyRoot As Long, _
strKeyName As String, _
strSubKeyName As String, _
ByRef strKeyVal As String) As Boolean
Dim cn As Long
Dim ret As Long
Dim hKey As Long
Dim hDisp As Long
Dim lpAttr As SECURITY_ATTRIBUTES
Dim lplType As Long
Dim strTemp As String
Dim lSize As Long
' open key reg
ret = RegOpenKeyEx(lKeyRoot, strKeyName, 0, KEY_ALL_ACCESS, hKey)
If (ret <> 0) Then GoTo REGERROR
strTemp = Space$(1024)
lSize = 1024
' retrieve key-value
ret = RegQueryValueEx(hKey, strSubKeyName, 0, _
lplType, strTemp, lSize)
If (ret <> 0) Then GoTo REGERROR
If (Asc(Mid(strTemp, lSize, 1)) = 0) Then
strTemp = Left(strTemp, lSize - 1)
Else
strTemp = Left(strTemp, lSize)
End If
' determine keytype and do convertions
Select Case lplType
Case REG_SZ
strKeyVal = strTemp
Case REG_DWORD
For cn = Len(strTemp) To 1 Step -1 ' Convert Each Bit
strKeyVal = strKeyVal + Hex(Asc(Mid(strTemp, cn, 1))) ' Build Value Char. By Char.
'strKeyVal = strKeyVal + Chr$(Asc(Mid(strTemp, cn, 1))) ' convert to chars
Next
strKeyVal = Format$("&h" + strKeyVal) ' Convert Double Word To String
End Select
' unlock
ret = RegCloseKey(hKey)
RegLoadKey = True
Exit Function
REGERROR:
ret = RegCloseKey(hKey)
RegLoadKey = False
End Function
'////////////////////////////////////////////////////////////////
'//// Writes key value
'//// LONG lKeyRoot - registry root (HKEY_LOCAL_MACHINE)
'//// LPCSTR strKeyName - main key name (SOFTWARE\DD2\PLAYERS)
'//// LPCSTR strSubKeyName - key name
'//// LPCSTR strKeyVal - data to copy
'////////////////////////////////////////////////////////////////
Public Function _
RegSetKey(lKeyRoot As Long, strKeyName As String, _
strSubKeyName As String, strSubKeyValue As String) As Boolean
Dim ret As Long
Dim hKey As Long
Dim hDisp As Long
Dim lpAttr As SECURITY_ATTRIBUTES
lpAttr.nLength = 50
lpAttr.lpSecurityDescriptor = 0
lpAttr.bInheritHandle = True
' create/open reg. key
ret = RegCreateKeyEx(lKeyRoot, strKeyName, 0, REG_SZ, _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
hKey, hDisp)
' incase of an error
If (ret <> 0) Then GoTo REGERROR
' create/modify key value
If (strSubKeyName = "") Then strSubKeyName = " " ' need space to work
ret = RegSetValueEx(hKey, strSubKeyName, 0, _
REG_SZ, strSubKeyValue, Len(strSubKeyValue))
If (ret <> 0) Then GoTo REGERROR
' close
ret = RegCloseKey(hKey)
RegSetKey = True
Exit Function
REGERROR:
ret = RegCloseKey(hKey)
RegSetKey = False
End Function