SpriteBatch.cc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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_H_
  23. #include "SpriteBatch.h"
  24. #endif
  25. #ifndef _SPRITE_BATCH_QUERY_H_
  26. #include "2d/core/SpriteBatchQuery.h"
  27. #endif
  28. #ifndef _SCENE_RENDER_OBJECT_H_
  29. #include "2d/scene/SceneRenderObject.h"
  30. #endif
  31. //------------------------------------------------------------------------------
  32. static StringTableEntry spritesNodeName = StringTable->insert( "Sprites" );
  33. //------------------------------------------------------------------------------
  34. SpriteBatch::SpriteBatch() :
  35. mMasterBatchId( 0 ),
  36. mSelectedSprite( NULL ),
  37. mBatchSortMode( SceneRenderQueue::RENDER_SORT_OFF ),
  38. mDefaultSpriteStride( 1.0f, 1.0f),
  39. mDefaultSpriteSize( 1.0f, 1.0f ),
  40. mDefaultSpriteAngle( 0.0f ),
  41. mpSpriteBatchQuery( NULL ),
  42. mBatchCulling( true )
  43. {
  44. // Reset batch transform.
  45. mBatchTransform.SetIdentity();
  46. mBatchTransformDirty = true;
  47. mBatchTransformId = 0;
  48. // Reset local extents.
  49. mLocalExtents.SetZero();
  50. mLocalExtentsDirty = true;
  51. }
  52. //------------------------------------------------------------------------------
  53. SpriteBatch::~SpriteBatch()
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. bool SpriteBatch::onAdd()
  58. {
  59. // Create the sprite batch query if required.
  60. if ( mBatchCulling )
  61. createSpriteBatchQuery();
  62. return true;
  63. }
  64. //-----------------------------------------------------------------------------
  65. void SpriteBatch::onRemove()
  66. {
  67. // Clear the sprites.
  68. clearSprites();
  69. // Delete the sprite batch query.
  70. destroySpriteBatchQuery();
  71. }
  72. //-----------------------------------------------------------------------------
  73. void SpriteBatch::prepareRender( SceneRenderObject* pSceneRenderObject, const SceneRenderState* pSceneRenderState, SceneRenderQueue* pSceneRenderQueue )
  74. {
  75. // Debug Profiling.
  76. PROFILE_SCOPE(SpriteBatch_PrepareRender);
  77. // Set the sort mode.
  78. pSceneRenderQueue->setSortMode( getBatchSortMode() );
  79. // Calculate local AABB.
  80. const b2AABB localAABB = calculateLocalAABB( pSceneRenderState->mRenderAABB );
  81. // Do we have a sprite batch query?
  82. if ( mpSpriteBatchQuery != NULL )
  83. {
  84. // Yes, so debug Profiling.
  85. PROFILE_START(SpriteBatch_PrepareRenderQuery);
  86. // Yes, so fetch sprite batch query and clear results.
  87. SpriteBatchQuery* pSpriteBatchQuery = getSpriteBatchQuery( true );
  88. // Perform query.
  89. pSpriteBatchQuery->queryArea( localAABB, false );
  90. // Debug Profiling.
  91. PROFILE_END(); // SpriteBatch_PrepareRenderQuery
  92. // Fetch result count.
  93. const U32 resultCount = pSpriteBatchQuery->getQueryResultsCount();
  94. // Finish if no results.
  95. if ( resultCount == 0 )
  96. return;
  97. // Fetch results.
  98. typeSpriteBatchQueryResultVector& queryResults = pSpriteBatchQuery->getQueryResults();
  99. // Add picked sprites.
  100. for ( U32 n = 0; n < resultCount; n++ )
  101. {
  102. // Fetch sprite batch Item.
  103. SpriteBatchItem* pSpriteBatchItem = queryResults[n].mpSpriteBatchItem;
  104. // Skip if not visible.
  105. if ( !pSpriteBatchItem->getVisible() )
  106. continue;
  107. // Create a render request.
  108. SceneRenderRequest* pSceneRenderRequest = pSceneRenderQueue->createRenderRequest();
  109. // Prepare batch item.
  110. pSpriteBatchItem->prepareRender( pSceneRenderRequest, mBatchTransformId );
  111. // Set identity.
  112. pSceneRenderRequest->mpSceneRenderObject = pSceneRenderObject;
  113. // Set custom data.
  114. pSceneRenderRequest->mpCustomData1 = pSpriteBatchItem;
  115. }
  116. // Clear sprite batch query.
  117. pSpriteBatchQuery->clearQuery();
  118. }
  119. else
  120. {
  121. // No, so perform a render request for all the sprites.
  122. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  123. {
  124. // Fetch sprite batch Item.
  125. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  126. // Skip if not visible.
  127. if ( !pSpriteBatchItem->getVisible() )
  128. continue;
  129. // Create a render request.
  130. SceneRenderRequest* pSceneRenderRequest = pSceneRenderQueue->createRenderRequest();
  131. // Prepare batch item.
  132. pSpriteBatchItem->prepareRender( pSceneRenderRequest, mBatchTransformId );
  133. // Set identity.
  134. pSceneRenderRequest->mpSceneRenderObject = pSceneRenderObject;
  135. // Set custom data.
  136. pSceneRenderRequest->mpCustomData1 = pSpriteBatchItem;
  137. }
  138. }
  139. }
  140. //------------------------------------------------------------------------------
  141. void SpriteBatch::render( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
  142. {
  143. // Fetch sprite batch Item.
  144. SpriteBatchItem* pSpriteBatchItem = (SpriteBatchItem*)pSceneRenderRequest->mpCustomData1;
  145. // Batch render.
  146. pSpriteBatchItem->render( pBatchRenderer, pSceneRenderRequest, mBatchTransformId );
  147. }
  148. //------------------------------------------------------------------------------
  149. void SpriteBatch::createQueryProxy( SpriteBatchItem* pSpriteBatchItem )
  150. {
  151. // Sanity!
  152. AssertFatal( pSpriteBatchItem != NULL, "SpriteBatch:createQueryProxy() - Cannot create query proxy on NULL sprite batch item." );
  153. // Debug Profiling.
  154. PROFILE_SCOPE(SpriteBatch_CreateTreeProxy);
  155. // Finish if the batch query is not available.
  156. if ( mpSpriteBatchQuery == NULL )
  157. return;
  158. // Create proxy.
  159. pSpriteBatchItem->mProxyId = mpSpriteBatchQuery->add( pSpriteBatchItem );
  160. }
  161. //------------------------------------------------------------------------------
  162. void SpriteBatch::destroyQueryProxy( SpriteBatchItem* pSpriteBatchItem )
  163. {
  164. // Sanity!
  165. AssertFatal( pSpriteBatchItem != NULL, "SpriteBatch:destroyQueryProxy() - Cannot destroy query proxy on NULL sprite batch item." );
  166. // Debug Profiling.
  167. PROFILE_SCOPE(SpriteBatch_DestroyTreeProxy);
  168. // Finish if the batch query is not available.
  169. if ( mpSpriteBatchQuery == NULL )
  170. return;
  171. // Destroy proxy.
  172. mpSpriteBatchQuery->remove( pSpriteBatchItem );
  173. // Remove proxy reference.
  174. pSpriteBatchItem->mProxyId = INVALID_SPRITE_PROXY;
  175. }
  176. //------------------------------------------------------------------------------
  177. void SpriteBatch::moveQueryProxy( SpriteBatchItem* pSpriteBatchItem, const b2AABB& localAABB )
  178. {
  179. // Sanity!
  180. AssertFatal( pSpriteBatchItem != NULL, "SpriteBatch:moveQueryProxy() - Cannot move query proxy on NULL sprite batch item." );
  181. // Debug Profiling.
  182. PROFILE_SCOPE(SpriteBatch_MoveTreeProxy);
  183. // Finish if the batch query is not available.
  184. if ( mpSpriteBatchQuery == NULL || pSpriteBatchItem->getProxyId() == INVALID_SPRITE_PROXY )
  185. return;
  186. // Move proxy.
  187. mpSpriteBatchQuery->update( pSpriteBatchItem, localAABB, b2Vec2(0.0f, 0.0f) );
  188. }
  189. //------------------------------------------------------------------------------
  190. SpriteBatchQuery* SpriteBatch::getSpriteBatchQuery( const bool clearQuery )
  191. {
  192. if ( mpSpriteBatchQuery == NULL )
  193. return NULL;
  194. // Clear the query if specified.
  195. if ( clearQuery )
  196. mpSpriteBatchQuery->clearQuery();
  197. return mpSpriteBatchQuery;
  198. }
  199. //------------------------------------------------------------------------------
  200. void SpriteBatch::copyTo( SpriteBatch* pSpriteBatch ) const
  201. {
  202. // Clear any existing sprites.
  203. pSpriteBatch->clearSprites();
  204. // Set master sprite Id.
  205. pSpriteBatch->mMasterBatchId = mMasterBatchId;
  206. // Set batch sort mode.
  207. pSpriteBatch->setBatchSortMode( getBatchSortMode() );
  208. // Set batch culling.
  209. pSpriteBatch->setBatchCulling( getBatchCulling() );
  210. // Set sprite default size and angle.
  211. pSpriteBatch->setDefaultSpriteStride( getDefaultSpriteStride() );
  212. pSpriteBatch->setDefaultSpriteSize( getDefaultSpriteSize() );
  213. pSpriteBatch->setDefaultSpriteAngle( getDefaultSpriteAngle() );
  214. // Copy sprites.
  215. for( typeSpriteBatchHash::const_iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  216. {
  217. // Fetch sprite.
  218. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  219. // Add a sprite.
  220. const U32 spriteBatchId = pSpriteBatch->addSprite( pSpriteBatchItem->getLogicalPosition() );
  221. // Fetch new sprite.
  222. SpriteBatchItem* pNewSpriteBatchItem = pSpriteBatch->findSpriteId( spriteBatchId );
  223. // Push a copy to it.
  224. pSpriteBatchItem->copyTo( pNewSpriteBatchItem );
  225. }
  226. }
  227. //------------------------------------------------------------------------------
  228. U32 SpriteBatch::addSprite( const SpriteBatchItem::LogicalPosition& logicalPosition )
  229. {
  230. // Debug Profiling.
  231. PROFILE_SCOPE(SpriteBatch_AddSprite);
  232. // Create sprite layout.
  233. mSelectedSprite = createSprite( logicalPosition );
  234. // Finish if no sprite created.
  235. if ( mSelectedSprite == NULL )
  236. return 0;
  237. // Insert logical position into sprite positions if it's valid.
  238. if ( logicalPosition.isValid() )
  239. mSpritePositions.insert( logicalPosition, mSelectedSprite );
  240. // Flag local extents as dirty.
  241. setLocalExtentsDirty();
  242. return mSelectedSprite->getBatchId();
  243. }
  244. //------------------------------------------------------------------------------
  245. bool SpriteBatch::removeSprite( void )
  246. {
  247. // Debug Profiling.
  248. PROFILE_SCOPE(SpriteBatch_RemoveSprite);
  249. // Finish if a sprite is not selected.
  250. if ( !checkSpriteSelected() )
  251. return false;
  252. // Remove the sprite logical position if it's valid.
  253. const SpriteBatchItem::LogicalPosition& logicalPosition = mSelectedSprite->getLogicalPosition();
  254. if ( logicalPosition.isValid() )
  255. mSpritePositions.erase( mSelectedSprite->getLogicalPosition() );
  256. // Fetch and remove any sprite name.
  257. StringTableEntry spriteName = mSelectedSprite->getName();
  258. if ( spriteName != StringTable->EmptyString )
  259. mSpriteNames.erase( spriteName );
  260. // Destroy the sprite.
  261. destroySprite( mSelectedSprite->getBatchId() );
  262. // Reset the selected sprite.
  263. mSelectedSprite = NULL;
  264. // Flag local extents as dirty.
  265. setLocalExtentsDirty();
  266. return true;
  267. }
  268. //------------------------------------------------------------------------------
  269. void SpriteBatch::clearSprites( void )
  270. {
  271. // Debug Profiling.
  272. PROFILE_SCOPE(SpriteBatch_ClearSprites);
  273. // Deselect any sprite.
  274. deselectSprite();
  275. // Clear sprite positions.
  276. mSpritePositions.clear();
  277. // Clear sprite names.
  278. mSpriteNames.clear();
  279. // Cache all sprites.
  280. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  281. {
  282. SpriteBatchItemFactory.cacheObject( spriteItr->value );
  283. }
  284. mSprites.clear();
  285. mMasterBatchId = 0;
  286. // Flag local extents as dirty.
  287. setLocalExtentsDirty();
  288. }
  289. //------------------------------------------------------------------------------
  290. void SpriteBatch::setBatchCulling( const bool batchCulling )
  291. {
  292. // Finish if no change.
  293. if ( mBatchCulling == batchCulling )
  294. return;
  295. // Set batch culling.
  296. mBatchCulling = batchCulling;
  297. // Create/destroy sprite batch query appropriately.
  298. if ( mBatchCulling )
  299. createSpriteBatchQuery();
  300. else
  301. destroySpriteBatchQuery();
  302. }
  303. //------------------------------------------------------------------------------
  304. bool SpriteBatch::selectSprite( const SpriteBatchItem::LogicalPosition& logicalPosition )
  305. {
  306. // Select sprite.
  307. mSelectedSprite = findSpritePosition( logicalPosition );
  308. // Finish if we selected the sprite.
  309. if ( mSelectedSprite != NULL )
  310. return true;
  311. // Not selected so warn.
  312. Con::warnf( "Cannot select sprite at logical position '%s' as one does not exist.", logicalPosition.getString() );
  313. return false;
  314. }
  315. //------------------------------------------------------------------------------
  316. bool SpriteBatch::selectSpriteId( const U32 batchId )
  317. {
  318. // Select sprite.
  319. mSelectedSprite = findSpriteId( batchId );
  320. // Finish if we selected the sprite.
  321. if ( mSelectedSprite != NULL )
  322. return true;
  323. // Not selected so warn.
  324. Con::warnf( "Cannot select sprite Id '%d' as it does not exist.", batchId );
  325. return false;
  326. }
  327. //------------------------------------------------------------------------------
  328. bool SpriteBatch::selectSpriteName( const char* pName )
  329. {
  330. // Fail if no name specified.
  331. if ( pName == NULL || pName == StringTable->EmptyString )
  332. return false;
  333. // Select sprite.
  334. mSelectedSprite = findSpriteName( pName );
  335. // Finish if we selected the sprite.
  336. if ( mSelectedSprite != NULL )
  337. return true;
  338. // Not selected so warn.
  339. Con::warnf( "Cannot select sprite name '%s' as it does not exist.", pName );
  340. return false;
  341. }
  342. //------------------------------------------------------------------------------
  343. U32 SpriteBatch::getSpriteId( void ) const
  344. {
  345. // Finish if a sprite is not selected.
  346. if ( !checkSpriteSelected() )
  347. return 0;
  348. // Get sprite id.
  349. return mSelectedSprite->getBatchId();
  350. }
  351. //------------------------------------------------------------------------------
  352. void SpriteBatch::setSpriteImage( const char* pAssetId, const U32 imageFrame )
  353. {
  354. // Debug Profiling.
  355. PROFILE_SCOPE(SpriteBatch_SetSpriteImage);
  356. // Sanity!
  357. AssertFatal( pAssetId, "Cannot set sprite image using a NULL asset Id." );
  358. // Finish if a sprite is not selected.
  359. if ( !checkSpriteSelected() )
  360. return;
  361. // Set image and frame.
  362. mSelectedSprite->setImage( pAssetId, imageFrame );
  363. }
  364. //------------------------------------------------------------------------------
  365. void SpriteBatch::setSpriteImage( const char* pAssetId, const char* namedFrame )
  366. {
  367. // Debug Profiling.
  368. PROFILE_SCOPE(SpriteBatch_SetSpriteImage);
  369. // Sanity!
  370. AssertFatal( pAssetId, "Cannot set sprite image using a NULL asset Id." );
  371. // Finish if a sprite is not selected.
  372. if ( !checkSpriteSelected() )
  373. return;
  374. // Set image and frame.
  375. mSelectedSprite->setImage( pAssetId, namedFrame );
  376. }
  377. //------------------------------------------------------------------------------
  378. StringTableEntry SpriteBatch::getSpriteImage( void ) const
  379. {
  380. // Finish if a sprite is not selected.
  381. if ( !checkSpriteSelected() )
  382. return StringTable->EmptyString;
  383. // Get sprite image.
  384. return mSelectedSprite->getImage();
  385. }
  386. //------------------------------------------------------------------------------
  387. void SpriteBatch::setSpriteImageFrame( const U32 imageFrame )
  388. {
  389. // Finish if a sprite is not selected.
  390. if ( !checkSpriteSelected() )
  391. return;
  392. // Set image frame.
  393. mSelectedSprite->setImageFrame( imageFrame );
  394. }
  395. //------------------------------------------------------------------------------
  396. U32 SpriteBatch::getSpriteImageFrame( void ) const
  397. {
  398. // Finish if a sprite is not selected.
  399. if ( !checkSpriteSelected() )
  400. return NULL;
  401. // Get image frame.
  402. return mSelectedSprite->getImageFrame();
  403. }
  404. //------------------------------------------------------------------------------
  405. void SpriteBatch::setSpriteNamedImageFrame( const char* namedFrame )
  406. {
  407. // Finish if a sprite is not selected.
  408. if ( !checkSpriteSelected() )
  409. return;
  410. // Set image frame.
  411. mSelectedSprite->setNamedImageFrame( namedFrame );
  412. }
  413. //------------------------------------------------------------------------------
  414. StringTableEntry SpriteBatch::getSpriteNamedImageFrame( void ) const
  415. {
  416. // Finish if a sprite is not selected.
  417. if ( !checkSpriteSelected() )
  418. return NULL;
  419. // Get image frame.
  420. return mSelectedSprite->getNamedImageFrame();
  421. }
  422. //------------------------------------------------------------------------------
  423. void SpriteBatch::setSpriteAnimation( const char* pAssetId )
  424. {
  425. // Debug Profiling.
  426. PROFILE_SCOPE(SpriteBatch_SetSpriteAnimation);
  427. // Sanity!
  428. AssertFatal( pAssetId, "Cannot set sprite animation using a NULL asset Id." );
  429. // Finish if a sprite is not selected.
  430. if ( !checkSpriteSelected() )
  431. return;
  432. // Set animation.
  433. mSelectedSprite->setAnimation( pAssetId );
  434. }
  435. //------------------------------------------------------------------------------
  436. StringTableEntry SpriteBatch::getSpriteAnimation( void ) const
  437. {
  438. // Finish if a sprite is not selected.
  439. if ( !checkSpriteSelected() )
  440. return StringTable->EmptyString;
  441. // Get animation.
  442. return mSelectedSprite->getAnimation();
  443. }
  444. //------------------------------------------------------------------------------
  445. void SpriteBatch::setSpriteAnimationFrame(const U32 animationFrame)
  446. {
  447. // Finish if a sprite is not selected.
  448. if (!checkSpriteSelected())
  449. return;
  450. // Set image frame.
  451. mSelectedSprite->setAnimationFrame(animationFrame);
  452. }
  453. //------------------------------------------------------------------------------
  454. U32 SpriteBatch::getSpriteAnimationFrame(void) const
  455. {
  456. // Finish if a sprite is not selected.
  457. if (!checkSpriteSelected())
  458. return NULL;
  459. // Get image frame.
  460. return mSelectedSprite->getAnimationFrame();
  461. }
  462. //------------------------------------------------------------------------------
  463. void SpriteBatch::clearSpriteAsset( void )
  464. {
  465. // Finish if a sprite is not selected.
  466. if ( !checkSpriteSelected() )
  467. return;
  468. // Clear the asset.
  469. mSelectedSprite->clearAssets();
  470. }
  471. //------------------------------------------------------------------------------
  472. void SpriteBatch::setSpriteVisible( const bool visible )
  473. {
  474. // Finish if a sprite is not selected.
  475. if ( !checkSpriteSelected() )
  476. return;
  477. // Set visibility.
  478. mSelectedSprite->setVisible( visible );
  479. }
  480. //------------------------------------------------------------------------------
  481. bool SpriteBatch::getSpriteVisible( void ) const
  482. {
  483. // Finish if a sprite is not selected.
  484. if ( !checkSpriteSelected() )
  485. return false;
  486. // Get visibility.
  487. return mSelectedSprite->getVisible();
  488. }
  489. //------------------------------------------------------------------------------
  490. void SpriteBatch::setSpriteLocalPosition( const Vector2& localPosition )
  491. {
  492. // Finish if a sprite is not selected.
  493. if ( !checkSpriteSelected() )
  494. return;
  495. // Set local position.
  496. mSelectedSprite->setLocalPosition( localPosition );
  497. // Flag local extents as dirty.
  498. setLocalExtentsDirty();
  499. }
  500. //------------------------------------------------------------------------------
  501. Vector2 SpriteBatch::getSpriteLocalPosition( void )
  502. {
  503. // Finish if a sprite is not selected.
  504. if ( !checkSpriteSelected() )
  505. return Vector2::getZero();
  506. // Get local position.
  507. return mSelectedSprite->getLocalPosition();
  508. }
  509. //------------------------------------------------------------------------------
  510. const SpriteBatchItem::LogicalPosition SpriteBatch::getSpriteLogicalPosition( void ) const
  511. {
  512. // Finish if a sprite is not selected.
  513. if ( !checkSpriteSelected() )
  514. return NULL;
  515. // Get logical position.
  516. return mSelectedSprite->getLogicalPosition();
  517. }
  518. //------------------------------------------------------------------------------
  519. void SpriteBatch::setSpriteAngle( const F32 localAngle )
  520. {
  521. // Finish if a sprite is not selected.
  522. if ( !checkSpriteSelected() )
  523. return;
  524. // Set local angle.
  525. mSelectedSprite->setLocalAngle( localAngle );
  526. // Flag local extents as dirty.
  527. setLocalExtentsDirty();
  528. }
  529. //------------------------------------------------------------------------------
  530. F32 SpriteBatch::getSpriteAngle( void ) const
  531. {
  532. // Finish if a sprite is not selected.
  533. if ( !checkSpriteSelected() )
  534. return 0.0f;
  535. // Get local angle.
  536. return mSelectedSprite->getLocalAngle();
  537. }
  538. //------------------------------------------------------------------------------
  539. void SpriteBatch::setSpriteDepth( const F32 depth )
  540. {
  541. // Finish if a sprite is not selected.
  542. if ( !checkSpriteSelected() )
  543. return;
  544. // Set depth.
  545. mSelectedSprite->setDepth( depth );
  546. }
  547. //------------------------------------------------------------------------------
  548. F32 SpriteBatch::getSpriteDepth( void ) const
  549. {
  550. // Finish if a sprite is not selected.
  551. if ( !checkSpriteSelected() )
  552. return 0.0f;
  553. // Get depth.
  554. return mSelectedSprite->getDepth();
  555. }
  556. //------------------------------------------------------------------------------
  557. void SpriteBatch::setSpriteSize( const Vector2& size )
  558. {
  559. // Finish if a sprite is not selected.
  560. if ( !checkSpriteSelected() )
  561. return;
  562. // Set size.
  563. mSelectedSprite->setSize( size );
  564. // Flag local extents as dirty.
  565. setLocalExtentsDirty();
  566. }
  567. //------------------------------------------------------------------------------
  568. Vector2 SpriteBatch::getSpriteSize( void ) const
  569. {
  570. // Finish if a sprite is not selected.
  571. if ( !checkSpriteSelected() )
  572. return Vector2::getZero();
  573. // Get size.
  574. return mSelectedSprite->getSize();
  575. }
  576. //------------------------------------------------------------------------------
  577. void SpriteBatch::setSpriteFlipX( const bool flipX )
  578. {
  579. // Finish if a sprite is not selected.
  580. if ( !checkSpriteSelected() )
  581. return;
  582. // Set flip X.
  583. mSelectedSprite->setFlipX( flipX );
  584. }
  585. //------------------------------------------------------------------------------
  586. bool SpriteBatch::getSpriteFlipX( void ) const
  587. {
  588. // Finish if a sprite is not selected.
  589. if ( !checkSpriteSelected() )
  590. return false;
  591. // Get flip X.
  592. return mSelectedSprite->getFlipX();
  593. }
  594. //------------------------------------------------------------------------------
  595. void SpriteBatch::setSpriteFlipY( const bool flipY )
  596. {
  597. // Finish if a sprite is not selected.
  598. if ( !checkSpriteSelected() )
  599. return;
  600. // Set flip Y.
  601. mSelectedSprite->setFlipY( flipY );
  602. }
  603. //------------------------------------------------------------------------------
  604. bool SpriteBatch::getSpriteFlipY( void ) const
  605. {
  606. // Finish if a sprite is not selected.
  607. if ( !checkSpriteSelected() )
  608. return false;
  609. // Get flip Y.
  610. return mSelectedSprite->getFlipY();
  611. }
  612. //------------------------------------------------------------------------------
  613. void SpriteBatch::setSpriteSortPoint( const Vector2& sortPoint )
  614. {
  615. // Finish if a sprite is not selected.
  616. if ( !checkSpriteSelected() )
  617. return;
  618. // Set sort point.
  619. mSelectedSprite->setSortPoint( sortPoint );
  620. }
  621. //------------------------------------------------------------------------------
  622. Vector2 SpriteBatch::getSpriteSortPoint( void ) const
  623. {
  624. // Finish if a sprite is not selected.
  625. if ( !checkSpriteSelected() )
  626. return Vector2::getZero();
  627. // Get sort point.
  628. return mSelectedSprite->getSortPoint();
  629. }
  630. //------------------------------------------------------------------------------
  631. void SpriteBatch::setSpriteRenderGroup( const char* pRenderGroup )
  632. {
  633. // Finish if a sprite is not selected.
  634. if ( !checkSpriteSelected() )
  635. return;
  636. // Set render group.
  637. mSelectedSprite->setRenderGroup( pRenderGroup );
  638. }
  639. //------------------------------------------------------------------------------
  640. StringTableEntry SpriteBatch::getSpriteRenderGroup( void ) const
  641. {
  642. // Finish if a sprite is not selected.
  643. if ( !checkSpriteSelected() )
  644. return NULL;
  645. // Get render group.
  646. return mSelectedSprite->getRenderGroup();
  647. }
  648. //------------------------------------------------------------------------------
  649. void SpriteBatch::setSpriteBlendMode( const bool blendMode )
  650. {
  651. // Finish if a sprite is not selected.
  652. if ( !checkSpriteSelected() )
  653. return;
  654. // Set blend mode.
  655. mSelectedSprite->setBlendMode( blendMode );
  656. }
  657. //------------------------------------------------------------------------------
  658. bool SpriteBatch::getSpriteBlendMode( void ) const
  659. {
  660. // Finish if a sprite is not selected.
  661. if ( !checkSpriteSelected() )
  662. return true;
  663. // Get blend mode.
  664. return mSelectedSprite->getBlendMode();
  665. }
  666. //------------------------------------------------------------------------------
  667. void SpriteBatch::setSpriteSrcBlendFactor( GLenum srcBlendFactor )
  668. {
  669. // Finish if a sprite is not selected.
  670. if ( !checkSpriteSelected() )
  671. return;
  672. // Set source blend factor.
  673. mSelectedSprite->setSrcBlendFactor( srcBlendFactor );
  674. }
  675. //------------------------------------------------------------------------------
  676. GLenum SpriteBatch::getSpriteSrcBlendFactor( void ) const
  677. {
  678. // Finish if a sprite is not selected.
  679. if ( !checkSpriteSelected() )
  680. return GL_SRC_ALPHA;
  681. // Get source blend factor.
  682. return mSelectedSprite->getSrcBlendFactor();
  683. }
  684. //------------------------------------------------------------------------------
  685. void SpriteBatch::setSpriteDstBlendFactor( GLenum dstBlendFactor )
  686. {
  687. // Finish if a sprite is not selected.
  688. if ( !checkSpriteSelected() )
  689. return ;
  690. // Set destination blend factor.
  691. mSelectedSprite->setDstBlendFactor( dstBlendFactor );
  692. }
  693. //------------------------------------------------------------------------------
  694. GLenum SpriteBatch::getSpriteDstBlendFactor( void ) const
  695. {
  696. // Finish if a sprite is not selected.
  697. if ( !checkSpriteSelected() )
  698. return GL_ONE_MINUS_SRC_ALPHA;
  699. // Get destination blend factor.
  700. return mSelectedSprite->getDstBlendFactor();
  701. }
  702. //------------------------------------------------------------------------------
  703. void SpriteBatch::setSpriteBlendColor( const ColorF& blendColor )
  704. {
  705. // Finish if a sprite is not selected.
  706. if ( !checkSpriteSelected() )
  707. return;
  708. // Set blend color.
  709. mSelectedSprite->setBlendColor( blendColor );
  710. }
  711. //------------------------------------------------------------------------------
  712. const ColorF& SpriteBatch::getSpriteBlendColor( void ) const
  713. {
  714. // Finish if a sprite is not selected.
  715. if ( !checkSpriteSelected() )
  716. return ColorF::StockColor("White");
  717. // Get blend color.
  718. return mSelectedSprite->getBlendColor();
  719. }
  720. //------------------------------------------------------------------------------
  721. void SpriteBatch::setSpriteBlendAlpha( const F32 alpha )
  722. {
  723. // Finish if a sprite is not selected.
  724. if ( !checkSpriteSelected() )
  725. return;
  726. // Set blend alpha.
  727. mSelectedSprite->setBlendAlpha( alpha );
  728. }
  729. //------------------------------------------------------------------------------
  730. F32 SpriteBatch::getSpriteBlendAlpha( void ) const
  731. {
  732. // Finish if a sprite is not selected.
  733. if ( !checkSpriteSelected() )
  734. return 0.0f;
  735. // Get blend alpha.
  736. return mSelectedSprite->getBlendAlpha();
  737. }
  738. //------------------------------------------------------------------------------
  739. void SpriteBatch::setSpriteAlphaTest( const F32 alphaTestMode )
  740. {
  741. // Finish if a sprite is not selected.
  742. if ( !checkSpriteSelected() )
  743. return;
  744. // Set alpha-test mode.
  745. mSelectedSprite->setAlphaTest( alphaTestMode );
  746. }
  747. //------------------------------------------------------------------------------
  748. F32 SpriteBatch::getSpriteAlphaTest( void ) const
  749. {
  750. // Finish if a sprite is not selected.
  751. if ( !checkSpriteSelected() )
  752. return -1.0f;
  753. // Get alpha-test mode.
  754. return mSelectedSprite->getAlphaTest();
  755. }
  756. //------------------------------------------------------------------------------
  757. void SpriteBatch::setSpriteDataObject( SimObject* pDataObject )
  758. {
  759. // Finish if a sprite is not selected.
  760. if ( !checkSpriteSelected() )
  761. return;
  762. // Set data object.
  763. mSelectedSprite->setDataObject( pDataObject );
  764. }
  765. //------------------------------------------------------------------------------
  766. SimObject* SpriteBatch::getSpriteDataObject( void ) const
  767. {
  768. // Finish if a sprite is not selected.
  769. if ( !checkSpriteSelected() )
  770. return NULL;
  771. // Get data object.
  772. return mSelectedSprite->getDataObject();
  773. }
  774. //------------------------------------------------------------------------------
  775. void SpriteBatch::setUserData( void* pUserData )
  776. {
  777. // Finish if a sprite is not selected.
  778. if ( !checkSpriteSelected() )
  779. return;
  780. // Set user data.
  781. mSelectedSprite->setUserData( pUserData );
  782. }
  783. //------------------------------------------------------------------------------
  784. void* SpriteBatch::getUserData( void ) const
  785. {
  786. // Finish if a sprite is not selected.
  787. if ( !checkSpriteSelected() )
  788. return NULL;
  789. // Get user data.
  790. return mSelectedSprite->getUserData();
  791. }
  792. //------------------------------------------------------------------------------
  793. void SpriteBatch::setSpriteName( const char* pName )
  794. {
  795. // Finish if a sprite is not selected.
  796. if ( !checkSpriteSelected() )
  797. return;
  798. // Finish if the sprite name already exists.
  799. if ( findSpriteName( pName ) )
  800. return;
  801. // Insert sprite name.
  802. mSpriteNames.insert( StringTable->insert( pName ), mSelectedSprite );
  803. // Set name.
  804. mSelectedSprite->setName( pName );
  805. }
  806. //------------------------------------------------------------------------------
  807. StringTableEntry SpriteBatch::getSpriteName( void ) const
  808. {
  809. // Finish if a sprite is not selected.
  810. if ( !checkSpriteSelected() )
  811. return StringTable->EmptyString;
  812. // Get name.
  813. return mSelectedSprite->getName();
  814. }
  815. //------------------------------------------------------------------------------
  816. SpriteBatchItem* SpriteBatch::createSprite( void )
  817. {
  818. // Debug Profiling.
  819. PROFILE_SCOPE(SpriteBatch_CreateSprite);
  820. // Allocate batch Id.
  821. const U32 batchId = ++mMasterBatchId;
  822. // Create sprite batch item,
  823. SpriteBatchItem* pSpriteBatchItem = SpriteBatchItemFactory.createObject();
  824. // Set batch parent.
  825. pSpriteBatchItem->setBatchParent( this, batchId );
  826. // Create sprite batch item,
  827. mSprites.insert( batchId, pSpriteBatchItem );
  828. return pSpriteBatchItem;
  829. }
  830. //------------------------------------------------------------------------------
  831. SpriteBatchItem* SpriteBatch::createSprite( const Vector2* explicitVertices )
  832. {
  833. // Debug Profiling.
  834. PROFILE_SCOPE(SpriteBatch_CreateSprite);
  835. // Allocate batch Id.
  836. const U32 batchId = ++mMasterBatchId;
  837. // Create sprite batch item,
  838. SpriteBatchItem* pSpriteBatchItem = SpriteBatchItemFactory.createObject();
  839. // Set batch parent.
  840. pSpriteBatchItem->setBatchParent( this, batchId );
  841. // Set explicit mode.
  842. pSpriteBatchItem->setExplicitMode( true );
  843. // Set explicit vertices.
  844. // Create sprite batch item,
  845. mSprites.insert( batchId, pSpriteBatchItem );
  846. return pSpriteBatchItem;
  847. }
  848. //------------------------------------------------------------------------------
  849. SpriteBatchItem* SpriteBatch::findSpritePosition( const SpriteBatchItem::LogicalPosition& logicalPosition )
  850. {
  851. // Debug Profiling.
  852. PROFILE_SCOPE(SpriteBatch_FindSpritePosition);
  853. // Invalid logical positions are not stored.
  854. if ( !logicalPosition.isValid() )
  855. return NULL;
  856. // Find sprite.
  857. typeSpritePositionHash::iterator spriteItr = mSpritePositions.find( logicalPosition );
  858. return spriteItr == mSpritePositions.end() ? NULL : spriteItr->value;
  859. }
  860. //------------------------------------------------------------------------------
  861. SpriteBatchItem* SpriteBatch::findSpriteId( const U32 batchId )
  862. {
  863. // Debug Profiling.
  864. PROFILE_SCOPE(SpriteBatch_FindSpriteId);
  865. // Find sprite.
  866. typeSpriteBatchHash::iterator spriteItr = mSprites.find( batchId );
  867. return spriteItr != mSprites.end() ? spriteItr->value : NULL;
  868. }
  869. //------------------------------------------------------------------------------
  870. SpriteBatchItem* SpriteBatch::findSpriteName( const char* pName )
  871. {
  872. // Debug Profiling.
  873. PROFILE_SCOPE(SpriteBatch_FindSpriteName);
  874. // Finish if no name specified.
  875. if ( pName == NULL || pName == StringTable->EmptyString )
  876. return NULL;
  877. // Find sprite.
  878. typeSpriteNameHash::iterator spriteItr = mSpriteNames.find( StringTable->insert(pName) );
  879. return spriteItr != mSpriteNames.end() ? spriteItr->value : NULL;
  880. }
  881. //------------------------------------------------------------------------------
  882. SpriteBatchItem* SpriteBatch::createSprite( const SpriteBatchItem::LogicalPosition& logicalPosition )
  883. {
  884. // Debug Profiling.
  885. PROFILE_SCOPE(SpriteBatch_CreateSpriteAtLogicalPosition);
  886. // Reset sprite batch item.
  887. SpriteBatchItem* pSpriteBatchItem = NULL;
  888. // Do we have a valid logical position?
  889. if ( logicalPosition.isValid() )
  890. {
  891. // Does it have the correct argument count?
  892. if ( logicalPosition.getArgCount() != 2 )
  893. {
  894. // No, so warn.
  895. Con::warnf( "Invalid logical position specified for composite sprite." );
  896. return NULL;
  897. }
  898. // Does the sprite already exist?
  899. if ( findSpritePosition( logicalPosition ) != NULL )
  900. {
  901. // Yes, so warn.
  902. Con::warnf( "Cannot add sprite at logical position '%s' as one already exists.", logicalPosition.getString() );
  903. return NULL;
  904. }
  905. // Create the sprite.
  906. pSpriteBatchItem = createSprite();
  907. // Set the logical position.
  908. pSpriteBatchItem->setLogicalPosition( logicalPosition );
  909. // Set the sprite default local position.
  910. pSpriteBatchItem->setLocalPosition( logicalPosition.getAsVector2() );
  911. }
  912. else
  913. {
  914. // Create the sprite.
  915. pSpriteBatchItem = createSprite();
  916. }
  917. // Set the sprite default size and angle.
  918. pSpriteBatchItem->setSize( getDefaultSpriteSize() );
  919. pSpriteBatchItem->setLocalAngle( getDefaultSpriteAngle() );
  920. return pSpriteBatchItem;
  921. }
  922. //------------------------------------------------------------------------------
  923. void SpriteBatch::setBatchTransform( const b2Transform& batchTransform )
  924. {
  925. // Update world transform.
  926. mBatchTransform = batchTransform;
  927. // Flag the batch transform as dirty.
  928. setBatchTransformDirty();
  929. }
  930. //------------------------------------------------------------------------------
  931. void SpriteBatch::updateLocalExtents( void )
  932. {
  933. // Debug Profiling.
  934. PROFILE_SCOPE(SpriteBatch_UpdateLocalExtents);
  935. // Finish if the local extents are not dirty.
  936. if ( !mLocalExtentsDirty )
  937. return;
  938. // Flag as NOT dirty.
  939. mLocalExtentsDirty = false;
  940. // Do we have any sprites?
  941. if ( mSprites.size() == 0 )
  942. {
  943. // No, so reset local extents.
  944. mLocalExtents.setOne();
  945. return;
  946. }
  947. // Fetch first sprite.
  948. typeSpriteBatchHash::iterator spriteItr = mSprites.begin();
  949. // Set render AABB to this sprite.
  950. b2AABB localAABB = spriteItr->value->getLocalAABB();
  951. // Combine with the rest of the sprites.
  952. for( ; spriteItr != mSprites.end(); ++spriteItr )
  953. {
  954. localAABB.Combine( spriteItr->value->getLocalAABB() );
  955. }
  956. // Fetch local render extents.
  957. const b2Vec2& localLowerExtent = localAABB.lowerBound;
  958. const b2Vec2& localUpperExtent = localAABB.upperBound;
  959. // Calculate maximum extents.
  960. const F32 lowerExtentX = mFabs(localLowerExtent.x);
  961. const F32 lowerExtentY = mFabs(localLowerExtent.y);
  962. const F32 upperExtentX = mFabs(localUpperExtent.x);
  963. const F32 upperExtentY = mFabs(localUpperExtent.y);
  964. // Calculate local extents.
  965. mLocalExtents.Set( mFabs(lowerExtentX > upperExtentX ? lowerExtentX : upperExtentX) * 2.0f, mFabs(lowerExtentY > upperExtentY ? lowerExtentY : upperExtentY) * 2.0f );
  966. }
  967. //------------------------------------------------------------------------------
  968. void SpriteBatch::createSpriteBatchQuery( void )
  969. {
  970. // Debug Profiling.
  971. PROFILE_SCOPE(SpriteBatch_CreateSpriteBatchQuery);
  972. // Finish if batch culling is off or there is already a sprite batch query.
  973. if ( !mBatchCulling || mpSpriteBatchQuery != NULL )
  974. return;
  975. // Set the sprite batch query appropriately.
  976. mpSpriteBatchQuery = new SpriteBatchQuery( this );
  977. // Finish if there are no sprites.
  978. if ( mSprites.size() == 0 )
  979. return;
  980. // Add proxies for all the sprites.
  981. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  982. {
  983. // Fetch sprite batch item.
  984. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  985. // Create query proxy for sprite.
  986. createQueryProxy( pSpriteBatchItem );
  987. }
  988. }
  989. //------------------------------------------------------------------------------
  990. void SpriteBatch::destroySpriteBatchQuery( void )
  991. {
  992. // Debug Profiling.
  993. PROFILE_SCOPE(SpriteBatch_DestroySpriteBatchQuery);
  994. // Finish if there is no sprite batch query.
  995. if ( mpSpriteBatchQuery == NULL )
  996. return;
  997. // Are there any sprites?
  998. if ( mSprites.size() > 0 )
  999. {
  1000. // Yes, so destroy proxies of all the sprites.
  1001. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  1002. {
  1003. // Destroy query proxy for sprite.
  1004. destroyQueryProxy( spriteItr->value );
  1005. }
  1006. }
  1007. // Finish if sprite clipping
  1008. delete mpSpriteBatchQuery;
  1009. mpSpriteBatchQuery = NULL;
  1010. }
  1011. //------------------------------------------------------------------------------
  1012. bool SpriteBatch::destroySprite( const U32 batchId )
  1013. {
  1014. // Debug Profiling.
  1015. PROFILE_SCOPE(SpriteBatch_DestroySprite);
  1016. // Find sprite.
  1017. typeSpriteBatchHash::iterator spriteItr = mSprites.find( batchId );
  1018. // Finish if sprite not found.
  1019. if ( spriteItr == mSprites.end() )
  1020. return false;
  1021. // Find sprite.
  1022. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  1023. // Sanity!
  1024. AssertFatal( pSpriteBatchItem != NULL, "SpriteBatch::destroySprite() - Found sprite but it was NULL." );
  1025. // Cache sprite.
  1026. SpriteBatchItemFactory.cacheObject( pSpriteBatchItem );
  1027. // Remove from sprites.
  1028. mSprites.erase( batchId );
  1029. return true;
  1030. }
  1031. //------------------------------------------------------------------------------
  1032. bool SpriteBatch::checkSpriteSelected( void ) const
  1033. {
  1034. // Finish if a sprite is selected.
  1035. if ( mSelectedSprite != NULL )
  1036. return true;
  1037. // No, so warn,
  1038. Con::warnf( "Cannot perform sprite operation no sprite is selected." );
  1039. return false;
  1040. }
  1041. //------------------------------------------------------------------------------
  1042. b2AABB SpriteBatch::calculateLocalAABB( const b2AABB& renderAABB )
  1043. {
  1044. // Debug Profiling.
  1045. PROFILE_SCOPE(SpriteBatch_CalculateLocalAABB);
  1046. // Calculate local OOBB.
  1047. b2Vec2 localOOBB[4];
  1048. CoreMath::mAABBtoOOBB( renderAABB, localOOBB );
  1049. CoreMath::mCalculateInverseOOBB( localOOBB, mBatchTransform, localOOBB );
  1050. // Calculate local AABB.
  1051. b2AABB localAABB;
  1052. CoreMath::mOOBBtoAABB( localOOBB, localAABB );
  1053. return localAABB;
  1054. }
  1055. //------------------------------------------------------------------------------
  1056. void SpriteBatch::onTamlCustomWrite( TamlCustomNodes& customNodes )
  1057. {
  1058. // Debug Profiling.
  1059. PROFILE_SCOPE(SpriteBatch_TamlCustomWrite);
  1060. // Finish if no sprites.
  1061. if ( getSpriteCount() == 0 )
  1062. return;
  1063. // Add sprites node.
  1064. TamlCustomNode* pSpritesNode = customNodes.addNode( spritesNodeName );
  1065. // Write all sprites.
  1066. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  1067. {
  1068. // Write type with sprite item.
  1069. spriteItr->value->onTamlCustomWrite( pSpritesNode );
  1070. }
  1071. }
  1072. //------------------------------------------------------------------------------
  1073. void SpriteBatch::onTamlCustomRead( const TamlCustomNodes& customNodes )
  1074. {
  1075. // Debug Profiling.
  1076. PROFILE_SCOPE(SpriteBatch_TamlCustomRead);
  1077. // Find sprites custom node.
  1078. const TamlCustomNode* pSpritesNode = customNodes.findNode( spritesNodeName );
  1079. // Finish if we don't have the node.
  1080. if ( pSpritesNode == NULL )
  1081. return;
  1082. // Fetch children nodes.
  1083. const TamlCustomNodeVector& spriteNodes = pSpritesNode->getChildren();
  1084. // Iterate sprite item types.
  1085. for( TamlCustomNodeVector::const_iterator spriteItr = spriteNodes.begin(); spriteItr != spriteNodes.end(); ++spriteItr )
  1086. {
  1087. // Fetch sprite node.
  1088. TamlCustomNode* pNode = *spriteItr;
  1089. // Fetch alias name.
  1090. StringTableEntry nodeName = pNode->getNodeName();
  1091. // Is this a known node name?
  1092. if ( nodeName != spritesItemTypeName )
  1093. {
  1094. // No, so warn.
  1095. Con::warnf( "SpriteBatch - Unknown custom type '%s'.", nodeName );
  1096. continue;
  1097. }
  1098. // Create sprite.
  1099. SpriteBatchItem* pSpriteBatchItem = createSprite();
  1100. // Read type with sprite item.
  1101. pSpriteBatchItem->onTamlCustomRead( pNode );
  1102. // Fetch logical position.
  1103. const SpriteBatchItem::LogicalPosition& logicalPosition = pSpriteBatchItem->getLogicalPosition();
  1104. // Did we get a logical position?
  1105. if ( logicalPosition.isValid() )
  1106. {
  1107. // Yes, so insert into sprite positions.
  1108. mSpritePositions.insert( logicalPosition, pSpriteBatchItem );
  1109. }
  1110. // Fetch sprite name.
  1111. StringTableEntry spriteName = pSpriteBatchItem->getName();
  1112. // Did we get a sprite name?
  1113. if ( spriteName != StringTable->EmptyString )
  1114. {
  1115. // Yes, so insert into sprite names if it doesn't already exist.
  1116. if ( mSpriteNames.find( spriteName ) != mSpriteNames.end() )
  1117. mSpriteNames.insert( spriteName, mSelectedSprite );
  1118. }
  1119. }
  1120. }
  1121. //------------------------------------------------------------------------------
  1122. void SpriteBatch::WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  1123. {
  1124. // Sanity!
  1125. AssertFatal( pClassRep != NULL, "SpriteBatch::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  1126. AssertFatal( pParentElement != NULL, "SpriteBatch::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  1127. char buffer[1024];
  1128. // Create sprite batch node element.
  1129. TiXmlElement* pBatchNodeElement = new TiXmlElement( "xs:element" );
  1130. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), spritesNodeName );
  1131. pBatchNodeElement->SetAttribute( "name", buffer );
  1132. pBatchNodeElement->SetAttribute( "minOccurs", 0 );
  1133. pBatchNodeElement->SetAttribute( "maxOccurs", 1 );
  1134. pParentElement->LinkEndChild( pBatchNodeElement );
  1135. // Create complex type.
  1136. TiXmlElement* pBatchNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1137. pBatchNodeElement->LinkEndChild( pBatchNodeComplexTypeElement );
  1138. // Create choice element.
  1139. TiXmlElement* pBatchNodeChoiceElement = new TiXmlElement( "xs:choice" );
  1140. pBatchNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  1141. pBatchNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  1142. pBatchNodeComplexTypeElement->LinkEndChild( pBatchNodeChoiceElement );
  1143. // Write sprite batch item.
  1144. SpriteBatchItem::WriteCustomTamlSchema( pClassRep, pBatchNodeChoiceElement );
  1145. }