cHitChecker.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "cHitChecker.h"
  3. #include <stdio.h>
  4. //////////////////////////////////////////////////////////////////////
  5. // Construction/Destruction
  6. //////////////////////////////////////////////////////////////////////
  7. cHitChecker::cHitChecker()
  8. {
  9. hBoundingPoly = NULL;
  10. }
  11. cHitChecker::~cHitChecker()
  12. {
  13. if(hBoundingPoly != NULL)
  14. {
  15. DeleteObject(hBoundingPoly);
  16. }
  17. }
  18. void cHitChecker::CreateRectBound(int nWidth,int nHeight)
  19. {
  20. // if this class already have a rgn defined, destroyed
  21. Destroy();
  22. hBoundingPoly = CreateRectRgn(0, 0, nWidth, nHeight);
  23. }
  24. void cHitChecker::RemoveRectFromBound(int nX, int nY, int nWidth, int nHeight)
  25. {
  26. // Use the combine Rgn API to remove a rect from the class internal region
  27. HRGN hRgnSrc = NULL;
  28. hRgnSrc = CreateRectRgn(nX, nY, nWidth+nX, nHeight+nY);
  29. int iRet = CombineRgn(hBoundingPoly, hBoundingPoly, hRgnSrc, RGN_DIFF);
  30. DeleteObject(hRgnSrc);
  31. }
  32. void cHitChecker::RemovePolyFromBound(LPPOINT lpPoints, int iCount, int iX, int iY)
  33. {
  34. // Use the combine Rgn API to remove a poly from the class internal region
  35. HRGN hRgnSrc = NULL;
  36. LPPOINT pStart = lpPoints;
  37. if(iX != 0)
  38. {
  39. for(int i=0;i<iCount;i++)
  40. {
  41. lpPoints->x = lpPoints->x + iX;
  42. lpPoints->y = lpPoints->y + iY;
  43. lpPoints++;
  44. }
  45. lpPoints = pStart;
  46. }
  47. hRgnSrc = CreatePolygonRgn(lpPoints, iCount, ALTERNATE);
  48. if(hRgnSrc == NULL)
  49. {
  50. DXTRACE_MSG("ERROR !");
  51. }
  52. CombineRgn(hBoundingPoly, hBoundingPoly, hRgnSrc, RGN_DIFF);
  53. DeleteObject(hRgnSrc);
  54. if(iX != 0)
  55. {
  56. for(int i=0;i<iCount;i++)
  57. {
  58. lpPoints->x = lpPoints->x - iX;
  59. lpPoints->y = lpPoints->y - iY;
  60. lpPoints++;
  61. }
  62. lpPoints = pStart;
  63. }
  64. }
  65. void cHitChecker::CreatePolygonBound(LPPOINT lpPoints, int nCount)
  66. {
  67. // Use the combine Rgn API to remove a poly from the class internal region
  68. Destroy();
  69. hBoundingPoly = CreatePolygonRgn(lpPoints, nCount, ALTERNATE);
  70. }
  71. BOOL cHitChecker::HaveHitted(cHitChecker *pHitCheck, int nX, int nY, int nSrcX, int nSrcY)
  72. {
  73. HRGN hSrcObjectRgn;
  74. HRGN hCompObjectRgn;
  75. BOOL bResult = FALSE;
  76. DWORD dwSize;
  77. UINT i = 0;
  78. RGNDATA* rgnData;
  79. // First check the bounding rectangle
  80. RECT rcObj;
  81. GetRgnBox(pHitCheck->hBoundingPoly,&rcObj);
  82. rcObj.top  += nY; rcObj.bottom  += nY;
  83. rcObj.left += nX; rcObj.right   += nX;
  84. if(nSrcX!=0 && nSrcY!=0)
  85. {
  86. OffsetRgn(hBoundingPoly, nSrcX, nSrcY);
  87. if(RectInRegion(hBoundingPoly, &rcObj) == 0)
  88. {
  89. OffsetRgn(hBoundingPoly, -nSrcX, -nSrcY);
  90. return FALSE;
  91. }
  92. OffsetRgn(hBoundingPoly, -nSrcX, -nSrcY);
  93. }
  94. else
  95. {
  96. if(RectInRegion(hBoundingPoly, &rcObj) == 0)
  97. return FALSE;
  98. }
  99. dwSize = GetRegionData(pHitCheck->hBoundingPoly, sizeof(RGNDATA), NULL);
  100. rgnData = (RGNDATA*) malloc(dwSize);
  101. GetRegionData(pHitCheck->hBoundingPoly, dwSize, rgnData);
  102. hSrcObjectRgn = ExtCreateRegion(NULL, dwSize, rgnData);
  103. OffsetRgn(hSrcObjectRgn, nX, nY);
  104. dwSize = GetRegionData(hSrcObjectRgn, sizeof(RGNDATA), NULL);
  105. rgnData = (RGNDATA*) realloc(rgnData, dwSize);
  106. GetRegionData(hSrcObjectRgn, dwSize, rgnData);
  107. if(nSrcX !=0 && nSrcY !=0)
  108. {
  109. RGNDATA* rgnData2;
  110. // Same copy for the region being tested
  111. dwSize = GetRegionData(hBoundingPoly, sizeof(RGNDATA), NULL);
  112. rgnData2 = (RGNDATA*) malloc(dwSize);
  113. GetRegionData(hBoundingPoly, dwSize, rgnData2);
  114. hCompObjectRgn = ExtCreateRegion(NULL, dwSize, rgnData2);
  115. OffsetRgn(hCompObjectRgn, nSrcX, nSrcY);
  116. bResult = TRUE;
  117. for(i=0;i<rgnData->rdh.nCount;i++)
  118. {
  119. memcpy(&rcObj, &rgnData->Buffer[i*sizeof(RECT)], sizeof(RECT));
  120. if(RectInRegion(hCompObjectRgn, &rcObj) != 0)
  121. {
  122. bResult = FALSE;
  123. break;
  124. }
  125. }
  126. free(rgnData2);
  127. DeleteObject(hCompObjectRgn);
  128. }
  129. else
  130. {
  131. bResult = TRUE;
  132. for(i=0;i<rgnData->rdh.nCount;i++)
  133. {
  134. memcpy(&rcObj, &rgnData->Buffer[i*sizeof(RECT)], sizeof(RECT));
  135. if(RectInRegion(hBoundingPoly, &rcObj) != 0)
  136. {
  137. bResult = FALSE;
  138. break;
  139. }
  140. }
  141. }
  142. free((void*)rgnData);
  143. // Free the resources
  144. DeleteObject(hSrcObjectRgn);
  145. return !bResult;
  146. }
  147. void cHitChecker::Destroy()
  148. {
  149. // If we have a region, destroy it
  150. if(hBoundingPoly != NULL)
  151. {
  152. DeleteObject(hBoundingPoly);
  153. }
  154. }