3 // this has been customized for WOLF
\r
6 =============================================================================
\r
8 Id Software Caching Manager
\r
9 ---------------------------
\r
11 Must be started BEFORE the memory manager, because it needs to get the headers
\r
12 loaded into the data segment
\r
14 =============================================================================
\r
17 #include "ID_HEADS.H"
\r
23 #define THREEBYTEGRSTARTS
\r
26 =============================================================================
\r
30 =============================================================================
\r
35 unsigned bit0,bit1; // 0-255 is a character, > is a pointer to a node
\r
42 long headeroffsets[100];
\r
48 =============================================================================
\r
52 =============================================================================
\r
58 unsigned _seg *mapsegs[MAPPLANES];
\r
59 maptype _seg *mapheaderseg[NUMMAPS];
\r
60 byte _seg *audiosegs[NUMSNDCHUNKS];
\r
61 void _seg *grsegs[NUMCHUNKS];
\r
63 byte far grneeded[NUMCHUNKS];
\r
64 byte ca_levelbit,ca_levelnum;
\r
66 int profilehandle,debughandle;
\r
68 char audioname[13]="AUDIO.";
\r
71 =============================================================================
\r
75 =============================================================================
\r
78 extern long far CGAhead;
\r
79 extern long far EGAhead;
\r
80 extern byte CGAdict;
\r
81 extern byte EGAdict;
\r
82 extern byte far maphead;
\r
83 extern byte mapdict;
\r
84 extern byte far audiohead;
\r
85 extern byte audiodict;
\r
88 char extension[5], // Need a string, not constant to change cache files
\r
89 gheadname[10]=GREXT"HEAD.",
\r
90 gfilename[10]=GREXT"GRAPH.",
\r
91 gdictname[10]=GREXT"DICT.",
\r
92 mheadname[10]="MAPHEAD.",
\r
93 mfilename[10]="MAPTEMP.",
\r
94 aheadname[10]="AUDIOHED.",
\r
95 afilename[10]="AUDIOT.";
\r
97 void CA_CannotOpen(char *string);
\r
99 long _seg *grstarts; // array of offsets in egagraph, -1 for sparse
\r
100 long _seg *audiostarts; // array of offsets in audio / audiot
\r
102 #ifdef GRHEADERLINKED
\r
103 huffnode *grhuffman;
\r
105 huffnode grhuffman[255];
\r
108 #ifdef AUDIOHEADERLINKED
\r
109 huffnode *audiohuffman;
\r
111 huffnode audiohuffman[255];
\r
115 int grhandle; // handle to EGAGRAPH
\r
116 int maphandle; // handle to MAPTEMP / GAMEMAPS
\r
117 int audiohandle; // handle to AUDIOT / AUDIO
\r
119 long chunkcomplen,chunkexplen;
\r
121 SDMode oldsoundmode;
\r
125 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest,
\r
129 #ifdef THREEBYTEGRSTARTS
\r
130 #define FILEPOSSIZE 3
\r
131 //#define GRFILEPOS(c) (*(long far *)(((byte far *)grstarts)+(c)*3)&0xffffff)
\r
132 long GRFILEPOS(int c)
\r
139 value = *(long far *)(((byte far *)grstarts)+offset);
\r
141 value &= 0x00ffffffl;
\r
143 if (value == 0xffffffl)
\r
149 #define FILEPOSSIZE 4
\r
150 #define GRFILEPOS(c) (grstarts[c])
\r
154 =============================================================================
\r
158 =============================================================================
\r
162 ============================
\r
164 = CA_OpenDebug / CA_CloseDebug
\r
166 = Opens a binary file with the handle "debughandle"
\r
168 ============================
\r
171 void CA_OpenDebug (void)
\r
173 unlink ("DEBUG.TXT");
\r
174 debughandle = open("DEBUG.TXT", O_CREAT | O_WRONLY | O_TEXT);
\r
177 void CA_CloseDebug (void)
\r
179 close (debughandle);
\r
185 ============================
\r
187 = CAL_GetGrChunkLength
\r
189 = Gets the length of an explicit length chunk (not tiles)
\r
190 = The file pointer is positioned so the compressed data can be read in next.
\r
192 ============================
\r
195 void CAL_GetGrChunkLength (int chunk)
\r
197 lseek(grhandle,GRFILEPOS(chunk),SEEK_SET);
\r
198 read(grhandle,&chunkexplen,sizeof(chunkexplen));
\r
199 chunkcomplen = GRFILEPOS(chunk+1)-GRFILEPOS(chunk)-4;
\r
204 ==========================
\r
208 = Read from a file to a far pointer
\r
210 ==========================
\r
213 boolean CA_FarRead (int handle, byte far *dest, long length)
\r
215 boolean flag=false;
\r
216 if (length>0xffffl)
\r
217 Quit ("CA_FarRead doesn't support 64K reads yet!");
\r
222 mov cx,[WORD PTR length]
\r
223 mov dx,[WORD PTR dest]
\r
224 mov ds,[WORD PTR dest+2]
\r
225 mov ah,0x3f // READ w/handle
\r
232 #ifdef __BORLANDC__
\r
236 #ifdef __BORLANDC__
\r
239 cmp ax,[WORD PTR length]
\r
241 // errno = EINVFMT; // user manager knows this is bad read
\r
244 #ifdef __BORLANDC__
\r
248 #ifdef __BORLANDC__
\r
252 #ifdef __BORLANDC__
\r
264 ==========================
\r
268 = Write from a file to a far pointer
\r
270 ==========================
\r
273 boolean CA_FarWrite (int handle, byte far *source, long length)
\r
275 boolean flag=false;
\r
276 if (length>0xffffl)
\r
277 Quit ("CA_FarWrite doesn't support 64K reads yet!");
\r
282 mov cx,[WORD PTR length]
\r
283 mov dx,[WORD PTR source]
\r
284 mov ds,[WORD PTR source+2]
\r
285 mov ah,0x40 // WRITE w/handle
\r
292 #ifdef __BORLANDC__
\r
296 #ifdef __BORLANDC__
\r
299 cmp ax,[WORD PTR length]
\r
301 // errno = ENOMEM; // user manager knows this is bad write
\r
304 #ifdef __BORLANDC__
\r
308 #ifdef __BORLANDC__
\r
312 #ifdef __BORLANDC__
\r
324 ==========================
\r
328 = Reads a file into an allready allocated buffer
\r
330 ==========================
\r
333 boolean CA_ReadFile (char *filename, memptr *ptr)
\r
338 if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
341 size = filelength (handle);
\r
342 if (!CA_FarRead (handle,*ptr,size))
\r
353 ==========================
\r
357 = Writes a file from a memory buffer
\r
359 ==========================
\r
362 boolean CA_WriteFile (char *filename, void far *ptr, long length)
\r
367 handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
\r
368 S_IREAD | S_IWRITE | S_IFREG);
\r
373 if (!CA_FarWrite (handle,ptr,length))
\r
385 ==========================
\r
389 = Allocate space for and load a file
\r
391 ==========================
\r
394 boolean CA_LoadFile (char *filename, memptr *ptr)
\r
399 if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
402 size = filelength (handle);
\r
403 MM_GetPtr (ptr,size);
\r
404 if (!CA_FarRead (handle,*ptr,size))
\r
414 ============================================================================
\r
416 COMPRESSION routines, see JHUFF.C for more
\r
418 ============================================================================
\r
426 = CAL_OptimizeNodes
\r
428 = Goes through a huffman table and changes the 256-511 node numbers to the
\r
429 = actular address of the node. Must be called before CAL_HuffExpand
\r
434 void CAL_OptimizeNodes (huffnode *table)
\r
441 for (i=0;i<255;i++)
\r
443 if (node->bit0 >= 256)
\r
444 node->bit0 = (unsigned)(table+(node->bit0-256));
\r
445 if (node->bit1 >= 256)
\r
446 node->bit1 = (unsigned)(table+(node->bit1-256));
\r
454 ======================
\r
458 = Length is the length of the EXPANDED data
\r
459 = If screenhack, the data is decompressed in four planes directly
\r
462 ======================
\r
465 void CAL_HuffExpand (byte huge *source, byte huge *dest,
\r
466 long length,huffnode *hufftable, boolean screenhack)
\r
468 // unsigned bit,byte,node,code;
\r
469 unsigned sourceseg,sourceoff,destseg,destoff,endoff;
\r
472 // huffnode *nodeon;
\r
474 headptr = hufftable+254; // head node is allways node 254
\r
476 source++; // normalize
\r
484 asm mov dx,SC_INDEX
\r
485 asm mov ax,SC_MAPMASK + 256
\r
490 sourceseg = FP_SEG(source);
\r
491 sourceoff = FP_OFF(source);
\r
492 destseg = FP_SEG(dest);
\r
493 destoff = FP_OFF(dest);
\r
494 endoff = destoff+length;
\r
499 // ss:bx node pointer
\r
502 if (length <0xfff0)
\r
505 //--------------------------
\r
506 // expand less than 64k of data
\r
507 //--------------------------
\r
510 mov bx,[word ptr headptr]
\r
518 mov ch,[si] // load first byte
\r
521 #ifdef __BORLANDC__
\r
525 #ifdef __BORLANDC__
\r
528 test ch,cl // bit set?
\r
530 mov dx,[ss:bx] // take bit0 path from node
\r
531 shl cl,1 // advance to next bit position
\r
534 #ifdef __BORLANDC__
\r
538 #ifdef __BORLANDC__
\r
541 mov dx,[ss:bx+2] // take bit1 path
\r
542 shl cl,1 // advance to next bit position
\r
544 #ifdef __BORLANDC__
\r
548 #ifdef __BORLANDC__
\r
551 mov ch,[si] // load next byte
\r
553 mov cl,1 // back to first bit
\r
554 #ifdef __BORLANDC__
\r
558 #ifdef __BORLANDC__
\r
561 or dh,dh // if dx<256 its a byte, else move node
\r
563 mov bx,dx // next node = (huffnode *)code
\r
565 #ifdef __BORLANDC__
\r
569 #ifdef __BORLANDC__
\r
573 inc di // write a decopmpressed byte out
\r
574 mov bx,[word ptr headptr] // back to the head node for next bit
\r
579 // perform screenhack if needed
\r
581 test [screenhack],1
\r
586 je notscreen // all four planes done
\r
593 #ifdef __BORLANDC__
\r
604 //--------------------------
\r
605 // expand more than 64k of data
\r
606 //--------------------------
\r
611 mov bx,[word ptr headptr]
\r
619 lodsb // load first byte
\r
620 #ifdef __BORLANDC__
\r
624 #ifdef __BORLANDC__
\r
627 test al,cl // bit set?
\r
629 mov dx,[ss:bx] // take bit0 path from node
\r
631 #ifdef __BORLANDC__
\r
635 #ifdef __BORLANDC__
\r
638 mov dx,[ss:bx+2] // take bit1 path
\r
639 #ifdef __BORLANDC__
\r
643 #ifdef __BORLANDC__
\r
646 shl cl,1 // advance to next bit position
\r
649 cmp si,0x10 // normalize ds:si
\r
655 #ifdef __BORLANDC__
\r
659 #ifdef __BORLANDC__
\r
662 mov cl,1 // back to first bit
\r
663 #ifdef __BORLANDC__
\r
667 #ifdef __BORLANDC__
\r
670 or dh,dh // if dx<256 its a byte, else move node
\r
672 mov bx,dx // next node = (huffnode *)code
\r
674 #ifdef __BORLANDC__
\r
678 #ifdef __BORLANDC__
\r
682 inc di // write a decopmpressed byte out
\r
683 mov bx,[word ptr headptr] // back to the head node for next bit
\r
685 cmp di,0x10 // normalize es:di
\r
691 #ifdef __BORLANDC__
\r
695 #ifdef __BORLANDC__
\r
698 sub [WORD PTR ss:length],1
\r
700 dec [WORD PTR ss:length+2]
\r
701 jns expand // when length = ffff ffff, done
\r
714 ======================
\r
716 = CAL_CarmackExpand
\r
718 = Length is the length of the EXPANDED data
\r
720 ======================
\r
723 #define NEARTAG 0xa7
\r
724 #define FARTAG 0xa8
\r
726 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest, unsigned length)
\r
728 unsigned ch,chhigh,count,offset;
\r
729 unsigned far *copyptr, far *inptr, far *outptr;
\r
740 if (chhigh == NEARTAG)
\r
744 { // have to insert a word containing the tag byte
\r
745 ch |= *(BYTEFARPTRCONV inptr)++;
\r
751 offset = *(BYTEFARPTRCONV inptr)++;
\r
752 copyptr = outptr - offset;
\r
755 *outptr++ = *copyptr++;
\r
758 else if (chhigh == FARTAG)
\r
762 { // have to insert a word containing the tag byte
\r
763 ch |= *(BYTEFARPTRCONV inptr)++;
\r
770 copyptr = dest + offset;
\r
773 *outptr++ = *copyptr++;
\r
787 ======================
\r
791 ======================
\r
794 long CA_RLEWCompress (unsigned huge *source, long length, unsigned huge *dest,
\r
798 unsigned value,count,i;
\r
799 unsigned huge *start,huge *end;
\r
803 end = source + (length+1)/2;
\r
812 while (*source == value && source<end)
\r
817 if (count>3 || value == rlewtag)
\r
820 // send a tag / count / value string
\r
829 // send word without compressing
\r
831 for (i=1;i<=count;i++)
\r
835 } while (source<end);
\r
837 complength = 2*(dest-start);
\r
843 ======================
\r
846 = length is EXPANDED length
\r
848 ======================
\r
851 void CA_RLEWexpand (unsigned huge *source, unsigned huge *dest,long length,
\r
854 // unsigned value,count,i;
\r
855 unsigned huge *end;
\r
856 unsigned sourceseg,sourceoff,destseg,destoff,endseg,endoff;
\r
866 if (value != rlewtag)
\r
874 // compressed string
\r
878 for (i=1;i<=count;i++)
\r
881 } while (dest<end);
\r
884 end = dest + (length)/2;
\r
885 sourceseg = FP_SEG(source);
\r
886 sourceoff = FP_OFF(source);
\r
887 destseg = FP_SEG(dest);
\r
888 destoff = FP_OFF(dest);
\r
889 endseg = FP_SEG(end);
\r
890 endoff = FP_OFF(end);
\r
894 // ax = source value
\r
896 // cx = repeat counts
\r
899 // NOTE: A repeat count that produces 0xfff0 bytes can blow this!
\r
908 #ifdef __BORLANDC__
\r
912 #ifdef __BORLANDC__
\r
920 #ifdef __BORLANDC__
\r
924 #ifdef __BORLANDC__
\r
928 mov cx,ax // repeat count
\r
929 lodsw // repeat value
\r
931 #ifdef __BORLANDC__
\r
935 #ifdef __BORLANDC__
\r
938 cmp si,0x10 // normalize ds:si
\r
949 #ifdef __BORLANDC__
\r
953 #ifdef __BORLANDC__
\r
956 cmp di,0x10 // normalize es:di
\r
967 #ifdef __BORLANDC__
\r
971 #ifdef __BORLANDC__
\r
989 =============================================================================
\r
991 CACHE MANAGER ROUTINES
\r
993 =============================================================================
\r
998 ======================
\r
1002 ======================
\r
1005 void CAL_SetupGrFile (void)
\r
1011 #ifdef GRHEADERLINKED
\r
1013 grhuffman = (huffnode *)&EGAdict;
\r
1014 grstarts = (long _seg *)FP_SEG(&EGAhead);
\r
1016 CAL_OptimizeNodes (grhuffman);
\r
1021 // load ???dict.ext (huffman dictionary for graphics files)
\r
1024 strcpy(fname,gdictname);
\r
1025 strcat(fname,extension);
\r
1027 if ((handle = open(fname,
\r
1028 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1029 CA_CannotOpen(fname);
\r
1031 read(handle, &grhuffman, sizeof(grhuffman));
\r
1033 CAL_OptimizeNodes (grhuffman);
\r
1035 // load the data offsets from ???head.ext
\r
1037 MM_GetPtr (MEMPTRCONV grstarts,(NUMCHUNKS+1)*FILEPOSSIZE);
\r
1039 strcpy(fname,gheadname);
\r
1040 strcat(fname,extension);
\r
1042 if ((handle = open(fname,
\r
1043 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1044 CA_CannotOpen(fname);
\r
1046 CA_FarRead(handle, (memptr)grstarts, (NUMCHUNKS+1)*FILEPOSSIZE);
\r
1054 // Open the graphics file, leaving it open until the game is finished
\r
1056 strcpy(fname,gfilename);
\r
1057 strcat(fname,extension);
\r
1059 grhandle = open(fname, O_RDONLY | O_BINARY);
\r
1060 if (grhandle == -1)
\r
1061 CA_CannotOpen(fname);
\r
1065 // load the pic and sprite headers into the arrays in the data segment
\r
1067 MM_GetPtr(MEMPTRCONV pictable,NUMPICS*sizeof(pictabletype));
\r
1068 CAL_GetGrChunkLength(STRUCTPIC); // position file pointer
\r
1069 printf("CAL_SetupGrFile:\n");
\r
1070 printf(" chunkcomplen size is %lu\n", chunkcomplen);
\r
1071 MM_GetPtr(MEMPTRANDPERCONV compseg,chunkcomplen);
\r
1072 CA_FarRead (grhandle,compseg,chunkcomplen);
\r
1073 CAL_HuffExpand (compseg, (byte huge *)pictable,NUMPICS*sizeof(pictabletype),grhuffman,false);
\r
1074 MM_FreePtr(MEMPTRANDPERCONV compseg);
\r
1077 //==========================================================================
\r
1081 ======================
\r
1083 = CAL_SetupMapFile
\r
1085 ======================
\r
1088 void CAL_SetupMapFile (void)
\r
1096 // load maphead.ext (offsets and tileinfo for map file)
\r
1098 #ifndef MAPHEADERLINKED
\r
1099 strcpy(fname,mheadname);
\r
1100 strcat(fname,extension);
\r
1102 if ((handle = open(fname,
\r
1103 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1104 CA_CannotOpen(fname);
\r
1106 length = filelength(handle);
\r
1107 MM_GetPtr (MEMPTRCONV tinf,length);
\r
1108 CA_FarRead(handle, tinf, length);
\r
1112 tinf = (byte _seg *)FP_SEG(&maphead);
\r
1117 // open the data file
\r
1120 strcpy(fname,"GAMEMAPS.");
\r
1121 strcat(fname,extension);
\r
1123 if ((maphandle = open(fname,
\r
1124 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1125 CA_CannotOpen(fname);
\r
1127 strcpy(fname,mfilename);
\r
1128 strcat(fname,extension);
\r
1130 if ((maphandle = open(fname,
\r
1131 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1132 CA_CannotOpen(fname);
\r
1136 // load all map header
\r
1138 for (i=0;i<NUMMAPS;i++)
\r
1140 pos = ((mapfiletype _seg *)tinf)->headeroffsets[i];
\r
1141 if (pos<0) // $FFFFFFFF start is a sparse map
\r
1144 MM_GetPtr(MEMPTRCONV mapheaderseg[i],sizeof(maptype));
\r
1145 MM_SetLock(MEMPTRCONV mapheaderseg[i],true);
\r
1146 lseek(maphandle,pos,SEEK_SET);
\r
1147 CA_FarRead (maphandle,(memptr)mapheaderseg[i],sizeof(maptype));
\r
1151 // allocate space for 3 64*64 planes
\r
1153 for (i=0;i<MAPPLANES;i++)
\r
1155 MM_GetPtr (MEMPTRCONV mapsegs[i],64*64*2);
\r
1156 MM_SetLock (MEMPTRCONV mapsegs[i],true);
\r
1161 //==========================================================================
\r
1165 ======================
\r
1167 = CAL_SetupAudioFile
\r
1169 ======================
\r
1172 void CAL_SetupAudioFile (void)
\r
1179 // load maphead.ext (offsets and tileinfo for map file)
\r
1181 #ifndef AUDIOHEADERLINKED
\r
1182 strcpy(fname,aheadname);
\r
1183 strcat(fname,extension);
\r
1185 if ((handle = open(fname,
\r
1186 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1187 CA_CannotOpen(fname);
\r
1189 length = filelength(handle);
\r
1190 MM_GetPtr (MEMPTRCONV audiostarts,length);
\r
1191 CA_FarRead(handle, (byte far *)audiostarts, length);
\r
1194 audiohuffman = (huffnode *)&audiodict;
\r
1195 CAL_OptimizeNodes (audiohuffman);
\r
1196 audiostarts = (long _seg *)FP_SEG(&audiohead);
\r
1200 // open the data file
\r
1202 #ifndef AUDIOHEADERLINKED
\r
1203 strcpy(fname,afilename);
\r
1204 strcat(fname,extension);
\r
1206 if ((audiohandle = open(fname,
\r
1207 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1208 CA_CannotOpen(fname);
\r
1210 if ((audiohandle = open("AUDIO."EXTENSION,
\r
1211 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1212 Quit ("Can't open AUDIO."EXTENSION"!");
\r
1216 //==========================================================================
\r
1220 ======================
\r
1224 = Open all files and load in headers
\r
1226 ======================
\r
1229 void CA_Startup (void)
\r
1232 unlink ("PROFILE.TXT");
\r
1233 profilehandle = open("PROFILE.TXT", O_CREAT | O_WRONLY | O_TEXT);
\r
1236 CAL_SetupMapFile ();
\r
1237 CAL_SetupGrFile ();
\r
1238 CAL_SetupAudioFile ();
\r
1246 //==========================================================================
\r
1250 ======================
\r
1254 = Closes all files
\r
1256 ======================
\r
1259 void CA_Shutdown (void)
\r
1262 close (profilehandle);
\r
1265 close (maphandle);
\r
1267 close (audiohandle);
\r
1270 //===========================================================================
\r
1273 ======================
\r
1275 = CA_CacheAudioChunk
\r
1277 ======================
\r
1280 void CA_CacheAudioChunk (int chunk)
\r
1282 long pos,compressed;
\r
1283 #ifdef AUDIOHEADERLINKED
\r
1285 memptr bigbufferseg;
\r
1289 if (audiosegs[chunk])
\r
1291 MM_SetPurge (MEMPTRCONV audiosegs[chunk],0);
\r
1292 return; // allready in memory
\r
1296 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1297 // a larger buffer
\r
1299 pos = audiostarts[chunk];
\r
1300 compressed = audiostarts[chunk+1]-pos;
\r
1302 lseek(audiohandle,pos,SEEK_SET);
\r
1304 #ifndef AUDIOHEADERLINKED
\r
1306 MM_GetPtr (MEMPTRCONV audiosegs[chunk],compressed);
\r
1310 CA_FarRead(audiohandle,audiosegs[chunk],compressed);
\r
1314 if (compressed<=BUFFERSIZE)
\r
1316 CA_FarRead(audiohandle,bufferseg,compressed);
\r
1317 source = bufferseg;
\r
1321 MM_GetPtr(MEMPTRANDPERCONV bigbufferseg,compressed);
\r
1324 MM_SetLock (MEMPTRANDPERCONV bigbufferseg,true);
\r
1325 CA_FarRead(audiohandle,bigbufferseg,compressed);
\r
1326 source = bigbufferseg;
\r
1329 expanded = *(long far *)source;
\r
1330 source += 4; // skip over length
\r
1331 MM_GetPtr (MEMPTRCONV audiosegs[chunk],expanded);
\r
1334 CAL_HuffExpand (source,audiosegs[chunk],expanded,audiohuffman,false);
\r
1337 if (compressed>BUFFERSIZE)
\r
1338 MM_FreePtr(MEMPTRANDPERCONV bigbufferseg);
\r
1342 //===========================================================================
\r
1345 ======================
\r
1347 = CA_LoadAllSounds
\r
1349 = Purges all sounds, then loads all new ones (mode switch)
\r
1351 ======================
\r
1354 void CA_LoadAllSounds (void)
\r
1358 switch (oldsoundmode)
\r
1363 start = STARTPCSOUNDS;
\r
1366 start = STARTADLIBSOUNDS;
\r
1370 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1371 if (audiosegs[start])
\r
1372 MM_SetPurge (MEMPTRCONV audiosegs[start],3); // make purgable
\r
1376 switch (SoundMode)
\r
1381 start = STARTPCSOUNDS;
\r
1384 start = STARTADLIBSOUNDS;
\r
1388 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1389 CA_CacheAudioChunk (start);
\r
1391 oldsoundmode = SoundMode;
\r
1394 //===========================================================================
\r
1398 ======================
\r
1400 = CAL_ExpandGrChunk
\r
1402 = Does whatever is needed with a pointer to a compressed chunk
\r
1404 ======================
\r
1407 void CAL_ExpandGrChunk (int chunk, byte far *source)
\r
1412 if (chunk >= STARTTILE8 && chunk < STARTEXTERNS)
\r
1415 // expanded sizes of tile8/16/32 are implicit
\r
1419 #define MASKBLOCK 128
\r
1421 if (chunk<STARTTILE8M) // tile 8s are all in one chunk!
\r
1422 expanded = BLOCK*NUMTILE8;
\r
1423 else if (chunk<STARTTILE16)
\r
1424 expanded = MASKBLOCK*NUMTILE8M;
\r
1425 else if (chunk<STARTTILE16M) // all other tiles are one/chunk
\r
1426 expanded = BLOCK*4;
\r
1427 else if (chunk<STARTTILE32)
\r
1428 expanded = MASKBLOCK*4;
\r
1429 else if (chunk<STARTTILE32M)
\r
1430 expanded = BLOCK*16;
\r
1432 expanded = MASKBLOCK*16;
\r
1437 // everything else has an explicit size longword
\r
1439 expanded = *(long far *)source;
\r
1440 source += 4; // skip over length
\r
1444 // allocate final space, decompress it, and free bigbuffer
\r
1445 // Sprites need to have shifts made and various other junk
\r
1447 MM_GetPtr (&grsegs[chunk],expanded);
\r
1450 CAL_HuffExpand (source,grsegs[chunk],expanded,grhuffman,false);
\r
1455 ======================
\r
1459 = Makes sure a given chunk is in memory, loadiing it if needed
\r
1461 ======================
\r
1464 void CA_CacheGrChunk (int chunk)
\r
1466 long pos,compressed;
\r
1467 memptr bigbufferseg;
\r
1471 grneeded[chunk] |= ca_levelbit; // make sure it doesn't get removed
\r
1472 if (grsegs[chunk])
\r
1474 MM_SetPurge (&grsegs[chunk],0);
\r
1475 return; // allready in memory
\r
1479 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1480 // a larger buffer
\r
1482 pos = GRFILEPOS(chunk);
\r
1483 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1487 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1490 compressed = GRFILEPOS(next)-pos;
\r
1492 lseek(grhandle,pos,SEEK_SET);
\r
1494 if (compressed<=BUFFERSIZE)
\r
1496 CA_FarRead(grhandle,bufferseg,compressed);
\r
1497 source = bufferseg;
\r
1501 MM_GetPtr(MEMPTRANDPERCONV bigbufferseg,compressed);
\r
1502 MM_SetLock (MEMPTRANDPERCONV bigbufferseg,true);
\r
1503 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1504 source = bigbufferseg;
\r
1507 CAL_ExpandGrChunk (chunk,source);
\r
1509 if (compressed>BUFFERSIZE)
\r
1510 MM_FreePtr(MEMPTRANDPERCONV bigbufferseg);
\r
1515 //==========================================================================
\r
1518 ======================
\r
1522 = Decompresses a chunk from disk straight onto the screen
\r
1524 ======================
\r
1527 void CA_CacheScreen (int chunk)
\r
1529 long pos,compressed,expanded;
\r
1530 memptr bigbufferseg;
\r
1535 // load the chunk into a buffer
\r
1537 pos = GRFILEPOS(chunk);
\r
1539 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1541 compressed = GRFILEPOS(next)-pos;
\r
1543 lseek(grhandle,pos,SEEK_SET);
\r
1545 MM_GetPtr(MEMPTRANDPERCONV bigbufferseg,compressed);
\r
1546 MM_SetLock (MEMPTRANDPERCONV bigbufferseg,true);
\r
1547 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1548 source = bigbufferseg;
\r
1550 expanded = *(long far *)source;
\r
1551 source += 4; // skip over length
\r
1554 // allocate final space, decompress it, and free bigbuffer
\r
1555 // Sprites need to have shifts made and various other junk
\r
1557 CAL_HuffExpand (source,MK_FP(SCREENSEG,bufferofs),expanded,grhuffman,true);
\r
1558 VW_MarkUpdateBlock (0,0,319,199);
\r
1559 MM_FreePtr(MEMPTRANDPERCONV bigbufferseg);
\r
1562 //==========================================================================
\r
1565 ======================
\r
1569 = WOLF: This is specialized for a 64*64 map size
\r
1571 ======================
\r
1574 void CA_CacheMap (int mapnum)
\r
1576 long pos,compressed;
\r
1578 memptr *dest,bigbufferseg;
\r
1580 unsigned far *source;
\r
1582 memptr buffer2seg;
\r
1589 // load the planes into the allready allocated buffers
\r
1593 for (plane = 0; plane<MAPPLANES; plane++)
\r
1595 pos = mapheaderseg[mapnum]->planestart[plane];
\r
1596 compressed = mapheaderseg[mapnum]->planelength[plane];
\r
1598 dest = MEMPTRCONV mapsegs[plane];
\r
1600 lseek(maphandle,pos,SEEK_SET);
\r
1601 if (compressed<=BUFFERSIZE)
\r
1602 source = bufferseg;
\r
1605 MM_GetPtr(MEMPTRANDPERCONV bigbufferseg,compressed);
\r
1606 MM_SetLock (MEMPTRANDPERCONV bigbufferseg,true);
\r
1607 source = bigbufferseg;
\r
1610 CA_FarRead(maphandle,(byte far *)source,compressed);
\r
1613 // unhuffman, then unRLEW
\r
1614 // The huffman'd chunk has a two byte expanded length first
\r
1615 // The resulting RLEW chunk also does, even though it's not really
\r
1618 expanded = *source;
\r
1620 MM_GetPtr (MEMPTRANDPERCONV buffer2seg,expanded);
\r
1621 CAL_CarmackExpand (source, (unsigned far *)buffer2seg,expanded);
\r
1622 CA_RLEWexpand (((unsigned far *)buffer2seg)+1,*dest,size,
\r
1623 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
1624 MM_FreePtr (MEMPTRANDPERCONV buffer2seg);
\r
1628 // unRLEW, skipping expanded length
\r
1630 CA_RLEWexpand (source+1, *dest,size,
\r
1631 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
1634 if (compressed>BUFFERSIZE)
\r
1635 MM_FreePtr(MEMPTRANDPERCONV bigbufferseg);
\r
1639 //===========================================================================
\r
1642 ======================
\r
1646 = Goes up a bit level in the needed lists and clears it out.
\r
1647 = Everything is made purgable
\r
1649 ======================
\r
1652 void CA_UpLevel (void)
\r
1656 if (ca_levelnum==7)
\r
1657 Quit ("CA_UpLevel: Up past level 7!");
\r
1659 for (i=0;i<NUMCHUNKS;i++)
\r
1661 MM_SetPurge (MEMPTRCONV grsegs[i],3);
\r
1666 //===========================================================================
\r
1669 ======================
\r
1673 = Goes down a bit level in the needed lists and recaches
\r
1674 = everything from the lower level
\r
1676 ======================
\r
1679 void CA_DownLevel (void)
\r
1682 Quit ("CA_DownLevel: Down past level 0!");
\r
1688 //===========================================================================
\r
1691 ======================
\r
1695 = Clears out all the marks at the current level
\r
1697 ======================
\r
1700 void CA_ClearMarks (void)
\r
1704 for (i=0;i<NUMCHUNKS;i++)
\r
1705 grneeded[i]&=~ca_levelbit;
\r
1709 //===========================================================================
\r
1712 ======================
\r
1714 = CA_ClearAllMarks
\r
1716 = Clears out all the marks on all the levels
\r
1718 ======================
\r
1721 void CA_ClearAllMarks (void)
\r
1723 _fmemset (grneeded,0,sizeof(grneeded));
\r
1729 //===========================================================================
\r
1733 ======================
\r
1737 ======================
\r
1741 void CA_SetGrPurge (void)
\r
1750 for (i=0;i<NUMCHUNKS;i++)
\r
1752 MM_SetPurge (MEMPTRCONV grsegs[i],3);
\r
1758 ======================
\r
1762 = Make everything possible purgable
\r
1764 ======================
\r
1767 void CA_SetAllPurge (void)
\r
1775 for (i=0;i<NUMSNDCHUNKS;i++)
\r
1777 MM_SetPurge (MEMPTRCONV audiosegs[i],3);
\r
1786 //===========================================================================
\r
1789 ======================
\r
1793 ======================
\r
1795 #define MAXEMPTYREAD 1024
\r
1797 void CA_CacheMarks (void)
\r
1799 int i,next,numcache;
\r
1800 long pos,endpos,nextpos,nextendpos,compressed;
\r
1801 long bufferstart,bufferend; // file position of general buffer
\r
1803 memptr bigbufferseg;
\r
1807 // go through and make everything not needed purgable
\r
1809 for (i=0;i<NUMCHUNKS;i++)
\r
1810 if (grneeded[i]&ca_levelbit)
\r
1812 if (grsegs[i]) // its allready in memory, make
\r
1813 MM_SetPurge(&grsegs[i],0); // sure it stays there!
\r
1819 if (grsegs[i]) // not needed, so make it purgeable
\r
1820 MM_SetPurge(&grsegs[i],3);
\r
1823 if (!numcache) // nothing to cache!
\r
1828 // go through and load in anything still needed
\r
1830 bufferstart = bufferend = 0; // nothing good in buffer now
\r
1832 for (i=0;i<NUMCHUNKS;i++)
\r
1833 if ( (grneeded[i]&ca_levelbit) && !grsegs[i])
\r
1835 pos = GRFILEPOS(i);
\r
1840 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1843 compressed = GRFILEPOS(next)-pos;
\r
1844 endpos = pos+compressed;
\r
1846 if (compressed<=BUFFERSIZE)
\r
1848 if (bufferstart<=pos
\r
1849 && bufferend>= endpos)
\r
1851 // data is allready in buffer
\r
1852 source = (byte _seg *)bufferseg+(pos-bufferstart);
\r
1856 // load buffer with a new block from disk
\r
1857 // try to get as many of the needed blocks in as possible
\r
1858 while ( next < NUMCHUNKS )
\r
1860 while (next < NUMCHUNKS &&
\r
1861 !(grneeded[next]&ca_levelbit && !grsegs[next]))
\r
1863 if (next == NUMCHUNKS)
\r
1866 nextpos = GRFILEPOS(next);
\r
1867 while (GRFILEPOS(++next) == -1) // skip past any sparse tiles
\r
1869 nextendpos = GRFILEPOS(next);
\r
1870 if (nextpos - endpos <= MAXEMPTYREAD
\r
1871 && nextendpos-pos <= BUFFERSIZE)
\r
1872 endpos = nextendpos;
\r
1874 next = NUMCHUNKS; // read pos to posend
\r
1877 lseek(grhandle,pos,SEEK_SET);
\r
1878 CA_FarRead(grhandle,bufferseg,endpos-pos);
\r
1879 bufferstart = pos;
\r
1880 bufferend = endpos;
\r
1881 source = bufferseg;
\r
1886 // big chunk, allocate temporary buffer
\r
1887 MM_GetPtr(MEMPTRANDPERCONV bigbufferseg,compressed);
\r
1890 MM_SetLock (MEMPTRANDPERCONV bigbufferseg,true);
\r
1891 lseek(grhandle,pos,SEEK_SET);
\r
1892 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1893 source = bigbufferseg;
\r
1896 CAL_ExpandGrChunk (i,source);
\r
1900 if (compressed>BUFFERSIZE)
\r
1901 MM_FreePtr(MEMPTRANDPERCONV bigbufferseg);
\r
1906 void CA_CannotOpen(char *string)
\r
1910 strcpy(str,"Can't open ");
\r
1911 strcat(str,string);
\r
1912 strcat(str,"!\n");
\r