CollisionQueue.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: CollisionQueue.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. **
  10. ** History:
  11. */
  12. #include "pch.h"
  13. CollisionQueue::CollisionQueue(void)
  14. :
  15. m_nCollisions(0),
  16. m_maxCollisions(c_maxHitTests * 20), //? best guess
  17. m_fDelete(true)
  18. {
  19. m_pCollisions = new CollisionEntry [m_maxCollisions];
  20. assert (m_pCollisions);
  21. }
  22. CollisionQueue::CollisionQueue(int maxCollisions,
  23. CollisionEntry* pCollisions)
  24. :
  25. m_nCollisions(0),
  26. m_pCollisions(pCollisions),
  27. m_maxCollisions(maxCollisions),
  28. m_fDelete(false)
  29. {
  30. }
  31. CollisionQueue::~CollisionQueue(void)
  32. {
  33. if (m_fDelete)
  34. delete [] m_pCollisions;
  35. }
  36. void CollisionQueue::sort(int start)
  37. {
  38. if ((m_nCollisions - 1) > start)
  39. {
  40. if (start == 0)
  41. {
  42. CollisionEntry::longSort(m_pCollisions,
  43. &m_pCollisions[m_nCollisions - 1]);
  44. }
  45. else
  46. {
  47. CollisionEntry::shortSort(&m_pCollisions[start],
  48. &m_pCollisions[m_nCollisions - 1]);
  49. }
  50. }
  51. }
  52. void CollisionQueue::flush(int n,
  53. HitTest* pHitTest1,
  54. HitTest* pHitTest2)
  55. {
  56. assert (n > 0);
  57. assert (n <= m_nCollisions);
  58. //Cheat and temporarily mark pHitTest1 & 2 as dead
  59. bool oldDead1;
  60. if (pHitTest1)
  61. {
  62. oldDead1 = pHitTest1->GetDeadF();
  63. pHitTest1->AddRef();
  64. pHitTest1->SetDeadF(true);
  65. }
  66. bool oldDead2;
  67. if (pHitTest2)
  68. {
  69. oldDead2 = pHitTest2->GetDeadF();
  70. pHitTest2->AddRef();
  71. pHitTest2->SetDeadF(true);
  72. }
  73. float t = m_pCollisions[n - 1].m_tCollision;
  74. int dest = n;
  75. for (int source = n; (source < m_nCollisions); source++)
  76. {
  77. CollisionEntry* pentrySource = &m_pCollisions[source];
  78. if (pentrySource->m_pHitTest1->GetDeadF() || pentrySource->m_pHitTest2->GetDeadF())
  79. {
  80. pentrySource->m_pHitTest1->Release();
  81. pentrySource->m_pHitTest2->Release();
  82. pentrySource->m_pHitTest1 = NULL; //NYI shouldn't be needed
  83. pentrySource->m_pHitTest2 = NULL;
  84. }
  85. else
  86. {
  87. CollisionEntry* pentryDest = &m_pCollisions[dest++];
  88. pentryDest->m_pHitTest1 = pentrySource->m_pHitTest1;
  89. pentryDest->m_pHitTest2 = pentrySource->m_pHitTest2;
  90. pentryDest->m_hts1 = pentrySource->m_hts1;
  91. pentryDest->m_hts2 = pentrySource->m_hts2;
  92. pentryDest->m_tCollision = pentrySource->m_tCollision - t;
  93. }
  94. }
  95. if (pHitTest2)
  96. {
  97. pHitTest2->SetDeadF(oldDead2);
  98. pHitTest2->Release();
  99. }
  100. if (pHitTest1)
  101. {
  102. pHitTest1->SetDeadF(oldDead1);
  103. pHitTest1->Release();
  104. }
  105. m_nCollisions = dest;
  106. }
  107. void CollisionQueue::purge(void)
  108. {
  109. int i = m_nCollisions;
  110. while (--i >= 0)
  111. {
  112. CollisionEntry* pentry = &m_pCollisions[i];
  113. pentry->m_pHitTest1->Release();
  114. pentry->m_pHitTest2->Release();
  115. }
  116. m_nCollisions = 0;
  117. }
  118. void CollisionQueue::addCollision(float tCollision,
  119. HitTest* pHitTest1,
  120. HitTestShape hts1,
  121. HitTest* pHitTest2,
  122. HitTestShape hts2)
  123. {
  124. assert (hts1 <= pHitTest1->GetTrueShape());
  125. assert (hts2 <= pHitTest2->GetTrueShape());
  126. assert (m_nCollisions <= m_maxCollisions);
  127. if (m_nCollisions == m_maxCollisions)
  128. {
  129. m_maxCollisions = (m_maxCollisions << 1);
  130. debugf("Extending collision queue from %d to %d entries\n", m_nCollisions, m_maxCollisions);
  131. CollisionEntry* p = new CollisionEntry[m_maxCollisions];
  132. assert (p);
  133. for (int i = 0; (i < m_nCollisions); i++)
  134. {
  135. p[i] = m_pCollisions[i];
  136. }
  137. delete [] m_pCollisions;
  138. m_pCollisions = p;
  139. }
  140. assert (m_nCollisions < m_maxCollisions);
  141. assert (pHitTest1);
  142. assert (pHitTest2);
  143. pHitTest1->AddRef();
  144. pHitTest2->AddRef();
  145. CollisionEntry* pentry = &m_pCollisions[m_nCollisions++];
  146. pentry->m_tCollision = tCollision;
  147. pentry->m_pHitTest1 = pHitTest1;
  148. pentry->m_hts1 = hts1;
  149. pentry->m_pHitTest2 = pHitTest2;
  150. pentry->m_hts2 = hts2;
  151. }