-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.cpp
348 lines (284 loc) · 8.66 KB
/
common.cpp
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
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
Copyright (C) 2010 Extreme Tuxracer Team
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.
---------------------------------------------------------------------*/
#include "common.h"
#include "spx.h"
// --------------------------------------------------------------------
// color utils
// --------------------------------------------------------------------
const TColor colWhite = {1.0, 1.0, 1.0, 1.0};
const TColor colDYell = {1.0, 0.8, 0.0, 1.0};
const TColor colDDYell = {0.8, 0.6, 0.0, 1.0};
const TColor colYellow = {1.0, 1.0, 0.0, 1.0};
const TColor colLYell = {1.0, 1.0, 0.4, 1.0};
const TColor colOrange = {1.0, 0.5, 0.0, 1.0};
const TColor colLRed = {1.0, 0.3, 0.3, 1.0};
const TColor colRed = {1.0, 0.0, 0.0, 1.0};
const TColor colDRed = {0.8, 0.0, 0.0, 1.0};
const TColor colGrey = {0.5, 0.5, 0.5, 1.0};
const TColor colLGrey = {0.7, 0.7, 0.7, 1.0};
const TColor colDGrey = {0.3, 0.3, 0.3, 1.0};
const TColor colBlack = {0.0, 0.0, 0.0, 1.0};
const TColor colBlue = {0.0, 0.0, 1.0, 1.0};
const TColor colLBlue = {0.5, 0.7, 1.0, 1.0};
const TColor colDBlue = {0.0, 0.0, 0.6, 1.0};
const TColor colLBackgr = {0.5, 0.7, 0.9, 1.0};
const TColor colBackgr = {0.4, 0.6, 0.8, 1.0};
const TColor colMBackgr = {0.35, 0.5, 0.7, 1.0};
const TColor colDBackgr = {0.2, 0.3, 0.6, 1.0};
const TColor colDDBackgr = {0.13, 0.2, 0.4, 1.0};
const TColor colMess = {0.3, 0.3, 0.7, 1.0};
const TColor colSky = {0.82, 0.86, 0.88, 1.0};
// --------------------------------------------------------------------
// print utils
// --------------------------------------------------------------------
void PrintInt (const int val) {
printf ("Integer: %i \n", val );
}
void PrintInt (string s, const int val) {
cout << s << val << endl;
}
void PrintStr (const char *val) {
printf ("%s \n", val);
}
void PrintString (string s) {
cout << s << endl;
}
void PrintFloat (const float val) {
printf ("%.3f \n", val);
}
void PrintDouble (const double val) {
printf ("%.3f \n", val);
}
void PrintFloat (char *s, const float val) {
char ss[128];
strcpy (ss, s);
strcat (ss," %.4f \n");
printf (ss, val);
}
void PrintFloat8 (const float val) {
printf ("%.8f \n", val);
}
void PrintBool (const bool val) {
if (val == true) printf ("bool: true\n");
else printf ("bool: false\n");
}
void PrintPointer (void *p) {
if (p == NULL) printf ("Pointer: NULL\n");
else printf ("Pointer: %p \n", &p);
}
void PrintVector4 (const TVector4 v) {
printf ("%.2f %.2f %.2f %.2f \n", v.x, v.y, v.z, v.w);
}
void PrintColor (const TColor v) {
printf ("%.2f %.2f %.2f %.2f \n", v.r, v.g, v.b, v.a);
}
void PrintVector2 (const TVector2 v) {
printf ("%.2f %.2f \n", v.x, v.y);
}
void PrintVector (const TVector3 v) {
printf ("%.4f %.4f %.4f \n", v.x, v.y, v.z);
}
void PrintIndex3 (TIndex3 idx) {
printf ("%i %i %i \n", idx.i, idx.j, idx.k);
}
void PrintIndex4 (TIndex4 idx) {
printf ("%i %i %i %i \n", idx.i, idx.j, idx.k, idx.l);
}
void PrintVector (char *s, const TVector3 v) {
char ss[128];
strcpy (ss, s);
strcat (ss, " %.4f %.4f %.4f \n");
printf (ss, v.x, v.y, v.z);
}
void PrintMatrix (TMatrix mat) {
printf ("\n");
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++)
if (mat[i][j]<0) printf (" %.2f", mat[i][j]);
else printf (" %.2f", mat[i][j]);
printf ("\n");
}
printf ("\n");
}
void PrintMatrixGL (TMatrixGL glmat) {
printf ("\n");
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++)
if (glmat[i*4+j]<0) printf (" %.2f", glmat[i*4+j]);
else printf (" %.2f", glmat[i*4+j]);
printf ("\n");
}
printf ("\n");
}
void PrintQuaternion (TQuaternion q) {
printf ("Quaternion: %.4f %.4f %.4f %.4f\n", q.x, q.y, q.z, q.w);
}
// --------------------------------------------------------------------
// message utils
// --------------------------------------------------------------------
static CSPList msg_list (100);
void InitMessages () {}
void SaveMessages () {
msg_list.Save (param.config_dir, "messages");
}
void Message (const char *msg, const char *desc) {
if (strlen(msg)<1 && strlen(desc)<1) {
printf (" \n");
return;
}
string aa = msg;
string bb = desc;
printf ("%s %s \n", msg, desc);
msg_list.Add (aa + bb);
}
void Message (const char *msg) {
if (strlen(msg)<1) {
printf (" \n");
return;
}
printf ("%s \n", msg);
msg_list.Add (msg);
}
void MessageN (string a, string b) {
cout << a << " " << b << endl;
msg_list.Add (a + b);
}
// --------------------------------------------------------------------
// file utils
// --------------------------------------------------------------------
bool FileExists (const char *filename) {
struct stat stat_info;
if (stat (filename, &stat_info) != 0) {
if (errno != ENOENT) Message ("couldn't stat ", filename);
return false;
} else return true;
}
bool FileExists (const string filename) {
return FileExists (filename.c_str());
}
bool FileExists (const string dir, const string filename) {
return FileExists (dir + SEP + filename);
}
bool DirExists (const char *dirname) {
#if defined ( OS_WIN32_NATIVE )
return DirExistsWin (dirname);
#else
DIR *xdir;
if ((xdir = opendir (dirname)) == 0)
return ((errno != ENOENT) && (errno != ENOTDIR));
if (closedir (xdir) != 0) Message ("Couldn't close directory", dirname);
return true;
#endif
}
bool FileExistsWin (const char *filename) {
FILE *xfile = fopen (filename, "r");
if (xfile == NULL) return false;
if (fclose (xfile) != 0) Message ("error closing file", filename);
return true;
}
bool DirExistsWin (const char *dirname) {
char curdir[1000];
if (getcwd (curdir, 1000) == 0)
Message ("DirExistsWin: getcwd failed");
if (chdir (dirname) == -1) return false;
if (chdir (curdir) == -1) {
Message ("Couldn't access directory", curdir);
return false;
}
return true;
}
// --------------------------------------------------------------------
// date and time
// --------------------------------------------------------------------
void GetTimeComponents (double time, int *min, int *sec, int *hundr) {
*min = (int) (time / 60);
*sec = ((int) time) % 60;
*hundr = ((int) (time * 100 + 0.5) ) % 100;
}
/*
struct tm {
int tm_sec;
int tm_min;
int tm_hour; // 0..23
int tm_mday; // day in month 1..31
int tm_mon; // 0..11
int tm_year; // year (sice 1900)
int tm_wday; // weekday 0..6, start with sunday
int tm_yday; // day in the year
int tm_isdst; // not zero in case of US sommertime converting
};
*/
void GetTestTime () {
time_t rawtime; // seconds since 1. January 1970
struct tm * timeinfo; // see above
time (&rawtime);
timeinfo = localtime (&rawtime);
PrintInt (timeinfo->tm_year + 1900);
PrintStr (asctime (timeinfo));
}
string GetTimeString1 () {
string line;
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
// line = Int_StrN (timeinfo->tm_year-100);
line = Int_StrN (timeinfo->tm_mon + 1);
line += "_" + Int_StrN (timeinfo->tm_mday);
line += "_" + Int_StrN (timeinfo->tm_hour);
line += Int_StrN (timeinfo->tm_min);
line += Int_StrN (timeinfo->tm_sec);
return line;
}
// --------------------------------------------------------------------
// FILE, read and write
// --------------------------------------------------------------------
unsigned short read_word (FILE *fp) {
unsigned char b0, b1;
b0 = getc(fp);
b1 = getc(fp);
return ((b1 << 8) | b0);
}
unsigned int read_dword (FILE *fp) {
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
int read_long (FILE *fp) {
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
int write_word (FILE *fp, unsigned short w) {
putc (w, fp);
return (putc (w >> 8, fp));
}
int write_dword(FILE *fp, unsigned int dw) {
putc (dw, fp);
putc (dw >> 8, fp);
putc (dw >> 16, fp);
return (putc (dw >> 24, fp));
}
int write_long (FILE *fp, int l) {
putc (l, fp);
putc (l >> 8, fp);
putc (l >> 16, fp);
return (putc (l >> 24, fp));
}