SpriteBatchQuery.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "2d/core/SpriteBatchQuery.h"
  23. #ifndef _SPRITE_BATCH_H_
  24. #include "2d/core/spriteBatch.h"
  25. #endif
  26. #ifndef _SPRITE_BATCH_ITEM_H_
  27. #include "2d/core/spriteBatchItem.h"
  28. #endif
  29. // Debug Profiling.
  30. #include "debug/profiler.h"
  31. //-----------------------------------------------------------------------------
  32. SpriteBatchQuery::SpriteBatchQuery( SpriteBatch* pSpriteBatch ) :
  33. mpSpriteBatch(pSpriteBatch),
  34. mIsRaycastQueryResult(false),
  35. mMasterQueryKey(0),
  36. mCheckFixturePoint(false),
  37. mFixturePoint(0.0f, 0.0f)
  38. {
  39. // Set debug associations.
  40. VECTOR_SET_ASSOCIATION( mQueryResults );
  41. // Clear the query.
  42. clearQuery();
  43. }
  44. //-----------------------------------------------------------------------------
  45. S32 SpriteBatchQuery::add( SpriteBatchItem* pSpriteBatchItem )
  46. {
  47. // Debug Profiling.
  48. PROFILE_SCOPE(SpriteBatchQuery_Add);
  49. return CreateProxy( pSpriteBatchItem->getLocalAABB(), pSpriteBatchItem );
  50. }
  51. //-----------------------------------------------------------------------------
  52. void SpriteBatchQuery::remove( SpriteBatchItem* pSpriteBatchItem )
  53. {
  54. // Debug Profiling.
  55. PROFILE_SCOPE(SpriteBatchQuery_Remove);
  56. DestroyProxy( pSpriteBatchItem->getProxyId() );
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool SpriteBatchQuery::update( SpriteBatchItem* pSpriteBatchItem, const b2AABB& aabb, const b2Vec2& displacement )
  60. {
  61. // Debug Profiling.
  62. PROFILE_SCOPE(SpriteBatchQuery_Update);
  63. return MoveProxy( pSpriteBatchItem->getProxyId(), aabb, displacement );
  64. }
  65. //-----------------------------------------------------------------------------
  66. U32 SpriteBatchQuery::renderQueryArea( const b2AABB& aabb )
  67. {
  68. // Debug Profiling.
  69. PROFILE_SCOPE(SpriteBatchQuery_RenderQueryArea);
  70. mMasterQueryKey++;
  71. // Flag as not a ray-cast query result.
  72. mIsRaycastQueryResult = false;
  73. // Query.
  74. Query( this, aabb );
  75. return getQueryResultsCount();
  76. }
  77. //-----------------------------------------------------------------------------
  78. U32 SpriteBatchQuery::renderQueryRay( const Vector2& point1, const Vector2& point2 )
  79. {
  80. // Debug Profiling.
  81. PROFILE_SCOPE(SpriteBatchQuery_RenderQueryRay);
  82. mMasterQueryKey++;
  83. // Flag as a ray-cast query result.
  84. mIsRaycastQueryResult = true;
  85. // Query.
  86. b2RayCastInput rayInput;
  87. rayInput.p1 = point1;
  88. rayInput.p2 = point2;
  89. rayInput.maxFraction = 1.0f;
  90. RayCast( this, rayInput );
  91. return getQueryResultsCount();
  92. }
  93. //-----------------------------------------------------------------------------
  94. U32 SpriteBatchQuery::renderQueryPoint( const Vector2& point )
  95. {
  96. // Debug Profiling.
  97. PROFILE_SCOPE(SpriteBatchQuery_RenderQueryPoint);
  98. mMasterQueryKey++;
  99. // Flag as not a ray-cast query result.
  100. mIsRaycastQueryResult = false;
  101. // Query.
  102. b2RayCastInput rayInput;
  103. rayInput.p1 = point;
  104. rayInput.p2 = b2Vec2( point.x + b2_linearSlop, point.y + b2_linearSlop );
  105. rayInput.maxFraction = 1.0f;
  106. RayCast( this, rayInput );
  107. return getQueryResultsCount();
  108. }
  109. //-----------------------------------------------------------------------------
  110. void SpriteBatchQuery::clearQuery( void )
  111. {
  112. mQueryResults.clear();
  113. }
  114. //-----------------------------------------------------------------------------
  115. void SpriteBatchQuery::sortRaycastQueryResult( void )
  116. {
  117. // Debug Profiling.
  118. PROFILE_SCOPE(SpriteBatchQuery_SortRayCastQueryResult);
  119. // Ignore if not a ray-cast query result or there are not results to sort.
  120. if ( !getIsRaycastQueryResult() || getQueryResultsCount() == 0 )
  121. return;
  122. // Sort query results.
  123. dQsort( mQueryResults.address(), mQueryResults.size(), sizeof(SpriteBatchQueryResult), rayCastFractionSort );
  124. }
  125. //-----------------------------------------------------------------------------
  126. bool SpriteBatchQuery::QueryCallback( S32 proxyId )
  127. {
  128. // Debug Profiling.
  129. PROFILE_SCOPE(SpriteBatchQuery_QueryCallback);
  130. // Fetch sprite batch item.
  131. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  132. // Ignore if already tagged with the sprite batch query key.
  133. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  134. return true;
  135. // Tag with world query key.
  136. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  137. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  138. mQueryResults.push_back( queryResult );
  139. return true;
  140. }
  141. //-----------------------------------------------------------------------------
  142. F32 SpriteBatchQuery::RayCastCallback( const b2RayCastInput& input, S32 proxyId )
  143. {
  144. // Debug Profiling.
  145. PROFILE_SCOPE(SpriteBatchQuery_RayCastCallback);
  146. // Fetch sprite batch item.
  147. SpriteBatchItem* pSpriteBatchItem = static_cast<SpriteBatchItem*>(GetUserData( proxyId ));
  148. // Ignore if already tagged with the sprite batch query key.
  149. if ( pSpriteBatchItem->getSpriteBatchQueryKey() == mMasterQueryKey )
  150. return 1.0f;
  151. // Tag with world query key.
  152. pSpriteBatchItem->setSpriteBatchQueryKey( mMasterQueryKey );
  153. SpriteBatchQueryResult queryResult( pSpriteBatchItem );
  154. mQueryResults.push_back( queryResult );
  155. return 1.0f;
  156. }
  157. //-----------------------------------------------------------------------------
  158. S32 QSORT_CALLBACK SpriteBatchQuery::rayCastFractionSort(const void* a, const void* b)
  159. {
  160. // Debug Profiling.
  161. PROFILE_SCOPE(SpriteBatchQuery_RayCastFractionSort);
  162. // Fetch scene objects.
  163. SpriteBatchQueryResult* pQueryResultA = (SpriteBatchQueryResult*)a;
  164. SpriteBatchQueryResult* pQueryResultB = (SpriteBatchQueryResult*)b;
  165. // Fetch fractions.
  166. const F32 queryFractionA = pQueryResultA->mFraction;
  167. const F32 queryFractionB = pQueryResultB->mFraction;
  168. if ( queryFractionA < queryFractionB )
  169. return -1;
  170. if ( queryFractionA > queryFractionB )
  171. return 1;
  172. return 0;
  173. }