Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. unsigned char** MapIndexTileset = NULL;
  2. unsigned char** MapIndexNPCSheet = NULL;
  3. unsigned char** MapIndexBackgroundImage = NULL;
  4. unsigned char** MapIndexName = NULL;
  5. unsigned char** CurrentMapIndex = NULL;
  6.  
  7. unsigned char* MapIndexBackgroundNum = NULL;
  8. unsigned char* MapIndexForegroundNum = NULL;
  9. int* MapIndexWaterLevel = NULL;
  10. unsigned char* MapIndexWaterScale = NULL;
  11.  
  12. /*struct MapIndexData {
  13. unsigned char BackgroundNum;
  14. unsigned char ForegroundNum;
  15. unsigned char WaterScale;
  16. int WaterLevel;
  17. } *MapIndexData
  18.  
  19. MapIntexData* Map
  20. */
  21.  
  22. void LoadMapDataFromFile(void) {
  23. printf("importing MapData File!\n");
  24. char MapDataString[50];
  25. snprintf(MapDataString, sizeof(MapDataString), "data/Maplist");
  26. FILE *mapdatafile = fopen(MapDataString, "rb");
  27. fseek(mapdatafile, 0, SEEK_END);
  28. mapdata_file_size = ftell(mapdatafile);
  29. rewind(mapdatafile);
  30. //grab first 2 bytes from file to know how many maps there are
  31. unsigned char byte1 = fgetc(mapdatafile);
  32. unsigned char byte2 = fgetc(mapdatafile);
  33. int MapsInFile = (byte1 << 8) | byte2;
  34. //close file
  35. mapdata_buffer = (unsigned char*)malloc(mapdata_file_size - 2);
  36. fread(mapdata_buffer, 1, mapdata_file_size - 2, mapdatafile);
  37. fclose(mapdatafile);
  38. //make malloc of unsigned char pointers to point to each string
  39. int BufferLoc = 0;
  40. MapIndexTileset = malloc(sizeof(unsigned char*) * MapsInFile);
  41. MapIndexNPCSheet = malloc(sizeof(unsigned char*) * MapsInFile);
  42. MapIndexBackgroundImage = malloc(sizeof(unsigned char*) * MapsInFile);
  43. MapIndexName = malloc(sizeof(unsigned char*) * MapsInFile);
  44. MapIndexBackgroundNum = malloc(sizeof(unsigned char) * MapsInFile);
  45. MapIndexForegroundNum = malloc(sizeof(unsigned char) * MapsInFile);
  46. MapIndexWaterLevel = malloc(sizeof(int) * MapsInFile);
  47. MapIndexWaterScale = malloc(sizeof(unsigned char) * MapsInFile);
  48.  
  49. //printf("There are %d Maps in the Maplist.\n", MapsInFile);
  50. for (int TimesLooped = 0; TimesLooped < MapsInFile; TimesLooped++) {
  51. for (unsigned char StringType = 0; StringType < 4; StringType++) {
  52. //printf("Loading String Type %d.\n", StringType);
  53. switch (StringType) {
  54. case 0:;
  55. CurrentMapIndex = MapIndexTileset;
  56. break;
  57. case 1:;
  58. CurrentMapIndex = MapIndexNPCSheet;
  59. break;
  60. case 2:;
  61. CurrentMapIndex = MapIndexBackgroundImage;
  62. break;
  63. case 3:;
  64. CurrentMapIndex = MapIndexName;
  65. break;
  66. default:;
  67. printf("ERROR, UNKNOWN KIND OF STRING TYPE ON MAP FILE LOAD!\n");
  68. break;
  69. }
  70.  
  71. unsigned char SizeOfString = 0;
  72. while (mapdata_buffer[BufferLoc+SizeOfString] != 0) {
  73. SizeOfString++;
  74. }
  75. //printf("Size of this String is %d.\n", SizeOfString);
  76. //make malloc for the pointer
  77. unsigned char** ThisCharString = malloc(SizeOfString * sizeof(unsigned char));
  78. //put pointer to the data in the index
  79. CurrentMapIndex[TimesLooped] = *ThisCharString;
  80. //dump the data from buffer into malloc, including termination character
  81. int c;
  82. for (c = 0; c < SizeOfString+1; c++) {
  83. ThisCharString[c] = mapdata_buffer[BufferLoc+c];
  84. if (ThisCharString[c] != 0) {
  85. //printf("loaded character %c into a character string!\n", ThisCharString[c]);
  86. }
  87. else {
  88. //printf("termination character found.\n");
  89. }
  90. }
  91. //the +1 above is to pass the termination character in the file
  92. BufferLoc += c;
  93. SizeOfString = 0;
  94. }
  95. //put rest of data for BK number, FG number, Water level and water scaling into their arrays.
  96. MapIndexBackgroundNum[TimesLooped] = mapdata_buffer[BufferLoc];
  97. BufferLoc++;
  98. MapIndexForegroundNum[TimesLooped] = mapdata_buffer[BufferLoc];
  99. BufferLoc++;
  100. unsigned char byte1 = mapdata_buffer[BufferLoc];
  101. unsigned char byte2 = mapdata_buffer[BufferLoc+1];
  102. MapIndexWaterLevel[TimesLooped] = (byte2 << 8) | byte1;
  103. BufferLoc += 2;
  104. MapIndexWaterScale[TimesLooped] = mapdata_buffer[BufferLoc];
  105. BufferLoc++;
  106. //printf("Map %d's mapdata loaded into memory!\n", TimesLooped);
  107. }
  108.  
  109. }
  110.  
  111. void LoadMapSpritesheets(void) {
  112.  
  113. ResetBackgrounds();
  114. LoadBackground(MapIndexBackgroundNum[MapNumber], 0);
  115. LoadBackground(MapIndexForegroundNum[MapNumber], 1);
  116. WaterHeight = *MapIndexWaterLevel * 0x20;
  117. WaterHeight -= (0x200*10);
  118. WaterScalingOffset = *MapIndexWaterScale;
  119.  
  120. printf("loading tileset %s....\n", MapIndexTileset[MapNumber]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement