res.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  3. // All rights reserved.
  4. //
  5. // This file is part of SDLPAL.
  6. //
  7. // SDLPAL is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. #include "main.h"
  21. typedef struct tagRESOURCES
  22. {
  23. BYTE bLoadFlags;
  24. LPPALMAP lpMap; // current loaded map
  25. LPSPRITE *lppEventObjectSprites; // event object sprites
  26. int nEventObject; // number of event objects
  27. LPSPRITE rglpPlayerSprite[MAX_PLAYERS_IN_PARTY + 1]; // player sprites
  28. } RESOURCES, *LPRESOURCES;
  29. static LPRESOURCES gpResources = NULL;
  30. static VOID
  31. PAL_FreeEventObjectSprites(
  32. VOID
  33. )
  34. /*++
  35. Purpose:
  36. Free all sprites of event objects on the scene.
  37. Parameters:
  38. None.
  39. Return value:
  40. None.
  41. --*/
  42. {
  43. int i;
  44. if (gpResources->lppEventObjectSprites != NULL)
  45. {
  46. for (i = 0; i < gpResources->nEventObject; i++)
  47. {
  48. free(gpResources->lppEventObjectSprites[i]);
  49. }
  50. free(gpResources->lppEventObjectSprites);
  51. gpResources->lppEventObjectSprites = NULL;
  52. gpResources->nEventObject = 0;
  53. }
  54. }
  55. static VOID
  56. PAL_FreePlayerSprites(
  57. VOID
  58. )
  59. /*++
  60. Purpose:
  61. Free all player sprites.
  62. Parameters:
  63. None.
  64. Return value:
  65. None.
  66. --*/
  67. {
  68. int i;
  69. for (i = 0; i < MAX_PLAYERS_IN_PARTY + 1; i++)
  70. {
  71. free(gpResources->rglpPlayerSprite[i]);
  72. gpResources->rglpPlayerSprite[i] = NULL;
  73. }
  74. }
  75. VOID
  76. PAL_InitResources(
  77. VOID
  78. )
  79. /*++
  80. Purpose:
  81. Initialze the resource manager.
  82. Parameters:
  83. None.
  84. Return value:
  85. None.
  86. --*/
  87. {
  88. gpResources = (LPRESOURCES)UTIL_calloc(1, sizeof(RESOURCES));
  89. }
  90. VOID
  91. PAL_FreeResources(
  92. VOID
  93. )
  94. /*++
  95. Purpose:
  96. Free all loaded resources.
  97. Parameters:
  98. None.
  99. Return value:
  100. None.
  101. --*/
  102. {
  103. if (gpResources != NULL)
  104. {
  105. //
  106. // Free all loaded sprites
  107. //
  108. PAL_FreePlayerSprites();
  109. PAL_FreeEventObjectSprites();
  110. //
  111. // Free map
  112. //
  113. PAL_FreeMap(gpResources->lpMap);
  114. //
  115. // Delete the instance
  116. //
  117. free(gpResources);
  118. }
  119. gpResources = NULL;
  120. }
  121. VOID
  122. PAL_SetLoadFlags(
  123. BYTE bFlags
  124. )
  125. /*++
  126. Purpose:
  127. Set flags to load resources.
  128. Parameters:
  129. [IN] bFlags - flags to be set.
  130. Return value:
  131. None.
  132. --*/
  133. {
  134. if (gpResources == NULL)
  135. {
  136. return;
  137. }
  138. gpResources->bLoadFlags |= bFlags;
  139. }
  140. VOID
  141. PAL_LoadResources(
  142. VOID
  143. )
  144. /*++
  145. Purpose:
  146. Load the game resources if needed.
  147. Parameters:
  148. None.
  149. Return value:
  150. None.
  151. --*/
  152. {
  153. int i, index, l, n;
  154. WORD wPlayerID, wSpriteNum;
  155. if (gpResources == NULL || gpResources->bLoadFlags == 0)
  156. {
  157. return;
  158. }
  159. //
  160. // Load scene
  161. //
  162. if (gpResources->bLoadFlags & kLoadScene)
  163. {
  164. FILE *fpMAP, *fpGOP;
  165. fpMAP = UTIL_OpenRequiredFile("map.mkf");
  166. fpGOP = UTIL_OpenRequiredFile("gop.mkf");
  167. if (gpGlobals->fEnteringScene)
  168. {
  169. gpGlobals->wScreenWave = 0;
  170. gpGlobals->sWaveProgression = 0;
  171. }
  172. //
  173. // Free previous loaded scene (sprites and map)
  174. //
  175. PAL_FreeEventObjectSprites();
  176. PAL_FreeMap(gpResources->lpMap);
  177. //
  178. // Load map
  179. //
  180. i = gpGlobals->wNumScene - 1;
  181. gpResources->lpMap = PAL_LoadMap(gpGlobals->g.rgScene[i].wMapNum,
  182. fpMAP, fpGOP);
  183. if (gpResources->lpMap == NULL)
  184. {
  185. fclose(fpMAP);
  186. fclose(fpGOP);
  187. TerminateOnError("PAL_LoadResources(): Fail to load map #%d (scene #%d) !",
  188. gpGlobals->g.rgScene[i].wMapNum, gpGlobals->wNumScene);
  189. }
  190. //
  191. // Load sprites
  192. //
  193. index = gpGlobals->g.rgScene[i].wEventObjectIndex;
  194. gpResources->nEventObject = gpGlobals->g.rgScene[i + 1].wEventObjectIndex;
  195. gpResources->nEventObject -= index;
  196. if (gpResources->nEventObject > 0)
  197. {
  198. gpResources->lppEventObjectSprites =
  199. (LPSPRITE *)UTIL_calloc(gpResources->nEventObject, sizeof(LPSPRITE));
  200. }
  201. for (i = 0; i < gpResources->nEventObject; i++, index++)
  202. {
  203. n = gpGlobals->g.lprgEventObject[index].wSpriteNum;
  204. if (n == 0)
  205. {
  206. //
  207. // this event object has no sprite
  208. //
  209. gpResources->lppEventObjectSprites[i] = NULL;
  210. continue;
  211. }
  212. l = PAL_MKFGetDecompressedSize(n, gpGlobals->f.fpMGO);
  213. gpResources->lppEventObjectSprites[i] = (LPSPRITE)UTIL_malloc(l);
  214. if (PAL_MKFDecompressChunk(gpResources->lppEventObjectSprites[i], l,
  215. n, gpGlobals->f.fpMGO) > 0)
  216. {
  217. gpGlobals->g.lprgEventObject[index].nSpriteFramesAuto =
  218. PAL_SpriteGetNumFrames(gpResources->lppEventObjectSprites[i]);
  219. }
  220. }
  221. gpGlobals->partyoffset = PAL_XY(160, 112);
  222. fclose(fpGOP);
  223. fclose(fpMAP);
  224. }
  225. //
  226. // Load player sprites
  227. //
  228. if (gpResources->bLoadFlags & kLoadPlayerSprite)
  229. {
  230. //
  231. // Free previous loaded player sprites
  232. //
  233. PAL_FreePlayerSprites();
  234. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  235. {
  236. wPlayerID = gpGlobals->rgParty[i].wPlayerRole;
  237. assert(wPlayerID < MAX_PLAYER_ROLES);
  238. //
  239. // Load player sprite
  240. //
  241. wSpriteNum = gpGlobals->g.PlayerRoles.rgwSpriteNum[wPlayerID];
  242. l = PAL_MKFGetDecompressedSize(wSpriteNum, gpGlobals->f.fpMGO);
  243. gpResources->rglpPlayerSprite[i] = (LPSPRITE)UTIL_malloc(l);
  244. PAL_MKFDecompressChunk(gpResources->rglpPlayerSprite[i], l, wSpriteNum,
  245. gpGlobals->f.fpMGO);
  246. }
  247. if (gpGlobals->nFollower > 0)
  248. {
  249. //
  250. // Load the follower sprite
  251. //
  252. wSpriteNum = gpGlobals->rgParty[i].wPlayerRole;
  253. l = PAL_MKFGetDecompressedSize(wSpriteNum, gpGlobals->f.fpMGO);
  254. gpResources->rglpPlayerSprite[i] = (LPSPRITE)UTIL_malloc(l);
  255. PAL_MKFDecompressChunk(gpResources->rglpPlayerSprite[i], l, wSpriteNum,
  256. gpGlobals->f.fpMGO);
  257. }
  258. }
  259. //
  260. // Clear all of the load flags
  261. //
  262. gpResources->bLoadFlags = 0;
  263. }
  264. LPPALMAP
  265. PAL_GetCurrentMap(
  266. VOID
  267. )
  268. /*++
  269. Purpose:
  270. Get the current loaded map.
  271. Parameters:
  272. None.
  273. Return value:
  274. Pointer to the current loaded map. NULL if no map is loaded.
  275. --*/
  276. {
  277. if (gpResources == NULL)
  278. {
  279. return NULL;
  280. }
  281. return gpResources->lpMap;
  282. }
  283. LPSPRITE
  284. PAL_GetPlayerSprite(
  285. BYTE bPlayerIndex
  286. )
  287. /*++
  288. Purpose:
  289. Get the player sprite.
  290. Parameters:
  291. [IN] bPlayerIndex - index of player in party (starts from 0).
  292. Return value:
  293. Pointer to the player sprite.
  294. --*/
  295. {
  296. if (gpResources == NULL || bPlayerIndex > MAX_PLAYERS_IN_PARTY)
  297. {
  298. return NULL;
  299. }
  300. return gpResources->rglpPlayerSprite[bPlayerIndex];
  301. }
  302. LPSPRITE
  303. PAL_GetEventObjectSprite(
  304. WORD wEventObjectID
  305. )
  306. /*++
  307. Purpose:
  308. Get the sprite of the specified event object.
  309. Parameters:
  310. [IN] wEventObjectID - the ID of event object.
  311. Return value:
  312. Pointer to the sprite.
  313. --*/
  314. {
  315. wEventObjectID -= gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex;
  316. wEventObjectID--;
  317. if (gpResources == NULL || wEventObjectID >= gpResources->nEventObject)
  318. {
  319. return NULL;
  320. }
  321. return gpResources->lppEventObjectSprites[wEventObjectID];
  322. }