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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#include "dcgf.h"
#include "utils.h"
#include "PlatformSDL.h"
#include "wintypes.h"
#include "PathUtil.h"
#include "BGame.h"
#include "common/str.h"
#include "common/textconsole.h"
namespace WinterMute {
//////////////////////////////////////////////////////////////////////
static inline unsigned Sqr(int x) {
return (x * x);
}
//////////////////////////////////////////////////////////////////////////////////
void CBUtils::Clip(int *DestX, int *DestY, RECT *SrcRect, RECT *DestRect) {
// If it's partly off the right side of the screen
if (*DestX + (SrcRect->right - SrcRect->left) > DestRect->right)
SrcRect->right -= *DestX + (SrcRect->right - SrcRect->left) - DestRect->right;
if (SrcRect->right < 0) SrcRect->right = 0;
// Partly off the left side of the screen
if (*DestX < DestRect->left) {
SrcRect->left += DestRect->left - *DestX;
*DestX = DestRect->left;
}
// Partly off the top of the screen
if (*DestY < DestRect->top) {
SrcRect->top += DestRect->top - *DestY;
*DestY = DestRect->top;
}
// If it's partly off the bottom side of the screen
if (*DestY + (SrcRect->bottom - SrcRect->top) > DestRect->bottom)
SrcRect->bottom -= ((SrcRect->bottom - SrcRect->top) + *DestY) - DestRect->bottom;
if (SrcRect->bottom < 0) SrcRect->bottom = 0;
return;
}
//////////////////////////////////////////////////////////////////////////////////
// Swap - swaps two integers
//////////////////////////////////////////////////////////////////////////////////
void CBUtils::Swap(int *a, int *b) {
int Temp = *a;
*a = *b;
*b = Temp;
}
//////////////////////////////////////////////////////////////////////////
bool CBUtils::StrBeginsI(const char *String, const char *Fragment) {
return (scumm_strnicmp(String, Fragment, strlen(Fragment)) == 0);
}
//////////////////////////////////////////////////////////////////////////
float CBUtils::NormalizeAngle(float Angle) {
while (Angle > 360) Angle -= 360;
while (Angle < 0) Angle += 360;
return Angle;
}
////////////////////////////////////////////////////////////////////////////////
void CBUtils::CreatePath(const char *Path, bool PathOnly) {
AnsiString path;
if (!PathOnly) path = PathUtil::GetDirectoryName(Path);
else path = Path;
// try {
warning("CBUtils::CreatePath - not implemented: %s", Path);
// boost::filesystem::create_directories(path);
// } catch (...) {
return;
// }
}
//////////////////////////////////////////////////////////////////////////
void CBUtils::DebugMessage(HWND hWnd, const char *Text) {
//MessageBox(hWnd, Text, "WME", MB_OK|MB_ICONINFORMATION);
}
//////////////////////////////////////////////////////////////////////////
char *CBUtils::SetString(char **String, const char *Value) {
delete[] *String;
*String = new char[strlen(Value) + 1];
if (*String) strcpy(*String, Value);
return *String;
}
//////////////////////////////////////////////////////////////////////////
int CBUtils::StrNumEntries(const char *Str, const char Delim) {
int NumEntries = 1;
for (int i = 0; i < strlen(Str); i++) {
if (Str[i] == Delim) NumEntries++;
}
return NumEntries;
}
//////////////////////////////////////////////////////////////////////////
char *CBUtils::StrEntry(int Entry, const char *Str, const char Delim) {
int NumEntries = 0;
const char *Start = NULL;
int Len = 0;
for (int i = 0; i <= strlen(Str); i++) {
if (NumEntries == Entry) {
if (!Start) Start = Str + i;
else Len++;
}
if (Str[i] == Delim || Str[i] == '\0') {
NumEntries++;
if (Start) {
char *Ret = new char[Len + 1];
memset(Ret, 0, Len + 1);
strncpy(Ret, Start, Len);
return Ret;
}
}
}
return NULL;
}
//////////////////////////////////////////////////////////////////////////
int CBUtils::RandomInt(int From, int To) {
if (To < From) {
int i = To;
To = From;
From = i;
}
return (rand() % (To - From + 1)) + From;
}
//////////////////////////////////////////////////////////////////////////
float CBUtils::RandomFloat(float From, float To) {
float RandNum = (float)rand() / (float)RAND_MAX;
return From + (To - From) * RandNum;
}
//////////////////////////////////////////////////////////////////////////
float CBUtils::RandomAngle(float From, float To) {
while (To < From) {
To += 360;
}
return NormalizeAngle(RandomFloat(From, To));
}
//////////////////////////////////////////////////////////////////////////
bool CBUtils::MatchesPattern(const char *Pattern, const char *String) {
char stringc, patternc;
for (;; ++String) {
stringc = toupper(*String);
patternc = toupper(*Pattern++);
switch (patternc) {
case 0:
return (stringc == 0);
case '?':
if (stringc == 0) return false;
break;
case '*':
if (!*Pattern) return true;
if (*Pattern == '.') {
char *dot;
if (Pattern[1] == '*' && Pattern[2] == 0) return true;
dot = (char *)strchr(String, '.');
if (Pattern[1] == 0) return (dot == NULL || dot[1] == 0);
if (dot != NULL) {
String = dot;
if (strpbrk(Pattern, "*?[") == NULL && strchr(String + 1, '.') == NULL)
return(scumm_stricmp(Pattern + 1, String + 1) == 0);
}
}
while (*String)
if (CBUtils::MatchesPattern(Pattern, String++))
return true;
return false;
default:
if (patternc != stringc)
if (patternc == '.' && stringc == 0)
return(CBUtils::MatchesPattern(Pattern, String));
else
return false;
break;
}
}
}
//////////////////////////////////////////////////////////////////////////
char *CBUtils::GetPath(char *Filename) {
AnsiString path = PathUtil::GetDirectoryName(Filename);
//path = boost::filesystem::syste_complete(path).string();
warning("CBUtils::GetPath: (%s), not implemented", Filename);
return Filename;
char *ret = new char[path.size() + 1];
strcpy(ret, path.c_str());
return ret;
}
//////////////////////////////////////////////////////////////////////////
char *CBUtils::GetFilename(char *Filename) {
AnsiString path = PathUtil::GetFileName(Filename);
char *ret = new char[path.size() + 1];
strcpy(ret, path.c_str());
return ret;
}
//////////////////////////////////////////////////////////////////////////
void CBUtils::RGBtoHSL(uint32 RGBColor, byte *OutH, byte *OutS, byte *OutL) {
float var_R = (D3DCOLGetR(RGBColor) / 255.0f);
float var_G = (D3DCOLGetG(RGBColor) / 255.0f);
float var_B = (D3DCOLGetB(RGBColor) / 255.0f);
//Min. value of RGB
float var_Min = MIN(var_R, var_G);
var_Min = MIN(var_Min, var_B);
//Max. value of RGB
float var_Max = MAX(var_R, var_G);
var_Max = MAX(var_Max, var_B);
//Delta RGB value
float del_Max = var_Max - var_Min;
float H, S, L;
L = (var_Max + var_Min) / 2.0f;
//This is a gray, no chroma...
if (del_Max == 0) {
H = 0;
S = 0;
}
//Chromatic data...
else {
if (L < 0.5f) S = del_Max / (var_Max + var_Min);
else S = del_Max / (2.0f - var_Max - var_Min);
float del_R = (((var_Max - var_R) / 6.0f) + (del_Max / 2.0f)) / del_Max;
float del_G = (((var_Max - var_G) / 6.0f) + (del_Max / 2.0f)) / del_Max;
float del_B = (((var_Max - var_B) / 6.0f) + (del_Max / 2.0f)) / del_Max;
if (var_R == var_Max) H = del_B - del_G;
else if (var_G == var_Max) H = (1.0f / 3.0f) + del_R - del_B;
else if (var_B == var_Max) H = (2.0f / 3.0f) + del_G - del_R;
if (H < 0) H += 1;
if (H > 1) H -= 1;
}
*OutH = H * 255;
*OutS = S * 255;
*OutL = L * 255;
}
//////////////////////////////////////////////////////////////////////////
uint32 CBUtils::HSLtoRGB(byte InH, byte InS, byte InL) {
float H = InH / 255.0f;
float S = InS / 255.0f;
float L = InL / 255.0f;
byte R, G, B;
if (S == 0) {
R = L * 255;
G = L * 255;
B = L * 255;
} else {
float var_1, var_2;
if (L < 0.5) var_2 = L * (1.0 + S);
else var_2 = (L + S) - (S * L);
var_1 = 2.0f * L - var_2;
R = 255 * Hue2RGB(var_1, var_2, H + (1.0f / 3.0f));
G = 255 * Hue2RGB(var_1, var_2, H);
B = 255 * Hue2RGB(var_1, var_2, H - (1.0f / 3.0f));
}
return DRGBA(255, R, G, B);
}
//////////////////////////////////////////////////////////////////////////
float CBUtils::Hue2RGB(float v1, float v2, float vH) {
if (vH < 0.0f) vH += 1.0f;
if (vH > 1.0f) vH -= 1.0f;
if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);
if ((2.0f * vH) < 1.0f) return (v2);
if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
return (v1);
}
} // end of namespace WinterMute
|