00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00027 #ifndef __TGF__H__
00028 #define __TGF__H__
00029
00030 #include <stdio.h>
00031 #ifndef WIN32
00032 #include <sys/param.h>
00033 #include <assert.h>
00034 #endif
00035 #include <stdlib.h>
00036 #ifdef WIN32
00037 #include <windows.h>
00038 #endif
00039 #include <stdarg.h>
00040 #include <string.h>
00041 #include <math.h>
00042 #include <osspec.h>
00043
00044
00045
00049 typedef float tdble;
00050
00051 extern void GfInit(void);
00052
00053 #ifndef MAX
00054 #define MAX(x,y) ((x) > (y) ? (x) : (y))
00055 #endif
00056
00057 #ifndef MIN
00058 #define MIN(x,y) ((x) < (y) ? (x) : (y))
00059 #endif
00060
00061 #define FREEZ(x) do { \
00062 if (x) { \
00063 free(x); \
00064 x = 0; \
00065 } \
00066 } while (0)
00067
00068 #define freez FREEZ
00069
00070 const double PI = 3.14159265358979323846;
00071 const tdble G = 9.80665;
00073
00074 #define RADS2RPM(x) ((x)*9.549296585)
00075 #define RPM2RADS(x) ((x)*.104719755)
00076 #define RAD2DEG(x) ((x)*(180.0/PI))
00077 #define DEG2RAD(x) ((x)*(PI/180.0))
00078 #define FEET2M(x) ((x)*0.304801)
00079 #define SIGN(x) ((x) < 0 ? -1.0 : 1.0)
00082 #define NORM0_2PI(x) \
00083 do { \
00084 while ((x) > 2*PI) { (x) -= 2*PI; } \
00085 while ((x) < 0) { (x) += 2*PI; } \
00086 } while (0)
00087
00089 #define NORM_PI_PI(x) \
00090 do { \
00091 while ((x) > PI) { (x) -= 2*PI; } \
00092 while ((x) < -PI) { (x) += 2*PI; } \
00093 } while (0)
00094
00095
00096 #ifndef DIST
00097
00098 #define DIST(x1, y1, x2, y2) sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
00099 #endif
00100
00101 #ifndef MIN
00102
00103 #define MIN(x,y) ((x) < (y) ? (x) : (y))
00104 #endif
00105
00106
00107 typedef struct {
00108 float x;
00109 float y;
00110 float z;
00111 } t3Df;
00112
00116 typedef struct {
00117 tdble x;
00118 tdble y;
00119 tdble z;
00120 } t3Dd;
00121
00122 typedef struct {
00123 int x;
00124 int y;
00125 int z;
00126 } t3Di;
00127
00131 typedef struct {
00132 tdble x;
00133 tdble y;
00134 tdble z;
00135 tdble ax;
00136 tdble ay;
00137 tdble az;
00138 } tPosd;
00139
00143 typedef struct
00144 {
00145 tPosd pos;
00146 tPosd vel;
00147 tPosd acc;
00148 } tDynPt;
00149
00151 typedef struct
00152 {
00153 t3Dd F;
00154 t3Dd M;
00155 } tForces;
00156
00157
00158
00159 #ifdef WIN32
00160 #define malloc _tgf_win_malloc
00161 #define calloc _tgf_win_calloc
00162 #define realloc _tgf_win_realloc
00163 #define free _tgf_win_free
00164 #define strdup _tgf_win_strdup
00165 #define _strdup _tgf_win_strdup
00166 extern void * _tgf_win_malloc(size_t size);
00167 extern void * _tgf_win_calloc(size_t num, size_t size);
00168 extern void * _tgf_win_realloc(void * memblock, size_t size);
00169 extern void _tgf_win_free(void * memblock);
00170 extern char * _tgf_win_strdup(const char * str);
00171 #endif // WIN32
00172
00173
00174
00175
00176
00177
00181 typedef int (*tfModPrivInit)(int index, void *);
00182
00186 #define MAX_MOD_ITF 10
00187
00189 typedef struct ModInfo {
00190 char *name;
00191 char *desc;
00192 tfModPrivInit fctInit;
00193 unsigned int gfId;
00194 int index;
00195 int prio;
00196 int magic;
00197 } tModInfo;
00198
00199
00200 typedef int (*tfModInfo)(tModInfo *);
00201
00202
00203 typedef int (*tfModShut)(void);
00204
00205
00207 typedef struct ModList {
00208 tModInfo modInfo[MAX_MOD_ITF];
00209 #ifdef _WIN32
00210 HMODULE handle;
00211 #else
00212 void *handle;
00213 #endif
00214 char *sopath;
00215 struct ModList *next;
00216 } tModList;
00217
00218
00219 extern int GfModLoad(unsigned int gfid, char *dllname, tModList **modlist);
00220 extern int GfModLoadDir(unsigned int gfid, char *dir, tModList **modlist);
00221 extern int GfModUnloadList(tModList **modlist);
00222 extern int GfModInfo(unsigned int gfid, char *filename, tModList **modlist);
00223 extern int GfModInfoDir(unsigned int gfid, char *dir, int level, tModList **modlist);
00224 extern int GfModFreeInfoList(tModList **modlist);
00225
00226
00227
00228
00229
00233 typedef struct FList
00234 {
00235 struct FList *next;
00236 struct FList *prev;
00237 char *name;
00238 char *dispName;
00239 void *userData;
00240 } tFList;
00241
00242 extern tFList *GfDirGetList(char *dir);
00243 typedef void (*tfDirfreeUserData)(void*);
00244 extern void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 #define GFPARM_PARAMETER 0
00259 #define GFPARM_TEMPLATE 1
00260 #define GFPARM_PARAM_STR "param"
00261 #define GFPARM_TEMPL_STR "template"
00262
00263
00264 #define GFPARM_MODIFIABLE 1
00265 #define GFPARM_WRITABLE 2
00267
00268 #define GFPARM_RMODE_STD 0x01
00269 #define GFPARM_RMODE_REREAD 0x02
00270 #define GFPARM_RMODE_CREAT 0x04
00271 #define GFPARM_RMODE_PRIVATE 0x08
00272
00273 extern void *GfParmReadFile(const char *file, int mode);
00274
00275 extern int GfParmWriteFile(const char *file, void* handle, char *name);
00276
00277 extern char *GfParmGetName(void *handle);
00278 extern char *GfParmGetFileName(void *handle);
00279
00280
00281 extern char *GfParmGetStr(void *handle, char *path, char *key, char *deflt);
00282
00283 extern char *GfParmGetCurStr(void *handle, char *path, char *key, char *deflt);
00284
00285 extern int GfParmSetStr(void *handle, char *path, char *key, char *val);
00286
00287 extern int GfParmSetCurStr(void *handle, char *path, char *key, char *val);
00288
00289
00290 extern tdble GfParmGetNum(void *handle, char *path, char *key, char *unit, tdble deflt);
00291
00292 extern tdble GfParmGetCurNum(void *handle, char *path, char *key, char *unit, tdble deflt);
00293
00294 extern int GfParmSetNum(void *handle, char *path, char *key, char *unit, tdble val);
00295
00296 extern int GfParmSetCurNum(void *handle, char *path, char *key, char *unit, tdble val);
00297
00298
00299
00300 extern void GfParmClean(void *handle);
00301
00302 extern void GfParmReleaseHandle(void *handle);
00303
00304
00305 extern tdble GfParmUnit2SI(char *unit, tdble val);
00306
00307 extern tdble GfParmSI2Unit(char *unit, tdble val);
00308
00309
00310 extern int GfParmCheckHandle(void *ref, void *tgt);
00311 #define GFPARM_MMODE_SRC 1
00312 #define GFPARM_MMODE_DST 2
00313 #define GFPARM_MMODE_RELSRC 4
00314 #define GFPARM_MMODE_RELDST 8
00315 extern void *GfParmMergeHandles(void *ref, void *tgt, int mode);
00316 extern int GfParmGetNumBoundaries(void *handle, char *path, char *key, tdble *min, tdble *max);
00317
00318
00319 extern int GfParmGetEltNb(void *handle, char *path);
00320 extern int GfParmListSeekFirst(void *handle, char *path);
00321 extern int GfParmListSeekNext(void *handle, char *path);
00322 extern char *GfParmListGetCurEltName(void *handle, char *path);
00323 extern int GfParmListClean(void *handle, char *path);
00324
00325
00326
00327
00328
00329 #ifdef WIN32
00330 #define GfTrace printf
00331 #define GfFatal printf
00332 #else
00333
00334 #define GfTrace printf
00335
00336 static inline void
00337 GfFatal(char *fmt, ...)
00338 {
00339 va_list ap;
00340 va_start(ap, fmt);
00341 vprintf(fmt, ap);
00342 va_end(ap);
00343
00344 assert (0);
00345 exit (1);
00346 }
00347 #endif
00348
00349 #define GfError printf
00350
00351 #if !(_DEBUG || DEBUG)
00352 #ifdef WIN32
00353 #define GfOut printf
00354 #else
00355
00356
00362 static inline void
00363 GfOut(char *fmt, ...)
00364 {
00365 }
00366
00367 #endif
00368
00369 #else
00370
00371 #define GfOut printf
00372
00373 #endif
00374
00375
00376
00377
00378 extern double GfTimeClock(void);
00379 extern char *GfGetTimeStr(void);
00380
00381
00382 #define GF_MEAN_MAX_VAL 5
00383
00384 typedef struct
00385 {
00386 int curNum;
00387 tdble val[GF_MEAN_MAX_VAL+1];
00388 } tMeanVal;
00389
00390 extern tdble gfMean(tdble v, tMeanVal *pvt, int n, int w);
00391 extern void gfMeanReset(tdble v, tMeanVal *pvt);
00392
00393
00394 extern char *GetLocalDir(void);
00395 extern void SetLocalDir(char *buf);
00396 extern char *GetLibDir(void);
00397 extern void SetLibDir(char *buf);
00398 extern char *GetDataDir(void);
00399 extern void SetDataDir(char *buf);
00400 extern int GetSingleTextureMode (void);
00401 extern void SetSingleTextureMode (void);
00402 extern int GfNearestPow2 (int x);
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00444 #define GF_TAILQ_HEAD(name, type) \
00445 typedef struct name { \
00446 type *tqh_first; \
00447 type **tqh_last; \
00448 } t ## name
00449
00452 #define GF_TAILQ_ENTRY(type) \
00453 struct { \
00454 type *tqe_next; \
00455 type **tqe_prev; \
00456 }
00457
00460 #define GF_TAILQ_FIRST(head) ((head)->tqh_first)
00461
00463 #define GF_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
00464
00466 #define GF_TAILQ_END(head) NULL
00467
00469 #define GF_TAILQ_LAST(head, headname) \
00470 (*(((struct headname *)((head)->tqh_last))->tqh_last))
00471
00473 #define GF_TAILQ_PREV(elm, headname, field) \
00474 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
00475
00476
00477
00478
00481 #define GF_TAILQ_INIT(head) do { \
00482 (head)->tqh_first = NULL; \
00483 (head)->tqh_last = &(head)->tqh_first; \
00484 } while (0)
00485
00488 #define GF_TAILQ_INIT_ENTRY(elm, field) do { \
00489 (elm)->field.tqe_next = 0; \
00490 (elm)->field.tqe_prev = 0; \
00491 } while (0)
00492
00495 #define GF_TAILQ_INSERT_HEAD(head, elm, field) do { \
00496 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
00497 (head)->tqh_first->field.tqe_prev = \
00498 &(elm)->field.tqe_next; \
00499 else \
00500 (head)->tqh_last = &(elm)->field.tqe_next; \
00501 (head)->tqh_first = (elm); \
00502 (elm)->field.tqe_prev = &(head)->tqh_first; \
00503 } while (0)
00504
00507 #define GF_TAILQ_INSERT_TAIL(head, elm, field) do { \
00508 (elm)->field.tqe_next = NULL; \
00509 (elm)->field.tqe_prev = (head)->tqh_last; \
00510 *(head)->tqh_last = (elm); \
00511 (head)->tqh_last = &(elm)->field.tqe_next; \
00512 } while (0)
00513
00516 #define GF_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
00517 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
00518 (elm)->field.tqe_next->field.tqe_prev = \
00519 &(elm)->field.tqe_next; \
00520 else \
00521 (head)->tqh_last = &(elm)->field.tqe_next; \
00522 (listelm)->field.tqe_next = (elm); \
00523 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
00524 } while (0)
00525
00528 #define GF_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
00529 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
00530 (elm)->field.tqe_next = (listelm); \
00531 *(listelm)->field.tqe_prev = (elm); \
00532 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
00533 } while (0)
00534
00537 #define GF_TAILQ_REMOVE(head, elm, field) do { \
00538 if (((elm)->field.tqe_next) != NULL) \
00539 (elm)->field.tqe_next->field.tqe_prev = \
00540 (elm)->field.tqe_prev; \
00541 else \
00542 (head)->tqh_last = (elm)->field.tqe_prev; \
00543 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
00544 } while (0)
00545
00546
00547
00548 #ifdef PROFILER
00549
00550 #include <vector>
00551 #include <map>
00552
00553 #define START_PROFILE(name) Profiler::getInstance()->startProfile(name)
00554 #define STOP_PROFILE(name) Profiler::getInstance()->stopProfile()
00555 #define STOP_ACTIVE_PROFILES() Profiler::getInstance()->stopActiveProfiles()
00556 #define PRINT_PROFILE() Profiler::getInstance()->printProfile()
00557
00558 class ProfileInstance {
00559 public:
00560 ProfileInstance(const char * pname);
00561 ~ProfileInstance();
00562 char name[256];
00563 int calls;
00564 int openCalls;
00565 double totalTime;
00566 double addTime;
00567 double subTime;
00568 double tmpStart;
00569 std::map<ProfileInstance *, void *> mapChildren;
00570 };
00571
00573 class Profiler {
00574 protected:
00575 Profiler();
00576 public:
00577 ~Profiler();
00578 static Profiler * getInstance();
00579 void startProfile(const char * pname);
00580 void stopProfile();
00581 void stopActiveProfiles();
00582 void printProfile();
00583 private:
00584 static Profiler * profiler;
00585 ProfileInstance * curProfile;
00586 double fStartTime;
00587 std::vector<ProfileInstance *> vecProfiles;
00588 std::vector<ProfileInstance *> stkProfiles;
00589 std::map<ProfileInstance *, void *> mapWarning;
00590 };
00591
00592 #else
00593 #define START_PROFILE(a)
00594 #define STOP_PROFILE(a)
00595 #define STOP_ACTIVE_PROFILES()
00596 #define PRINT_PROFILE()
00597 #endif
00598
00599
00600
00601
00602 #define GF_HASH_TYPE_STR 0
00603 #define GF_HASH_TYPE_BUF 1
00605 typedef void (*tfHashFree)(void*);
00607 void *GfHashCreate(int type);
00608 int GfHashAddStr(void *hash, char *key, void *data);
00609 void *GfHashRemStr(void *hash, char *key);
00610 void *GfHashGetStr(void *hash, char *key);
00611 void GfHashAddBuf(void *hash, char *key, size_t sz, void *data);
00612 void *GfHashRemBuf(void *hash, char *key, size_t sz);
00613 void *GfHashGetBuf(void *hash, char *key, size_t sz);
00614 void GfHashRelease(void *hash, tfHashFree hashFree);
00615 void *GfHashGetFirst(void *hash);
00616 void *GfHashGetNext(void *hash);
00617
00618 #endif
00619
00620