sprites.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. // sprite.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 06/27/97 JMI Copied CSprite and derived sprite classes here from
  23. // scene.h.
  24. //
  25. // 07/02/97 JMI Now AddChild() resets the m_sX2,Y2 to zero when adding
  26. // a sprite that formerly had no parent.
  27. //
  28. // 08/04/97 JMI Now initializes m_pthing to NULL.
  29. //
  30. // 08/18/97 JMI Now ASSERTs that we're not in a scene list.
  31. //
  32. // 09/28/99 JMI Changed the m_iter member of CSprite to a non-const iter
  33. // to work with VC++ 6.0.
  34. //
  35. ////////////////////////////////////////////////////////////////////////////////
  36. #ifndef SPRITES_H
  37. #define SPRITES_H
  38. ////////////////////////////////////////////////////////////////////////////////
  39. // C includes -- must be before RSPiX includes.
  40. ////////////////////////////////////////////////////////////////////////////////
  41. #include <vector>
  42. #if _MSC_VER >= 1020 || __MWERKS__ >= 0x1100 || __GNUC__
  43. #include <set>
  44. #else
  45. #include <multiset.h>
  46. #endif
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // RSPiX includes.
  49. ////////////////////////////////////////////////////////////////////////////////
  50. #include "RSPiX.h"
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Typedefs.
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // NOTE: While I wanted this to be within the CScene namespace, putting it there
  55. // created a circular dependancy between CScene and CSprite since they both
  56. // needed to use this. It's now at global scope, which I hate, but it works.
  57. // Define a container of sprites. A multiset allows for duplicates keys, which
  58. // is necessary because our sorting function might think of two sprites as being
  59. // the "same" as far as sorting goes.
  60. class CSprite; // Forward declaration
  61. class CThing; // Another handy forward.
  62. // This is the function object used to sort the set of sprites.
  63. // It is a template NOT because we want to use it with other types
  64. // (after all, how many types will have a m_sZ member?!) but because
  65. // making it a template causes it to be "evaluated" by the compiler
  66. // AFTER the CSprite class is fully defined! In other words, it's a
  67. // trick!
  68. template <class T>
  69. struct SpriteLess : binary_function<T*, T*, bool>
  70. {
  71. bool operator()(const T* a, const T* b) const
  72. {
  73. return a->m_sPriority < b->m_sPriority;
  74. }
  75. };
  76. #if _MSC_VER >= 1020 || __MWERKS__ >= 0x1100
  77. #if __MWERKS__ >= 0x1100
  78. ITERATOR_TRAITS(const CSprite*);
  79. #endif
  80. typedef multiset<CSprite*, SpriteLess<CSprite>, allocator<CSprite*> > msetSprites;
  81. typedef vector<CSprite*, allocator<CSprite*> > vSprites;
  82. #else
  83. typedef multiset<CSprite*, SpriteLess<CSprite> > msetSprites;
  84. typedef vector<CSprite*> vSprites;
  85. #endif
  86. // A CSprite is a base class for sprites designed to work with CScene.
  87. //class CScene; // Forward declaration
  88. class CSprite
  89. {
  90. // Make CScene a friend so it can access private stuff
  91. friend class CScene;
  92. public:
  93. // Define bit usage within "m_sInFlags"
  94. typedef enum
  95. {
  96. InAlpha = 0x0001, // Set if on alpha layer, clear otherwise
  97. InOpaque = 0x0002, // Set if on opaque layer, clear otherwise
  98. InXrayee = 0x0004, // Set if xray target, clear otherwise
  99. InHidden = 0x0008, // Set if hidden, clear otherwise
  100. InDeleteOnClear = 0x0010, // Set to delete sprite when layer is cleared
  101. InHighIntensity = 0x0020, // Set to use higher light intensities when
  102. // BLiT'ing/rendering (currently only supported
  103. // for 3D objects).
  104. InDeleteOnRender = 0x0040, // After rendering object, delete it.
  105. InBlitOpaque = 0x0080 // Blit sprite opaque (currently only supported
  106. // for 2D uncompressed, non-alpha objects).
  107. };
  108. // Define bit usage within "m_sOutFlags"
  109. typedef enum
  110. {
  111. OutRendered = 1 // Set whenever rendered (cleared by user)
  112. };
  113. // Define bit usage within "m_sPrivFlags"
  114. typedef enum
  115. {
  116. PrivInserted = 1 // Set if inserted in scene
  117. };
  118. // Types of sprites (or primitives).
  119. typedef enum
  120. {
  121. Standard2d,
  122. Standard3d,
  123. Line2d,
  124. Cylinder3d
  125. } Type;
  126. public:
  127. short m_sX2; // Sprite's 2d x coord
  128. short m_sY2; // Sprite's 2d y coord
  129. short m_sPriority; // Sprite's priority
  130. short m_sLayer; // Sprite's layer
  131. short m_sInFlags; // Sprite's input flags
  132. short m_sOutFlags; // Sprite's output flags
  133. CThing* m_pthing; // Owner of this sprite (for debugging).
  134. char* m_pszText; // Point this at your text.
  135. // DO NOT strcpy/cat/etc to this until
  136. // you've pointed it at some memory!!!
  137. CSprite* m_psprHeadChild; // First child sprite.
  138. CSprite* m_psprNext; // Next sibling sprite.
  139. CSprite* m_psprParent; // Parent sprite.
  140. protected:
  141. Type m_type; // Sprite's type
  142. short m_sPrivFlags; // Sprite's private flags
  143. short m_sSavedLayer; // Sprite's saved layer (used to detect changes)
  144. short m_sSavedPriority; // Sprite's saved priority (used to detect changes)
  145. msetSprites::iterator m_iter; // Sprite's iterator into layer's container
  146. public:
  147. CSprite()
  148. {
  149. m_sInFlags = 0;
  150. m_sOutFlags = 0;
  151. m_sPrivFlags = 0;
  152. m_sX2 = 0; // Any sprite's 2D dest x coord.
  153. m_sY2 = 0; // Any sprite's 2D dest y coord.
  154. m_pszText = NULL;
  155. m_psprHeadChild = NULL;
  156. m_psprNext = NULL;
  157. m_psprParent = NULL;
  158. m_pthing = NULL;
  159. }
  160. virtual ~CSprite()
  161. {
  162. // If we have a parent . . .
  163. if (m_psprParent != NULL)
  164. {
  165. // Get outta there!
  166. m_psprParent->RemoveChild(this);
  167. }
  168. // While we have children . . .
  169. while (m_psprHeadChild != NULL)
  170. {
  171. // Remove the head child.
  172. RemoveChild(m_psprHeadChild);
  173. }
  174. // Make sure we're not in any lists.
  175. ASSERT( (m_sPrivFlags & CSprite::PrivInserted) == 0);
  176. }
  177. // Adds a child CSprite.
  178. void AddChild( // Returns nothing. Cannot fail.
  179. CSprite* psprNewChild) // In: New child.
  180. {
  181. // If we already had a parent . . .
  182. if (psprNewChild->m_psprParent != NULL)
  183. {
  184. psprNewChild->m_psprParent->RemoveChild(psprNewChild);
  185. }
  186. else
  187. {
  188. // Clear the positions which will now be parent
  189. // relative.
  190. // Note that if you already set a parent relative
  191. // position, this will override it.
  192. psprNewChild->m_sX2 = 0; // Convenient placed right
  193. psprNewChild->m_sY2 = 0; // up parent's *ss.
  194. }
  195. // Make new sprite the head for ease of insertion.
  196. // Point new child's next at current head.
  197. psprNewChild->m_psprNext = m_psprHeadChild;
  198. // Make head the new child.
  199. m_psprHeadChild = psprNewChild;
  200. // Let the child know who its parent is.
  201. psprNewChild->m_psprParent = this;
  202. }
  203. // Removes a child CSprite3.
  204. void RemoveChild( // Returns nothing. Fails if not in list.
  205. CSprite* psprRemove) // In: Child to remove.
  206. {
  207. CSprite* psprCur = m_psprHeadChild; // Current traversal sprite.
  208. CSprite** ppsprPrevNext = &m_psprHeadChild; // Ptr to the previous sprite's
  209. // next ptr.
  210. while (psprCur != NULL)
  211. {
  212. // If we found the item to remove . . .
  213. if (psprCur == psprRemove)
  214. {
  215. // Point the previous' next at this item's next.
  216. *ppsprPrevNext = psprCur->m_psprNext;
  217. // Clean this item.
  218. psprCur->m_psprParent = NULL;
  219. psprCur->m_psprNext = NULL;
  220. // Done.
  221. break;
  222. }
  223. // Store this item's next ptr.
  224. ppsprPrevNext = &psprCur->m_psprNext;
  225. // Get the next one.
  226. psprCur = psprCur->m_psprNext;
  227. }
  228. if (psprCur == NULL)
  229. {
  230. TRACE("CSprite3::RemoveChild(): No such child sprite.\n");
  231. }
  232. }
  233. // Get the type of sprite.
  234. // Read access to our protected member.
  235. // This will be inlined and requires no local stack.
  236. // It is important to make sure the types are as allocated.
  237. Type GetType(void)
  238. {
  239. return m_type;
  240. }
  241. };
  242. // A CSprite2 is a 2d sprite designed to work with CScene.
  243. // It has two optional ways of alpha'ing during blit:
  244. // 1) m_pimAlpha, an alpha mask.
  245. // 2) m_sAlphaLevel, an alpha value to apply to the whole image.
  246. class CSprite2 : public CSprite
  247. {
  248. public:
  249. RImage* m_pImage; // Pointer to image
  250. RImage* m_pimAlpha; // Alpha image pointer.
  251. short m_sAlphaLevel; // Constant alpha level to
  252. // use if no m_pimAlpha.
  253. CSprite2()
  254. {
  255. m_pImage = NULL;
  256. m_pimAlpha = NULL;
  257. m_sAlphaLevel = 255;
  258. m_type = Standard2d;
  259. }
  260. };
  261. // A CSpriteLine2d is a 2D line designed to work with CScene.
  262. class CSpriteLine2d : public CSprite
  263. {
  264. public:
  265. short m_sX2End; // 2D end point for line.
  266. short m_sY2End; // 2D end point for line.
  267. U8 m_u8Color; // Color for line segment.
  268. CSpriteLine2d()
  269. {
  270. m_type = Line2d;
  271. }
  272. };
  273. // A CSpriteCylinder3d is a cylinder designed to work with CScene.
  274. class CSpriteCylinder3d : public CSprite
  275. {
  276. public:
  277. short m_sRadius; // Radius of cylinder
  278. short m_sHeight; // Height of cylinder.
  279. U8 m_u8Color; // Color for line segment.
  280. CSpriteCylinder3d()
  281. {
  282. m_type = Cylinder3d;
  283. }
  284. };
  285. // A CSprite3 is a 3d sprite designed to work with CScene.
  286. class CSprite3 : public CSprite
  287. {
  288. public:
  289. CSprite3()
  290. {
  291. Init();
  292. }
  293. public:
  294. void Init(void)
  295. {
  296. m_pmesh = NULL; // Mesh.
  297. m_psop = NULL; // Sea Of Points.
  298. m_ptrans = NULL; // Transform.
  299. m_ptex = NULL; // Texture.
  300. m_psphere = NULL; // Bounding sphere.
  301. m_sRadius = 0; // Radius of collision circle.
  302. m_sCenX = 0; // Location of collision circle.
  303. m_sCenY = 0; // Location of collision circle.
  304. m_sBrightness = 0; // Brightness (start in the middle).
  305. m_sDirectRenderX = 0; // Location to Render() directly to composite buffer.
  306. m_sDirectRenderY = 0; // Location to Render() directly to composite buffer.
  307. m_sDirectRenderZ = 0; // The same z in either case.
  308. m_sIndirectRenderX = 0; // Location to Render() indirectly to clip buffer.
  309. m_sIndirectRenderY = 0; // Location to Render() indirectly to clip buffer.
  310. m_sRenderOffX = 0; // Offset to Render() to account for bounding
  311. // sphere.
  312. m_sRenderOffY = 0; // Offset to Render() to account for bounding
  313. // sphere.
  314. m_type = Standard3d;
  315. }
  316. public:
  317. RMesh* m_pmesh; // Mesh.
  318. RSop* m_psop; // Sea Of Points.
  319. RTransform* m_ptrans; // Transform.
  320. RTexture* m_ptex; // Texture.
  321. RP3d* m_psphere; // Bounding sphere def.
  322. short m_sRadius; // Radius for collision (with buildings for
  323. // X Ray). Autoset by Render3D().
  324. short m_sCenX; // Position of center of collision circle.
  325. // Autoset by Render3D().
  326. short m_sCenY; // Position of center of collision circle.
  327. // Autoset by Render3D().
  328. short m_sBrightness; // Indicates the brightness with which the
  329. // object will be fogged (0 .. 255).
  330. short m_sDirectRenderX; // Location to Render() directly to composite buffer.
  331. short m_sDirectRenderY; // Location to Render() directly to composite buffer.
  332. short m_sDirectRenderZ; // Location to render in either case
  333. short m_sIndirectRenderX; // Location to Render() indirectly to clip buffer.
  334. short m_sIndirectRenderY; // Location to Render() indirectly to clip buffer.
  335. short m_sRenderOffX; // Offset to Render() to account for bounding
  336. // sphere.
  337. short m_sRenderOffY; // Offset to Render() to account for bounding
  338. // sphere.
  339. bool m_bIndirect; // Indicates an indirect Render() (into the
  340. // clip buffer).
  341. };
  342. #endif // SPRITES_H
  343. ////////////////////////////////////////////////////////////////////////////////
  344. // EOF
  345. ////////////////////////////////////////////////////////////////////////////////