/***************************************************************************** * * IO.cc * * Generic I/O routine * *****************************************************************************/ #include #include #include "io.h" /***************************************************************************** * * LoadBinaryFile(char *filename) * * Uses GS/OS native routines to read in a file * *****************************************************************************/ Handle LoadBinaryFile( char *filename ) { RefNumRecGS clRec; /* CloseGS record */ OpenRecGS opRec; /* OpenGS record */ IORecGS rdRec; /* ReadGS record */ GSString255 fileStr; Handle hndl = NULL; /* handle to the memory it's in */ fileStr.length = strlen(filename); /* make the file string */ strcpy(fileStr.text, filename); opRec.pCount = 12; /* open the file */ opRec.pathname = &fileStr; opRec.requestAccess = 1; opRec.resourceNumber = 0; opRec.optionList = NULL; OpenGS(&opRec); if (toolerror()) return NULL; clRec.pCount = 1; clRec.refNum = opRec.refNum; /* If the file is less than 64K, try and put it within a single bank */ if ( opRec.eof <= 0x010000 ) hndl = NewHandle(opRec.eof, myMemoryID, 0x8010, NULL); else hndl = NewHandle(opRec.eof, myMemoryID, 0x8000, NULL); if (toolerror()) { DisposeHandle(hndl); /* get rid of our memeory */ hndl = NULL; goto fail; } rdRec.pCount = 4; /* read in the file */ rdRec.refNum = opRec.refNum; rdRec.dataBuffer = *hndl; rdRec.requestCount = opRec.eof; ReadGS(&rdRec); /* call the OS */ if (toolerror()) { DisposeHandle(hndl); /* get rid of our memeory */ hndl = NULL; } fail: CloseGS(&clRec); /* close the file */ return hndl; /* return the handle */ } /***************************************************************************** * * LoadGTEMap(char *filename) * * Uses GS/OS native routines to read in a GTE map file * *****************************************************************************/ Handle LoadGTEMap( char *filename ) { RefNumRecGS clRec; /* CloseGS record */ OpenRecGS opRec; /* OpenGS record */ IORecGS rdRec; /* ReadGS record */ GSString255 fileStr; SetPositionRecGS spRec; /* SetMarkGS record */ Handle hndl = NULL; /* handle to the memory it's in */ char kind[4]; Word mapWidth, mapHeight; long displacement = 0; fileStr.length = strlen(filename); /* make the file string */ strcpy(fileStr.text, filename); opRec.pCount = 12; /* open the file */ opRec.pathname = &fileStr; opRec.requestAccess = 1; opRec.resourceNumber = 0; opRec.optionList = NULL; OpenGS(&opRec); if (toolerror()) { printf( "Could not open file\n" ); return NULL; } clRec.pCount = 1; clRec.refNum = opRec.refNum; /* First read the block header to see if it is the "GTE2" block */ rdRec.pCount = 4; rdRec.refNum = opRec.refNum; rdRec.dataBuffer = (Pointer) kind; rdRec.requestCount = 4; ReadGS(&rdRec); if (toolerror() != 0) { printf( "Failed to read file\n" ); goto fail; } /* If this is not the GTE2 block, return a NULL handle */ if ( strncmp( "GTE2", kind, 4 ) != 0 ) { printf( "Not GTE2 file\n" ); goto fail; } /* It if our file, so read in the width and height of the map */ rdRec.dataBuffer = (Pointer) &mapWidth; rdRec.requestCount = 2; ReadGS(&rdRec); rdRec.dataBuffer = (Pointer) &mapHeight; rdRec.requestCount = 2; ReadGS(&rdRec); if ( mapHeight * mapWidth > 32677 ) { printf( "Map size too large\n" ); goto fail; } /* These parameters remain the same in the inner loop */ spRec.pCount = 3; spRec.refNum = opRec.refNum; spRec.base = markPlus; spRec.displacement = 0; /* Keep track of our byte offset */ displacement = 8; /* For now, just run through and load in the FGND and FFRG layers */ while ( displacement < opRec.eof ) { /* Read the name of the current layer */ rdRec.dataBuffer = (Pointer) kind; rdRec.requestCount = 4; ReadGS(&rdRec); if (toolerror() != 0) { printf( "Error reading layer header\n" ); break; } if ( strncmp( "FGND", kind, 4 ) != 0 ) { printf( "Found FGND\n" ); /* Allocate the buffer and read in the data. */ hndl = NewHandle( 2 * mapHeight * mapWidth, myMemoryID, 0x8014, 0 ); rdRec.dataBuffer = (Pointer) *hndl; rdRec.requestCount = 2 * mapHeight * mapWidth; ReadGS(&rdRec); /* Set the engine to point to the data */ /*GTESetBG0TileMap( *hndl, mapWidth, mapHeight); */ break; } else { printf( "Skipping layer\n" ); /* Skip the rest of this layer */ spRec.displacement = 2 * mapWidth * mapHeight; spRec.base = markPlus; SetMarkGS( &spRec ); displacement += 2 * mapWidth * mapHeight; } } fail: CloseGS(&clRec); /* close the file */ return hndl; /* return the handle */ }