1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
26 =============================================================================
\r
28 Id Software Caching Manager
\r
29 ---------------------------
\r
31 Must be started BEFORE the memory manager, because it needs to get the headers
\r
32 loaded into the data segment
\r
34 =============================================================================
\r
37 #include "src/lib/16_ca.h"
\r
43 #define THREEBYTEGRSTARTS
\r
44 //https://github.com/open-watcom/open-watcom-v2/issues/279#issuecomment-244587566 for _seg
\r
45 //http://www.shikadi.net/moddingwiki/GameMaps_Format for info on the code
\r
47 =============================================================================
\r
51 =============================================================================
\r
56 word bit0,bit1; // 0-255 is a character, > is a pointer to a node
\r
63 long headeroffsets[100];
\r
69 =============================================================================
\r
73 =============================================================================
\r
79 unsigned _seg *mapsegs[3];
\r
80 maptype _seg *mapheaderseg[NUMMAPS];
\r
81 byte _seg *audiosegs[NUMSNDCHUNKS];
\r
82 void _seg *grsegs[NUMCHUNKS];
\r
84 byte far grneeded[NUMCHUNKS];*/
\r
86 void (*drawcachebox) (char *title, unsigned numcache);
\r
87 void (*updatecachebox) (void);
\r
88 void (*finishcachebox) (void);
\r
91 =============================================================================
\r
95 =============================================================================
\r
98 /*extern long far CGAhead;
\r
99 extern long far EGAhead;
\r
100 extern byte CGAdict;
\r
101 extern byte EGAdict;
\r
102 extern byte far maphead;
\r
103 extern byte mapdict;
\r
104 extern byte far audiohead;
\r
105 extern byte audiodict;*/
\r
107 void CA_CannotOpen(char *string, global_game_variables_t *gvar);
\r
109 /*long _seg *grstarts; // array of offsets in egagraph, -1 for sparse
\r
110 long _seg *audiostarts; // array of offsets in audio / audiot
\r
112 #ifdef GRHEADERLINKED
\r
113 huffnode *gvar->ca.grhuffman;
\r
115 huffnode gvar->ca.grhuffman[255];
\r
118 #ifdef AUDIOHEADERLINKED
\r
119 huffnode *audiohuffman;
\r
121 huffnode audiohuffman[255];
\r
125 int grhandle; // handle to EGAGRAPH
\r
126 int maphandle; // handle to MAPTEMP / GAMEMAPS
\r
127 int audiohandle; // handle to AUDIOT / AUDIO
\r
129 long chunkcomplen,chunkexplen;
\r
131 SDMode oldsoundmode;
\r
135 void CAL_DialogDraw (char *title,unsigned numcache);
\r
136 void CAL_DialogUpdate (void);
\r
137 void CAL_DialogFinish (void);*/
\r
138 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest,
\r
142 #ifdef THREEBYTEGRSTARTS
\r
143 #define FILEPOSSIZE 3
\r
144 //#define GRFILEPOS(c) (*(long far *)(((byte far *)grstarts)+(c)*3)&0xffffff)
\r
145 CASVT GRFILEPOS(int c, global_game_variables_t *gvar)
\r
152 value = *(CASVT far *)(((byte far *)gvar->ca.grstarts)+offset);
\r
154 value &= 0x00ffffffl;
\r
156 if (value == 0xffffffl)
\r
162 #define FILEPOSSIZE 4
\r
163 //#define GRFILEPOS(c) (gvar->ca.grstarts[c])
\r
164 CASVT GRFILEPOS(int c, global_game_variables_t *gvar)
\r
166 return gvar->ca.grstarts[c];
\r
170 //#define EXTENSION "hb1"
\r
173 =============================================================================
\r
177 =============================================================================
\r
181 ============================
\r
183 = CA_OpenDebug / CA_CloseDebug
\r
185 = Opens a binary file with the handle "debughandle"
\r
187 ============================
\r
189 void CA_OpenDebug(global_game_variables_t *gvar)
\r
191 #ifdef __BORLANDC__
\r
192 unlink("debug.16b");
\r
193 gvar->handle.debughandle = open("debug.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
196 unlink("debug.16w");
\r
197 gvar->handle.debughandle = open("debug.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
201 void CA_CloseDebug(global_game_variables_t *gvar)
\r
203 close(gvar->handle.debughandle);
\r
209 ============================
\r
211 = CAL_GetGrChunkLength
\r
213 = Gets the length of an explicit length chunk (not tiles)
\r
214 = The file pointer is positioned so the compressed data can be read in next.
\r
216 ============================
\r
219 void CAL_GetGrChunkLength (int chunk,global_game_variables_t *gvar)
\r
221 lseek(gvar->ca.file.grhandle,GRFILEPOS(chunk,gvar),SEEK_SET);
\r
222 read(gvar->ca.file.grhandle,&gvar->ca.chunkexplen,sizeof(gvar->ca.chunkexplen));
\r
223 gvar->ca.chunkcomplen = GRFILEPOS(chunk+1,gvar)-GRFILEPOS(chunk,gvar)-4;
\r
228 ==========================
\r
232 = Read from a file to a far pointer
\r
234 ==========================
\r
237 boolean CA_FarRead(int handle, byte far *dest, dword length, global_game_variables_t *gvar)
\r
240 if (length>0xfffflu)
\r
241 Quit (gvar, "CA_FarRead doesn't support 64K reads yet!");//TODO: EXPAND!!!
\r
246 mov cx,[WORD PTR length]
\r
247 mov dx,[WORD PTR dest]
\r
248 mov ds,[WORD PTR dest+2]
\r
249 mov ah,0x3f // READ w/handle
\r
256 #ifdef __BORLANDC__
\r
260 #ifdef __BORLANDC__
\r
263 cmp ax,[WORD PTR length]
\r
265 // errno = EINVFMT; // user manager knows this is bad read
\r
268 #ifdef __BORLANDC__
\r
272 #ifdef __BORLANDC__
\r
276 #ifdef __BORLANDC__
\r
288 ==========================
\r
292 = Write from a file to a far pointer
\r
294 ==========================
\r
297 boolean CA_FarWrite(int handle, byte far *source, dword length, global_game_variables_t *gvar)
\r
300 if (length>0xfffflu)
\r
301 Quit (gvar, "CA_FarWrite doesn't support 64K reads yet!");//TODO: EXPAND!!!
\r
306 mov cx,[WORD PTR length]
\r
307 mov dx,[WORD PTR source]
\r
308 mov ds,[WORD PTR source+2]
\r
309 mov ah,0x40 // WRITE w/handle
\r
316 #ifdef __BORLANDC__
\r
320 #ifdef __BORLANDC__
\r
323 cmp ax,[WORD PTR length]
\r
325 // errno = ENOMEM; // user manager knows this is bad write
\r
328 #ifdef __BORLANDC__
\r
332 #ifdef __BORLANDC__
\r
336 #ifdef __BORLANDC__
\r
348 ==========================
\r
352 = Reads a file into an allready allocated buffer
\r
354 ==========================
\r
357 boolean CA_ReadFile(char *filename, memptr *ptr, global_game_variables_t *gvar)
\r
363 if((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
366 size = filelength(handle);
\r
367 #ifdef __DEBUG_CA__
\r
369 printf("===============================================================================\n");
\r
370 printf(" CA_ReadFile\n");
\r
371 printf("===============================================================================\n");
\r
373 printf(" ptr=%Fp\n", ptr);
\r
374 printf(" *ptr=%Fp\n", *ptr);
\r
375 printf(" &ptr=%Fp\n", &ptr);
\r
378 if(!CA_FarRead(handle,*ptr,size, gvar))
\r
389 ==========================
\r
393 = Writes a file from a memory buffer
\r
395 ==========================
\r
398 boolean CA_WriteFile (char *filename, void far *ptr, long length, global_game_variables_t *gvar)
\r
404 handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
\r
405 S_IREAD | S_IWRITE | S_IFREG);
\r
410 if (!CA_FarWrite (handle,ptr,length, gvar))
\r
422 ==========================
\r
426 = Allocate space for and load a file
\r
428 ==========================
\r
431 boolean CA_LoadFile(char *filename, memptr *ptr, global_game_variables_t *gvar)
\r
437 if((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
440 size = filelength(handle);
\r
441 #ifdef __DEBUG_CA__
\r
443 printf("===============================================================================\n");
\r
444 printf(" CA_LoadFile\n");
\r
445 printf("===============================================================================\n");
\r
447 printf(" ptr=%Fp\n", ptr);
\r
448 printf(" *ptr=%Fp\n", *ptr);
\r
449 printf(" &ptr=%Fp\n", &ptr);
\r
452 MM_GetPtr(ptr,size, gvar);
\r
453 #ifdef __DEBUG_CA__
\r
456 printf("---------------------------------------\n");
\r
457 printf(" ptr=%Fp\n", ptr);
\r
458 printf(" *ptr=%Fp\n", *ptr);
\r
459 printf(" &ptr=%Fp\n", &ptr);
\r
460 printf("-------------------------------------------------------------------------------\n");
\r
463 if(!CA_FarRead(handle,*ptr,size, gvar))
\r
473 ============================================================================
\r
475 COMPRESSION routines, see JHUFF.C for more
\r
477 ============================================================================
\r
485 = CAL_OptimizeNodes
\r
487 = Goes through a huffman table and changes the 256-511 node numbers to the
\r
488 = actular address of the node. Must be called before CAL_HuffExpand
\r
493 void CAL_OptimizeNodes(huffnode *table)
\r
500 for (i=0;i<255;i++)
\r
502 if (node->bit0 >= 256)
\r
503 node->bit0 = (unsigned)(table+(node->bit0-256));
\r
504 if (node->bit1 >= 256)
\r
505 node->bit1 = (unsigned)(table+(node->bit1-256));
\r
513 ======================
\r
517 = Length is the length of the EXPANDED data
\r
519 ======================
\r
522 void CAL_HuffExpand (byte far *source, byte far *dest,
\r
523 long length,huffnode *hufftable)
\r
525 // unsigned bit,byte,node,code;
\r
526 unsigned sourceseg,sourceoff,destseg,destoff,endoff;
\r
528 // huffnode *nodeon;
\r
530 headptr = hufftable+254; // head node is allways node 254
\r
532 source++; // normalize
\r
537 sourceseg = FP_SEG(source);
\r
538 sourceoff = FP_OFF(source);
\r
539 destseg = FP_SEG(dest);
\r
540 destoff = FP_OFF(dest);
\r
541 endoff = destoff+length;
\r
546 // ss:bx node pointer
\r
549 if (length <0xfff0)
\r
552 //--------------------------
\r
553 // expand less than 64k of data
\r
554 //--------------------------
\r
557 mov bx,[word ptr headptr]
\r
565 mov ch,[si] // load first byte
\r
568 #ifdef __BORLANDC__
\r
572 #ifdef __BORLANDC__
\r
575 test ch,cl // bit set?
\r
577 mov dx,[ss:bx] // take bit0 path from node
\r
578 shl cl,1 // advance to next bit position
\r
581 #ifdef __BORLANDC__
\r
585 #ifdef __BORLANDC__
\r
588 mov dx,[ss:bx+2] // take bit1 path
\r
589 shl cl,1 // advance to next bit position
\r
591 #ifdef __BORLANDC__
\r
595 #ifdef __BORLANDC__
\r
598 mov ch,[si] // load next byte
\r
600 mov cl,1 // back to first bit
\r
601 #ifdef __BORLANDC__
\r
605 #ifdef __BORLANDC__
\r
608 or dh,dh // if dx<256 its a byte, else move node
\r
610 mov bx,dx // next node = (huffnode *)code
\r
612 #ifdef __BORLANDC__
\r
616 #ifdef __BORLANDC__
\r
620 inc di // write a decopmpressed byte out
\r
621 mov bx,[word ptr headptr] // back to the head node for next bit
\r
630 //--------------------------
\r
631 // expand more than 64k of data
\r
632 //--------------------------
\r
637 mov bx,[word ptr headptr]
\r
645 lodsb // load first byte
\r
646 #ifdef __BORLANDC__
\r
650 #ifdef __BORLANDC__
\r
653 test al,cl // bit set?
\r
655 mov dx,[ss:bx] // take bit0 path from node
\r
657 #ifdef __BORLANDC__
\r
661 #ifdef __BORLANDC__
\r
664 mov dx,[ss:bx+2] // take bit1 path
\r
665 #ifdef __BORLANDC__
\r
669 #ifdef __BORLANDC__
\r
672 shl cl,1 // advance to next bit position
\r
675 cmp si,0x10 // normalize ds:si
\r
681 #ifdef __BORLANDC__
\r
685 #ifdef __BORLANDC__
\r
688 mov cl,1 // back to first bit
\r
689 #ifdef __BORLANDC__
\r
693 #ifdef __BORLANDC__
\r
696 or dh,dh // if dx<256 its a byte, else move node
\r
698 mov bx,dx // next node = (huffnode *)code
\r
700 #ifdef __BORLANDC__
\r
704 #ifdef __BORLANDC__
\r
708 inc di // write a decopmpressed byte out
\r
709 mov bx,[word ptr headptr] // back to the head node for next bit
\r
711 cmp di,0x10 // normalize es:di
\r
717 #ifdef __BORLANDC__
\r
721 #ifdef __BORLANDC__
\r
724 sub [WORD PTR ss:length],1
\r
726 dec [WORD PTR ss:length+2]
\r
727 jns expand // when length = ffff ffff, done
\r
740 ======================
\r
742 = CAL_CarmackExpand
\r
744 = Length is the length of the EXPANDED data
\r
746 ======================
\r
749 #define NEARTAG 0xa7
\r
750 #define FARTAG 0xa8
\r
752 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest, unsigned length)
\r
754 unsigned ch,chhigh,count,offset;
\r
755 unsigned far *copyptr, far *inptr, far *outptr;
\r
766 if (chhigh == NEARTAG)
\r
770 { // have to insert a word containing the tag byte
\r
771 ch |= *(/*(unsigned char far *)*/inptr)++;
\r
777 offset = *(/*(unsigned char far *)*/inptr)++;
\r
778 copyptr = outptr - offset;
\r
781 *outptr++ = *copyptr++;
\r
784 else if (chhigh == FARTAG)
\r
788 { // have to insert a word containing the tag byte
\r
789 ch |= *(/*(unsigned char far *)*/inptr)++;
\r
796 copyptr = dest + offset;
\r
799 *outptr++ = *copyptr++;
\r
812 ======================
\r
816 ======================
\r
819 long CA_RLEWCompress (unsigned far *source, long length, unsigned far *dest,
\r
823 unsigned value,count,i;
\r
824 unsigned far *start,far *end;
\r
828 end = source + (length+1)/2;
\r
837 while (*source == value && source<end)
\r
842 if (count>3 || value == rlewtag)
\r
845 // send a tag / count / value string
\r
854 // send word without compressing
\r
856 for (i=1;i<=count;i++)
\r
860 } while (source<end);
\r
862 complength = 2*(dest-start);
\r
868 ======================
\r
871 = length is EXPANDED length
\r
873 ======================
\r
876 void CA_RLEWexpand (unsigned far *source, unsigned far *dest,long length,
\r
879 // unsigned value,count,i;
\r
881 unsigned sourceseg,sourceoff,destseg,destoff,endseg,endoff;
\r
891 if (value != rlewtag)
\r
899 // compressed string
\r
903 for (i=1;i<=count;i++)
\r
906 } while (dest<end);
\r
909 end = dest + (length)/2;
\r
910 sourceseg = FP_SEG(source);
\r
911 sourceoff = FP_OFF(source);
\r
912 destseg = FP_SEG(dest);
\r
913 destoff = FP_OFF(dest);
\r
914 endseg = FP_SEG(end);
\r
915 endoff = FP_OFF(end);
\r
919 // ax = source value
\r
921 // cx = repeat counts
\r
924 // NOTE: A repeat count that produces 0xfff0 bytes can blow this!
\r
932 #ifdef __BORLANDC__
\r
936 #ifdef __BORLANDC__
\r
944 #ifdef __BORLANDC__
\r
948 #ifdef __BORLANDC__
\r
952 mov cx,ax // repeat count
\r
953 lodsw // repeat value
\r
955 #ifdef __BORLANDC__
\r
959 #ifdef __BORLANDC__
\r
962 cmp si,0x10 // normalize ds:si
\r
973 #ifdef __BORLANDC__
\r
977 #ifdef __BORLANDC__
\r
980 cmp di,0x10 // normalize es:di
\r
991 #ifdef __BORLANDC__
\r
995 #ifdef __BORLANDC__
\r
1011 =============================================================================
\r
1013 CACHE MANAGER ROUTINES
\r
1015 =============================================================================
\r
1019 ======================
\r
1023 ======================
\r
1026 void CAL_SetupGrFile (global_game_variables_t *gvar)
\r
1034 #ifdef GRHEADERLINKED
\r
1036 gvar->ca.grhuffman = (huffnode *)&VGAdict;
\r
1037 grstarts = (long _seg *)FP_SEG(&VGAhead);
\r
1039 CAL_OptimizeNodes (gvar->ca.grhuffman);
\r
1044 // load ???dict.ext (huffman dictionary for graphics files)
\r
1047 strcpy(fname,GDICTNAME);
\r
1048 strcat(fname,"hb1");
\r
1050 if ((handle = open(fname,
\r
1051 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1052 CA_CannotOpen(fname,gvar);
\r
1054 read(handle, &gvar->ca.grhuffman, sizeof(gvar->ca.grhuffman));
\r
1056 CAL_OptimizeNodes (gvar->ca.grhuffman);
\r
1058 // load the data offsets from ???head.ext
\r
1060 MM_GetPtr (MEMPTRCONV gvar->ca.grstarts,(NUMCHUNKS+1)*FILEPOSSIZE, gvar);
\r
1062 strcpy(fname,GHEADNAME);
\r
1063 strcat(fname,"hb1");
\r
1065 if ((handle = open(fname,
\r
1066 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1067 CA_CannotOpen(fname,gvar);
\r
1069 CA_FarRead(handle, (memptr)gvar->ca.grstarts, (NUMCHUNKS+1)*FILEPOSSIZE, gvar);
\r
1077 // Open the graphics file, leaving it open until the game is finished
\r
1079 strcpy(fname,GFILENAME);
\r
1080 strcat(fname,"hb1");
\r
1082 gvar->ca.file.grhandle = open(fname, O_RDONLY | O_BINARY);
\r
1083 if (gvar->ca.file.grhandle == -1)
\r
1084 CA_CannotOpen(fname,gvar);
\r
1088 // load the pic and sprite headers into the arrays in the data segment
\r
1091 MM_GetPtr(MEMPTRCONV gvar->video.pictable,NUMPICS*sizeof(pictabletype),gvar);
\r
1092 CAL_GetGrChunkLength(STRUCTPIC,gvar); // position file pointer
\r
1093 printf("CAL_SetupGrFile:\n");
\r
1094 printf(" gvar->ca.chunkcomplen size is %lu\n", gvar->ca.chunkcomplen);
\r
1095 MM_GetPtr(MEMPTRANDPERCONV compseg,gvar->ca.chunkcomplen,gvar); IN_Ack(gvar);
\r
1096 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen,gvar);
\r
1097 CAL_HuffExpand (compseg, (byte far *)gvar->video.pictable,NUMPICS*sizeof(pictabletype),gvar->ca.grhuffman);
\r
1098 MM_FreePtr(MEMPTRANDPERCONV compseg,gvar);
\r
1103 MM_GetPtr(MEMPTRCONV picmtable,NUMPICM*sizeof(pictabletype));
\r
1104 CAL_GetGrChunkLength(STRUCTPICM); // position file pointer
\r
1105 MM_GetPtr(&compseg,gvar->ca.chunkcomplen);
\r
1106 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen);
\r
1107 CAL_HuffExpand (compseg, (byte far *)picmtable,NUMPICS*sizeof(pictabletype),gvar->ca.grhuffman);
\r
1108 MM_FreePtr(&compseg);
\r
1111 //#if NUMSPRITES>0
\r
1112 MM_GetPtr(MEMPTRCONV spritetable,NUMSPRITES*sizeof(spritetabletype));
\r
1113 CAL_GetGrChunkLength(STRUCTSPRITE); // position file pointer
\r
1114 MM_GetPtr(&compseg,gvar->ca.chunkcomplen);
\r
1115 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen);
\r
1116 CAL_HuffExpand (compseg, (byte far *)spritetable,NUMSPRITES*sizeof(spritetabletype),gvar->ca.grhuffman);
\r
1117 MM_FreePtr(&compseg);
\r
1122 //==========================================================================
\r
1126 ======================
\r
1128 = CAL_SetupMapFile
\r
1130 ======================
\r
1133 void CAL_SetupMapFile (global_game_variables_t *gvar)
\r
1135 #ifndef MAPHEADERLINKED
\r
1141 // load maphead.ext (offsets and tileinfo for map file)
\r
1143 #ifndef MAPHEADERLINKED
\r
1144 if ((handle = open("maphead.mph",
\r
1145 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1146 Quit (gvar, "Can't open maphead.mph");
\r
1147 length = filelength(handle);
\r
1148 MM_GetPtr (MEMPTRCONV gvar->ca.tinf,length,gvar);
\r
1149 CA_FarRead(handle, gvar->ca.tinf, length,gvar);
\r
1153 gvar->ca.tinf = (byte _seg *)FP_SEG(&maphead);
\r
1158 // open the data file
\r
1160 //TODO: multiple files
\r
1161 if ((gvar->ca.file.maphandle = open("data/test.map",
\r
1162 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1163 Quit (gvar, "Can't open data/test.map!");
\r
1164 /*#ifdef MAPHEADERLINKED
\r
1165 if ((maphandle = open("GAMEMAPS.16"ENSION,
\r
1166 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1167 Quit (gvar, "Can't open GAMEMAPS.16"ENSION"!");
\r
1169 if ((maphandle = open("MAPTEMP.16"ENSION,
\r
1170 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1171 Quit (gvar, "Can't open MAPTEMP.16"ENSION"!");
\r
1175 //==========================================================================
\r
1179 ======================
\r
1181 = CAL_SetupAudioFile
\r
1183 ======================
\r
1186 /*void CAL_SetupAudioFile (void)
\r
1192 // load maphead.ext (offsets and tileinfo for map file)
\r
1194 #ifndef AUDIOHEADERLINKED
\r
1195 if ((handle = open("AUDIOHED.16",
\r
1196 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1197 Quit (gvar, "Can't open AUDIOHED.16""!");
\r
1198 length = filelength(handle);
\r
1199 MM_GetPtr (MEMPTRCONV audiostarts,length);
\r
1200 CA_FarRead(handle, (byte far *)audiostarts, length);
\r
1203 audiohuffman = (huffnode *)&audiodict;
\r
1204 CAL_OptimizeNodes (audiohuffman);
\r
1205 audiostarts = (long _seg *)FP_SEG(&audiohead);
\r
1209 // open the data file
\r
1211 #ifndef AUDIOHEADERLINKED
\r
1212 if ((audiohandle = open("AUDIOT.16",
\r
1213 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1214 Quit (gvar, "Can't open AUDIOT.16""!");
\r
1216 if ((audiohandle = open("AUDIO.16",
\r
1217 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1218 Quit (gvar, "Can't open AUDIO.16""!");
\r
1222 //==========================================================================
\r
1226 ======================
\r
1230 = Open all files and load in headers
\r
1232 ======================
\r
1235 void CA_Startup(global_game_variables_t *gvar)
\r
1238 #ifdef __BORLANDC__
\r
1239 unlink("profile.16b");
\r
1240 gvar->handle.profilehandle = open("profile.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
1242 #ifdef __WATCOMC__
\r
1243 unlink("profile.16w");
\r
1244 gvar->handle.profilehandle = open("profile.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
1248 #ifdef SHOWMEMINFO
\r
1249 #ifdef __BORLANDC__
\r
1250 unlink("meminfo.16b");
\r
1251 gvar->handle.showmemhandle = open("meminfo.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
1253 #ifdef __WATCOMC__
\r
1254 unlink("meminfo.16w");
\r
1255 gvar->handle.showmemhandle = open("meminfo.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
1261 CAL_SetupMapFile (gvar);
\r
1263 #ifndef NOGRAPHICS
\r
1264 CAL_SetupGrFile (gvar);
\r
1267 CAL_SetupMapFile (gvar);
\r
1270 gvar->ca.mapon = -1;
\r
1271 gvar->ca.ca_levelbit = 1;
\r
1272 gvar->ca.ca_levelnum = 0;
\r
1274 /* drawcachebox = CAL_DialogDraw;
\r
1275 updatecachebox = CAL_DialogUpdate;
\r
1276 finishcachebox = CAL_DialogFinish;*/
\r
1279 //==========================================================================
\r
1283 ======================
\r
1287 = Closes all files
\r
1289 ======================
\r
1292 void CA_Shutdown(global_game_variables_t *gvar)
\r
1295 close(gvar->handle.profilehandle);
\r
1297 #ifdef SHOWMEMINFO
\r
1298 close(gvar->handle.showmemhandle);
\r
1301 close(gvar->ca.file.maphandle);
\r
1302 close(gvar->ca.file.grhandle);
\r
1303 close(gvar->ca.file.audiohandle);
\r
1306 //===========================================================================
\r
1309 ======================
\r
1311 = CA_CacheAudioChunk
\r
1313 ======================
\r
1316 void CA_CacheAudioChunk (int chunk)
\r
1318 long pos,compressed;
\r
1319 #ifdef AUDIOHEADERLINKED
\r
1321 memptr bigbufferseg;
\r
1325 if (audiosegs[chunk])
\r
1327 MM_SetPurge (MEMPTRCONV audiosegs[chunk],0);
\r
1328 return; // allready in memory
\r
1331 // MDM begin - (GAMERS EDGE)
\r
1333 if (!FindFile("AUDIO.16",NULL,2))
\r
1334 Quit (gvar, "CA_CacheAudioChunk(): Can't find audio files.");
\r
1339 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1340 // a larger buffer
\r
1342 pos = audiostarts[chunk];
\r
1343 compressed = audiostarts[chunk+1]-pos;
\r
1345 lseek(audiohandle,pos,SEEK_SET);
\r
1347 #ifndef AUDIOHEADERLINKED
\r
1349 MM_GetPtr (MEMPTRCONV audiosegs[chunk],compressed);
\r
1353 CA_FarRead(audiohandle,audiosegs[chunk],compressed);
\r
1357 if (compressed<=BUFFERSIZE)
\r
1359 CA_FarRead(audiohandle,bufferseg,compressed);
\r
1360 source = bufferseg;
\r
1364 MM_GetPtr(&bigbufferseg,compressed);
\r
1367 MM_SetLock (&bigbufferseg,true);
\r
1368 CA_FarRead(audiohandle,bigbufferseg,compressed);
\r
1369 source = bigbufferseg;
\r
1372 expanded = *(long far *)source;
\r
1373 source += 4; // skip over length
\r
1374 MM_GetPtr (MEMPTRCONV audiosegs[chunk],expanded);
\r
1377 CAL_HuffExpand (source,audiosegs[chunk],expanded,audiohuffman);
\r
1380 if (compressed>BUFFERSIZE)
\r
1381 MM_FreePtr(&bigbufferseg);
\r
1385 //===========================================================================
\r
1388 ======================
\r
1390 = CA_LoadAllSounds
\r
1392 = Purges all sounds, then loads all new ones (mode switch)
\r
1394 ======================
\r
1397 void CA_LoadAllSounds (void)
\r
1401 switch (oldsoundmode)
\r
1406 start = STARTPCSOUNDS;
\r
1409 start = STARTADLIBSOUNDS;
\r
1413 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1414 if (audiosegs[start])
\r
1415 MM_SetPurge (MEMPTRCONV audiosegs[start],3); // make purgable
\r
1419 switch (SoundMode)
\r
1424 start = STARTPCSOUNDS;
\r
1427 start = STARTADLIBSOUNDS;
\r
1431 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1432 CA_CacheAudioChunk (start);
\r
1434 oldsoundmode = SoundMode;
\r
1437 //===========================================================================
\r
1439 //????#if GRMODE == EGAGR
\r
1442 ======================
\r
1446 = Make a shifted (one byte wider) copy of a sprite into another area
\r
1448 ======================
\r
1451 unsigned static sheight,swidth;
\r
1452 boolean static dothemask;
\r
1454 void CAL_ShiftSprite (unsigned segment,unsigned source,unsigned dest,
\r
1455 unsigned width, unsigned height, unsigned pixshift, boolean domask, global_game_variables_t *gvar)
\r
1458 sheight = height; // because we are going to reassign bp
\r
1460 dothemask = domask;
\r
1464 mov ds,ax // source and dest are in same segment, and all local
\r
1471 mov bp,WORD PTR [shifttabletable+bp] // bp holds pointer to shift table
\r
1472 // mov bp,WORD PTR [gvar->video.shifttabletable+bp] // bp holds pointer to shift table
\r
1474 cmp [ss:dothemask],0
\r
1478 // table shift the mask
\r
1480 mov dx,[ss:sheight]
\r
1481 #ifdef __BORLANDC__
\r
1485 #ifdef __BORLANDC__
\r
1488 mov BYTE PTR [di],255 // 0xff first byte
\r
1489 mov cx,ss:[swidth]
\r
1490 #ifdef __BORLANDC__
\r
1494 #ifdef __BORLANDC__
\r
1497 mov al,[bx] // source
\r
1499 inc bx // next source byte
\r
1503 mov ax,[bp+si] // table shift into two bytes
\r
1505 and [di],al // and with first byte
\r
1507 mov [di],ah // replace next byte
\r
1511 inc di // the last shifted byte has 1s in it
\r
1514 #ifdef __BORLANDC__
\r
1518 #ifdef __BORLANDC__
\r
1522 // table shift the data
\r
1524 mov dx,ss:[sheight]
\r
1526 shl dx,1 // four planes of data
\r
1527 #ifdef __BORLANDC__
\r
1531 #ifdef __BORLANDC__
\r
1534 mov BYTE PTR [di],0 // 0 first byte
\r
1535 mov cx,ss:[swidth]
\r
1536 #ifdef __BORLANDC__
\r
1540 #ifdef __BORLANDC__
\r
1543 mov al,[bx] // source
\r
1544 inc bx // next source byte
\r
1548 mov ax,[bp+si] // table shift into two bytes
\r
1549 or [di],al // or with first byte
\r
1551 mov [di],ah // replace next byte
\r
1555 inc di // the last shifted byte has 0s in it
\r
1563 mov ax,ss // restore data segment
\r
1571 //===========================================================================
\r
1574 ======================
\r
1578 = Generate shifts and set up sprite structure for a given sprite
\r
1580 ======================
\r
1583 void CAL_CacheSprite (int chunk, byte far *compressed, global_game_variables_t *gvar)
\r
1586 unsigned shiftstarts[5];
\r
1587 unsigned smallplane,bigplane,expanded;
\r
1588 spritetabletype far *spr;
\r
1589 spritetype _seg *dest;
\r
1594 // CGA has no pel panning, so shifts are never needed
\r
1596 spr = &gvar->video.spritetable[chunk-STARTSPRITES];
\r
1597 smallplane = spr->width*spr->height;
\r
1598 MM_GetPtr (&grsegs[chunk],smallplane*2+MAXSHIFTS*6);
\r
1601 dest = (spritetype _seg *)grsegs[chunk];
\r
1602 dest->sourceoffset[0] = MAXSHIFTS*6; // start data after 3 unsigned tables
\r
1603 dest->planesize[0] = smallplane;
\r
1604 dest->width[0] = spr->width;
\r
1607 // expand the unshifted shape
\r
1609 CAL_HuffExpand (compressed, &dest->data[0],smallplane*2,gvar->ca.grhuffman);
\r
1614 //#if GRMODE == EGAGR
\r
1617 // calculate sizes
\r
1619 spr = &gvar->video.spritetable[chunk-STARTSPRITES];
\r
1620 smallplane = spr->width*spr->height;
\r
1621 bigplane = (spr->width+1)*spr->height;
\r
1623 shiftstarts[0] = MAXSHIFTS*6; // start data after 3 unsigned tables
\r
1624 shiftstarts[1] = shiftstarts[0] + smallplane*5; // 5 planes in a sprite
\r
1625 shiftstarts[2] = shiftstarts[1] + bigplane*5;
\r
1626 shiftstarts[3] = shiftstarts[2] + bigplane*5;
\r
1627 shiftstarts[4] = shiftstarts[3] + bigplane*5; // nothing ever put here
\r
1629 expanded = shiftstarts[spr->shifts];
\r
1630 MM_GetPtr (MEMPTRCONV gvar->ca.grsegs[chunk],expanded, gvar);
\r
1631 if (gvar->mm.mmerror)
\r
1633 dest = (spritetype _seg *)gvar->ca.grsegs[chunk];
\r
1636 // expand the unshifted shape
\r
1638 CAL_HuffExpand (compressed, &dest->data[0],smallplane*5,gvar->ca.grhuffman);
\r
1641 // make the shifts!
\r
1643 switch (spr->shifts)
\r
1648 dest->sourceoffset[i] = shiftstarts[0];
\r
1649 dest->planesize[i] = smallplane;
\r
1650 dest->width[i] = spr->width;
\r
1657 dest->sourceoffset[i] = shiftstarts[0];
\r
1658 dest->planesize[i] = smallplane;
\r
1659 dest->width[i] = spr->width;
\r
1663 dest->sourceoffset[i] = shiftstarts[1];
\r
1664 dest->planesize[i] = bigplane;
\r
1665 dest->width[i] = spr->width+1;
\r
1667 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1668 dest->sourceoffset[2],spr->width,spr->height,4,true,gvar);
\r
1672 dest->sourceoffset[0] = shiftstarts[0];
\r
1673 dest->planesize[0] = smallplane;
\r
1674 dest->width[0] = spr->width;
\r
1676 dest->sourceoffset[1] = shiftstarts[1];
\r
1677 dest->planesize[1] = bigplane;
\r
1678 dest->width[1] = spr->width+1;
\r
1679 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1680 dest->sourceoffset[1],spr->width,spr->height,2,true,gvar);
\r
1682 dest->sourceoffset[2] = shiftstarts[2];
\r
1683 dest->planesize[2] = bigplane;
\r
1684 dest->width[2] = spr->width+1;
\r
1685 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1686 dest->sourceoffset[2],spr->width,spr->height,4,true,gvar);
\r
1688 dest->sourceoffset[3] = shiftstarts[3];
\r
1689 dest->planesize[3] = bigplane;
\r
1690 dest->width[3] = spr->width+1;
\r
1691 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1692 dest->sourceoffset[3],spr->width,spr->height,6,true,gvar);
\r
1697 Quit (gvar, "CAL_CacheSprite: Bad shifts number!");
\r
1703 //===========================================================================
\r
1707 ======================
\r
1709 = CAL_ExpandGrChunk
\r
1711 = Does whatever is needed with a pointer to a compressed chunk
\r
1713 ======================
\r
1716 void CAL_ExpandGrChunk (int chunk, byte far *source)
\r
1721 if (chunk >= STARTTILE8 && chunk < STARTEXTERNS)
\r
1724 // expanded sizes of tile8/16/32 are implicit
\r
1727 #if GRMODE == EGAGR
\r
1729 #define MASKBLOCK 40
\r
1732 #if GRMODE == CGAGR
\r
1734 #define MASKBLOCK 32
\r
1737 if (chunk<STARTTILE8M) // tile 8s are all in one chunk!
\r
1738 expanded = BLOCK*NUMTILE8;
\r
1739 else if (chunk<STARTTILE16)
\r
1740 expanded = MASKBLOCK*NUMTILE8M;
\r
1741 else if (chunk<STARTTILE16M) // all other tiles are one/chunk
\r
1742 expanded = BLOCK*4;
\r
1743 else if (chunk<STARTTILE32)
\r
1744 expanded = MASKBLOCK*4;
\r
1745 else if (chunk<STARTTILE32M)
\r
1746 expanded = BLOCK*16;
\r
1748 expanded = MASKBLOCK*16;
\r
1753 // everything else has an explicit size longword
\r
1755 expanded = *(long far *)source;
\r
1756 source += 4; // skip over length
\r
1760 // allocate final space, decompress it, and free bigbuffer
\r
1761 // Sprites need to have shifts made and various other junk
\r
1763 if (chunk>=STARTSPRITES && chunk< STARTTILE8)
\r
1764 CAL_CacheSprite(chunk,source);
\r
1767 MM_GetPtr (&grsegs[chunk],expanded);
\r
1770 CAL_HuffExpand (source,grsegs[chunk],expanded,gvar->ca.grhuffman);
\r
1776 ======================
\r
1780 = Gets a chunk off disk, optimizing reads to general buffer
\r
1782 ======================
\r
1785 void CAL_ReadGrChunk (int chunk)
\r
1787 long pos,compressed;
\r
1788 memptr bigbufferseg;
\r
1793 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1794 // a larger buffer
\r
1796 pos = GRFILEPOS(chunk,gvar);
\r
1797 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1801 while (GRFILEPOS(next,gvar) == -1) // skip past any sparse tiles
\r
1804 compressed = GRFILEPOS(next,gvar)-pos;
\r
1806 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
1808 if (compressed<=BUFFERSIZE)
\r
1810 CA_FarRead(gvar->ca.file.grhandle,bufferseg,compressed);
\r
1811 source = bufferseg;
\r
1815 MM_GetPtr(&bigbufferseg,compressed);
\r
1818 MM_SetLock (&bigbufferseg,true);
\r
1819 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed);
\r
1820 source = bigbufferseg;
\r
1823 CAL_ExpandGrChunk (chunk,source);
\r
1825 if (compressed>BUFFERSIZE)
\r
1826 MM_FreePtr(&bigbufferseg);
\r
1830 ======================
\r
1834 = Makes sure a given chunk is in memory, loadiing it if needed
\r
1836 ======================
\r
1839 void CA_CacheGrChunk (int chunk)
\r
1841 long pos,compressed;
\r
1842 memptr bigbufferseg;
\r
1846 gvar->ca.grneeded[chunk] |= ca_levelbit; // make sure it doesn't get removed
\r
1847 if (grsegs[chunk])
\r
1849 MM_SetPurge (&grsegs[chunk],0);
\r
1850 return; // allready in memory
\r
1853 // MDM begin - (GAMERS EDGE)
\r
1855 if (!FindFile("EGAGRAPH.16",NULL,2))
\r
1856 Quit (gvar, "CA_CacheGrChunk(): Can't find graphics files.");
\r
1861 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1862 // a larger buffer
\r
1864 pos = GRFILEPOS(chunk,gvar);
\r
1865 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1869 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1872 compressed = GRFILEPOS(next,gvar)-pos;
\r
1874 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
1876 if (compressed<=BUFFERSIZE)
\r
1878 CA_FarRead(gvar->ca.file.grhandle,bufferseg,compressed);
\r
1879 source = bufferseg;
\r
1883 MM_GetPtr(&bigbufferseg,compressed);
\r
1884 MM_SetLock (&bigbufferseg,true);
\r
1885 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed);
\r
1886 source = bigbufferseg;
\r
1889 CAL_ExpandGrChunk (chunk,source);
\r
1891 if (compressed>BUFFERSIZE)
\r
1892 MM_FreePtr (MEMPTRCONV bigbufferseg);
\r
1897 //==========================================================================
\r
1900 ======================
\r
1904 ======================
\r
1907 void CA_CacheMap (global_game_variables_t *gvar)
\r
1909 long pos,compressed;
\r
1911 memptr *dest,bigbufferseg;
\r
1913 unsigned far *source;
\r
1914 #ifdef MAPHEADERLINKED
\r
1915 memptr buffer2seg;
\r
1921 // free up memory from last map
\r
1923 if (gvar->ca.mapon>-1 && gvar->ca.mapheaderseg[gvar->ca.mapon])
\r
1924 MM_SetPurge ((MEMPTRCONV gvar->ca.mapheaderseg[(gvar->ca.mapon)]), 3, gvar);
\r
1925 for (plane=0;plane<MAPPLANES;plane++)
\r
1926 if (gvar->ca.mapsegs[plane])
\r
1927 MM_FreePtr (MEMPTRCONV gvar->ca.mapsegs[plane], gvar);
\r
1929 gvar->ca.mapon = gvar->ca.mapnum;
\r
1933 // load map header
\r
1934 // The header will be cached if it is still around
\r
1936 if (!gvar->ca.mapheaderseg[gvar->ca.mapnum])
\r
1938 pos = ((mapfiletype _seg *)gvar->ca.tinf)->headeroffsets[gvar->ca.mapnum];
\r
1939 if (pos<0) // $FFFFFFFF start is a sparse map
\r
1940 Quit (gvar, "CA_CacheMap: Tried to load a non existent map!");
\r
1942 MM_GetPtr(MEMPTRCONV gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype), gvar);
\r
1943 lseek(gvar->ca.file.maphandle,pos,SEEK_SET);
\r
1944 #ifdef MAPHEADERLINKED
\r
1945 //#if BUFFERSIZE < sizeof(maptype)
\r
1946 //The general buffer size is too small!
\r
1949 // load in, then unhuffman to the destination
\r
1951 CA_FarRead (gvar->ca.file.maphandle,gvar->mm.bufferseg,((mapfiletype _seg *)gvar->ca.tinf)->headersize[gvar->ca.mapnum], gvar);
\r
1952 CAL_HuffExpand ((byte huge *)bufferseg,
\r
1953 (byte huge *)gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype),maphuffman, gvar);
\r
1955 CA_FarRead (gvar->ca.file.maphandle,(memptr)gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype), gvar);
\r
1959 MM_SetPurge (MEMPTRCONV gvar->ca.mapheaderseg[gvar->ca.mapnum], 0, gvar);
\r
1962 // load the planes in
\r
1963 // If a plane's pointer still exists it will be overwritten (levels are
\r
1964 // allways reloaded, never cached)
\r
1967 size = gvar->ca.mapheaderseg[gvar->ca.mapnum]->width * gvar->ca.mapheaderseg[gvar->ca.mapnum]->height * 2;
\r
1969 for (plane = 0; plane<MAPPLANES; plane++)
\r
1971 pos = gvar->ca.mapheaderseg[gvar->ca.mapnum]->planestart[plane];
\r
1972 compressed = gvar->ca.mapheaderseg[gvar->ca.mapnum]->planelength[plane];
\r
1975 continue; // the plane is not used in this game
\r
1977 dest = MEMPTRCONV gvar->ca.mapsegs[plane];
\r
1978 MM_GetPtr(dest,size, gvar);
\r
1980 lseek(gvar->ca.file.maphandle,pos,SEEK_SET);
\r
1981 if (compressed<=BUFFERSIZE)
\r
1982 source = gvar->mm.bufferseg;
\r
1985 MM_GetPtr(MEMPTRCONV bigbufferseg,compressed, gvar);
\r
1986 MM_SetLock (MEMPTRCONV bigbufferseg,true, gvar);
\r
1987 source = bigbufferseg;
\r
1990 CA_FarRead(gvar->ca.file.maphandle,(byte far *)source,compressed, gvar);
\r
1991 #ifdef MAPHEADERLINKED
\r
1993 // unhuffman, then unRLEW
\r
1994 // The huffman'd chunk has a two byte expanded length first
\r
1995 // The resulting RLEW chunk also does, even though it's not really
\r
1998 expanded = *source;
\r
2000 MM_GetPtr (&buffer2seg,expanded, gvar);
\r
2001 CAL_CarmackExpand (source, (unsigned far *)buffer2seg,expanded);
\r
2002 CA_RLEWexpand (((unsigned far *)buffer2seg)+1,*dest,size,
\r
2003 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
2004 MM_FreePtr (&buffer2seg);
\r
2008 // unRLEW, skipping expanded length
\r
2010 CA_RLEWexpand (source+1, *dest,size,
\r
2011 ((mapfiletype _seg *)gvar->ca.tinf)->RLEWtag);
\r
2014 if (compressed>BUFFERSIZE)
\r
2015 MM_FreePtr(MEMPTRCONV bigbufferseg, gvar);
\r
2019 //===========================================================================
\r
2022 ======================
\r
2026 = Goes up a bit level in the needed lists and clears it out.
\r
2027 = Everything is made purgable
\r
2029 ======================
\r
2032 void CA_UpLevel (global_game_variables_t *gvar)
\r
2034 if (gvar->ca.ca_levelnum==7)
\r
2035 printf("CA_UpLevel: Up past level 7!");
\r
2037 gvar->ca.ca_levelbit<<=1;
\r
2038 gvar->ca.ca_levelnum++;
\r
2041 //===========================================================================
\r
2044 ======================
\r
2048 = Goes down a bit level in the needed lists and recaches
\r
2049 = everything from the lower level
\r
2051 ======================
\r
2054 void CA_DownLevel (global_game_variables_t *gvar)
\r
2056 if (!gvar->ca.ca_levelnum)
\r
2057 printf("CA_DownLevel: Down past level 0!");
\r
2058 gvar->ca.ca_levelbit>>=1;
\r
2059 gvar->ca.ca_levelnum--;
\r
2060 ////++++++++++++++++++++++++++++++++++++++++++CA_CacheMarks(NULL);
\r
2063 //===========================================================================
\r
2066 ======================
\r
2070 = Clears out all the marks at the current level
\r
2072 ======================
\r
2075 void CA_ClearMarks (global_game_variables_t *gvar)
\r
2079 for (i=0;i<NUMCHUNKS;i++)
\r
2080 gvar->ca.grneeded[i]&=~gvar->ca.ca_levelbit;
\r
2083 //===========================================================================
\r
2086 ======================
\r
2088 = CA_ClearAllMarks
\r
2090 = Clears out all the marks on all the levels
\r
2092 ======================
\r
2095 void CA_ClearAllMarks (global_game_variables_t *gvar)
\r
2097 _fmemset (gvar->ca.grneeded,0,sizeof(gvar->ca.grneeded));
\r
2098 gvar->ca.ca_levelbit = 1;
\r
2099 gvar->ca.ca_levelnum = 0;
\r
2102 //===========================================================================
\r
2105 ======================
\r
2109 ======================
\r
2112 void CA_SetGrPurge (global_game_variables_t *gvar)
\r
2119 CA_ClearMarks (gvar);
\r
2121 for (i=0;i<NUMCHUNKS;i++)
\r
2122 if (gvar->ca.grsegs[i])
\r
2123 MM_SetPurge (MEMPTRCONV gvar->ca.grsegs[i],3, gvar);
\r
2128 ======================
\r
2132 = Make everything possible purgable
\r
2134 ======================
\r
2137 void CA_SetAllPurge (global_game_variables_t *gvar)
\r
2141 CA_ClearMarks (gvar);
\r
2144 // free cursor sprite and background save
\r
2146 //____VW_FreeCursor ();
\r
2149 // free map headers and map planes
\r
2151 for (i=0;i<NUMMAPS;i++)
\r
2152 if (gvar->ca.mapheaderseg[i])
\r
2153 MM_SetPurge (MEMPTRCONV gvar->ca.mapheaderseg[i],3, gvar);
\r
2156 if (gvar->ca.mapsegs[i])
\r
2157 MM_FreePtr (MEMPTRCONV gvar->ca.mapsegs[i], gvar);
\r
2162 for (i=0;i<NUMSNDCHUNKS;i++)
\r
2163 if (gvar->ca.audiosegs[i])
\r
2164 MM_SetPurge (MEMPTRCONV gvar->ca.audiosegs[i],3, gvar);
\r
2169 CA_SetGrPurge (gvar);
\r
2173 //===========================================================================
\r
2177 ======================
\r
2181 ======================
\r
2184 #define NUMBARS (17l*8)
\r
2187 unsigned thx,thy,lastx;
\r
2188 long barx,barstep;
\r
2190 void CAL_DialogDraw (char *title,unsigned numcache)
\r
2192 unsigned homex,homey,x;
\r
2194 barstep = (NUMBARS<<16)/numcache;
\r
2197 // draw dialog window (masked tiles 12 - 20 are window borders)
\r
2199 US_CenterWindow (20,8);
\r
2203 US_CPrint ("Loading");
\r
2204 fontcolor = F_SECONDCOLOR;
\r
2205 US_CPrint (title);
\r
2206 fontcolor = F_BLACK;
\r
2209 // draw thermometer bar
\r
2213 VWB_DrawTile8(thx,thy,0); // CAT3D numbers
\r
2214 VWB_DrawTile8(thx,thy+8,3);
\r
2215 VWB_DrawTile8(thx,thy+16,6);
\r
2216 VWB_DrawTile8(thx+17*8,thy,2);
\r
2217 VWB_DrawTile8(thx+17*8,thy+8,5);
\r
2218 VWB_DrawTile8(thx+17*8,thy+16,8);
\r
2219 for (x=thx+8;x<thx+17*8;x+=8)
\r
2221 VWB_DrawTile8(x,thy,1);
\r
2222 VWB_DrawTile8(x,thy+8,4);
\r
2223 VWB_DrawTile8(x,thy+16,7);
\r
2226 thx += 4; // first line location
\r
2228 barx = (long)thx<<16;
\r
2231 VW_UpdateScreen();
\r
2236 ======================
\r
2238 = CAL_DialogUpdate
\r
2240 ======================
\r
2243 void CAL_DialogUpdate (void)
\r
2249 if (xh - lastx > BARSTEP)
\r
2251 for (x=lastx;x<=xh;x++)
\r
2252 #if GRMODE == EGAGR
\r
2253 VWB_Vlin (thy,thy+13,x,14);
\r
2255 #if GRMODE == CGAGR
\r
2256 VWB_Vlin (thy,thy+13,x,SECONDCOLOR);
\r
2259 VW_UpdateScreen();
\r
2264 ======================
\r
2266 = CAL_DialogFinish
\r
2268 ======================
\r
2271 void CAL_DialogFinish (void)
\r
2275 xh = thx + NUMBARS;
\r
2276 for (x=lastx;x<=xh;x++)
\r
2277 #if GRMODE == EGAGR
\r
2278 VWB_Vlin (thy,thy+13,x,14);
\r
2280 #if GRMODE == CGAGR
\r
2281 VWB_Vlin (thy,thy+13,x,SECONDCOLOR);
\r
2283 VW_UpdateScreen();
\r
2287 //===========================================================================
\r
2290 ======================
\r
2294 ======================
\r
2296 #define MAXEMPTYREAD 1024
\r
2298 void CAL_CacheMarks (char *title, global_game_variables_t *gvar)
\r
2301 int i,next,numcache;
\r
2302 long pos,endpos,nextpos,nextendpos,compressed;
\r
2303 long bufferstart,bufferend; // file position of general buffer
\r
2305 memptr bigbufferseg;
\r
2307 dialog = (title!=NULL);
\r
2311 // go through and make everything not needed purgable
\r
2313 for (i=0;i<NUMCHUNKS;i++)
\r
2314 if (gvar->ca.grneeded[i]&(gvar->ca.ca_levelbit))//if (grneeded[i]&ca_levelbit)
\r
2316 if (gvar->ca.grsegs[i]) // its allready in memory, make
\r
2317 MM_SetPurge(gvar->ca.grsegs[i],0,gvar); // sure it stays there!
\r
2323 if (gvar->ca.grsegs[i]) // not needed, so make it purgeable
\r
2324 MM_SetPurge(gvar->ca.grsegs[i],3,gvar);
\r
2327 if (!numcache) // nothing to cache!
\r
2330 // MDM begin - (GAMERS EDGE)
\r
2332 //???? if (!FindFile("EGAGRAPH.16",NULL,2))
\r
2333 //???? Quit (gvar, "CA_CacheMarks(): Can't find graphics files.");
\r
2340 write(profilehandle,title,strlen(title));
\r
2341 write(profilehandle,"\n",1);
\r
2344 drawcachebox(title,numcache);
\r
2348 // go through and load in anything still needed
\r
2350 bufferstart = bufferend = 0; // nothing good in buffer now
\r
2352 for (i=0;i<NUMCHUNKS;i++)
\r
2353 if ( (gvar->ca.grneeded[i]&(gvar->ca.ca_levelbit)) && !gvar->ca.grsegs[i])
\r
2356 // update thermometer
\r
2358 if (dialog && updatecachebox)
\r
2359 updatecachebox ();
\r
2361 pos = GRFILEPOS(i,gvar);
\r
2366 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
2369 compressed = GRFILEPOS(next,gvar)-pos;
\r
2370 endpos = pos+compressed;
\r
2372 if (compressed<=BUFFERSIZE)
\r
2374 if (bufferstart<=pos
\r
2375 && bufferend>= endpos)
\r
2377 // data is allready in buffer
\r
2378 source = (byte _seg *)bufferseg+(pos-bufferstart);
\r
2382 // load buffer with a new block from disk
\r
2383 // try to get as many of the needed blocks in as possible
\r
2384 while ( next < NUMCHUNKS )
\r
2386 while (next < NUMCHUNKS &&
\r
2387 !(gvar->video.grneeded[next]&ca_levelbit && !grsegs[next]))
\r
2389 if (next == NUMCHUNKS)
\r
2392 nextpos = GRFILEPOS(next,gvar);
\r
2393 while (GRFILEPOS(++next,gvar) == -1) // skip past any sparse tiles
\r
2395 nextendpos = GRFILEPOS(next,gvar);
\r
2396 if (nextpos - endpos <= MAXEMPTYREAD
\r
2397 && nextendpos-pos <= BUFFERSIZE)
\r
2398 endpos = nextendpos;
\r
2400 next = NUMCHUNKS; // read pos to posend
\r
2403 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
2404 CA_FarRead(gvar->ca.file.grhandle,(gvar->mm.bufferseg),endpos-pos,gvar);
\r
2405 bufferstart = pos;
\r
2406 bufferend = endpos;
\r
2407 source = bufferseg;
\r
2412 // big chunk, allocate temporary buffer
\r
2413 MM_GetPtr(&bigbufferseg,compressed,gvar);
\r
2416 MM_SetLock (&bigbufferseg,true);
\r
2417 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
2418 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed,gvar);
\r
2419 source = bigbufferseg;
\r
2422 CAL_ExpandGrChunk (i,source);
\r
2426 if (compressed>BUFFERSIZE)
\r
2427 MM_FreePtr(&bigbufferseg);
\r
2432 // finish up any thermometer remnants
\r
2434 if (dialog && finishcachebox)
\r
2438 void CA_CannotOpen(char *string, global_game_variables_t *gvar)
\r
2442 strcpy(str,"Can't open ");
\r
2443 strcat(str,string);
\r
2444 strcat(str,"!\n");
\r