SpriteBatch.cc 44 KB

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