SpriteBatchItem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SPRITE_BATCH_ITEM_H_
  23. #define _SPRITE_BATCH_ITEM_H_
  24. #ifndef _IMAGE_FRAME_PROVIDER_H
  25. #include "2d/core/ImageFrameProvider.h"
  26. #endif
  27. //------------------------------------------------------------------------------
  28. class SpriteBatch;
  29. class SceneRenderRequest;
  30. //------------------------------------------------------------------------------
  31. extern StringTableEntry spritesItemTypeName;
  32. //------------------------------------------------------------------------------
  33. class SpriteBatchItem : public ImageFrameProvider
  34. {
  35. friend class SpriteBatch;
  36. typedef ImageFrameProvider Parent;
  37. public:
  38. // Holds items required by call to SubmitTriangles. Currently, this is only used when
  39. // mTriangleRun is true due to processing of Spine's mesh attachments.
  40. typedef struct DrawDatType
  41. {
  42. U32 vertexCount;
  43. vector<Vector2> vertexArray;
  44. vector<Vector2> textureArray;
  45. vector<ColorF> colorArray;
  46. inline void size(U32 newSize) {
  47. vertexCount = newSize;
  48. vertexArray.resize(newSize);
  49. textureArray.resize(newSize);
  50. colorArray.resize(newSize);
  51. }
  52. } drawData;
  53. // Represents a logical position.
  54. struct LogicalPosition : public IFactoryObjectReset
  55. {
  56. const static S32 MAX_ARGUMENTS = 6;
  57. LogicalPosition()
  58. {
  59. resetState();
  60. }
  61. LogicalPosition( const char* pLogicalPositionArgs )
  62. {
  63. // Sanity!
  64. AssertFatal( pLogicalPositionArgs != NULL, "LogicalPosition() - Cannot use NULL position arguments." );
  65. resetState();
  66. // Finish if no position arguments were specified.
  67. if ( dStrlen(pLogicalPositionArgs) == 0 )
  68. return;
  69. // Fetch argument count.
  70. const char* pLogicalPositionSeparator = ",\t ";
  71. mArgCount = (S32)StringUnit::getUnitCount( pLogicalPositionArgs, pLogicalPositionSeparator );
  72. // Do we have a valid argument count?
  73. if ( mArgCount > MAX_ARGUMENTS )
  74. {
  75. // No, so warn.
  76. Con::warnf( "LogicalPosition: Encountered an invalid logical position of '%s'.", pLogicalPositionArgs );
  77. // Set no arguments.
  78. mArgCount = 0;
  79. return;
  80. }
  81. // Set arguments.
  82. for ( S32 index = 0; index < mArgCount; ++index )
  83. {
  84. mArgs[index] = StringUnit::getStringTableUnit( pLogicalPositionArgs, index, pLogicalPositionSeparator );
  85. }
  86. // Set argument string.
  87. mArgString = StringTable->insert( pLogicalPositionArgs );
  88. }
  89. inline bool isValid( void ) const { return mArgCount != 0; }
  90. inline S32 getArgCount( void ) const { return mArgCount; }
  91. inline StringTableEntry getArg( const S32 argIndex ) const
  92. {
  93. // Is the argument index valid?
  94. if ( argIndex < 0 || argIndex > 5 )
  95. {
  96. // No, so warn.
  97. Con::warnf( "LogicalPosition::getArg() - Cannot get a logical position argument; index '%d' out of range.", argIndex );
  98. return StringTable->EmptyString;
  99. }
  100. return mArgs[argIndex];
  101. }
  102. inline Vector2 getAsVector2( void ) const
  103. {
  104. // Do we have enough arguments?
  105. if ( mArgCount != 2 )
  106. {
  107. // No, so warn.
  108. Con::warnf( "LogicalPosition::getAsVector2() - Cannot get a logical position as Vector2; not enough arguments." );
  109. return Vector2::getZero();
  110. }
  111. return Vector2( getFloatArg(0), getFloatArg(1) );
  112. }
  113. inline F32 getFloatArg( const S32 argIndex ) const { return dAtof( getArg(argIndex) ); }
  114. inline S32 getIntArg( const S32 argIndex ) const { return dAtoi( getArg(argIndex) ); }
  115. inline bool getBoolArg( const S32 argIndex ) const { return dAtob( getArg(argIndex) ); }
  116. inline StringTableEntry getString( void ) const { return mArgString; }
  117. virtual void resetState( void )
  118. {
  119. mArgCount = 0;
  120. dMemset( mArgs, 0, sizeof(mArgs) );
  121. mArgString = StringTable->EmptyString;
  122. }
  123. // This should be as unique as possible as it is used for hashing.
  124. operator const U32() const
  125. {
  126. #ifdef TORQUE_CPU_X64
  127. return (U32)((U64)(mArgString) * (U32)2654435761);
  128. #else
  129. return (U32)(mArgString) * (U32)2654435761;
  130. #endif
  131. }
  132. /// Value equality check for hashing.
  133. bool operator==( const LogicalPosition& logicalPosition ) const
  134. {
  135. // Not equal if argument counts are different.
  136. if ( mArgCount != logicalPosition.getArgCount() )
  137. return false;
  138. // Not equal if arguments are different.
  139. for ( S32 index = 0; index < mArgCount; ++index )
  140. {
  141. if ( mArgs[index] != logicalPosition.getArg(index) )
  142. return false;
  143. }
  144. // Equal.
  145. return true;
  146. }
  147. /// Value inequality check.
  148. bool operator!=( const LogicalPosition& logicalPosition ) const
  149. {
  150. // Not equal if argument counts are different.
  151. if ( mArgCount != logicalPosition.getArgCount() )
  152. return true;
  153. // Not equal if arguments are different.
  154. for ( S32 index = 0; index < mArgCount; ++index )
  155. {
  156. if ( mArgs[index] != logicalPosition.getArg(index) )
  157. return true;
  158. }
  159. // Equal.
  160. return false;
  161. }
  162. private:
  163. S32 mArgCount;
  164. StringTableEntry mArgs[MAX_ARGUMENTS];
  165. StringTableEntry mArgString;
  166. };
  167. protected:
  168. SpriteBatch* mSpriteBatch;
  169. U32 mBatchId;
  170. S32 mProxyId;
  171. StringTableEntry mName;
  172. LogicalPosition mLogicalPosition;
  173. bool mVisible;
  174. bool mExplicitMode;
  175. Vector2 mLocalPosition;
  176. Vector2 mExplicitVerts[4];
  177. Vector2 mExplicitUVs[4];
  178. bool mTriangleRun;
  179. drawData mDrawData;
  180. F32 mLocalAngle;
  181. Vector2 mSize;
  182. F32 mDepth;
  183. bool mFlipX;
  184. bool mFlipY;
  185. Vector2 mSortPoint;
  186. StringTableEntry mRenderGroup;
  187. bool mBlendMode;
  188. GLenum mSrcBlendFactor;
  189. GLenum mDstBlendFactor;
  190. ColorF mBlendColor;
  191. F32 mAlphaTest;
  192. bool mUseComplexColor;
  193. ColorF mComplexColor0;
  194. ColorF mComplexColor1;
  195. ColorF mComplexColor2;
  196. ColorF mComplexColor3;
  197. SimObjectPtr<SimObject> mDataObject;
  198. Vector2 mLocalOOBB[4];
  199. b2AABB mLocalAABB;
  200. bool mLocalTransformDirty;
  201. Vector2 mRenderOOBB[4];
  202. b2AABB mRenderAABB;
  203. Vector2 mRenderPosition;
  204. U32 mLastBatchTransformId;
  205. U32 mSpriteBatchQueryKey;
  206. void* mUserData;
  207. public:
  208. SpriteBatchItem();
  209. virtual ~SpriteBatchItem();
  210. virtual void resetState( void );
  211. inline SpriteBatch* getBatchParent( void ) const { return mSpriteBatch; }
  212. inline U32 getBatchId( void ) const { return mBatchId; }
  213. inline S32 getProxyId( void ) const { return mProxyId; }
  214. inline StringTableEntry getName( void ) const { return mName; }
  215. inline void setLogicalPosition( const LogicalPosition& logicalPosition ) { mLogicalPosition = logicalPosition; }
  216. inline const LogicalPosition& getLogicalPosition( void ) const { return mLogicalPosition; }
  217. inline void setVisible( const bool visible ) { mVisible = visible; }
  218. inline bool getVisible( void ) const { return mVisible; }
  219. inline void setExplicitMode( const bool explicitMode ) { mExplicitMode = explicitMode; }
  220. inline bool getExplicitMode( void ) const { return mExplicitMode; }
  221. inline void setTriangleRun(const bool usesTriangles) { mTriangleRun = usesTriangles; }
  222. inline bool getTriangleRun(void) const { return mTriangleRun; }
  223. inline drawData *getDrawData(void) { return &mDrawData; }
  224. inline void setLocalPosition( const Vector2& localPosition ) { mLocalPosition = localPosition; mLocalTransformDirty = true; }
  225. inline Vector2 getLocalPosition( void ) const { return mLocalPosition; }
  226. void setExplicitVertices( const F32* vertices, const F32* uvs = 0 );
  227. inline void setLocalAngle( const F32 localAngle ) { mLocalAngle = localAngle; mLocalTransformDirty = true; }
  228. inline F32 getLocalAngle( void ) const { return mLocalAngle; }
  229. inline void setSize( const Vector2& size ) { mSize = size; mLocalTransformDirty = true; }
  230. inline Vector2 getSize( void ) const { return mSize; }
  231. inline const b2AABB& getLocalAABB( void ) { if ( mLocalTransformDirty ) updateLocalTransform(); return mLocalAABB; }
  232. void setDepth( const F32 depth ) { mDepth = depth; }
  233. F32 getDepth( void ) const { return mDepth; }
  234. inline void setFlipX( const bool flipX ) { mFlipX = flipX; }
  235. inline bool getFlipX( void ) const { return mFlipX; }
  236. inline void setFlipY( const bool flipY ) { mFlipY = flipY; }
  237. inline bool getFlipY( void ) const { return mFlipY; }
  238. inline void setSortPoint( const Vector2& sortPoint ) { mSortPoint = sortPoint; }
  239. inline Vector2 getSortPoint( void ) const { return mSortPoint; }
  240. inline void setRenderGroup( const char* pRenderGroup ) { mRenderGroup = StringTable->insert( pRenderGroup ); }
  241. inline StringTableEntry getRenderGroup( void ) const { return mRenderGroup; }
  242. inline void setBlendMode( const bool blendMode ) { mBlendMode = blendMode; }
  243. inline bool getBlendMode( void ) const { return mBlendMode; }
  244. inline void setSrcBlendFactor( GLenum srcBlendFactor ) { mSrcBlendFactor = srcBlendFactor; }
  245. inline GLenum getSrcBlendFactor( void ) const { return mSrcBlendFactor; }
  246. inline void setDstBlendFactor( GLenum dstBlendFactor ) { mDstBlendFactor = dstBlendFactor; }
  247. inline GLenum getDstBlendFactor( void ) const { return mDstBlendFactor; }
  248. inline void setBlendColor( const ColorF& blendColor ) { mBlendColor = blendColor; }
  249. inline const ColorF& getBlendColor( void ) const { return mBlendColor; }
  250. inline void setBlendAlpha( const F32 alpha ) { mBlendColor.alpha = alpha; }
  251. inline F32 getBlendAlpha( void ) const { return mBlendColor.alpha; }
  252. inline void setUseComplexColor(const bool complexColor) { mUseComplexColor = complexColor; }
  253. inline bool getUseComplexColor(void) const { return mUseComplexColor; }
  254. inline void setComplexColor(const ColorF& blendColor0, const ColorF& blendColor1, const ColorF& blendColor2, const ColorF& blendColor3) { mComplexColor0 = blendColor0; mComplexColor1 = blendColor1; mComplexColor2 = blendColor2; mComplexColor3 = blendColor3; }
  255. const ColorF& getComplexColor(const S8 cornerID);
  256. inline void setAlphaTest( const F32 alphaTest ) { mAlphaTest = alphaTest; }
  257. inline F32 getAlphaTest( void ) const { return mAlphaTest; }
  258. inline void setDataObject( SimObject* pDataObject ) { mDataObject = pDataObject; }
  259. inline SimObject* getDataObject( void ) const { return mDataObject; }
  260. inline void setUserData( void* pUserData ) { mUserData = pUserData; }
  261. inline void* getUserData( void ) const { return mUserData; }
  262. template<class T> T* getUserData( void ) const { return (T*)mUserData; }
  263. inline void setSpriteBatchQueryKey( const U32 key ) { mSpriteBatchQueryKey = key; }
  264. inline U32 getSpriteBatchQueryKey( void ) const { return mSpriteBatchQueryKey; }
  265. virtual void copyTo( SpriteBatchItem* pSpriteBatchItem ) const;
  266. inline const Vector2* getLocalOOBB( void ) const { return mLocalOOBB; }
  267. inline const Vector2* getRenderOOBB( void ) const { return mRenderOOBB; }
  268. void prepareRender( SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId );
  269. void render( BatchRender* pBatchRenderer, const SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId );
  270. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement );
  271. protected:
  272. void setBatchParent( SpriteBatch* pSpriteBatch, const U32 batchId );
  273. inline void setProxyId( const S32 proxyId ) { mProxyId = proxyId; }
  274. inline void setName( const char* pName ) { mName = StringTable->insert( pName ); }
  275. void updateLocalTransform( void );
  276. void updateWorldTransform( const U32 batchTransformId );
  277. void onTamlCustomWrite( TamlCustomNode* pParentNode );
  278. void onTamlCustomRead( const TamlCustomNode* pSpriteNode );
  279. };
  280. //------------------------------------------------------------------------------
  281. static FactoryCache<SpriteBatchItem> SpriteBatchItemFactory;
  282. #endif // _SPRITE_BATCH_ITEM_H_