diff options
author | Franklin Wei <git@fwei.tk> | 2017-01-21 15:18:31 -0500 |
---|---|---|
committer | Franklin Wei <git@fwei.tk> | 2017-12-23 21:01:26 -0500 |
commit | a855d6202536ff28e5aae4f22a0f31d8f5b325d0 (patch) | |
tree | 8c75f224dd64ed360505afa8843d016b0d75000b /apps/plugins/sdl/progs/duke3d/Game/src/global.c | |
parent | 01c6dcf6c7b9bb1ad2fa0450f99bacc5f3d3e04b (diff) | |
download | rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.tar.gz rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.zip |
Port of Duke Nukem 3D
This ports Fabien Sanglard's Chocolate Duke to run on a version of SDL
for Rockbox.
Change-Id: I8f2c4c78af19de10c1633ed7bb7a997b43256dd9
Diffstat (limited to 'apps/plugins/sdl/progs/duke3d/Game/src/global.c')
-rw-r--r-- | apps/plugins/sdl/progs/duke3d/Game/src/global.c | 931 |
1 files changed, 931 insertions, 0 deletions
diff --git a/apps/plugins/sdl/progs/duke3d/Game/src/global.c b/apps/plugins/sdl/progs/duke3d/Game/src/global.c new file mode 100644 index 0000000000..861e1326e8 --- /dev/null +++ b/apps/plugins/sdl/progs/duke3d/Game/src/global.c | |||
@@ -0,0 +1,931 @@ | |||
1 | //------------------------------------------------------------------------- | ||
2 | /* | ||
3 | Copyright (C) 1996, 2003 - 3D Realms Entertainment | ||
4 | |||
5 | This file is part of Duke Nukem 3D version 1.5 - Atomic Edition | ||
6 | |||
7 | Duke Nukem 3D is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU General Public License | ||
9 | as published by the Free Software Foundation; either version 2 | ||
10 | of the License, or (at your option) any later version. | ||
11 | |||
12 | This program is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
15 | |||
16 | See the GNU General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU General Public License | ||
19 | aint32_t with this program; if not, write to the Free Software | ||
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | |||
22 | Original Source: 1996 - Todd Replogle | ||
23 | Prepared for public release: 03/21/2003 - Charlie Wiederhold, 3D Realms | ||
24 | */ | ||
25 | //------------------------------------------------------------------------- | ||
26 | |||
27 | #include <stdio.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | #include <stdarg.h> | ||
31 | #include <errno.h> | ||
32 | #include "global.h" | ||
33 | #include "duke3d.h" | ||
34 | |||
35 | uint16_t readLE16(void *addr) | ||
36 | { | ||
37 | uint8_t *ptr = addr; | ||
38 | return (*(ptr+1) << 8) | *ptr; | ||
39 | } | ||
40 | |||
41 | uint32_t readLE32(void *addr) | ||
42 | { | ||
43 | uint8_t *ptr = addr; | ||
44 | return (*(ptr+3) << 24) |(*(ptr+2) << 16) | (*(ptr+1) << 8) | *ptr; | ||
45 | } | ||
46 | |||
47 | char *mymembuf; | ||
48 | uint8_t MusicPtr[72000]; | ||
49 | |||
50 | |||
51 | crc32_t crc32lookup[] = { | ||
52 | // good: | ||
53 | { CRC_BASE_GRP_SHAREWARE_13, "SHAREWARE 1.3D", 11035779 }, | ||
54 | { CRC_BASE_GRP_FULL_13, "FULL 1.3D ", 26524524 }, | ||
55 | { CRC_BASE_GRP_PLUTONIUM_14, "PLUTONIUM 1.4 ", 44348015 }, | ||
56 | { CRC_BASE_GRP_ATOMIC_15, "ATOMIC 1.5 ", 44356548 }, | ||
57 | // unknown: | ||
58 | { 0, "HACK/UNKNOWN ", 0} | ||
59 | }; | ||
60 | |||
61 | uint8_t conVersion = 13; | ||
62 | uint8_t grpVersion = 0; | ||
63 | |||
64 | // FIX_00015: Backward compliance with older demos (down to demos v27, 28, 116 and 117 only) | ||
65 | |||
66 | // For BYTEVERSION diff, 27/116 vs 28/117 see extras\duke3d.h vs source\duke3d.h | ||
67 | // from the official source code release. | ||
68 | |||
69 | int BYTEVERSION_27 = 27; // 1.3 under 1.4 Plutonium. Not supported anymore | ||
70 | int BYTEVERSION_116 = 116; // 1.4 Plutonium. Not supported anymore | ||
71 | |||
72 | int BYTEVERSION_28 = 28; // 1.3 under 1.5 engine | ||
73 | int BYTEVERSION_117 = 117; // 1.5 Atomic | ||
74 | |||
75 | int BYTEVERSION_29 = 29; // 1.3 under xDuke v19.6. | ||
76 | int BYTEVERSION_118 = 118; // 1.5 Atomic under xDuke v19.6. | ||
77 | |||
78 | int BYTEVERSION_1_3 = 1; // for 1.3 demos (Not compatible) | ||
79 | |||
80 | int BYTEVERSION = 119; // xDuke v19.7 | ||
81 | |||
82 | short global_random; | ||
83 | short neartagsector, neartagwall, neartagsprite; | ||
84 | |||
85 | int32_t gc,neartaghitdist,lockclock,max_player_health,max_armour_amount,max_ammo_amount[MAX_WEAPONS]; | ||
86 | |||
87 | // int32_t temp_data[MAXSPRITES][6]; | ||
88 | struct weaponhit hittype[MAXSPRITES]; | ||
89 | short spriteq[1024],spriteqloc,spriteqamount=64; | ||
90 | |||
91 | // ported build engine has this, too. --ryan. | ||
92 | #if PLATFORM_DOS | ||
93 | short moustat = 0; | ||
94 | #endif | ||
95 | |||
96 | struct animwalltype animwall[MAXANIMWALLS]; | ||
97 | short numanimwalls; | ||
98 | int32_t *animateptr[MAXANIMATES], animategoal[MAXANIMATES], animatevel[MAXANIMATES], animatecnt; | ||
99 | // int32_t oanimateval[MAXANIMATES]; | ||
100 | short animatesect[MAXANIMATES]; | ||
101 | int32_t msx[2048],msy[2048]; | ||
102 | short cyclers[MAXCYCLERS][6],numcyclers; | ||
103 | |||
104 | char fta_quotes[NUMOFFIRSTTIMEACTIVE][64]; | ||
105 | |||
106 | uint8_t tempbuf[2048]; | ||
107 | uint8_t packbuf[576]; | ||
108 | |||
109 | char buf[80]; | ||
110 | |||
111 | short camsprite; | ||
112 | short mirrorwall[64], mirrorsector[64], mirrorcnt; | ||
113 | |||
114 | int current_menu; | ||
115 | |||
116 | uint8_t betaname[80]; | ||
117 | |||
118 | char level_names[44][33]; | ||
119 | char level_file_names[44][128]; | ||
120 | int32_t partime[44],designertime[44]; | ||
121 | char volume_names[4][33] = { "L.A. MELTDOWN", "LUNAR APOCALYPSE", "SHRAPNEL CITY", "" }; // Names are not in 1.3 con files. MUST be in code. | ||
122 | char skill_names[5][33] = { "PIECE OF CAKE", "LET'S ROCK", "COME GET SOME", "DAMN I'M GOOD", "" }; | ||
123 | |||
124 | volatile int32_t checksume; | ||
125 | int32_t soundsiz[NUM_SOUNDS]; | ||
126 | |||
127 | short soundps[NUM_SOUNDS],soundpe[NUM_SOUNDS],soundvo[NUM_SOUNDS]; | ||
128 | uint8_t soundm[NUM_SOUNDS],soundpr[NUM_SOUNDS]; | ||
129 | char sounds[NUM_SOUNDS][14]; | ||
130 | |||
131 | short title_zoom; | ||
132 | |||
133 | fx_device device; | ||
134 | |||
135 | SAMPLE Sound[ NUM_SOUNDS ]; | ||
136 | SOUNDOWNER SoundOwner[NUM_SOUNDS][4]; | ||
137 | |||
138 | uint8_t numplayersprites,earthquaketime; | ||
139 | |||
140 | int32_t fricxv,fricyv; | ||
141 | struct player_orig po[MAXPLAYERS]; | ||
142 | struct player_struct ps[MAXPLAYERS]; | ||
143 | struct user_defs ud; | ||
144 | |||
145 | uint8_t pus, pub; | ||
146 | uint8_t syncstat, syncval[MAXPLAYERS][MOVEFIFOSIZ]; | ||
147 | int32_t syncvalhead[MAXPLAYERS], syncvaltail, syncvaltottail; | ||
148 | |||
149 | input sync[MAXPLAYERS], loc; | ||
150 | input recsync[RECSYNCBUFSIZ]; | ||
151 | int32_t avgfvel, avgsvel, avgavel, avghorz, avgbits; | ||
152 | |||
153 | |||
154 | input inputfifo[MOVEFIFOSIZ][MAXPLAYERS]; | ||
155 | input recsync[RECSYNCBUFSIZ]; | ||
156 | |||
157 | int32_t movefifosendplc; | ||
158 | |||
159 | //Multiplayer syncing variables | ||
160 | short screenpeek; | ||
161 | int32_t movefifoend[MAXPLAYERS]; | ||
162 | |||
163 | |||
164 | //Game recording variables | ||
165 | |||
166 | uint8_t playerreadyflag[MAXPLAYERS],ready2send; | ||
167 | uint8_t playerquitflag[MAXPLAYERS]; | ||
168 | int32_t vel, svel, angvel, horiz, ototalclock, respawnactortime=768, respawnitemtime=768, groupfile; | ||
169 | |||
170 | int32_t script[MAXSCRIPTSIZE],*scriptptr,*insptr,*labelcode,labelcnt; | ||
171 | int32_t *actorscrptr[MAXTILES],*parsing_actor; | ||
172 | char *label,*textptr,error,warning ; | ||
173 | uint8_t killit_flag; | ||
174 | uint8_t *music_pointer; | ||
175 | uint8_t actortype[MAXTILES]; | ||
176 | |||
177 | |||
178 | uint8_t display_mirror,typebuflen; | ||
179 | char typebuf[41]; | ||
180 | |||
181 | char music_fn[4][11][13]; | ||
182 | uint8_t music_select; | ||
183 | char env_music_fn[4][13]; | ||
184 | uint8_t rtsplaying; | ||
185 | |||
186 | |||
187 | short weaponsandammosprites[15] = { | ||
188 | RPGSPRITE, | ||
189 | CHAINGUNSPRITE, | ||
190 | DEVISTATORAMMO, | ||
191 | RPGAMMO, | ||
192 | RPGAMMO, | ||
193 | JETPACK, | ||
194 | SHIELD, | ||
195 | FIRSTAID, | ||
196 | STEROIDS, | ||
197 | RPGAMMO, | ||
198 | RPGAMMO, | ||
199 | RPGSPRITE, | ||
200 | RPGAMMO, | ||
201 | FREEZESPRITE, | ||
202 | FREEZEAMMO | ||
203 | }; | ||
204 | |||
205 | int32_t impact_damage; | ||
206 | |||
207 | //GLOBAL.C - replace the end "my's" with this | ||
208 | int32_t myx, omyx, myxvel, myy, omyy, myyvel, myz, omyz, myzvel; | ||
209 | short myhoriz, omyhoriz, myhorizoff, omyhorizoff; | ||
210 | short myang, omyang, mycursectnum, myjumpingcounter,frags[MAXPLAYERS][MAXPLAYERS]; | ||
211 | |||
212 | uint8_t myjumpingtoggle, myonground, myhardlanding, myreturntocenter; | ||
213 | int8_t multiwho, multipos, multiwhat, multiflag; | ||
214 | |||
215 | int32_t fakemovefifoplc,movefifoplc; | ||
216 | int32_t myxbak[MOVEFIFOSIZ], myybak[MOVEFIFOSIZ], myzbak[MOVEFIFOSIZ]; | ||
217 | int32_t myhorizbak[MOVEFIFOSIZ],dukefriction = 0xcc00, show_shareware; | ||
218 | |||
219 | short myangbak[MOVEFIFOSIZ]; | ||
220 | char myname[2048] = "XDUKE"; | ||
221 | uint8_t camerashitable,freezerhurtowner=0,lasermode; | ||
222 | // CTW - MODIFICATION | ||
223 | // uint8_t networkmode = 255, movesperpacket = 1,gamequit = 0,playonten = 0,everyothertime; | ||
224 | uint8_t networkmode = 255, movesperpacket = 1,gamequit = 0,everyothertime; | ||
225 | // CTW END - MODIFICATION | ||
226 | int32_t numfreezebounces=3,rpgblastradius,pipebombblastradius,tripbombblastradius,shrinkerblastradius,morterblastradius,bouncemineblastradius,seenineblastradius; | ||
227 | STATUSBARTYPE sbar; | ||
228 | |||
229 | int32_t myminlag[MAXPLAYERS], mymaxlag, otherminlag, bufferjitter = 1; | ||
230 | short numclouds,clouds[128],cloudx[128],cloudy[128]; | ||
231 | int32_t cloudtotalclock = 0,totalmemory = 0; | ||
232 | int32_t numinterpolations = 0, startofdynamicinterpolations = 0; | ||
233 | int32_t oldipos[MAXINTERPOLATIONS]; | ||
234 | int32_t bakipos[MAXINTERPOLATIONS]; | ||
235 | int32_t *curipos[MAXINTERPOLATIONS]; | ||
236 | |||
237 | |||
238 | // portability stuff. --ryan. | ||
239 | // A good portion of this was ripped from GPL'd Rise of the Triad. --ryan. | ||
240 | |||
241 | #ifndef PATH_SEP_CHAR | ||
242 | #define PATH_SEP_CHAR '/' | ||
243 | #endif | ||
244 | |||
245 | void FixFilePath(char *filename) | ||
246 | { | ||
247 | #if PLATFORM_UNIX || PLATFORM_ROCKBOX | ||
248 | uint8_t *ptr; | ||
249 | uint8_t *lastsep = filename; | ||
250 | |||
251 | if ((!filename) || (*filename == '\0')) | ||
252 | return; | ||
253 | if(filename[0] != '/') | ||
254 | LOGF("%s is not absolute", filename); | ||
255 | |||
256 | if (rb->file_exists(filename)) /* File exists; we're good to go. */ | ||
257 | return; | ||
258 | |||
259 | for (ptr = filename; 1; ptr++) | ||
260 | { | ||
261 | if (*ptr == '\\') | ||
262 | *ptr = PATH_SEP_CHAR; | ||
263 | |||
264 | if ((*ptr == PATH_SEP_CHAR) || (*ptr == '\0')) | ||
265 | { | ||
266 | uint8_t pch = *ptr; | ||
267 | struct dirent *dent = NULL; | ||
268 | DIR *dir; | ||
269 | |||
270 | if ((pch == PATH_SEP_CHAR) && (*(ptr + 1) == '\0')) | ||
271 | return; /* eos is pathsep; we're done. */ | ||
272 | |||
273 | if (lastsep == ptr) | ||
274 | continue; /* absolute path; skip to next one. */ | ||
275 | |||
276 | *ptr = '\0'; | ||
277 | if (lastsep == filename) { | ||
278 | dir = opendir((*lastsep == PATH_SEP_CHAR) ? "/" : "/"); | ||
279 | |||
280 | if (*lastsep == PATH_SEP_CHAR) { | ||
281 | lastsep++; | ||
282 | } | ||
283 | } | ||
284 | else | ||
285 | { | ||
286 | *lastsep = '\0'; | ||
287 | dir = opendir(filename); | ||
288 | *lastsep = PATH_SEP_CHAR; | ||
289 | lastsep++; | ||
290 | } | ||
291 | |||
292 | if (dir == NULL) | ||
293 | { | ||
294 | *ptr = PATH_SEP_CHAR; | ||
295 | return; /* maybe dir doesn't exist? give up. */ | ||
296 | } | ||
297 | |||
298 | while ((dent = readdir(dir)) != NULL) | ||
299 | { | ||
300 | if (strcasecmp(dent->d_name, lastsep) == 0) | ||
301 | { | ||
302 | /* found match; replace it. */ | ||
303 | strcpy(lastsep, dent->d_name); | ||
304 | break; | ||
305 | } | ||
306 | } | ||
307 | |||
308 | closedir(dir); | ||
309 | *ptr = pch; | ||
310 | lastsep = ptr; | ||
311 | |||
312 | if (dent == NULL) | ||
313 | return; /* no match. oh well. */ | ||
314 | |||
315 | if (pch == '\0') /* eos? */ | ||
316 | return; | ||
317 | } | ||
318 | } | ||
319 | #endif | ||
320 | } | ||
321 | |||
322 | |||
323 | #if PLATFORM_DOS | ||
324 | /* no-op. */ | ||
325 | |||
326 | #elif PLATFORM_WIN32 | ||
327 | int _dos_findfirst(uint8_t *filename, int x, struct find_t *f) | ||
328 | { | ||
329 | int32_t rc = _findfirst(filename, &f->data); | ||
330 | f->handle = rc; | ||
331 | if (rc != -1) | ||
332 | { | ||
333 | strncpy(f->name, f->data.name, sizeof (f->name) - 1); | ||
334 | f->name[sizeof (f->name) - 1] = '\0'; | ||
335 | return(0); | ||
336 | } | ||
337 | return(1); | ||
338 | } | ||
339 | |||
340 | int _dos_findnext(struct find_t *f) | ||
341 | { | ||
342 | int rc = 0; | ||
343 | if (f->handle == -1) | ||
344 | return(1); /* invalid handle. */ | ||
345 | |||
346 | rc = _findnext(f->handle, &f->data); | ||
347 | if (rc == -1) | ||
348 | { | ||
349 | _findclose(f->handle); | ||
350 | f->handle = -1; | ||
351 | return(1); | ||
352 | } | ||
353 | |||
354 | strncpy(f->name, f->data.name, sizeof (f->name) - 1); | ||
355 | f->name[sizeof (f->name) - 1] = '\0'; | ||
356 | return(0); | ||
357 | } | ||
358 | |||
359 | #elif defined(PLATFORM_UNIX) || defined(PLATFORM_MACOSX) || defined(PLATFORM_ROCKBOX) | ||
360 | int _dos_findfirst(char *filename, int x, struct find_t *f) | ||
361 | { | ||
362 | char *ptr; | ||
363 | |||
364 | if (strlen(filename) >= sizeof (f->pattern)) | ||
365 | return(1); | ||
366 | |||
367 | strcpy(f->pattern, filename); | ||
368 | FixFilePath(f->pattern); | ||
369 | ptr = strrchr(f->pattern, PATH_SEP_CHAR); | ||
370 | |||
371 | if (ptr == NULL) | ||
372 | { | ||
373 | ptr = filename; | ||
374 | f->dir = opendir(CURDIR); | ||
375 | } | ||
376 | else | ||
377 | { | ||
378 | *ptr = '\0'; | ||
379 | f->dir = opendir(f->pattern); | ||
380 | memmove(f->pattern, ptr + 1, strlen(ptr + 1) + 1); | ||
381 | } | ||
382 | |||
383 | return(_dos_findnext(f)); | ||
384 | } | ||
385 | |||
386 | |||
387 | static int check_pattern_nocase(const char *x, const char *y) | ||
388 | { | ||
389 | if ((x == NULL) || (y == NULL)) | ||
390 | return(0); /* not a match. */ | ||
391 | |||
392 | while ((*x) && (*y)) | ||
393 | { | ||
394 | if (*x == '*') | ||
395 | { | ||
396 | x++; | ||
397 | while (*y != '\0') | ||
398 | { | ||
399 | if (toupper((int) *x) == toupper((int) *y)) | ||
400 | break; | ||
401 | y++; | ||
402 | } | ||
403 | } | ||
404 | |||
405 | else if (*x == '?') | ||
406 | { | ||
407 | if (*y == '\0') | ||
408 | return(0); /* anything but EOS is okay. */ | ||
409 | } | ||
410 | |||
411 | else | ||
412 | { | ||
413 | if (toupper((int) *x) != toupper((int) *y)) | ||
414 | return(0); /* not a match. */ | ||
415 | } | ||
416 | |||
417 | x++; | ||
418 | y++; | ||
419 | } | ||
420 | |||
421 | return(*x == *y); /* it's a match (both should be EOS). */ | ||
422 | } | ||
423 | |||
424 | int _dos_findnext(struct find_t *f) | ||
425 | { | ||
426 | struct dirent *dent; | ||
427 | |||
428 | if (f->dir == NULL) | ||
429 | return(1); /* no such dir or we're just done searching. */ | ||
430 | |||
431 | while ((dent = readdir(f->dir)) != NULL) | ||
432 | { | ||
433 | if (check_pattern_nocase(f->pattern, dent->d_name)) | ||
434 | { | ||
435 | if (strlen(dent->d_name) < sizeof (f->name)) | ||
436 | { | ||
437 | strcpy(f->name, dent->d_name); | ||
438 | return(0); /* match. */ | ||
439 | } | ||
440 | } | ||
441 | } | ||
442 | |||
443 | closedir(f->dir); | ||
444 | f->dir = NULL; | ||
445 | return(1); /* no match in whole directory. */ | ||
446 | } | ||
447 | #else | ||
448 | #error please define for your platform. | ||
449 | #endif | ||
450 | |||
451 | |||
452 | #if !PLATFORM_DOS | ||
453 | void _dos_getdate(struct dosdate_t *date) | ||
454 | { | ||
455 | time_t curtime = time(NULL); | ||
456 | struct tm *tm; | ||
457 | |||
458 | if (date == NULL) { | ||
459 | return; | ||
460 | } | ||
461 | |||
462 | memset(date, 0, sizeof(struct dosdate_t)); | ||
463 | |||
464 | rb->mktime(&tm); | ||
465 | date->day = tm->tm_mday; | ||
466 | date->month = tm->tm_mon + 1; | ||
467 | date->year = tm->tm_year + 1900; | ||
468 | date->dayofweek = tm->tm_wday + 1; | ||
469 | } | ||
470 | #endif | ||
471 | |||
472 | |||
473 | int FindDistance2D(int ix, int iy) | ||
474 | { | ||
475 | int t; | ||
476 | |||
477 | ix= abs(ix); /* absolute values */ | ||
478 | iy= abs(iy); | ||
479 | |||
480 | if (ix<iy) | ||
481 | { | ||
482 | int tmp = ix; | ||
483 | ix = iy; | ||
484 | iy = tmp; | ||
485 | } | ||
486 | |||
487 | t = iy + (iy>>1); | ||
488 | |||
489 | return (ix - (ix>>5) - (ix>>7) + (t>>2) + (t>>6)); | ||
490 | } | ||
491 | |||
492 | int FindDistance3D(int ix, int iy, int iz) | ||
493 | { | ||
494 | int t; | ||
495 | |||
496 | ix= abs(ix); /* absolute values */ | ||
497 | iy= abs(iy); | ||
498 | iz= abs(iz); | ||
499 | |||
500 | if (ix<iy) | ||
501 | { | ||
502 | int tmp = ix; | ||
503 | ix = iy; | ||
504 | iy = tmp; | ||
505 | } | ||
506 | |||
507 | if (ix<iz) | ||
508 | { | ||
509 | int tmp = ix; | ||
510 | ix = iz; | ||
511 | iz = tmp; | ||
512 | } | ||
513 | |||
514 | t = iy + iz; | ||
515 | |||
516 | return (ix - (ix>>4) + (t>>2) + (t>>3)); | ||
517 | } | ||
518 | #include "SDL.h" | ||
519 | void Error (int errorType, char *error, ...) | ||
520 | { | ||
521 | va_list argptr; | ||
522 | |||
523 | SDL_Quit(); | ||
524 | |||
525 | //FCS: http://duke3d.m-klein.com is obscolete :/ ! | ||
526 | /* | ||
527 | if(errorType==EXIT_FAILURE) | ||
528 | printf("ERROR: Please copy that screen and visit http://duke3d.m-klein.com for report:\n"); | ||
529 | else | ||
530 | printf("http://duke3d.m-klein.com\n"); | ||
531 | */ | ||
532 | |||
533 | |||
534 | va_start (argptr, error); | ||
535 | vprintf(error, argptr); | ||
536 | va_end (argptr); | ||
537 | |||
538 | //printf("Press any key to continue...\n"); | ||
539 | |||
540 | // FIX_00043: Nicer exit on error. Ask the user to hit a key on exits and error exits. | ||
541 | //getch(); | ||
542 | |||
543 | exit (errorType); | ||
544 | } | ||
545 | |||
546 | void write2disk(int line, char * cfilename, char *filename2write, char *message) | ||
547 | { | ||
548 | // usage: write2disk(__LINE__, __FILE__, "c:\temp\my_dbug_file.txt", uint8_t * msg); | ||
549 | |||
550 | int i, k=0; | ||
551 | char filename[2048]; | ||
552 | FILE *pFile; | ||
553 | |||
554 | for(i=0; cfilename[i]; i++) | ||
555 | { | ||
556 | if(cfilename[i]=='\\') | ||
557 | { | ||
558 | i++; | ||
559 | k = 0; | ||
560 | } | ||
561 | filename[k++]=(cfilename[i]=='.')?0:cfilename[i]; | ||
562 | } | ||
563 | pFile = fopen(filename2write,"a"); | ||
564 | fprintf(pFile,"%-4d %-5s %s", line, filename, message); | ||
565 | fclose(pFile); | ||
566 | } | ||
567 | |||
568 | int32 SafeOpenAppend (const char *_filename, int32 filetype) | ||
569 | { | ||
570 | int handle; | ||
571 | char filename[MAX_PATH]; | ||
572 | |||
573 | strncpy(filename, _filename, sizeof (filename)); | ||
574 | filename[sizeof (filename) - 1] = '\0'; | ||
575 | FixFilePath(filename); | ||
576 | |||
577 | #if (defined PLATFORM_WIN32) | ||
578 | handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_APPEND ); | ||
579 | #else | ||
580 | handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_APPEND , 0666); | ||
581 | #endif | ||
582 | |||
583 | if (handle == -1) | ||
584 | Error (EXIT_FAILURE, "Error opening for append %s: %s",filename,strerror(errno)); | ||
585 | |||
586 | return handle; | ||
587 | } | ||
588 | |||
589 | boolean SafeFileExists ( const char * _filename ) | ||
590 | { | ||
591 | char filename[MAX_PATH]; | ||
592 | strncpy(filename, _filename, sizeof (filename)); | ||
593 | filename[sizeof (filename) - 1] = '\0'; | ||
594 | FixFilePath(filename); | ||
595 | |||
596 | #if( defined PLATFORM_WIN32) | ||
597 | return(access(filename, 6) == 0); | ||
598 | #else | ||
599 | return(rb->file_exists(filename)); | ||
600 | #endif | ||
601 | } | ||
602 | |||
603 | |||
604 | int32 SafeOpenWrite (const char *_filename, int32 filetype) | ||
605 | { | ||
606 | int handle; | ||
607 | char filename[MAX_PATH]; | ||
608 | strncpy(filename, _filename, sizeof (filename)); | ||
609 | filename[sizeof (filename) - 1] = '\0'; | ||
610 | FixFilePath(filename); | ||
611 | |||
612 | #if (defined PLATFORM_WIN32) | ||
613 | handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_TRUNC ); | ||
614 | #else | ||
615 | handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_TRUNC | ||
616 | , 0666); | ||
617 | #endif | ||
618 | |||
619 | if (handle == -1) | ||
620 | Error (EXIT_FAILURE, "Error opening %s: %s",filename,strerror(errno)); | ||
621 | |||
622 | return handle; | ||
623 | } | ||
624 | |||
625 | |||
626 | |||
627 | |||
628 | int32 SafeOpenRead (const char *_filename, int32 filetype) | ||
629 | { | ||
630 | int handle; | ||
631 | char filename[MAX_PATH]; | ||
632 | strncpy(filename, _filename, sizeof (filename)); | ||
633 | filename[sizeof (filename) - 1] = '\0'; | ||
634 | FixFilePath(filename); | ||
635 | |||
636 | handle = open(filename,O_RDONLY | O_BINARY); | ||
637 | |||
638 | if (handle == -1) | ||
639 | Error (EXIT_FAILURE, "Error opening %s: %s",filename,strerror(errno)); | ||
640 | |||
641 | return handle; | ||
642 | } | ||
643 | |||
644 | |||
645 | void SafeRead (int32 handle, void *buffer, int32 count) | ||
646 | { | ||
647 | unsigned iocount; | ||
648 | |||
649 | while (count) | ||
650 | { | ||
651 | iocount = count > 0x8000 ? 0x8000 : count; | ||
652 | if (read (handle,buffer,iocount) != (int)iocount) | ||
653 | Error (EXIT_FAILURE, "File read failure reading %ld bytes",count); | ||
654 | buffer = (void *)( (byte *)buffer + iocount ); | ||
655 | count -= iocount; | ||
656 | } | ||
657 | } | ||
658 | |||
659 | |||
660 | void SafeWrite (int32 handle, void *buffer, int32 count) | ||
661 | { | ||
662 | unsigned iocount; | ||
663 | |||
664 | while (count) | ||
665 | { | ||
666 | iocount = count > 0x8000 ? 0x8000 : count; | ||
667 | if (write (handle,buffer,iocount) != (int)iocount) | ||
668 | Error (EXIT_FAILURE, "File write failure writing %ld bytes",count); | ||
669 | buffer = (void *)( (byte *)buffer + iocount ); | ||
670 | count -= iocount; | ||
671 | } | ||
672 | } | ||
673 | |||
674 | void SafeWriteString (int handle, char * buffer) | ||
675 | { | ||
676 | unsigned iocount; | ||
677 | |||
678 | iocount=strlen(buffer); | ||
679 | if (write (handle,buffer,iocount) != (int)iocount) | ||
680 | Error (EXIT_FAILURE, "File write string failure writing %s\n",buffer); | ||
681 | } | ||
682 | |||
683 | void *SafeMalloc (int32_t size) | ||
684 | { | ||
685 | void *ptr; | ||
686 | |||
687 | ptr = malloc(size); | ||
688 | |||
689 | if (!ptr) | ||
690 | Error (EXIT_FAILURE, "SafeMalloc failure for %lu bytes",size); | ||
691 | |||
692 | return ptr; | ||
693 | } | ||
694 | |||
695 | void SafeRealloc (void **x, int32 size) | ||
696 | { | ||
697 | void *ptr; | ||
698 | |||
699 | ptr = realloc(*x, size); | ||
700 | |||
701 | if (!ptr) | ||
702 | Error (EXIT_FAILURE, "SafeRealloc failure for %lu bytes",size); | ||
703 | |||
704 | *x = ptr; | ||
705 | } | ||
706 | |||
707 | void *SafeLevelMalloc (int32_t size) | ||
708 | { | ||
709 | void *ptr; | ||
710 | |||
711 | ptr = malloc(size); | ||
712 | |||
713 | if (!ptr) | ||
714 | Error (EXIT_FAILURE, "SafeLevelMalloc failure for %lu bytes",size); | ||
715 | |||
716 | return ptr; | ||
717 | } | ||
718 | |||
719 | void SafeFree (void * ptr) | ||
720 | { | ||
721 | if ( ptr == NULL ) | ||
722 | Error (EXIT_FAILURE, "SafeFree : Tried to free a freed pointer\n"); | ||
723 | |||
724 | free(ptr); | ||
725 | |||
726 | } | ||
727 | |||
728 | short SwapShort (short l) | ||
729 | { | ||
730 | byte b1,b2; | ||
731 | |||
732 | b1 = l&255; | ||
733 | b2 = (l>>8)&255; | ||
734 | |||
735 | return (b1<<8) + b2; | ||
736 | } | ||
737 | |||
738 | short KeepShort (short l) | ||
739 | { | ||
740 | return l; | ||
741 | } | ||
742 | |||
743 | |||
744 | int32_t Swapint32_t (int32_t l) | ||
745 | { | ||
746 | byte b1,b2,b3,b4; | ||
747 | |||
748 | b1 = l&255; | ||
749 | b2 = (l>>8)&255; | ||
750 | b3 = (l>>16)&255; | ||
751 | b4 = (l>>24)&255; | ||
752 | |||
753 | return ((int32_t)b1<<24) + ((int32_t)b2<<16) + ((int32_t)b3<<8) + b4; | ||
754 | } | ||
755 | |||
756 | int32_t Keepint32_t (int32_t l) | ||
757 | { | ||
758 | return l; | ||
759 | } | ||
760 | |||
761 | |||
762 | #undef KeepShort | ||
763 | #undef KeepLong | ||
764 | #undef SwapShort | ||
765 | #undef SwapLong | ||
766 | |||
767 | void SwapIntelLong(int32_t *l) | ||
768 | { | ||
769 | *l = IntelLong(*l); | ||
770 | } | ||
771 | |||
772 | void SwapIntelShort(short *s) | ||
773 | { | ||
774 | *s = IntelShort(*s); | ||
775 | } | ||
776 | |||
777 | void SwapIntelLongArray(int32_t *l, int num) | ||
778 | { | ||
779 | while (num--) { | ||
780 | SwapIntelLong(l); | ||
781 | l++; | ||
782 | } | ||
783 | } | ||
784 | |||
785 | void SwapIntelShortArray(short *s, int num) | ||
786 | { | ||
787 | while (num--) { | ||
788 | SwapIntelShort(s); | ||
789 | s++; | ||
790 | } | ||
791 | } | ||
792 | |||
793 | |||
794 | /* | ||
795 | Copied over from Wolf3D Linux: http://www.icculus.org/wolf3d/ | ||
796 | Modified for ROTT. | ||
797 | Stolen for Duke3D, too. | ||
798 | */ | ||
799 | |||
800 | #if PLATFORM_UNIX | ||
801 | uint8_t *strlwr(uint8_t *s) | ||
802 | { | ||
803 | uint8_t *p = s; | ||
804 | |||
805 | while (*p) { | ||
806 | *p = tolower(*p); | ||
807 | p++; | ||
808 | } | ||
809 | |||
810 | return s; | ||
811 | } | ||
812 | |||
813 | uint8_t *strupr(uint8_t *s) | ||
814 | { | ||
815 | uint8_t *p = s; | ||
816 | |||
817 | while (*p) { | ||
818 | *p = toupper(*p); | ||
819 | p++; | ||
820 | } | ||
821 | |||
822 | return s; | ||
823 | } | ||
824 | |||
825 | uint8_t *itoa(int value, uint8_t *string, int radix) | ||
826 | { | ||
827 | switch (radix) { | ||
828 | case 10: | ||
829 | sprintf(string, "%d", value); | ||
830 | break; | ||
831 | case 16: | ||
832 | sprintf(string, "%x", value); | ||
833 | break; | ||
834 | default: | ||
835 | STUBBED("unknown radix"); | ||
836 | break; | ||
837 | } | ||
838 | |||
839 | return string; | ||
840 | } | ||
841 | |||
842 | uint8_t *ltoa(int32_t value, uint8_t *string, int radix) | ||
843 | { | ||
844 | switch (radix) { | ||
845 | case 10: | ||
846 | sprintf(string, "%d", value); | ||
847 | break; | ||
848 | case 16: | ||
849 | sprintf(string, "%x", value); | ||
850 | break; | ||
851 | default: | ||
852 | STUBBED("unknown radix"); | ||
853 | break; | ||
854 | } | ||
855 | |||
856 | return string; | ||
857 | } | ||
858 | |||
859 | uint8_t *ultoa(uint32_t value, uint8_t *string, int radix) | ||
860 | { | ||
861 | switch (radix) { | ||
862 | case 10: | ||
863 | sprintf(string, "%u", value); | ||
864 | break; | ||
865 | case 16: | ||
866 | sprintf(string, "%ux", value); | ||
867 | break; | ||
868 | default: | ||
869 | STUBBED("unknown radix"); | ||
870 | break; | ||
871 | } | ||
872 | |||
873 | return string; | ||
874 | } | ||
875 | #endif | ||
876 | |||
877 | char ApogeePath[256]; | ||
878 | |||
879 | int setup_homedir (void) | ||
880 | { | ||
881 | #if PLATFORM_UNIX | ||
882 | int err; | ||
883 | |||
884 | snprintf (ApogeePath, sizeof (ApogeePath), "%s/.duke3d/", getenv ("HOME")); | ||
885 | |||
886 | //err = mkdir (ApogeePath, S_IRWXU); | ||
887 | err = mkdir (ApogeePath); | ||
888 | if (err == -1 && errno != EEXIST) | ||
889 | { | ||
890 | fprintf (stderr, "Couldn't create preferences directory: %s\n", | ||
891 | strerror (errno)); | ||
892 | return -1; | ||
893 | } | ||
894 | #else | ||
895 | sprintf(ApogeePath, ".%s", PATH_SEP_STR); | ||
896 | #endif | ||
897 | |||
898 | return 0; | ||
899 | } | ||
900 | |||
901 | |||
902 | uint8_t CheckParm (char *check) | ||
903 | { | ||
904 | int i; | ||
905 | for (i = 1; i < _argc; i++) | ||
906 | { | ||
907 | if ((*(_argv[i]) == '-') && (strcmpi(_argv[i] + 1, check) == 0)) | ||
908 | return(i); | ||
909 | } | ||
910 | |||
911 | return(0); | ||
912 | } | ||
913 | |||
914 | |||
915 | static void (*shutdown_func)(void) = NULL; | ||
916 | |||
917 | void RegisterShutdownFunction( void (* shutdown) (void) ) | ||
918 | { | ||
919 | shutdown_func = shutdown; | ||
920 | } | ||
921 | |||
922 | void Shutdown(void) | ||
923 | { | ||
924 | if (shutdown_func != NULL) | ||
925 | { | ||
926 | shutdown_func(); | ||
927 | shutdown_func = NULL; | ||
928 | } | ||
929 | } | ||
930 | |||
931 | |||