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.2KB

  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. #include <cstring>
  20. using namespace std;
  21. b2BroadPhase::b2BroadPhase()
  22. {
  23. m_proxyCount = 0;
  24. m_pairCapacity = 16;
  25. m_pairCount = 0;
  26. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  27. m_moveCapacity = 16;
  28. m_moveCount = 0;
  29. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  30. }
  31. b2BroadPhase::~b2BroadPhase()
  32. {
  33. b2Free(m_moveBuffer);
  34. b2Free(m_pairBuffer);
  35. }
  36. int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
  37. {
  38. int32 proxyId = m_tree.CreateProxy(aabb, userData);
  39. ++m_proxyCount;
  40. BufferMove(proxyId);
  41. return proxyId;
  42. }
  43. void b2BroadPhase::DestroyProxy(int32 proxyId)
  44. {
  45. UnBufferMove(proxyId);
  46. --m_proxyCount;
  47. m_tree.DestroyProxy(proxyId);
  48. }
  49. void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
  50. {
  51. bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
  52. if (buffer)
  53. {
  54. BufferMove(proxyId);
  55. }
  56. }
  57. void b2BroadPhase::TouchProxy(int32 proxyId)
  58. {
  59. BufferMove(proxyId);
  60. }
  61. void b2BroadPhase::BufferMove(int32 proxyId)
  62. {
  63. if (m_moveCount == m_moveCapacity)
  64. {
  65. int32* oldBuffer = m_moveBuffer;
  66. m_moveCapacity *= 2;
  67. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  68. memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
  69. b2Free(oldBuffer);
  70. }
  71. m_moveBuffer[m_moveCount] = proxyId;
  72. ++m_moveCount;
  73. }
  74. void b2BroadPhase::UnBufferMove(int32 proxyId)
  75. {
  76. for (int32 i = 0; i < m_moveCount; ++i)
  77. {
  78. if (m_moveBuffer[i] == proxyId)
  79. {
  80. m_moveBuffer[i] = e_nullProxy;
  81. return;
  82. }
  83. }
  84. }
  85. // This is called from b2DynamicTree::Query when we are gathering pairs.
  86. bool b2BroadPhase::QueryCallback(int32 proxyId)
  87. {
  88. // A proxy cannot form a pair with itself.
  89. if (proxyId == m_queryProxyId)
  90. {
  91. return true;
  92. }
  93. // Grow the pair buffer as needed.
  94. if (m_pairCount == m_pairCapacity)
  95. {
  96. b2Pair* oldBuffer = m_pairBuffer;
  97. m_pairCapacity *= 2;
  98. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  99. memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
  100. b2Free(oldBuffer);
  101. }
  102. m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
  103. m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
  104. ++m_pairCount;
  105. return true;
  106. }