SpriteBatch.cc 47 KB

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