res.c 8.0 KB

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