AnimThing.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // AnimThing.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 02/18/97 JMI Started.
  23. //
  24. // 02/19/97 JMI Added ability to send any message to any CThing when done
  25. // with animation.
  26. //
  27. // 02/20/97 JMI Tweaked positioning; I think it's more correct.
  28. //
  29. // 02/20/97 MJR Added mini-runtime-editor.
  30. //
  31. // 02/21/97 JMI After tweaking the position, I hosed the sprite priority.
  32. // Fixed. Now it's beauteous if you place a dude just a
  33. // bit in front of a looping explosion.
  34. //
  35. // 02/24/97 JMI No longer sets the m_type member of the m_sprite b/c it
  36. // is set by m_sprite's constructor.
  37. //
  38. // 02/24/97 JMI Changed m_pthingSendMsg to m_u16IdSendMsg.
  39. //
  40. // 02/25/97 JMI Forgot to call FreeResources() in GetResources().
  41. //
  42. // 03/13/97 JMI Load now takes a version number.
  43. //
  44. // 03/27/97 JMI Now EditModify() returns failure if there's no resource
  45. // name. GetResources() should've taken care of this via
  46. // rspGetResource() failing, but it seems to crash in Release
  47. // mode, so now EditModify() checks.
  48. //
  49. // 04/10/97 BRH Updated this to work with the new multi layer attribute
  50. // maps.
  51. //
  52. // 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
  53. // exists.
  54. //
  55. // 06/29/97 JMI Converted EditRect(), EditRender(), and/or Render() to
  56. // use Map3Dto2D().
  57. // Also, now determines priority via 2D mapped Y.
  58. //
  59. // 07/27/97 JMI Now determines priority totally based on Z.
  60. // Also, now uses CRealm::Make2dResPath() on res names.
  61. //
  62. //////////////////////////////////////////////////////////////////////////////
  63. //
  64. // This CThing-derived class will play an animation to its finish and then
  65. // destroy itself. It's a mini Postal movie player.
  66. //
  67. //////////////////////////////////////////////////////////////////////////////
  68. #define ANIMTHING_CPP
  69. #include "RSPiX.h"
  70. #include <math.h>
  71. #include "AnimThing.h"
  72. #include "dude.h"
  73. #include "game.h"
  74. ////////////////////////////////////////////////////////////////////////////////
  75. // Macros/types/etc.
  76. ////////////////////////////////////////////////////////////////////////////////
  77. #define GUI_FILE_NAME "res/editor/EditAnim.gui"
  78. #define GUI_ID_RESOURCE 3
  79. #define GUI_ID_OPTIONS 4
  80. #define GUI_ID_DONTLOOP 5
  81. #define GUI_ID_DOLOOP 6
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Variables/data
  84. ////////////////////////////////////////////////////////////////////////////////
  85. ////////////////////////////////////////////////////////////////////////////////
  86. // Load object (should call base class version!)
  87. ////////////////////////////////////////////////////////////////////////////////
  88. short CAnimThing::Load( // Returns 0 if successfull, non-zero otherwise
  89. RFile* pFile, // In: File to load from
  90. bool bEditMode, // In: True for edit mode, false otherwise
  91. short sFileCount, // In: File count (unique per file, never 0)
  92. ULONG ulFileVersion) // In: Version of file format to load.
  93. {
  94. short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  95. if (sResult == 0)
  96. {
  97. switch (ulFileVersion)
  98. {
  99. default:
  100. case 1:
  101. pFile->Read(&m_dX);
  102. pFile->Read(&m_dY);
  103. pFile->Read(&m_dZ);
  104. pFile->Read(m_szResName);
  105. pFile->Read(&m_sLoop);
  106. break;
  107. }
  108. // Make sure there were no file errors or format errors . . .
  109. if (!pFile->Error() && sResult == 0)
  110. {
  111. // Get resources
  112. sResult = GetResources();
  113. }
  114. else
  115. {
  116. sResult = -1;
  117. TRACE("CAnimThing::Load(): Error reading from file!\n");
  118. }
  119. }
  120. return sResult;
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. // Save object (should call base class version!)
  124. ////////////////////////////////////////////////////////////////////////////////
  125. short CAnimThing::Save( // Returns 0 if successfull, non-zero otherwise
  126. RFile* pFile, // In: File to save to
  127. short sFileCount) // In: File count (unique per file, never 0)
  128. {
  129. short sResult = CThing::Save(pFile, sFileCount);
  130. if (sResult == 0)
  131. {
  132. pFile->Write(&m_dX);
  133. pFile->Write(&m_dY);
  134. pFile->Write(&m_dZ);
  135. pFile->Write(m_szResName);
  136. pFile->Write(&m_sLoop);
  137. // Make sure there were no file errors
  138. sResult = pFile->Error();
  139. }
  140. return sResult;
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////
  143. // Startup object
  144. ////////////////////////////////////////////////////////////////////////////////
  145. short CAnimThing::Startup(void) // Returns 0 if successfull, non-zero otherwise
  146. {
  147. m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
  148. m_lAnimTime = 0;
  149. return 0;
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. // Shutdown object
  153. ////////////////////////////////////////////////////////////////////////////////
  154. short CAnimThing::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
  155. {
  156. return 0;
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////
  159. // Suspend object
  160. ////////////////////////////////////////////////////////////////////////////////
  161. void CAnimThing::Suspend(void)
  162. {
  163. m_sSuspend++;
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////
  166. // Resume object
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void CAnimThing::Resume(void)
  169. {
  170. m_sSuspend--;
  171. // If we're actually going to start updating again, we need to reset
  172. // the time so as to ignore any time that passed while we were suspended.
  173. // This method is far from precise, but I'm hoping it's good enough.
  174. if (m_sSuspend == 0)
  175. {
  176. m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
  177. }
  178. }
  179. ////////////////////////////////////////////////////////////////////////////////
  180. // Update object
  181. ////////////////////////////////////////////////////////////////////////////////
  182. void CAnimThing::Update(void)
  183. {
  184. if (!m_sSuspend)
  185. {
  186. if (m_lAnimTime >= m_paachannel->TotalTime() && m_sLoop == FALSE)
  187. {
  188. // If there's a thing to send a message to . . .
  189. if (m_u16IdSendMsg != CIdBank::IdNil)
  190. {
  191. // Send the message.
  192. SendThingMessage(&m_msg, m_u16IdSendMsg);
  193. }
  194. // Done.
  195. delete this;
  196. return;
  197. }
  198. }
  199. }
  200. ////////////////////////////////////////////////////////////////////////////////
  201. // Render object
  202. ////////////////////////////////////////////////////////////////////////////////
  203. short sEdit;
  204. double dScale = 1.0;
  205. double dScaleInc = 0.025;
  206. void CAnimThing::Render(void)
  207. {
  208. // Get current time diff.
  209. long lCurTime = m_pRealm->m_time.GetGameTime();
  210. m_lAnimTime += lCurTime - m_lAnimPrevTime;
  211. m_lAnimPrevTime = lCurTime;
  212. CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
  213. if (paa != NULL)
  214. {
  215. // No special flags
  216. m_sprite.m_sInFlags = 0;
  217. // Map from 3d to 2d coords
  218. // m_sprite.m_sX2 = m_dX + paa->m_sX;
  219. // m_sprite.m_sY2 = m_dZ - (m_dY - paa->m_sY);
  220. Map3Dto2D(
  221. m_dX,
  222. m_dY,
  223. m_dZ,
  224. &(m_sprite.m_sX2),
  225. &(m_sprite.m_sY2) );
  226. // Offset by hotspot.
  227. m_sprite.m_sX2 += paa->m_sX;
  228. m_sprite.m_sY2 += paa->m_sY;
  229. // Priority is based on our position in 3D realm coords.
  230. m_sprite.m_sPriority = m_dZ;
  231. // Layer should be based on info we get from attribute map.
  232. m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
  233. // Copy the color info and the alpha channel to the Alpha Sprite
  234. m_sprite.m_pImage = &(paa->m_imColor);
  235. m_sprite.m_pimAlpha = &(paa->m_pimAlphaArray[0]); // This is an array? What changes my index?!
  236. ///////////////////////////////////////////////////////////
  237. // Tiny little built-in editor that gets acctivated via
  238. // the debugger -- just set sEdit to non-zero. It assumes
  239. // the alpha anim has 2 layers, with the second layer at
  240. // 100%. It copies that layer to the first layer, scaling
  241. // it by the specified value. Press LEFT and RIGHT arrows
  242. // (quickly) to modify the scaling value and watch as the
  243. // alpha effect changes.
  244. ///////////////////////////////////////////////////////////
  245. if (sEdit && (paa->m_sNumAlphas > 2))
  246. {
  247. unsigned char auc[128];
  248. rspScanKeys(auc);
  249. if (auc[RSP_SK_LEFT])
  250. {
  251. dScale -= dScaleInc;
  252. if (dScale < 0.0)
  253. dScale = 0;
  254. }
  255. if (auc[RSP_SK_RIGHT])
  256. {
  257. dScale += dScaleInc;
  258. }
  259. for (short y = 0; y < paa->m_pimAlphaArray[1].m_sHeight; y++)
  260. {
  261. U8* pSrc = paa->m_pimAlphaArray[1].m_pData + (y * paa->m_pimAlphaArray[1].m_lPitch);
  262. U8* pDst = paa->m_pimAlphaArray[0].m_pData + (y * paa->m_pimAlphaArray[0].m_lPitch);
  263. for (short x = 0; x < paa->m_pimAlphaArray[1].m_sWidth; x++)
  264. {
  265. double dVal = (double)(*pSrc);
  266. dVal *= dScale;
  267. if (dVal < 256.0)
  268. *pDst = (U8)dVal;
  269. else
  270. *pDst = (U8)255;
  271. pSrc++;
  272. pDst++;
  273. }
  274. }
  275. }
  276. ///////////////////////////////////////////////////////////
  277. // Update sprite in scene
  278. m_pRealm->m_scene.UpdateSprite(&m_sprite);
  279. }
  280. }
  281. short CAnimThing::Setup( // Returns 0 if successfull, non-zero otherwise
  282. short sX, // In: New x coord
  283. short sY, // In: New y coord
  284. short sZ) // In: New z coord
  285. {
  286. short sResult = 0;
  287. // Use specified position
  288. m_dX = (double)sX;
  289. m_dY = (double)sY;
  290. m_dZ = (double)sZ;
  291. m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
  292. // Load resources
  293. sResult = GetResources();
  294. return sResult;
  295. }
  296. ////////////////////////////////////////////////////////////////////////////////
  297. // Called by editor to init new object at specified position
  298. ////////////////////////////////////////////////////////////////////////////////
  299. short CAnimThing::EditNew( // Returns 0 if successfull, non-zero otherwise
  300. short sX, // In: New x coord
  301. short sY, // In: New y coord
  302. short sZ) // In: New z coord
  303. {
  304. short sResult = 0;
  305. // Use specified position
  306. m_dX = (double)sX;
  307. m_dY = (double)sY;
  308. m_dZ = (double)sZ;
  309. sResult = EditModify();
  310. return sResult;
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////
  313. // Called by editor to modify object
  314. ////////////////////////////////////////////////////////////////////////////////
  315. short CAnimThing::EditModify(void)
  316. {
  317. short sResult = 0;
  318. RGuiItem* pgui = RGuiItem::LoadInstantiate(FullPathVD(GUI_FILE_NAME));
  319. if (pgui != NULL)
  320. {
  321. RListBox* plb = (RListBox*)pgui->GetItemFromId(GUI_ID_OPTIONS);
  322. if (plb != NULL)
  323. {
  324. ASSERT(plb->m_type == RGuiItem::ListBox);
  325. // Select current setting.
  326. plb->SetSel(pgui->GetItemFromId((m_sLoop == FALSE) ? GUI_ID_DONTLOOP : GUI_ID_DOLOOP) );
  327. REdit* pedit = (REdit*)pgui->GetItemFromId(GUI_ID_RESOURCE);
  328. if (pedit != NULL)
  329. {
  330. ASSERT(pedit->m_type == RGuiItem::Edit);
  331. // Set text.
  332. pedit->SetText("%s", m_szResName);
  333. // Realize text.
  334. pedit->Compose();
  335. if (DoGui(pgui) == 1)
  336. {
  337. RGuiItem* pguiSel = plb->GetSel();
  338. if (pguiSel)
  339. {
  340. switch (pguiSel->m_lId)
  341. {
  342. case GUI_ID_DONTLOOP:
  343. m_sLoop = FALSE;
  344. break;
  345. case GUI_ID_DOLOOP:
  346. m_sLoop = TRUE;
  347. break;
  348. }
  349. }
  350. // Get new resource name.
  351. pedit->GetText(m_szResName, sizeof(m_szResName) );
  352. // If no resource name . . .
  353. if (m_szResName[0] == '\0')
  354. {
  355. sResult = 1;
  356. }
  357. }
  358. else
  359. {
  360. sResult = 1;
  361. }
  362. }
  363. else
  364. {
  365. sResult = -3;
  366. }
  367. }
  368. else
  369. {
  370. sResult = -2;
  371. }
  372. // Done with GUI.
  373. delete pgui;
  374. }
  375. else
  376. {
  377. sResult = -1;
  378. }
  379. // If successful so far . . .
  380. if (sResult == 0)
  381. {
  382. // Load resources
  383. sResult = GetResources();
  384. }
  385. return sResult;
  386. }
  387. ////////////////////////////////////////////////////////////////////////////////
  388. // Called by editor to move object to specified position
  389. ////////////////////////////////////////////////////////////////////////////////
  390. short CAnimThing::EditMove( // Returns 0 if successfull, non-zero otherwise
  391. short sX, // In: New x coord
  392. short sY, // In: New y coord
  393. short sZ) // In: New z coord
  394. {
  395. m_dX = (double)sX;
  396. m_dY = (double)sY;
  397. m_dZ = (double)sZ;
  398. return 0;
  399. }
  400. ////////////////////////////////////////////////////////////////////////////////
  401. // Called by editor to get the clickable pos/area of an object in 2D.
  402. // (virtual (Overridden here)).
  403. ////////////////////////////////////////////////////////////////////////////////
  404. void CAnimThing::EditRect( // Returns nothiing.
  405. RRect* prc) // Out: Clickable pos/area of object.
  406. {
  407. Map3Dto2D(
  408. m_dX,
  409. m_dY,
  410. m_dZ,
  411. &(prc->sX),
  412. &(prc->sY) );
  413. prc->sW = 10; // Safety.
  414. prc->sH = 10; // Safety.
  415. if (m_paachannel != NULL)
  416. {
  417. CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
  418. if (paa != NULL)
  419. {
  420. // Offset by hotspot.
  421. prc->sX += paa->m_sX;
  422. prc->sY += paa->m_sY;
  423. prc->sW = paa->m_imColor.m_sWidth;
  424. prc->sH = paa->m_imColor.m_sHeight;
  425. }
  426. }
  427. }
  428. ////////////////////////////////////////////////////////////////////////////////
  429. // Called by editor to get the hotspot of an object in 2D.
  430. // (virtual (Overridden here)).
  431. ////////////////////////////////////////////////////////////////////////////////
  432. void CAnimThing::EditHotSpot( // Returns nothiing.
  433. short* psX, // Out: X coord of 2D hotspot relative to
  434. // EditRect() pos.
  435. short* psY) // Out: Y coord of 2D hotspot relative to
  436. // EditRect() pos.
  437. {
  438. *psX = 0; // Safety.
  439. *psY = 0; // Safety.
  440. if (m_paachannel != NULL)
  441. {
  442. CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
  443. if (paa != NULL)
  444. {
  445. *psX = -paa->m_sX;
  446. *psY = -paa->m_sY;
  447. }
  448. }
  449. }
  450. ////////////////////////////////////////////////////////////////////////////////
  451. // Called by editor to update object
  452. ////////////////////////////////////////////////////////////////////////////////
  453. void CAnimThing::EditUpdate(void)
  454. {
  455. }
  456. ////////////////////////////////////////////////////////////////////////////////
  457. // Called by editor to render object
  458. ////////////////////////////////////////////////////////////////////////////////
  459. void CAnimThing::EditRender(void)
  460. {
  461. // In some cases, object's might need to do a special-case render in edit
  462. // mode because Startup() isn't called. In this case it doesn't matter, so
  463. // we can call the normal Render().
  464. Render();
  465. }
  466. ////////////////////////////////////////////////////////////////////////////////
  467. // Get all required resources
  468. ////////////////////////////////////////////////////////////////////////////////
  469. short CAnimThing::GetResources(void) // Returns 0 if successfull, non-zero otherwise
  470. {
  471. short sResult = 0;
  472. // Safe to call even if no resource.
  473. FreeResources();
  474. sResult = rspGetResource(
  475. &g_resmgrGame,
  476. m_pRealm->Make2dResPath(m_szResName),
  477. &m_paachannel);
  478. if (sResult == 0)
  479. {
  480. m_lAnimTime = 0;
  481. m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
  482. if (m_sLoop != FALSE)
  483. {
  484. m_paachannel->SetLooping(RChannel_LoopAtStart | RChannel_LoopAtEnd);
  485. }
  486. else
  487. {
  488. m_paachannel->SetLooping(0);
  489. }
  490. }
  491. else
  492. {
  493. TRACE("GetResources(): Failed to load resource \"%s\".\n",
  494. m_pRealm->Make2dResPath(m_szResName) );
  495. }
  496. return sResult;
  497. }
  498. ////////////////////////////////////////////////////////////////////////////////
  499. // Free all resources
  500. ////////////////////////////////////////////////////////////////////////////////
  501. short CAnimThing::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
  502. {
  503. short sResult = 0;
  504. if (m_paachannel != NULL)
  505. {
  506. rspReleaseResource(&g_resmgrGame, &m_paachannel);
  507. }
  508. return sResult;
  509. }
  510. ////////////////////////////////////////////////////////////////////////////////
  511. // EOF
  512. ////////////////////////////////////////////////////////////////////////////////