SpriteBatchItem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // Represents a logical position.
  39. struct LogicalPosition : public IFactoryObjectReset
  40. {
  41. const static S32 MAX_ARGUMENTS = 6;
  42. LogicalPosition()
  43. {
  44. resetState();
  45. }
  46. LogicalPosition( const char* pLogicalPositionArgs )
  47. {
  48. // Sanity!
  49. AssertFatal( pLogicalPositionArgs != NULL, "LogicalPosition() - Cannot use NULL position arguments." );
  50. resetState();
  51. // Finish if no position arguments were specified.
  52. if ( dStrlen(pLogicalPositionArgs) == 0 )
  53. return;
  54. // Fetch argument count.
  55. const char* pLogicalPositionSeparator = ",\t ";
  56. mArgCount = (S32)StringUnit::getUnitCount( pLogicalPositionArgs, pLogicalPositionSeparator );
  57. // Do we have a valid argument count?
  58. if ( mArgCount > MAX_ARGUMENTS )
  59. {
  60. // No, so warn.
  61. Con::warnf( "LogicalPosition: Encountered an invalid logical position of '%s'.", pLogicalPositionArgs );
  62. // Set no arguments.
  63. mArgCount = 0;
  64. return;
  65. }
  66. // Set arguments.
  67. for ( S32 index = 0; index < mArgCount; ++index )
  68. {
  69. mArgs[index] = StringUnit::getStringTableUnit( pLogicalPositionArgs, index, pLogicalPositionSeparator );
  70. }
  71. // Set argument string.
  72. mArgString = StringTable->insert( pLogicalPositionArgs );
  73. }
  74. inline bool isValid( void ) const { return mArgCount != 0; }
  75. inline S32 getArgCount( void ) const { return mArgCount; }
  76. inline StringTableEntry getArg( const S32 argIndex ) const
  77. {
  78. // Is the argument index valid?
  79. if ( argIndex < 0 || argIndex > 5 )
  80. {
  81. // No, so warn.
  82. Con::warnf( "LogicalPosition::getArg() - Cannot get a logical position argument; index '%d' out of range.", argIndex );
  83. return StringTable->EmptyString;
  84. }
  85. return mArgs[argIndex];
  86. }
  87. inline Vector2 getAsVector2( void ) const
  88. {
  89. // Do we have enough arguments?
  90. if ( mArgCount != 2 )
  91. {
  92. // No, so warn.
  93. Con::warnf( "LogicalPosition::getAsVector2() - Cannot get a logical position as Vector2; not enough arguments." );
  94. return Vector2::getZero();
  95. }
  96. return Vector2( getFloatArg(0), getFloatArg(1) );
  97. }
  98. inline F32 getFloatArg( const S32 argIndex ) const { return dAtof( getArg(argIndex) ); }
  99. inline S32 getIntArg( const S32 argIndex ) const { return dAtoi( getArg(argIndex) ); }
  100. inline bool getBoolArg( const S32 argIndex ) const { return dAtob( getArg(argIndex) ); }
  101. inline StringTableEntry getString( void ) const { return mArgString; }
  102. virtual void resetState( void )
  103. {
  104. mArgCount = 0;
  105. dMemset( mArgs, 0, sizeof(mArgs) );
  106. mArgString = StringTable->EmptyString;
  107. }
  108. // This should be as unique as possible as it is used for hashing.
  109. operator const U32() const
  110. {
  111. return (U32)(mArgString) * (U32)2654435761;
  112. }
  113. /// Value equality check for hashing.
  114. bool operator==( const LogicalPosition& logicalPosition ) const
  115. {
  116. // Not equal if argument counts are different.
  117. if ( mArgCount != logicalPosition.getArgCount() )
  118. return false;
  119. // Not equal if arguments are different.
  120. for ( S32 index = 0; index < mArgCount; ++index )
  121. {
  122. if ( mArgs[index] != logicalPosition.getArg(index) )
  123. return false;
  124. }
  125. // Equal.
  126. return true;
  127. }
  128. /// Value inequality check.
  129. bool operator!=( const LogicalPosition& logicalPosition ) const
  130. {
  131. // Not equal if argument counts are different.
  132. if ( mArgCount != logicalPosition.getArgCount() )
  133. return true;
  134. // Not equal if arguments are different.
  135. for ( S32 index = 0; index < mArgCount; ++index )
  136. {
  137. if ( mArgs[index] != logicalPosition.getArg(index) )
  138. return true;
  139. }
  140. // Equal.
  141. return false;
  142. }
  143. private:
  144. S32 mArgCount;
  145. StringTableEntry mArgs[MAX_ARGUMENTS];
  146. StringTableEntry mArgString;
  147. };
  148. protected:
  149. SpriteBatch* mSpriteBatch;
  150. U32 mBatchId;
  151. S32 mProxyId;
  152. StringTableEntry mName;
  153. LogicalPosition mLogicalPosition;
  154. bool mVisible;
  155. Vector2 mLocalPosition;
  156. F32 mLocalAngle;
  157. Vector2 mSize;
  158. F32 mDepth;
  159. bool mFlipX;
  160. bool mFlipY;
  161. Vector2 mSortPoint;
  162. StringTableEntry mRenderGroup;
  163. bool mBlendMode;
  164. GLenum mSrcBlendFactor;
  165. GLenum mDstBlendFactor;
  166. ColorF mBlendColor;
  167. F32 mAlphaTest;
  168. SimObjectPtr<SimObject> mDataObject;
  169. Vector2 mLocalOOBB[4];
  170. b2AABB mLocalAABB;
  171. bool mLocalTransformDirty;
  172. Vector2 mRenderOOBB[4];
  173. b2AABB mRenderAABB;
  174. Vector2 mRenderPosition;
  175. U32 mLastBatchTransformId;
  176. U32 mSpriteBatchQueryKey;
  177. void* mUserData;
  178. public:
  179. SpriteBatchItem();
  180. virtual ~SpriteBatchItem();
  181. virtual void resetState( void );
  182. inline SpriteBatch* getBatchParent( void ) const { return mSpriteBatch; }
  183. inline U32 getBatchId( void ) const { return mBatchId; }
  184. inline S32 getProxyId( void ) const { return mProxyId; }
  185. inline StringTableEntry getName( void ) const { return mName; }
  186. inline void setLogicalPosition( const LogicalPosition& logicalPosition ) { mLogicalPosition = logicalPosition; }
  187. inline const LogicalPosition& getLogicalPosition( void ) const { return mLogicalPosition; }
  188. inline void setVisible( const bool visible ) { mVisible = visible; }
  189. inline bool getVisible( void ) const { return mVisible; }
  190. inline void setLocalPosition( const Vector2& localPosition ) { mLocalPosition = localPosition; mLocalTransformDirty = true; }
  191. inline Vector2 getLocalPosition( void ) const { return mLocalPosition; }
  192. inline void setLocalAngle( const F32 localAngle ) { mLocalAngle = localAngle; mLocalTransformDirty = true; }
  193. inline F32 getLocalAngle( void ) const { return mLocalAngle; }
  194. inline void setSize( const Vector2& size ) { mSize = size; mLocalTransformDirty = true; }
  195. inline Vector2 getSize( void ) const { return mSize; }
  196. inline const b2AABB& getLocalAABB( void ) { if ( mLocalTransformDirty ) updateLocalTransform(); return mLocalAABB; }
  197. void setDepth( const F32 depth ) { mDepth = depth; }
  198. F32 getDepth( void ) const { return mDepth; }
  199. inline void setFlipX( const bool flipX ) { mFlipX = flipX; }
  200. inline bool getFlipX( void ) const { return mFlipX; }
  201. inline void setFlipY( const bool flipY ) { mFlipY = flipY; }
  202. inline bool getFlipY( void ) const { return mFlipY; }
  203. inline void setSortPoint( const Vector2& sortPoint ) { mSortPoint = sortPoint; }
  204. inline Vector2 getSortPoint( void ) const { return mSortPoint; }
  205. inline void setRenderGroup( const char* pRenderGroup ) { mRenderGroup = StringTable->insert( pRenderGroup ); }
  206. inline StringTableEntry getRenderGroup( void ) const { return mRenderGroup; }
  207. inline void setBlendMode( const bool blendMode ) { mBlendMode = blendMode; }
  208. inline bool getBlendMode( void ) const { return mBlendMode; }
  209. inline void setSrcBlendFactor( GLenum srcBlendFactor ) { mSrcBlendFactor = srcBlendFactor; }
  210. inline GLenum getSrcBlendFactor( void ) const { return mSrcBlendFactor; }
  211. inline void setDstBlendFactor( GLenum dstBlendFactor ) { mDstBlendFactor = dstBlendFactor; }
  212. inline GLenum getDstBlendFactor( void ) const { return mDstBlendFactor; }
  213. inline void setBlendColor( const ColorF& blendColor ) { mBlendColor = blendColor; }
  214. inline const ColorF& getBlendColor( void ) const { return mBlendColor; }
  215. inline void setBlendAlpha( const F32 alpha ) { mBlendColor.alpha = alpha; }
  216. inline F32 getBlendAlpha( void ) const { return mBlendColor.alpha; }
  217. inline void setAlphaTest( const F32 alphaTest ) { mAlphaTest = alphaTest; }
  218. inline F32 getAlphaTest( void ) const { return mAlphaTest; }
  219. inline void setDataObject( SimObject* pDataObject ) { mDataObject = pDataObject; }
  220. inline SimObject* getDataObject( void ) const { return mDataObject; }
  221. inline void setUserData( void* pUserData ) { mUserData = pUserData; }
  222. inline void* getUserData( void ) const { return mUserData; }
  223. template<class T> T* getUserData( void ) const { return (T*)mUserData; }
  224. inline void setSpriteBatchQueryKey( const U32 key ) { mSpriteBatchQueryKey = key; }
  225. inline U32 getSpriteBatchQueryKey( void ) const { return mSpriteBatchQueryKey; }
  226. virtual void copyTo( SpriteBatchItem* pSpriteBatchItem ) const;
  227. inline const Vector2* getLocalOOBB( void ) const { return mLocalOOBB; }
  228. inline const Vector2* getRenderOOBB( void ) const { return mRenderOOBB; }
  229. void prepareRender( SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId );
  230. void render( BatchRender* pBatchRenderer, const SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId );
  231. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement );
  232. protected:
  233. void setBatchParent( SpriteBatch* pSpriteBatch, const U32 batchId );
  234. inline void setProxyId( const S32 proxyId ) { mProxyId = proxyId; }
  235. inline void setName( const char* pName ) { mName = StringTable->insert( pName ); }
  236. void updateLocalTransform( void );
  237. void updateWorldTransform( const U32 batchTransformId );
  238. void onTamlCustomWrite( TamlCustomNode* pParentNode );
  239. void onTamlCustomRead( const TamlCustomNode* pSpriteNode );
  240. };
  241. //------------------------------------------------------------------------------
  242. static FactoryCache<SpriteBatchItem> SpriteBatchItemFactory;
  243. #endif // _SPRITE_BATCH_ITEM_H_