TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
tgf.cpp
Go to the documentation of this file.
1/***************************************************************************
2 tgf.cpp -- The Gaming Framework
3 -------------------
4 created : Fri Aug 13 22:31:43 CEST 1999
5 copyright : (C) 1999-2026 by Eric EspiƩ, Bernhard Wymann
6 email : berniw@bluewin.ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifdef WIN32
19#include <windows.h>
20#include <direct.h>
21#else
22#include <sys/stat.h>
23#include <sys/types.h>
24#endif
25#include <errno.h>
26#include <portability.h>
27
28#include <tgf.h>
29#include <time.h>
30
31extern void gfDirInit(void);
32extern void gfModInit(void);
33extern void gfOsInit(void);
34extern void GfParmInit(void);
35extern void gfRlstInit(void);
36
37
38#ifdef WIN32
39#include <crtdbg.h>
40#include <assert.h>
41
42
43void * _tgf_win_malloc(size_t size)
44{
45#ifdef _DEBUG
46 char * p = (char*)GlobalAlloc(GMEM_FIXED, size + 3*sizeof(int));
47 *(int*)(p) = size + 3*sizeof(int);
48 *((int*)p + 1) = 123456789;
49 *((int*)(p + size + 3*sizeof(int)) - 1) = 987654321;
50
51 return p + 2*sizeof(int);
52#else // _DEBUG
53 char * p = (char*)GlobalAlloc(GMEM_FIXED, size + sizeof(int));
54 if (p == NULL) {
55 return NULL;
56 }
57 *(int*)(p) = size;
58 return p + sizeof(int);
59#endif // _DEBUG
60}
61
62
63void * _tgf_win_calloc(size_t num, size_t size)
64{
65 void * p = _tgf_win_malloc(num * size);
66 memset(p, 0, num * size);
67 return p;
68}
69
70
71void * _tgf_win_realloc(void * memblock, size_t size)
72{
73 if (size == 0) {
74 _tgf_win_free(memblock);
75 return NULL;
76 }
77
78 void * p = _tgf_win_malloc(size);
79 if (p == NULL) {
80 return NULL;
81 }
82
83 if (memblock != NULL) {
84#ifdef _DEBUG
85 memcpy(p, memblock,
86 min(*(int*)((char*)memblock-2*sizeof(int)) - 3*sizeof(int), (int)size));
87#else // _DEBUG
88 memcpy(p, memblock, min(*(int*)((char*)memblock-sizeof(int)), (int)size));
89#endif // _DEBUG
90 _tgf_win_free(memblock);
91 }
92 return p;
93}
94
95
96void _tgf_win_free(void * memblock)
97{
98 if (!memblock) {
99 return;
100 }
101
102#ifdef _DEBUG
103 char * p = (char*)memblock - 2*sizeof(int);
104
105 if (!_CrtIsValidPointer(p, sizeof(int), TRUE)) {
106 assert(0);
107 }
108
109 if (!_CrtIsValidPointer(p, *(int*)p, TRUE)) {
110 assert( 0 );
111 }
112
113 if (*((int*)p + 1) != 123456789) {
114 assert( 0 );
115 }
116
117 if(*((int*)(p + *(int*)p ) - 1) != 987654321) {
118 assert( 0 );
119 }
120
121 GlobalFree((char*)memblock - 2*sizeof(int));
122#else // _DEBUG
123 GlobalFree((char*)memblock - sizeof(int));
124#endif // _DEBUG
125}
126
127
128char * _tgf_win_strdup(const char * str)
129{
130 char * s = (char*)_tgf_win_malloc(strlen(str)+1);
131 strcpy(s,str);
132
133 return s;
134}
135#endif // WIN32
136
137
138void GfInit(void)
139{
140 gfDirInit();
141 gfModInit();
142 gfOsInit();
143 GfParmInit();
144}
145
146
148{
149 int i;
150
151 for (i = 0; i < GF_MEAN_MAX_VAL; i++) {
152 pvt->val[i] = v;
153 }
154}
155
156
157tdble gfMean(tdble v, tMeanVal *pvt, int n, int w)
158{
159 int i;
160 tdble sum;
161
162 if (n > pvt->curNum) {
163 if (pvt->curNum < GF_MEAN_MAX_VAL) {
164 pvt->curNum++;
165 }
166 n = pvt->curNum;
167 } else {
168 pvt->curNum = n;
169 }
170
171 sum = 0;
172 for (i = 0; i < n; i++) {
173 pvt->val[i] = pvt->val[i + 1];
174 sum += pvt->val[i];
175 }
176
177 pvt->val[n] = v;
178 sum += (tdble)w * v;
179 sum /= (tdble)(n + w);
180
181 return sum;
182}
183
184
192void GfTime2Str(char *result, int resultSize, tdble sec, int sgn)
193{
194 const char* sign;
195
196 if (sec < 0.0) {
197 sec = -sec;
198 sign = "-";
199 } else {
200 if (sgn) {
201 sign = "+";
202 } else {
203 sign = " ";
204 }
205 }
206
207 int h = (int)(sec / 3600.0);
208 sec -= 3600 * h;
209 int m = (int)(sec / 60.0);
210 sec -= 60 * m;
211 int s = (int)(sec);
212 sec -= s;
213 int c = (int)floor((sec) * 100.0);
214
215 if (h) {
216 snprintf(result, resultSize, "%s%2.2d:%2.2d:%2.2d:%2.2d", sign, h, m, s, c);
217 } else if (m) {
218 snprintf(result, resultSize, " %s%2.2d:%2.2d:%2.2d", sign, m, s, c);
219 } else {
220 snprintf(result, resultSize, " %s%2.2d:%2.2d", sign, s, c);
221 }
222}
223
224
225static char *localDir = strdup("");
226static char *libDir = strdup("");
227static char *dataDir = strdup("");
228
229
230char * GetLocalDir(void)
231{
232 return localDir;
233}
234
235
236void SetLocalDir(char *buf)
237{
238 free(localDir);
239 localDir = strdup(buf);
240}
241
242
243char * GetLibDir(void)
244{
245 return libDir;
246}
247
248
249void SetLibDir(char *buf)
250{
251 free(libDir);
252 libDir = strdup(buf);
253}
254
255
256char * GetDataDir(void)
257{
258 return dataDir;
259}
260
261
262void SetDataDir(char *buf)
263{
264 free(dataDir);
265 dataDir = strdup(buf);
266}
267
268
269static int singleTextureMode = 0;
270
271
273{
274 return singleTextureMode;
275}
276
277
279{
281}
282
283
284int GfNearestPow2 (int x)
285{
286 int r;
287
288 if (!x) {
289 return 0;
290 }
291
292 x++;
293 r = 1;
294 while ((1 << r) < x) {
295 r++;
296 }
297 r--;
298
299 return (1 << r);
300}
301
302
308int GfCreateDir(char *path)
309{
310 if (path == NULL) {
312 }
313
314 const int BUFSIZE = 1024;
315 char buf[BUFSIZE];
316 size_t pathLen = strlen(path);
317 if (pathLen >= BUFSIZE) {
318 GfError("GfCreateDir: path too long: %s\n", path);
320 }
321 memcpy(buf, path, pathLen + 1);
322
323#ifdef WIN32
324#define mkdir(x) _mkdir(x)
325
326 // Translate path.
327 const char DELIM = '\\';
328 int i;
329 for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) {
330 if (buf[i] == '/') {
331 buf[i] = DELIM;
332 }
333 }
334
335#else // WIN32
336#define mkdir(x) mkdir((x), S_IRWXU);
337
338 const char DELIM = '/';
339
340#endif // WIN32
341
342 int err = mkdir(buf);
343 if (err == -1) {
344 if (errno == ENOENT) {
345 char *end = strrchr(buf, DELIM);
346 *end = '\0';
347 GfCreateDir(buf);
348 *end = DELIM;
349 err = mkdir(buf);
350
351 }
352 }
353
354 if (err == -1 && errno != EEXIST) {
356 } else {
357 return GF_DIR_CREATED;
358 }
359}
360
361
368int GfCreateDirForFile(const char *filenameandpath)
369{
370 if (filenameandpath == 0) {
372 }
373
374 const char* lastdelim = strrchr(filenameandpath, '/');
375 if (lastdelim != NULL && lastdelim != filenameandpath) {
376 const int BUFSIZE = 1024;
377 char buf[BUFSIZE];
378 const int size = MIN(lastdelim - filenameandpath, BUFSIZE - 1);
379 snprintf(buf, BUFSIZE, "%s", filenameandpath);
380 buf[size] = '\0';
381
382 return GfCreateDir(buf);
383 }
384
385 return GF_DIR_CREATED;
386}
Scalar min(Scalar x, Scalar y)
Definition Basic.h:49
int sgn(Scalar x)
Definition Basic.h:46
static Point p[4]
Definition Convex.cpp:54
const T sign(const T &x)
int errno
Definition errno.cpp:5
void GfParmInit(void)
Parameter set library API initialization, set up parameter set handle cache.
Definition params.cpp:160
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
void GfTime2Str(char *result, int resultSize, tdble sec, int sgn)
Convert a time in seconds (float) to an ascii string.
Definition tgf.cpp:192
tdble val[GF_MEAN_MAX_VAL+1]
Definition tgf.h:387
int curNum
Definition tgf.h:386
static char * localDir
Definition tgf.cpp:225
static int singleTextureMode
Definition tgf.cpp:269
void gfRlstInit(void)
void SetDataDir(char *buf)
Definition tgf.cpp:262
void SetSingleTextureMode(void)
Definition tgf.cpp:278
int GfNearestPow2(int x)
Definition tgf.cpp:284
void gfMeanReset(tdble v, tMeanVal *pvt)
Definition tgf.cpp:147
void SetLocalDir(char *buf)
Definition tgf.cpp:236
int GfCreateDir(char *path)
Create directory for given path recursively, so all missing parent directories are created as well.
Definition tgf.cpp:308
static char * dataDir
Definition tgf.cpp:227
char * GetDataDir(void)
Definition tgf.cpp:256
void gfOsInit(void)
Init of the module.
Definition os.cpp:41
void gfDirInit(void)
Definition directory.cpp:33
void GfInit(void)
Definition tgf.cpp:138
int GfCreateDirForFile(const char *filenameandpath)
Create directory for given file path recursively, so all missing parent directories are created as we...
Definition tgf.cpp:368
int GetSingleTextureMode(void)
Definition tgf.cpp:272
void gfModInit(void)
Definition module.cpp:42
char * GetLibDir(void)
Definition tgf.cpp:243
#define mkdir(x)
tdble gfMean(tdble v, tMeanVal *pvt, int n, int w)
Definition tgf.cpp:157
static char * libDir
Definition tgf.cpp:226
char * GetLocalDir(void)
Definition tgf.cpp:230
void SetLibDir(char *buf)
Definition tgf.cpp:249
The Gaming Framework API.
#define MIN(x, y)
Definition tgf.h:56
#define GF_MEAN_MAX_VAL
Definition tgf.h:382
#define GF_DIR_CREATED
Definition tgf.h:622
#define GF_DIR_CREATION_FAILED
Definition tgf.h:621
#define GfError
Definition tgf.h:350