Advertisement
Siapran

map.c

Oct 2nd, 2011
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. #include "map.h"
  2.  
  3. Map * map_Load(ARCH * archmap, ARCH * archtile, const char * mapFileName, const char * tilesFileName)
  4. {
  5.     Map * map;
  6.     register unsigned long i,j;
  7.     ARCH * tilesFile;
  8.     ARCH * mapFile;
  9.     unsigned short nbTiles, Width, Height;
  10.     unsigned long lWidth, lHeight;
  11.     unsigned char * tmp;
  12.  
  13.  
  14.     map = (Map *)malloc(sizeof(Map));
  15.    
  16.  
  17.     tilesFile = aopen(archtile, tilesFileName);
  18.     mapFile = aopen(archmap, mapFileName);
  19.  
  20.     aread (&(map->dimX), sizeof(short), 1, mapFile);
  21.     aread (&(map->dimY), sizeof(short), 1, mapFile);
  22.     aseek (tilesFile, 2, SEEK_SET);
  23.     aread (&nbTiles, sizeof(short), 1, tilesFile);
  24.     aread (&Width, sizeof(short), 1, tilesFile);
  25.     aread (&Height, sizeof(short), 1, tilesFile);
  26.     lWidth = (long)Width;
  27.     lHeight = (long)Height;
  28.  
  29.     tmp = (unsigned char *)malloc(lWidth * lHeight * sizeof(unsigned char));
  30.  
  31.     map->map = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY);
  32.     map->map0 = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY * lWidth * lHeight);
  33. #if ENABLE_GRAYSCALE == 1
  34.     map->map1 = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY * lWidth * lHeight);
  35. #endif
  36.  
  37.  
  38.     for(i=0;i < ((map->dimX) * (map->dimY));i++)
  39.     {
  40.         aread (&(map->map[i]), sizeof(char), 1, mapFile);
  41.     }
  42.  
  43.     for(i=0;i < ((map->dimX) * (map->dimY));i++)
  44.     {
  45.  
  46. #if ENABLE_GRAYSCALE == 1
  47.         aseek (tilesFile, 8 + (map->map[i]) * lWidth * lHeight * 2, SEEK_SET);
  48. #else
  49.         aseek (tilesFile, 8 + (map->map[i]) * lWidth * lHeight, SEEK_SET);
  50. #endif
  51.  
  52.  
  53.         aread(tmp, sizeof(unsigned char), lWidth * lHeight, tilesFile);
  54.  
  55.  
  56.         for(j=0; j<lHeight; j++)
  57.         {
  58.             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);
  59.         }
  60. #if ENABLE_GRAYSCALE == 1
  61.         aread(tmp, sizeof(unsigned char), lWidth * lHeight, tilesFile);
  62.         for(j=0; j<lHeight; j++)
  63.         {
  64.             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);
  65.         }
  66. #endif
  67.  
  68.     }
  69.  
  70.     aclose(tilesFile);
  71.     aclose(mapFile);
  72.  
  73.     map->dimY *= lHeight;
  74.     map->dimX *= lHeight;
  75.  
  76.     free(tmp);
  77.     return map;
  78.    
  79. }
  80.  
  81. void map_Free(Map * map)
  82. {
  83.     free(map->map);
  84.     free(map->map0);
  85. #if ENABLE_GRAYSCALE == 1
  86.     free(map->map1);
  87. #endif
  88.     free(map);
  89. }
  90.  
  91.  
  92. #if ENABLE_LARGE_MAPS == 2
  93.  
  94. Tiles * map_TilesLoad(ARCH * arch, const char * tilesFileName, const char * tilesSelectionFileName)
  95. {
  96.     register unsigned short i, j, k;
  97.     Tiles * tiles;
  98.     ARCH * tilesFile;
  99.     ARCH * selFile;
  100.     unsigned short nbTiles, nbRanges;
  101.     unsigned char byte, * offset, * ranges, * tilesnum;
  102.     tilesFile = aopen(arch, tilesFileName);
  103.     selFile = aopen(arch, tilesSelectionFileName);
  104.  
  105.     tiles = (Tiles*)malloc(sizeof(Tiles));
  106.     aread (&(tiles->nbPlanes), sizeof(short), 1, tilesFile);
  107.     aread (&nbTiles, sizeof(short), 1, tilesFile);
  108.     aread (&nbTiles, sizeof(short), 1, selFile);
  109.     aread (&nbRanges, sizeof(short), 1, selFile);
  110.    
  111.     aread (&(tiles->tilesWidth), sizeof(short), 1, tilesFile);
  112.     aread (&(tiles->tilesHeight), sizeof(short), 1, tilesFile);
  113.  
  114.     ranges = (unsigned char *)malloc(sizeof(unsigned char *) * nbRanges * 2);
  115.     tilesnum = (unsigned char *)malloc(sizeof(unsigned char *) * nbTiles);
  116.     for(i=0;i<nbRanges;i++)
  117.     {
  118.         aread (&(ranges[i * 2]), sizeof(unsigned char), 1, selFile);
  119.         aread (&(ranges[(i * 2)+1]), sizeof(unsigned char), 1, selFile);
  120.     }
  121.     aclose(selFile);
  122.  
  123.     k = 0;
  124.     for(i=0;i<nbRanges;i++)
  125.     {
  126.         for(j=ranges[i * 2]; j <= ranges[(i * 2)+1]; j++)
  127.             tilesnum[k++] = j;
  128.     }
  129.     free(ranges);
  130.  
  131.  
  132.     tiles->tiles = (unsigned char *)malloc(sizeof(unsigned char) * tiles->tilesWidth * tiles->tilesHeight * nbTiles * 8 * tiles->nbPlanes);
  133.  
  134.  
  135.     offset = tiles->tiles;
  136.  
  137.     for (i=0; i < nbTiles; i++)
  138.     {
  139.         aseek(tilesFile, 8 + (tilesnum[i] * tiles->tilesWidth * tiles->tilesHeight * tiles->nbPlanes), SEEK_SET);
  140.         for(j=0; j < (tiles->tilesWidth * tiles->tilesHeight * tiles->nbPlanes); j++)
  141.         {
  142.             aread (&byte, sizeof(unsigned char), 1, tilesFile);
  143.             for (k=0; k<8; k++)
  144.             {
  145.                 *offset = (byte >> (7-k)) & 0x1;
  146.                 offset++;
  147.             }
  148.         }
  149.     }
  150.  
  151.     free(tilesnum);
  152.  
  153.     aclose(tilesFile);
  154.    
  155.     return tiles;
  156. }
  157.  
  158. void map_TilesFree(Tiles * tiles)
  159. {
  160.     free(tiles->tiles);
  161.     free(tiles);
  162. }
  163.  
  164. ExtraMap * map_XMapLoad(ARCH * arch, const char * mapFileName, Tiles * tiles)
  165. {
  166.     ExtraMap * map;
  167.     register unsigned short i;
  168.     ARCH * mapFile;
  169.     unsigned short tilesoffset;
  170.  
  171.     map = (ExtraMap *)malloc(sizeof(ExtraMap));
  172.    
  173.  
  174.     mapFile = aopen(arch, mapFileName);
  175.  
  176.     aread (&(map->dimX), sizeof(short), 1, mapFile);
  177.     aread (&(map->dimY), sizeof(short), 1, mapFile);
  178.  
  179.     map->map = (unsigned char *)malloc(sizeof(unsigned char) * map->dimX * map->dimY);
  180.     map->maptiles = (unsigned char **)malloc(sizeof(unsigned char *) * map->dimX * map->dimY);
  181.     map->tiles = tiles;
  182.    
  183.     tilesoffset = tiles->tilesWidth * tiles->tilesHeight * 8;
  184.  
  185.     for(i=0;i < ((map->dimX) * (map->dimY));i++)
  186.     {
  187.         aread (&(map->map[i]), sizeof(char), 1, mapFile);
  188. #if ENABLE_GRAYSCALE == 1
  189.         map->maptiles[i] = (tiles->tiles) + (map->map[i] * tilesoffset * 2);
  190. #else
  191.         map->maptiles[i] = (tiles->tiles) + (map->map[i] * tilesoffset);
  192. #endif
  193.     }
  194.  
  195.     aclose(mapFile);
  196.  
  197.     return map;
  198.  
  199. }
  200.  
  201. void map_XMapFree(ExtraMap * map)
  202. {
  203.     free(map->map);
  204.     free(map->maptiles);
  205.     free(map);
  206. }
  207.  
  208. #endif
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement