SpriteBatch.cc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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::clearSpriteAsset( void )
  446. {
  447. // Finish if a sprite is not selected.
  448. if ( !checkSpriteSelected() )
  449. return;
  450. // Clear the asset.
  451. mSelectedSprite->clearAssets();
  452. }
  453. //------------------------------------------------------------------------------
  454. void SpriteBatch::setSpriteVisible( const bool visible )
  455. {
  456. // Finish if a sprite is not selected.
  457. if ( !checkSpriteSelected() )
  458. return;
  459. // Set visibility.
  460. mSelectedSprite->setVisible( visible );
  461. }
  462. //------------------------------------------------------------------------------
  463. bool SpriteBatch::getSpriteVisible( void ) const
  464. {
  465. // Finish if a sprite is not selected.
  466. if ( !checkSpriteSelected() )
  467. return false;
  468. // Get visibility.
  469. return mSelectedSprite->getVisible();
  470. }
  471. //------------------------------------------------------------------------------
  472. void SpriteBatch::setSpriteLocalPosition( const Vector2& localPosition )
  473. {
  474. // Finish if a sprite is not selected.
  475. if ( !checkSpriteSelected() )
  476. return;
  477. // Set local position.
  478. mSelectedSprite->setLocalPosition( localPosition );
  479. // Flag local extents as dirty.
  480. setLocalExtentsDirty();
  481. }
  482. //------------------------------------------------------------------------------
  483. Vector2 SpriteBatch::getSpriteLocalPosition( void )
  484. {
  485. // Finish if a sprite is not selected.
  486. if ( !checkSpriteSelected() )
  487. return Vector2::getZero();
  488. // Get local position.
  489. return mSelectedSprite->getLocalPosition();
  490. }
  491. //------------------------------------------------------------------------------
  492. const SpriteBatchItem::LogicalPosition SpriteBatch::getSpriteLogicalPosition( void ) const
  493. {
  494. // Finish if a sprite is not selected.
  495. if ( !checkSpriteSelected() )
  496. return NULL;
  497. // Get logical position.
  498. return mSelectedSprite->getLogicalPosition();
  499. }
  500. //------------------------------------------------------------------------------
  501. void SpriteBatch::setSpriteAngle( const F32 localAngle )
  502. {
  503. // Finish if a sprite is not selected.
  504. if ( !checkSpriteSelected() )
  505. return;
  506. // Set local angle.
  507. mSelectedSprite->setLocalAngle( localAngle );
  508. // Flag local extents as dirty.
  509. setLocalExtentsDirty();
  510. }
  511. //------------------------------------------------------------------------------
  512. F32 SpriteBatch::getSpriteAngle( void ) const
  513. {
  514. // Finish if a sprite is not selected.
  515. if ( !checkSpriteSelected() )
  516. return 0.0f;
  517. // Get local angle.
  518. return mSelectedSprite->getLocalAngle();
  519. }
  520. //------------------------------------------------------------------------------
  521. void SpriteBatch::setSpriteDepth( const F32 depth )
  522. {
  523. // Finish if a sprite is not selected.
  524. if ( !checkSpriteSelected() )
  525. return;
  526. // Set depth.
  527. mSelectedSprite->setDepth( depth );
  528. }
  529. //------------------------------------------------------------------------------
  530. F32 SpriteBatch::getSpriteDepth( void ) const
  531. {
  532. // Finish if a sprite is not selected.
  533. if ( !checkSpriteSelected() )
  534. return 0.0f;
  535. // Get depth.
  536. return mSelectedSprite->getDepth();
  537. }
  538. //------------------------------------------------------------------------------
  539. void SpriteBatch::setSpriteSize( const Vector2& size )
  540. {
  541. // Finish if a sprite is not selected.
  542. if ( !checkSpriteSelected() )
  543. return;
  544. // Set size.
  545. mSelectedSprite->setSize( size );
  546. // Flag local extents as dirty.
  547. setLocalExtentsDirty();
  548. }
  549. //------------------------------------------------------------------------------
  550. Vector2 SpriteBatch::getSpriteSize( void ) const
  551. {
  552. // Finish if a sprite is not selected.
  553. if ( !checkSpriteSelected() )
  554. return Vector2::getZero();
  555. // Get size.
  556. return mSelectedSprite->getSize();
  557. }
  558. //------------------------------------------------------------------------------
  559. void SpriteBatch::setSpriteFlipX( const bool flipX )
  560. {
  561. // Finish if a sprite is not selected.
  562. if ( !checkSpriteSelected() )
  563. return;
  564. // Set flip X.
  565. mSelectedSprite->setFlipX( flipX );
  566. }
  567. //------------------------------------------------------------------------------
  568. bool SpriteBatch::getSpriteFlipX( void ) const
  569. {
  570. // Finish if a sprite is not selected.
  571. if ( !checkSpriteSelected() )
  572. return false;
  573. // Get flip X.
  574. return mSelectedSprite->getFlipX();
  575. }
  576. //------------------------------------------------------------------------------
  577. void SpriteBatch::setSpriteFlipY( const bool flipY )
  578. {
  579. // Finish if a sprite is not selected.
  580. if ( !checkSpriteSelected() )
  581. return;
  582. // Set flip Y.
  583. mSelectedSprite->setFlipY( flipY );
  584. }
  585. //------------------------------------------------------------------------------
  586. bool SpriteBatch::getSpriteFlipY( void ) const
  587. {
  588. // Finish if a sprite is not selected.
  589. if ( !checkSpriteSelected() )
  590. return false;
  591. // Get flip Y.
  592. return mSelectedSprite->getFlipY();
  593. }
  594. //------------------------------------------------------------------------------
  595. void SpriteBatch::setSpriteSortPoint( const Vector2& sortPoint )
  596. {
  597. // Finish if a sprite is not selected.
  598. if ( !checkSpriteSelected() )
  599. return;
  600. // Set sort point.
  601. mSelectedSprite->setSortPoint( sortPoint );
  602. }
  603. //------------------------------------------------------------------------------
  604. Vector2 SpriteBatch::getSpriteSortPoint( void ) const
  605. {
  606. // Finish if a sprite is not selected.
  607. if ( !checkSpriteSelected() )
  608. return Vector2::getZero();
  609. // Get sort point.
  610. return mSelectedSprite->getSortPoint();
  611. }
  612. //------------------------------------------------------------------------------
  613. void SpriteBatch::setSpriteRenderGroup( const char* pRenderGroup )
  614. {
  615. // Finish if a sprite is not selected.
  616. if ( !checkSpriteSelected() )
  617. return;
  618. // Set render group.
  619. mSelectedSprite->setRenderGroup( pRenderGroup );
  620. }
  621. //------------------------------------------------------------------------------
  622. StringTableEntry SpriteBatch::getSpriteRenderGroup( void ) const
  623. {
  624. // Finish if a sprite is not selected.
  625. if ( !checkSpriteSelected() )
  626. return NULL;
  627. // Get render group.
  628. return mSelectedSprite->getRenderGroup();
  629. }
  630. //------------------------------------------------------------------------------
  631. void SpriteBatch::setSpriteBlendMode( const bool blendMode )
  632. {
  633. // Finish if a sprite is not selected.
  634. if ( !checkSpriteSelected() )
  635. return;
  636. // Set blend mode.
  637. mSelectedSprite->setBlendMode( blendMode );
  638. }
  639. //------------------------------------------------------------------------------
  640. bool SpriteBatch::getSpriteBlendMode( void ) const
  641. {
  642. // Finish if a sprite is not selected.
  643. if ( !checkSpriteSelected() )
  644. return true;
  645. // Get blend mode.
  646. return mSelectedSprite->getBlendMode();
  647. }
  648. //------------------------------------------------------------------------------
  649. void SpriteBatch::setSpriteSrcBlendFactor( GLenum srcBlendFactor )
  650. {
  651. // Finish if a sprite is not selected.
  652. if ( !checkSpriteSelected() )
  653. return;
  654. // Set source blend factor.
  655. mSelectedSprite->setSrcBlendFactor( srcBlendFactor );
  656. }
  657. //------------------------------------------------------------------------------
  658. GLenum SpriteBatch::getSpriteSrcBlendFactor( void ) const
  659. {
  660. // Finish if a sprite is not selected.
  661. if ( !checkSpriteSelected() )
  662. return GL_SRC_ALPHA;
  663. // Get source blend factor.
  664. return mSelectedSprite->getSrcBlendFactor();
  665. }
  666. //------------------------------------------------------------------------------
  667. void SpriteBatch::setSpriteDstBlendFactor( GLenum dstBlendFactor )
  668. {
  669. // Finish if a sprite is not selected.
  670. if ( !checkSpriteSelected() )
  671. return ;
  672. // Set destination blend factor.
  673. mSelectedSprite->setDstBlendFactor( dstBlendFactor );
  674. }
  675. //------------------------------------------------------------------------------
  676. GLenum SpriteBatch::getSpriteDstBlendFactor( void ) const
  677. {
  678. // Finish if a sprite is not selected.
  679. if ( !checkSpriteSelected() )
  680. return GL_ONE_MINUS_SRC_ALPHA;
  681. // Get destination blend factor.
  682. return mSelectedSprite->getDstBlendFactor();
  683. }
  684. //------------------------------------------------------------------------------
  685. void SpriteBatch::setSpriteBlendColor( const ColorF& blendColor )
  686. {
  687. // Finish if a sprite is not selected.
  688. if ( !checkSpriteSelected() )
  689. return;
  690. // Set blend color.
  691. mSelectedSprite->setBlendColor( blendColor );
  692. }
  693. //------------------------------------------------------------------------------
  694. const ColorF& SpriteBatch::getSpriteBlendColor( void ) const
  695. {
  696. // Finish if a sprite is not selected.
  697. if ( !checkSpriteSelected() )
  698. return ColorF::StockColor("White");
  699. // Get blend color.
  700. return mSelectedSprite->getBlendColor();
  701. }
  702. //------------------------------------------------------------------------------
  703. void SpriteBatch::setSpriteBlendAlpha( const F32 alpha )
  704. {
  705. // Finish if a sprite is not selected.
  706. if ( !checkSpriteSelected() )
  707. return;
  708. // Set blend alpha.
  709. mSelectedSprite->setBlendAlpha( alpha );
  710. }
  711. //------------------------------------------------------------------------------
  712. F32 SpriteBatch::getSpriteBlendAlpha( void ) const
  713. {
  714. // Finish if a sprite is not selected.
  715. if ( !checkSpriteSelected() )
  716. return 0.0f;
  717. // Get blend alpha.
  718. return mSelectedSprite->getBlendAlpha();
  719. }
  720. //------------------------------------------------------------------------------
  721. void SpriteBatch::setSpriteAlphaTest( const F32 alphaTestMode )
  722. {
  723. // Finish if a sprite is not selected.
  724. if ( !checkSpriteSelected() )
  725. return;
  726. // Set alpha-test mode.
  727. mSelectedSprite->setAlphaTest( alphaTestMode );
  728. }
  729. //------------------------------------------------------------------------------
  730. F32 SpriteBatch::getSpriteAlphaTest( void ) const
  731. {
  732. // Finish if a sprite is not selected.
  733. if ( !checkSpriteSelected() )
  734. return -1.0f;
  735. // Get alpha-test mode.
  736. return mSelectedSprite->getAlphaTest();
  737. }
  738. //------------------------------------------------------------------------------
  739. void SpriteBatch::setSpriteDataObject( SimObject* pDataObject )
  740. {
  741. // Finish if a sprite is not selected.
  742. if ( !checkSpriteSelected() )
  743. return;
  744. // Set data object.
  745. mSelectedSprite->setDataObject( pDataObject );
  746. }
  747. //------------------------------------------------------------------------------
  748. SimObject* SpriteBatch::getSpriteDataObject( void ) const
  749. {
  750. // Finish if a sprite is not selected.
  751. if ( !checkSpriteSelected() )
  752. return NULL;
  753. // Get data object.
  754. return mSelectedSprite->getDataObject();
  755. }
  756. //------------------------------------------------------------------------------
  757. void SpriteBatch::setUserData( void* pUserData )
  758. {
  759. // Finish if a sprite is not selected.
  760. if ( !checkSpriteSelected() )
  761. return;
  762. // Set user data.
  763. mSelectedSprite->setUserData( pUserData );
  764. }
  765. //------------------------------------------------------------------------------
  766. void* SpriteBatch::getUserData( void ) const
  767. {
  768. // Finish if a sprite is not selected.
  769. if ( !checkSpriteSelected() )
  770. return NULL;
  771. // Get user data.
  772. return mSelectedSprite->getUserData();
  773. }
  774. //------------------------------------------------------------------------------
  775. void SpriteBatch::setSpriteName( const char* pName )
  776. {
  777. // Finish if a sprite is not selected.
  778. if ( !checkSpriteSelected() )
  779. return;
  780. // Finish if the sprite name already exists.
  781. if ( findSpriteName( pName ) )
  782. return;
  783. // Insert sprite name.
  784. mSpriteNames.insert( StringTable->insert( pName ), mSelectedSprite );
  785. // Set name.
  786. mSelectedSprite->setName( pName );
  787. }
  788. //------------------------------------------------------------------------------
  789. StringTableEntry SpriteBatch::getSpriteName( void ) const
  790. {
  791. // Finish if a sprite is not selected.
  792. if ( !checkSpriteSelected() )
  793. return StringTable->EmptyString;
  794. // Get name.
  795. return mSelectedSprite->getName();
  796. }
  797. //------------------------------------------------------------------------------
  798. SpriteBatchItem* SpriteBatch::createSprite( void )
  799. {
  800. // Debug Profiling.
  801. PROFILE_SCOPE(SpriteBatch_CreateSprite);
  802. // Allocate batch Id.
  803. const U32 batchId = ++mMasterBatchId;
  804. // Create sprite batch item,
  805. SpriteBatchItem* pSpriteBatchItem = SpriteBatchItemFactory.createObject();
  806. // Set batch parent.
  807. pSpriteBatchItem->setBatchParent( this, batchId );
  808. // Create sprite batch item,
  809. mSprites.insert( batchId, pSpriteBatchItem );
  810. return pSpriteBatchItem;
  811. }
  812. //------------------------------------------------------------------------------
  813. SpriteBatchItem* SpriteBatch::createSprite( const Vector2* explicitVertices )
  814. {
  815. // Debug Profiling.
  816. PROFILE_SCOPE(SpriteBatch_CreateSprite);
  817. // Allocate batch Id.
  818. const U32 batchId = ++mMasterBatchId;
  819. // Create sprite batch item,
  820. SpriteBatchItem* pSpriteBatchItem = SpriteBatchItemFactory.createObject();
  821. // Set batch parent.
  822. pSpriteBatchItem->setBatchParent( this, batchId );
  823. // Set explicit mode.
  824. pSpriteBatchItem->setExplicitMode( true );
  825. // Set explicit vertices.
  826. // Create sprite batch item,
  827. mSprites.insert( batchId, pSpriteBatchItem );
  828. return pSpriteBatchItem;
  829. }
  830. //------------------------------------------------------------------------------
  831. SpriteBatchItem* SpriteBatch::findSpritePosition( const SpriteBatchItem::LogicalPosition& logicalPosition )
  832. {
  833. // Debug Profiling.
  834. PROFILE_SCOPE(SpriteBatch_FindSpritePosition);
  835. // Invalid logical positions are not stored.
  836. if ( !logicalPosition.isValid() )
  837. return NULL;
  838. // Find sprite.
  839. typeSpritePositionHash::iterator spriteItr = mSpritePositions.find( logicalPosition );
  840. return spriteItr == mSpritePositions.end() ? NULL : spriteItr->value;
  841. }
  842. //------------------------------------------------------------------------------
  843. SpriteBatchItem* SpriteBatch::findSpriteId( const U32 batchId )
  844. {
  845. // Debug Profiling.
  846. PROFILE_SCOPE(SpriteBatch_FindSpriteId);
  847. // Find sprite.
  848. typeSpriteBatchHash::iterator spriteItr = mSprites.find( batchId );
  849. return spriteItr != mSprites.end() ? spriteItr->value : NULL;
  850. }
  851. //------------------------------------------------------------------------------
  852. SpriteBatchItem* SpriteBatch::findSpriteName( const char* pName )
  853. {
  854. // Debug Profiling.
  855. PROFILE_SCOPE(SpriteBatch_FindSpriteName);
  856. // Finish if no name specified.
  857. if ( pName == NULL || pName == StringTable->EmptyString )
  858. return NULL;
  859. // Find sprite.
  860. typeSpriteNameHash::iterator spriteItr = mSpriteNames.find( StringTable->insert(pName) );
  861. return spriteItr != mSpriteNames.end() ? spriteItr->value : NULL;
  862. }
  863. //------------------------------------------------------------------------------
  864. SpriteBatchItem* SpriteBatch::createSprite( const SpriteBatchItem::LogicalPosition& logicalPosition )
  865. {
  866. // Debug Profiling.
  867. PROFILE_SCOPE(SpriteBatch_CreateSpriteAtLogicalPosition);
  868. // Reset sprite batch item.
  869. SpriteBatchItem* pSpriteBatchItem = NULL;
  870. // Do we have a valid logical position?
  871. if ( logicalPosition.isValid() )
  872. {
  873. // Does it have the correct argument count?
  874. if ( logicalPosition.getArgCount() != 2 )
  875. {
  876. // No, so warn.
  877. Con::warnf( "Invalid logical position specified for composite sprite." );
  878. return NULL;
  879. }
  880. // Does the sprite already exist?
  881. if ( findSpritePosition( logicalPosition ) != NULL )
  882. {
  883. // Yes, so warn.
  884. Con::warnf( "Cannot add sprite at logical position '%s' as one already exists.", logicalPosition.getString() );
  885. return NULL;
  886. }
  887. // Create the sprite.
  888. pSpriteBatchItem = createSprite();
  889. // Set the logical position.
  890. pSpriteBatchItem->setLogicalPosition( logicalPosition );
  891. // Set the sprite default local position.
  892. pSpriteBatchItem->setLocalPosition( logicalPosition.getAsVector2() );
  893. }
  894. else
  895. {
  896. // Create the sprite.
  897. pSpriteBatchItem = createSprite();
  898. }
  899. // Set the sprite default size and angle.
  900. pSpriteBatchItem->setSize( getDefaultSpriteSize() );
  901. pSpriteBatchItem->setLocalAngle( getDefaultSpriteAngle() );
  902. return pSpriteBatchItem;
  903. }
  904. //------------------------------------------------------------------------------
  905. void SpriteBatch::setBatchTransform( const b2Transform& batchTransform )
  906. {
  907. // Update world transform.
  908. mBatchTransform = batchTransform;
  909. // Flag the batch transform as dirty.
  910. setBatchTransformDirty();
  911. }
  912. //------------------------------------------------------------------------------
  913. void SpriteBatch::updateLocalExtents( void )
  914. {
  915. // Debug Profiling.
  916. PROFILE_SCOPE(SpriteBatch_UpdateLocalExtents);
  917. // Finish if the local extents are not dirty.
  918. if ( !mLocalExtentsDirty )
  919. return;
  920. // Flag as NOT dirty.
  921. mLocalExtentsDirty = false;
  922. // Do we have any sprites?
  923. if ( mSprites.size() == 0 )
  924. {
  925. // No, so reset local extents.
  926. mLocalExtents.setOne();
  927. return;
  928. }
  929. // Fetch first sprite.
  930. typeSpriteBatchHash::iterator spriteItr = mSprites.begin();
  931. // Set render AABB to this sprite.
  932. b2AABB localAABB = spriteItr->value->getLocalAABB();
  933. // Combine with the rest of the sprites.
  934. for( ; spriteItr != mSprites.end(); ++spriteItr )
  935. {
  936. localAABB.Combine( spriteItr->value->getLocalAABB() );
  937. }
  938. // Fetch local render extents.
  939. const b2Vec2& localLowerExtent = localAABB.lowerBound;
  940. const b2Vec2& localUpperExtent = localAABB.upperBound;
  941. // Calculate maximum extents.
  942. const F32 lowerExtentX = mFabs(localLowerExtent.x);
  943. const F32 lowerExtentY = mFabs(localLowerExtent.y);
  944. const F32 upperExtentX = mFabs(localUpperExtent.x);
  945. const F32 upperExtentY = mFabs(localUpperExtent.y);
  946. // Calculate local extents.
  947. mLocalExtents.Set( mFabs(lowerExtentX > upperExtentX ? lowerExtentX : upperExtentX) * 2.0f, mFabs(lowerExtentY > upperExtentY ? lowerExtentY : upperExtentY) * 2.0f );
  948. }
  949. //------------------------------------------------------------------------------
  950. void SpriteBatch::createSpriteBatchQuery( void )
  951. {
  952. // Debug Profiling.
  953. PROFILE_SCOPE(SpriteBatch_CreateSpriteBatchQuery);
  954. // Finish if batch culling is off or there is already a sprite batch query.
  955. if ( !mBatchCulling || mpSpriteBatchQuery != NULL )
  956. return;
  957. // Set the sprite batch query appropriately.
  958. mpSpriteBatchQuery = new SpriteBatchQuery( this );
  959. // Finish if there are no sprites.
  960. if ( mSprites.size() == 0 )
  961. return;
  962. // Add proxies for all the sprites.
  963. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  964. {
  965. // Fetch sprite batch item.
  966. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  967. // Create query proxy for sprite.
  968. createQueryProxy( pSpriteBatchItem );
  969. }
  970. }
  971. //------------------------------------------------------------------------------
  972. void SpriteBatch::destroySpriteBatchQuery( void )
  973. {
  974. // Debug Profiling.
  975. PROFILE_SCOPE(SpriteBatch_DestroySpriteBatchQuery);
  976. // Finish if there is no sprite batch query.
  977. if ( mpSpriteBatchQuery == NULL )
  978. return;
  979. // Are there any sprites?
  980. if ( mSprites.size() > 0 )
  981. {
  982. // Yes, so destroy proxies of all the sprites.
  983. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  984. {
  985. // Destroy query proxy for sprite.
  986. destroyQueryProxy( spriteItr->value );
  987. }
  988. }
  989. // Finish if sprite clipping
  990. delete mpSpriteBatchQuery;
  991. mpSpriteBatchQuery = NULL;
  992. }
  993. //------------------------------------------------------------------------------
  994. bool SpriteBatch::destroySprite( const U32 batchId )
  995. {
  996. // Debug Profiling.
  997. PROFILE_SCOPE(SpriteBatch_DestroySprite);
  998. // Find sprite.
  999. typeSpriteBatchHash::iterator spriteItr = mSprites.find( batchId );
  1000. // Finish if sprite not found.
  1001. if ( spriteItr == mSprites.end() )
  1002. return false;
  1003. // Find sprite.
  1004. SpriteBatchItem* pSpriteBatchItem = spriteItr->value;
  1005. // Sanity!
  1006. AssertFatal( pSpriteBatchItem != NULL, "SpriteBatch::destroySprite() - Found sprite but it was NULL." );
  1007. // Cache sprite.
  1008. SpriteBatchItemFactory.cacheObject( pSpriteBatchItem );
  1009. // Remove from sprites.
  1010. mSprites.erase( batchId );
  1011. return true;
  1012. }
  1013. //------------------------------------------------------------------------------
  1014. bool SpriteBatch::checkSpriteSelected( void ) const
  1015. {
  1016. // Finish if a sprite is selected.
  1017. if ( mSelectedSprite != NULL )
  1018. return true;
  1019. // No, so warn,
  1020. Con::warnf( "Cannot perform sprite operation no sprite is selected." );
  1021. return false;
  1022. }
  1023. //------------------------------------------------------------------------------
  1024. b2AABB SpriteBatch::calculateLocalAABB( const b2AABB& renderAABB )
  1025. {
  1026. // Debug Profiling.
  1027. PROFILE_SCOPE(SpriteBatch_CalculateLocalAABB);
  1028. // Calculate local OOBB.
  1029. b2Vec2 localOOBB[4];
  1030. CoreMath::mAABBtoOOBB( renderAABB, localOOBB );
  1031. CoreMath::mCalculateInverseOOBB( localOOBB, mBatchTransform, localOOBB );
  1032. // Calculate local AABB.
  1033. b2AABB localAABB;
  1034. CoreMath::mOOBBtoAABB( localOOBB, localAABB );
  1035. return localAABB;
  1036. }
  1037. //------------------------------------------------------------------------------
  1038. void SpriteBatch::onTamlCustomWrite( TamlCustomNodes& customNodes )
  1039. {
  1040. // Debug Profiling.
  1041. PROFILE_SCOPE(SpriteBatch_TamlCustomWrite);
  1042. // Finish if no sprites.
  1043. if ( getSpriteCount() == 0 )
  1044. return;
  1045. // Add sprites node.
  1046. TamlCustomNode* pSpritesNode = customNodes.addNode( spritesNodeName );
  1047. // Write all sprites.
  1048. for( typeSpriteBatchHash::iterator spriteItr = mSprites.begin(); spriteItr != mSprites.end(); ++spriteItr )
  1049. {
  1050. // Write type with sprite item.
  1051. spriteItr->value->onTamlCustomWrite( pSpritesNode );
  1052. }
  1053. }
  1054. //------------------------------------------------------------------------------
  1055. void SpriteBatch::onTamlCustomRead( const TamlCustomNodes& customNodes )
  1056. {
  1057. // Debug Profiling.
  1058. PROFILE_SCOPE(SpriteBatch_TamlCustomRead);
  1059. // Find sprites custom node.
  1060. const TamlCustomNode* pSpritesNode = customNodes.findNode( spritesNodeName );
  1061. // Finish if we don't have the node.
  1062. if ( pSpritesNode == NULL )
  1063. return;
  1064. // Fetch children nodes.
  1065. const TamlCustomNodeVector& spriteNodes = pSpritesNode->getChildren();
  1066. // Iterate sprite item types.
  1067. for( TamlCustomNodeVector::const_iterator spriteItr = spriteNodes.begin(); spriteItr != spriteNodes.end(); ++spriteItr )
  1068. {
  1069. // Fetch sprite node.
  1070. TamlCustomNode* pNode = *spriteItr;
  1071. // Fetch alias name.
  1072. StringTableEntry nodeName = pNode->getNodeName();
  1073. // Is this a known node name?
  1074. if ( nodeName != spritesItemTypeName )
  1075. {
  1076. // No, so warn.
  1077. Con::warnf( "SpriteBatch - Unknown custom type '%s'.", nodeName );
  1078. continue;
  1079. }
  1080. // Create sprite.
  1081. SpriteBatchItem* pSpriteBatchItem = createSprite();
  1082. // Read type with sprite item.
  1083. pSpriteBatchItem->onTamlCustomRead( pNode );
  1084. // Fetch logical position.
  1085. const SpriteBatchItem::LogicalPosition& logicalPosition = pSpriteBatchItem->getLogicalPosition();
  1086. // Did we get a logical position?
  1087. if ( logicalPosition.isValid() )
  1088. {
  1089. // Yes, so insert into sprite positions.
  1090. mSpritePositions.insert( logicalPosition, pSpriteBatchItem );
  1091. }
  1092. // Fetch sprite name.
  1093. StringTableEntry spriteName = pSpriteBatchItem->getName();
  1094. // Did we get a sprite name?
  1095. if ( spriteName != StringTable->EmptyString )
  1096. {
  1097. // Yes, so insert into sprite names if it doesn't already exist.
  1098. if ( mSpriteNames.find( spriteName ) != mSpriteNames.end() )
  1099. mSpriteNames.insert( spriteName, mSelectedSprite );
  1100. }
  1101. }
  1102. }
  1103. //------------------------------------------------------------------------------
  1104. void SpriteBatch::WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  1105. {
  1106. // Sanity!
  1107. AssertFatal( pClassRep != NULL, "SpriteBatch::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  1108. AssertFatal( pParentElement != NULL, "SpriteBatch::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  1109. char buffer[1024];
  1110. // Create sprite batch node element.
  1111. TiXmlElement* pBatchNodeElement = new TiXmlElement( "xs:element" );
  1112. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), spritesNodeName );
  1113. pBatchNodeElement->SetAttribute( "name", buffer );
  1114. pBatchNodeElement->SetAttribute( "minOccurs", 0 );
  1115. pBatchNodeElement->SetAttribute( "maxOccurs", 1 );
  1116. pParentElement->LinkEndChild( pBatchNodeElement );
  1117. // Create complex type.
  1118. TiXmlElement* pBatchNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1119. pBatchNodeElement->LinkEndChild( pBatchNodeComplexTypeElement );
  1120. // Create choice element.
  1121. TiXmlElement* pBatchNodeChoiceElement = new TiXmlElement( "xs:choice" );
  1122. pBatchNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  1123. pBatchNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  1124. pBatchNodeComplexTypeElement->LinkEndChild( pBatchNodeChoiceElement );
  1125. // Write sprite batch item.
  1126. SpriteBatchItem::WriteCustomTamlSchema( pClassRep, pBatchNodeChoiceElement );
  1127. }