Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "map.h"
- Map * map_Load(ARCH * archmap, ARCH * archtile, const char * mapFileName, const char * tilesFileName)
- {
- Map * map;
- register unsigned long i,j;
- ARCH * tilesFile;
- ARCH * mapFile;
- unsigned short nbTiles, Width, Height;
- unsigned long lWidth, lHeight;
- unsigned char * tmp;
- map = (Map *)malloc(sizeof(Map));
- tilesFile = aopen(archtile, tilesFileName);
- mapFile = aopen(archmap, mapFileName);
- aread (&(map->dimX), sizeof(short), 1, mapFile);
- aread (&(map->dimY), sizeof(short), 1, mapFile);
- aseek (tilesFile, 2, SEEK_SET);
- aread (&nbTiles, sizeof(short), 1, tilesFile);
- aread (&Width, sizeof(short), 1, tilesFile);
- aread (&Height, sizeof(short), 1, tilesFile);
- lWidth = (long)Width;
- lHeight = (long)Height;
- tmp = (unsigned char *)malloc(lWidth * lHeight * sizeof(unsigned char));
- map->map = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY);
- map->map0 = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY * lWidth * lHeight);
- #if ENABLE_GRAYSCALE == 1
- map->map1 = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY * lWidth * lHeight);
- #endif
- for(i=0;i < ((map->dimX) * (map->dimY));i++)
- {
- aread (&(map->map[i]), sizeof(char), 1, mapFile);
- }
- for(i=0;i < ((map->dimX) * (map->dimY));i++)
- {
- #if ENABLE_GRAYSCALE == 1
- aseek (tilesFile, 8 + (map->map[i]) * lWidth * lHeight * 2, SEEK_SET);
- #else
- aseek (tilesFile, 8 + (map->map[i]) * lWidth * lHeight, SEEK_SET);
- #endif
- aread(tmp, sizeof(unsigned char), lWidth * lHeight, tilesFile);
- for(j=0; j<lHeight; j++)
- {
- memcpy(&(map->map0[((i * lWidth) % (map->dimX * lWidth)) + (map->dimX * lWidth * lHeight) * ((i * lWidth)/(map->dimX * lWidth)) + (j * (map->dimX * lWidth))]), &(tmp[j * lWidth]), lWidth);
- }
- #if ENABLE_GRAYSCALE == 1
- aread(tmp, sizeof(unsigned char), lWidth * lHeight, tilesFile);
- for(j=0; j<lHeight; j++)
- {
- memcpy(&(map->map1[((i * lWidth) % (map->dimX * lWidth)) + (map->dimX * lWidth * lHeight) * ((i * lWidth)/(map->dimX * lWidth)) + (j * (map->dimX * lWidth))]), &(tmp[j * lWidth]), lWidth);
- }
- #endif
- }
- aclose(tilesFile);
- aclose(mapFile);
- map->dimY *= lHeight;
- map->dimX *= lHeight;
- free(tmp);
- return map;
- }
- void map_Free(Map * map)
- {
- free(map->map);
- free(map->map0);
- #if ENABLE_GRAYSCALE == 1
- free(map->map1);
- #endif
- free(map);
- }
- #if ENABLE_LARGE_MAPS == 2
- Tiles * map_TilesLoad(ARCH * arch, const char * tilesFileName, const char * tilesSelectionFileName)
- {
- register unsigned short i, j, k;
- Tiles * tiles;
- ARCH * tilesFile;
- ARCH * selFile;
- unsigned short nbTiles, nbRanges;
- unsigned char byte, * offset, * ranges, * tilesnum;
- tilesFile = aopen(arch, tilesFileName);
- selFile = aopen(arch, tilesSelectionFileName);
- tiles = (Tiles*)malloc(sizeof(Tiles));
- aread (&(tiles->nbPlanes), sizeof(short), 1, tilesFile);
- aread (&nbTiles, sizeof(short), 1, tilesFile);
- aread (&nbTiles, sizeof(short), 1, selFile);
- aread (&nbRanges, sizeof(short), 1, selFile);
- aread (&(tiles->tilesWidth), sizeof(short), 1, tilesFile);
- aread (&(tiles->tilesHeight), sizeof(short), 1, tilesFile);
- ranges = (unsigned char *)malloc(sizeof(unsigned char *) * nbRanges * 2);
- tilesnum = (unsigned char *)malloc(sizeof(unsigned char *) * nbTiles);
- for(i=0;i<nbRanges;i++)
- {
- aread (&(ranges[i * 2]), sizeof(unsigned char), 1, selFile);
- aread (&(ranges[(i * 2)+1]), sizeof(unsigned char), 1, selFile);
- }
- aclose(selFile);
- k = 0;
- for(i=0;i<nbRanges;i++)
- {
- for(j=ranges[i * 2]; j <= ranges[(i * 2)+1]; j++)
- tilesnum[k++] = j;
- }
- free(ranges);
- tiles->tiles = (unsigned char *)malloc(sizeof(unsigned char) * tiles->tilesWidth * tiles->tilesHeight * nbTiles * 8 * tiles->nbPlanes);
- offset = tiles->tiles;
- for (i=0; i < nbTiles; i++)
- {
- aseek(tilesFile, 8 + (tilesnum[i] * tiles->tilesWidth * tiles->tilesHeight * tiles->nbPlanes), SEEK_SET);
- for(j=0; j < (tiles->tilesWidth * tiles->tilesHeight * tiles->nbPlanes); j++)
- {
- aread (&byte, sizeof(unsigned char), 1, tilesFile);
- for (k=0; k<8; k++)
- {
- *offset = (byte >> (7-k)) & 0x1;
- offset++;
- }
- }
- }
- free(tilesnum);
- aclose(tilesFile);
- return tiles;
- }
- void map_TilesFree(Tiles * tiles)
- {
- free(tiles->tiles);
- free(tiles);
- }
- ExtraMap * map_XMapLoad(ARCH * arch, const char * mapFileName, Tiles * tiles)
- {
- ExtraMap * map;
- register unsigned short i;
- ARCH * mapFile;
- unsigned short tilesoffset;
- map = (ExtraMap *)malloc(sizeof(ExtraMap));
- mapFile = aopen(arch, mapFileName);
- aread (&(map->dimX), sizeof(short), 1, mapFile);
- aread (&(map->dimY), sizeof(short), 1, mapFile);
- map->map = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY);
- map->maptiles = (unsigned char **)malloc(sizeof(unsigned char *) * map->dimX * map->dimY);
- map->tiles = tiles;
- tilesoffset = tiles->tilesWidth * tiles->tilesHeight * 8;
- for(i=0;i < ((map->dimX) * (map->dimY));i++)
- {
- aread (&(map->map[i]), sizeof(char), 1, mapFile);
- #if ENABLE_GRAYSCALE == 1
- map->maptiles[i] = (tiles->tiles) + (map->map[i] * tilesoffset * 2);
- #else
- map->maptiles[i] = (tiles->tiles) + (map->map[i] * tilesoffset);
- #endif
- }
- aclose(mapFile);
- return map;
- }
- void map_XMapFree(ExtraMap * map)
- {
- free(map->map);
- free(map->maptiles);
- free(map);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement