Skip to content

Instantly share code, notes, and snippets.

@LeonBlade
Created August 23, 2011 11:22
Show Gist options
  • Save LeonBlade/1164890 to your computer and use it in GitHub Desktop.
Save LeonBlade/1164890 to your computer and use it in GitHub Desktop.
Blade Brother's Map OnLoad
bool Map::OnLoad(const char* filename)
{
FILE* fileHandle = fopen(filename, "r");
if (fileHandle == NULL)
return false;
// read in the magic
fread(&magic, 4, 1, fileHandle);
if (magic != 1179468354)
return false;
fread(&mapHeader.tilesX, sizeof(unsigned short), 1, fileHandle);
fread(&mapHeader.tilesY, sizeof(unsigned short), 1, fileHandle);
fread(&mapHeader.mapNameSize, sizeof(unsigned short), 1, fileHandle);
mapHeader.mapName = (char*)malloc(mapHeader.mapNameSize);
fread(mapHeader.mapName, mapHeader.mapNameSize, 1, fileHandle);
mapHeader.mapName[mapHeader.mapNameSize] = '\0';
fread(&mapHeader.tilesetNameSize, sizeof(unsigned short), 1, fileHandle);
mapHeader.tileset = (char*)malloc(mapHeader.tilesetNameSize);
fread(mapHeader.tileset, mapHeader.tilesetNameSize, 1, fileHandle);
mapHeader.tileset[mapHeader.tilesetNameSize] = '\0';
fread(&mapHeader.startX, sizeof(unsigned short), 1, fileHandle);
fread(&mapHeader.startY, sizeof(unsigned short), 1, fileHandle);
unsigned int tileCount = mapHeader.tilesX * mapHeader.tilesY;
unsigned int tileBufSize = tileCount * sizeof(MapData);
mapData = (MapData*) malloc(tileBufSize);
// read in map data
fread(mapData, tileBufSize, 1, fileHandle);
fclose(fileHandle);
// by passing NULL we tell the function we don't want to re-assign the tileset name itself
setTileset(NULL);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment