The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1KB

  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "b2BroadPhase.h"
  19. using namespace std;
  20. b2BroadPhase::b2BroadPhase()
  21. {
  22. m_proxyCount = 0;
  23. m_pairCapacity = 16;
  24. m_pairCount = 0;
  25. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  26. m_moveCapacity = 16;
  27. m_moveCount = 0;
  28. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  29. }
  30. b2BroadPhase::~b2BroadPhase()
  31. {
  32. b2Free(m_moveBuffer);
  33. b2Free(m_pairBuffer);
  34. }
  35. int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
  36. {
  37. int32 proxyId = m_tree.CreateProxy(aabb, userData);
  38. ++m_proxyCount;
  39. BufferMove(proxyId);
  40. return proxyId;
  41. }
  42. void b2BroadPhase::DestroyProxy(int32 proxyId)
  43. {
  44. UnBufferMove(proxyId);
  45. --m_proxyCount;
  46. m_tree.DestroyProxy(proxyId);
  47. }
  48. void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
  49. {
  50. bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
  51. if (buffer)
  52. {
  53. BufferMove(proxyId);
  54. }
  55. }
  56. void b2BroadPhase::TouchProxy(int32 proxyId)
  57. {
  58. BufferMove(proxyId);
  59. }
  60. void b2BroadPhase::BufferMove(int32 proxyId)
  61. {
  62. if (m_moveCount == m_moveCapacity)
  63. {
  64. int32* oldBuffer = m_moveBuffer;
  65. m_moveCapacity *= 2;
  66. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  67. memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
  68. b2Free(oldBuffer);
  69. }
  70. m_moveBuffer[m_moveCount] = proxyId;
  71. ++m_moveCount;
  72. }
  73. void b2BroadPhase::UnBufferMove(int32 proxyId)
  74. {
  75. for (int32 i = 0; i < m_moveCount; ++i)
  76. {
  77. if (m_moveBuffer[i] == proxyId)
  78. {
  79. m_moveBuffer[i] = e_nullProxy;
  80. return;
  81. }
  82. }
  83. }
  84. // This is called from b2DynamicTree::Query when we are gathering pairs.
  85. bool b2BroadPhase::QueryCallback(int32 proxyId)
  86. {
  87. // A proxy cannot form a pair with itself.
  88. if (proxyId == m_queryProxyId)
  89. {
  90. return true;
  91. }
  92. // Grow the pair buffer as needed.
  93. if (m_pairCount == m_pairCapacity)
  94. {
  95. b2Pair* oldBuffer = m_pairBuffer;
  96. m_pairCapacity *= 2;
  97. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  98. memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
  99. b2Free(oldBuffer);
  100. }
  101. m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
  102. m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
  103. ++m_pairCount;
  104. return true;
  105. }