Word OpenAResourceFile(Byte *FileName) { MyRezHeader MyHeader; /* Struct for resource file header */ void **RezHandle; Word Index; Stream *fp; Index = OpenFileCount; if (Index<MAXREZFILES) { /* Open index? */ fp = OpenDiskStream((char *)FileName,0); /* Open the resource file */ if (!fp) { /* Did the file open? */ NonFatal("Can't open the resource file"); /* Nope, leave */ return -1; } ReadDiskStream(fp,(char *)&MyHeader,sizeof(MyHeader)); /* Read in the header */ if (memcmp((char *)&MyHeader.Name,"BRGR",4)) { /* Valid header? */ NonFatal("Invalid Resource file!"); return -1; } RezHandle = AllocAHandle2(MyHeader.MemSize,0xFFF1UL<<16); /* Get the memory */ if (!RezHandle) { CloseDiskStream(fp); /* Release the file */ return -1; /* Return the error code from memory manager */ } ReadDiskStream(fp,(char *)LockAHandle(RezHandle),MyHeader.MemSize); UnlockAHandle(RezHandle); Rezfp[Index] = fp; /* Save the file referance */ RezCounts[Index] = MyHeader.Count; /* Get the resource count */ RezHandles[Index] = RezHandle; /* Get the memory */ OpenFileCount = Index+1; /* I now have an open file! */ return Index; } NonFatal("Resource map full!"); /* Too many files! */ return -1; }
static void LoadBlockMap(Word lump) { Word Count; Word Entries; Byte *MyLumpPtr; void **BlockHandle; LongWord *StartIndex; BlockHandle = LoadAResourceHandle(lump); /* Load the data */ MyLumpPtr = (Byte *)LockAHandle(BlockHandle); BlockMapOrgX = ((Word *)MyLumpPtr)[0]; /* Get the orgx and y */ BlockMapOrgY = ((Word *)MyLumpPtr)[1]; BlockMapWidth = ((Word *)MyLumpPtr)[2]; /* Get the map size */ BlockMapHeight = ((Word *)MyLumpPtr)[3]; Entries = BlockMapWidth*BlockMapHeight; /* How many entries are there? */ /* Convert the loaded block map table into a huge array of block entries */ Count = Entries; /* Init the longword count */ StartIndex = &((LongWord *)MyLumpPtr)[4]; /* Index to the offsets! */ BlockMapLines = (line_t ***)StartIndex; /* Save in global */ do { StartIndex[0] = (LongWord)&MyLumpPtr[StartIndex[0]]; /* Convert to pointer */ ++StartIndex; /* Next offset entry */ } while (--Count); /* All done? */ /* Convert the lists appended to the array into pointers to lines */ Count = GetAHandleSize(BlockHandle)/4; /* How much memory is needed? (Longs) */ Count -= (Entries+4); /* Remove the header count */ do { if (StartIndex[0]!=-1) { /* End of a list? */ StartIndex[0] = (LongWord)&lines[StartIndex[0]]; /* Get the line pointer */ } else { StartIndex[0] = 0; /* Insert a null pointer */ } ++StartIndex; /* Next entry */ } while (--Count); /* Clear out mobj chains */ Count = sizeof(*BlockLinkPtr)*Entries; /* Get memory */ BlockLinkPtr = (mobj_t **)AllocAPointer(Count); /* Allocate memory */ memset(BlockLinkPtr,0,Count); /* Clear it out */ }
void CloseAResourceFile(Word RezRef) { if (RezRef<OpenFileCount) { /* Failsafe */ Word GroupCount; /* Number of resource groups */ CloseDiskStream(Rezfp[RezRef]); /* Close the open resource file */ GroupCount = RezCounts[RezRef]; /* Any valid entries? */ if (GroupCount) { MyRezEntry *MainPtr; /* Group array pointer */ MainPtr = (MyRezEntry*)LockAHandle(RezHandles[RezRef]); /* Lock it down */ do { Word EntryCount; /* Number of entries per group */ MyRezEntry2 *EntryPtr; /* Entry pointer */ EntryPtr = &MainPtr->Array[0]; EntryCount = MainPtr->Count; do { if (EntryPtr->MemPtr) { /* Dispose of all handles */ DeallocAHandle(EntryPtr->MemPtr); } ++EntryPtr; /* Next entry */ } while (--EntryCount); MainPtr = (MyRezEntry *)EntryPtr; } while (--GroupCount); } DeallocAHandle(RezHandles[RezRef]); /* Free the resource map */ --OpenFileCount; /* Remove from the list */ if (RezRef<OpenFileCount) { /* Not the last one? */ do { Word Next; /* Move all other entries down one */ Next = RezRef+1; Rezfp[RezRef] = Rezfp[Next]; RezHandles[RezRef] = RezHandles[Next]; RezCounts[RezRef] = RezCounts[Next]; RezRef = Next; } while (RezRef<OpenFileCount); } } }