⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chitchecker.cpp

📁 一个2D的赛车游戏 在VC下运行 开发库DIRECTX8.1以上
💻 CPP
字号:
#include "stdafx.h"
#include "cHitChecker.h"
#include <stdio.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cHitChecker::cHitChecker()
{
	hBoundingPoly = NULL;
}

cHitChecker::~cHitChecker()
{
	if(hBoundingPoly != NULL)
	{
		DeleteObject(hBoundingPoly);
	}
}

void cHitChecker::CreateRectBound(int nWidth,int nHeight)
{
	// if this class already have a rgn defined, destroyed
	Destroy();

	hBoundingPoly = CreateRectRgn(0, 0, nWidth, nHeight);
}

void cHitChecker::RemoveRectFromBound(int nX, int nY, int nWidth, int nHeight)
{
	// Use the combine Rgn API to remove a rect from the class internal region
	HRGN	hRgnSrc = NULL;

	hRgnSrc = CreateRectRgn(nX, nY, nWidth+nX, nHeight+nY);
	
	int iRet = CombineRgn(hBoundingPoly, hBoundingPoly, hRgnSrc, RGN_DIFF);
	
	DeleteObject(hRgnSrc);
}

void cHitChecker::RemovePolyFromBound(LPPOINT lpPoints, int iCount, int iX, int iY)
{
	// Use the combine Rgn API to remove a poly from the class internal region
	HRGN	hRgnSrc = NULL;
	LPPOINT	pStart = lpPoints;

	if(iX != 0)
	{
		for(int i=0;i<iCount;i++)
		{
			lpPoints->x = lpPoints->x + iX;
			lpPoints->y = lpPoints->y + iY;
			lpPoints++;
		}
		lpPoints = pStart;
	}
	

	hRgnSrc = CreatePolygonRgn(lpPoints, iCount, ALTERNATE);
	if(hRgnSrc == NULL)
	{
		DXTRACE_MSG("ERROR !");
	}
	CombineRgn(hBoundingPoly, hBoundingPoly, hRgnSrc, RGN_DIFF);
	DeleteObject(hRgnSrc);

	if(iX != 0)
	{
		for(int i=0;i<iCount;i++)
		{
			lpPoints->x = lpPoints->x - iX;
			lpPoints->y = lpPoints->y - iY;
			lpPoints++;
		}
		lpPoints = pStart;
	}
}

void cHitChecker::CreatePolygonBound(LPPOINT lpPoints, int nCount)
{
	// Use the combine Rgn API to remove a poly from the class internal region
	Destroy();
	hBoundingPoly = CreatePolygonRgn(lpPoints, nCount, ALTERNATE);
}


BOOL cHitChecker::HaveHitted(cHitChecker *pHitCheck, int nX, int nY, int nSrcX, int nSrcY)
{
	HRGN	hSrcObjectRgn;
	HRGN	hCompObjectRgn;
	BOOL	bResult = FALSE;
	DWORD	dwSize;
	UINT	i = 0;

	RGNDATA*	rgnData;

	// First check the bounding rectangle
	RECT	rcObj;
	GetRgnBox(pHitCheck->hBoundingPoly,&rcObj);
	rcObj.top  += nY; rcObj.bottom  += nY;
	rcObj.left += nX; rcObj.right   += nX;

	if(nSrcX!=0 && nSrcY!=0)
	{
		OffsetRgn(hBoundingPoly, nSrcX, nSrcY);
		if(RectInRegion(hBoundingPoly, &rcObj) == 0)
		{
			OffsetRgn(hBoundingPoly, -nSrcX, -nSrcY);
			return FALSE;
		}
		OffsetRgn(hBoundingPoly, -nSrcX, -nSrcY);

	}
	else
	{
		if(RectInRegion(hBoundingPoly, &rcObj) == 0)
			return FALSE;
	}

	dwSize = GetRegionData(pHitCheck->hBoundingPoly, sizeof(RGNDATA), NULL);
	rgnData = (RGNDATA*) malloc(dwSize);
	GetRegionData(pHitCheck->hBoundingPoly, dwSize, rgnData);
	hSrcObjectRgn = ExtCreateRegion(NULL, dwSize, rgnData);
	
	OffsetRgn(hSrcObjectRgn, nX, nY);

	dwSize = GetRegionData(hSrcObjectRgn, sizeof(RGNDATA), NULL);
	rgnData = (RGNDATA*) realloc(rgnData, dwSize);
	GetRegionData(hSrcObjectRgn, dwSize, rgnData);
	
	if(nSrcX !=0 && nSrcY !=0)
	{
		RGNDATA*	rgnData2;

		// Same copy for the region being tested
		dwSize = GetRegionData(hBoundingPoly, sizeof(RGNDATA), NULL);
		rgnData2 = (RGNDATA*) malloc(dwSize);
		GetRegionData(hBoundingPoly, dwSize, rgnData2);
		hCompObjectRgn = ExtCreateRegion(NULL, dwSize, rgnData2);
		OffsetRgn(hCompObjectRgn, nSrcX, nSrcY);

		bResult = TRUE;

		for(i=0;i<rgnData->rdh.nCount;i++)
		{
			memcpy(&rcObj, &rgnData->Buffer[i*sizeof(RECT)], sizeof(RECT));
			if(RectInRegion(hCompObjectRgn, &rcObj) != 0)
			{
				bResult = FALSE;
				break;
			}
		}

		free(rgnData2);
		DeleteObject(hCompObjectRgn);

	}
	else
	{
		bResult = TRUE;

		for(i=0;i<rgnData->rdh.nCount;i++)
		{
			memcpy(&rcObj, &rgnData->Buffer[i*sizeof(RECT)], sizeof(RECT));

			if(RectInRegion(hBoundingPoly, &rcObj) != 0)
			{
				bResult = FALSE;
				break;
			}
		}
	}
	free((void*)rgnData);
	// Free the resources
	DeleteObject(hSrcObjectRgn);
	return !bResult;
}

void cHitChecker::Destroy()
{
	// If we have a region, destroy it
	if(hBoundingPoly != NULL)
	{
		DeleteObject(hBoundingPoly);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -