res.c 8.0 KB

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