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

📄 dxlibrary.h

📁 A Star 算法 的C++实现, 有很好的类实现
💻 H
📖 第 1 页 / 共 5 页
字号:

	if (g_screenDepth == 16){
	short* pSurface1 = (short*) ddsd1.lpSurface; //Grab surface pointer
	short* pSurface2 = (short*) ddsd2.lpSurface; //Grab surface pointer
	pitch1 = pitch1/2; //adjust pitch to short value
	pitch2 = pitch2/2; //adjust pitch to short value
	for (int x = 0; x < width; x++){
		for (int y = 0; y < height; y++){
		if (*(pSurface1 + a+x + (b+y)*pitch1) != 
			pImage1->convertedMaskColor){
		if (*(pSurface2 + c+x + (d+y)*pitch2) != 
			pImage2->convertedMaskColor){
			collision = 1;
			break;
		}//if (*(pSurface2
		if (collision == 1) break;
		}//if (*(pSurface1
		}//for (int y = 0; y < height; y++){
	}//for (int x = 0; x < width; x++){
	}//if (g_screenDepth == 16){

	if (g_screenDepth == 24){
	byte* pSurface1 = (byte*) ddsd1.lpSurface; //Grab surface pointer
	byte* pSurface2 = (byte*) ddsd2.lpSurface; //Grab surface pointer
	for (int x = 0; x < width; x++){
		for (int y = 0; y < height; y++){
		if ((*((int*) (pSurface1+3*(a+x)+(b+y)*pitch1)) & 0x00FFFFFF) != 
			pImage1->convertedMaskColor){
		if ((*((int*) (pSurface2+3*(c+x)+(d+y)*pitch2)) & 0x00FFFFFF) != 
			pImage2->convertedMaskColor){
			collision = 1;
			break;
		}//if (*(pSurface2
		if (collision == 1) break;
		}//if (*(pSurface1
		}//for (int y = 0; y < height; y++){
	}//for (int x = 0; x < width; x++){
	}//if (g_screenDepth == 24){

	if (g_screenDepth == 32){
	int* pSurface1 = (int*) ddsd1.lpSurface; //Grab surface pointer
	int* pSurface2 = (int*) ddsd2.lpSurface; //Grab surface pointer
	pitch1 = pitch1/4; //adjust pitch to short value
	pitch2 = pitch2/4; //adjust pitch to short value
	for (int x = 0; x < width; x++){
		for (int y = 0; y < height; y++){
		if (*(pSurface1 + a+x + (b+y)*pitch1) != 
			pImage1->convertedMaskColor){
		if (*(pSurface2 + c+x + (d+y)*pitch2) != 
			pImage2->convertedMaskColor){
			collision = 1;
			break;
		}//if (*(pSurface2
		if (collision == 1) break;
		}//if (*(pSurface1
		}//for (int y = 0; y < height; y++){
	}//for (int x = 0; x < width; x++){
	}//if (g_screenDepth == 32){

	//Unlock both surfaces
	pImage1->pSurface->Unlock(NULL);
	pImage2->pSurface->Unlock(NULL);

	return collision; //return collision status
}


//-----------------------------------------------------------------------------
// Name: ImagesOverlap
// Desc: Checks to see if two images overlap. This command does not
//	consider whether pixels are transparent or not.
//-----------------------------------------------------------------------------
int ImagesOverlap (cImage* pImage1, int x1, int y1,
					 cImage* pImage2, int x2, int y2)
{
	if(pImage1 == NULL) return -1;
	x1 = x1-pImage1->xHandle;
	y1 = y1-pImage1->yHandle;

	if(pImage2 == NULL) return -2;
	x2 = x2-pImage2->xHandle;
	y2 = y2-pImage2->yHandle;

	int overlaps = 0;
	if (x2 < x1+pImage1->imageWidth){
		if (x2+pImage2->imageWidth > x1){
			if (y2 < y1+pImage1->imageHeight){
				if (y2+pImage2->imageHeight > y1){
				overlaps = 1;
	}}}}

	return overlaps;
}


//-----------------------------------------------------------------------------
// Name: ImageWidth
// Desc: Gets the width of a given image.
//-----------------------------------------------------------------------------
int ImageWidth (cImage* pImage)
{
	if(pImage == NULL) return -1;
	return(pImage->imageWidth);
}


//-----------------------------------------------------------------------------
// Name: ImageXHandle
// Desc: Returns x handle of an image
//-----------------------------------------------------------------------------
int ImageXHandle (cImage* pImage)
{ 	
	if(pImage == NULL) return -1;
	if(pImage->pSurface == NULL) return -2;
	return (pImage->xHandle);
}


//-----------------------------------------------------------------------------
// Name: ImageYHandle
// Desc: Returns y handle of an image
//-----------------------------------------------------------------------------
int ImageYHandle (cImage* pImage)
{ 	
	if(pImage == NULL) return -1;
    if(pImage->pSurface == NULL) return -2;
	return (pImage->yHandle);
}


//-----------------------------------------------------------------------------
// Name: KeyDown()
// Desc: Returns whether a specific key is down. The key is the virtual
//	key code of the key. For A through Z, a through z, or 0 through 9,
//	use the ASCII value. For other keys, use the virtual key code.
//  - Example = FreeImage demo
//-----------------------------------------------------------------------------
bool KeyDown (int nVirtKey)
{
	if ((GetKeyState(nVirtKey) & 0x8000) != 0)
		return true;
	else
		return false;
}


//-----------------------------------------------------------------------------
// Name: KeyHit()
// Desc: Returns whether a specific key has been pressed and released.
//-----------------------------------------------------------------------------
bool KeyHit (int nVirtKey)
{
	static int keyPressed;
	if ((GetKeyState(nVirtKey) & 0x8000) != 0) //if key is down
	{
		keyPressed = nVirtKey;
		return false;
	}
	else if (keyPressed == nVirtKey) //if key is released after being pressed
	{
		keyPressed = 0;
		return true;
	}
	return false;
}


//-----------------------------------------------------------------------------
// Name: Line()
// Desc: Draws a line at the specified location using the current pen.
//	Example = GUI Commands demo
//-----------------------------------------------------------------------------
int Line (int startX, int startY, int endX, int endY)
{
	//Get hDC of backbuffer
	HDC hDC = NULL;
	g_pBackBuffer->Restore(); //Make sure the surface is restored	
	g_pBackBuffer->GetDC(&hDC);//Get the DC of the surface

	if (g_hPen != NULL) SelectObject(hDC, g_hPen); 
	SetBkMode(hDC, TRANSPARENT);

	//Move current drawing location
	MoveToEx(
		hDC,// handle to DC
		startX, // x-coordinate of starting point
		startY, // y-coordinate of starting point
		NULL); //pointer to a POINT structure that receives 
			//the previous current position

	//Draw the line
	LineTo(
		hDC,// handle to DC
		endX, // x-coordinate of ending point
		endY); // y-coordinate of ending point

	g_pBackBuffer->ReleaseDC(hDC);
	return (1);
}


//-----------------------------------------------------------------------------
// Name: Load3DImage()
// Desc: Loads a 3D-enabled sprite or image. Such images have the can use
//	real time rotations, scaling, and alpha blending. They do not
//	work in 8 bit (paletted) display modes, however. The loaded image
//	should also be square, and its width and height both a power of 2.
//	Example dimensions are 32x32, 64x64, 128x128, etc. Some video
//	cards may not be able to load images much larger than that.
//	- Example = Draw3DImage demo
//-----------------------------------------------------------------------------
cImage* Load3DImage (char* szBitmap)
{
	if (g_screenDepth == 8 || g_screenDepth == 24) return 0;

    HRESULT hr;

	//Try to load the bitmap from disk
	HBITMAP                 hbm;  //handle of the loaded bitmap
	BITMAP                  bm;   //bitmap class
    hbm = (HBITMAP) LoadImage( //type cast the handle
        NULL,  //hInstance of image
        szBitmap, //string indicating source of file
        IMAGE_BITMAP,//image type, as opposed to IMAGE_CURSOR or IMAGE_ICON
        0, //desired image width if resizing, 0 if using original width
        0, //desired image width if resizing, 0 if using original height
        LR_LOADFROMFILE | LR_CREATEDIBSECTION);

	//If that fails, exit
	if (hbm == NULL) return 0;

	//Save the bitmap in a Windows GUI-compatible device context
	HDC hdcImage = CreateCompatibleDC(NULL);//create device context for image
	SelectObject( //place the bitmap into the memory DC
	hdcImage,//device context of image created above
	hbm);//handle of the loaded bitmap

	//Set up the Direct Draw surface description
    DDSURFACEDESC2 ddsd;
    ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) );
    ddsd.dwSize          = sizeof(DDSURFACEDESC2);
	ddsd.dwFlags = DDSD_CAPS       | //ddsCaps structure is valid (see below
                 DDSD_HEIGHT       | //dwHeight member is valid
                 DDSD_WIDTH        | //dwWidth member is valid
                 DDSD_PIXELFORMAT  |
				 DDSD_TEXTURESTAGE | //it's a D3D texture
                 DDSD_CKSRCBLT;      //color key enabled
	ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE; 
    
	GetObject(hbm, sizeof(bm), &bm); // Get the size of the bitmap
    DWORD dwWidth  = (DWORD)bm.bmWidth;
    DWORD dwHeight = (DWORD)bm.bmHeight;
	ddsd.dwWidth = dwWidth;  //record image width (gotten from GetObject)
	ddsd.dwHeight = dwHeight;  //record image height (gotten from GetObject)

    // Adjust width and height, if the driver requires it
    D3DDEVICEDESC7 ddDesc;
    if(FAILED(g_pD3DDevice->GetCaps(&ddDesc)))
        return 0;
    if(ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2)
    {
        for( ddsd.dwWidth=1;  dwWidth>ddsd.dwWidth;   ddsd.dwWidth<<=1 );
        for( ddsd.dwHeight=1; dwHeight>ddsd.dwHeight; ddsd.dwHeight<<=1 );
    }
    if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY )
    {
        if(ddsd.dwWidth > ddsd.dwHeight) ddsd.dwHeight = ddsd.dwWidth;
        else                             ddsd.dwWidth  = ddsd.dwHeight;
    }

    // Enumerate the texture formats, and find the closest device-supported
    // texture pixel format. Use the TextureSearchCallback function.
    g_pD3DDevice->EnumTextureFormats( TextureSearchCallback, &ddsd.ddpfPixelFormat );
    if( 0L == ddsd.ddpfPixelFormat.dwRGBBitCount )
        return 0;

	//Create the Direct Draw surface in specified memory area
	LPDIRECTDRAWSURFACE7 pSurface; //pointer to Direct Draw surface
	if (g_pDirectDraw->CreateSurface(
	&ddsd, //address of above DDSURFACEDESC2 structure that describes surface
	&pSurface, //address of the variable pointing to the surface
	NULL)  //advanced COM stuff
	!= DD_OK) return 0;

	//If this fails, exit
	if (pSurface == NULL) return 0;

	// Make sure the Direct Draw surface is restored.
	pSurface->Restore();

	//Get device context of Direct Draw surface
	HDC hdc;  //handle of display version of image
	if ((hr = pSurface->GetDC(&hdc)) == DD_OK)

	//Copy the bitmap to Direct Draw surface.
	{
	BitBlt(
		hdc,	// handle of VRAM destination DC
		0,		// x-coord of destination upper-left corner
		0,		// y-coord of destination upper-left corner
		ddsd.dwWidth,	// width of destination rectangle
		ddsd.dwHeight,	// height of destination rectangle
		hdcImage,	// handle of RAM source DC
		0, // width of source rectangle
		0, // height of source rectangle
		SRCCOPY );	// raster operation code
	}

	//Clean up by releasing stuff we don't need anymore
	if( FAILED( hr = pSurface->ReleaseDC(hdc))) return 0;
	DeleteDC(hdcImage);//delete the GUI device context (DC)
	DeleteObject(hbm);//delete the bitmap object.

	//Create a new cImage class instance to hold image data 
	cImage* pImage = new cImage;
	pImage->pSurface = pSurface; //pointer to the image
	pImage->imageWidth = ddsd.dwWidth;
	pImage->imageHeight = ddsd.dwHeight;
	pImage->isMasked = false;
	pImage->memLocation = 0;
	pImage->imageIs3D = true;
	if (g_AutoMidHandle == false)
	{
	pImage->xHandle = 0;
	pImage->yHandle = 0;
	}
	if (g_AutoMidHandle == true)
	{
	pImage->xHandle = pImage->imageWidth/2;
	pImage->yHandle = pImage->imageHeight/2;
	}

	//Update linked list of images
	if (g_pFirstImage == NULL) g_pFirstImage = g_pLastImage = pImage;
	if (g_pFirstImage != pImage) 
	{
	g_pLastImage->nextImage = pImage;//link this item to end of list
	pImage->previousImage = g_pLastImage;//do a return link
	g_pLastImage = pImage;//make this item the last one on the list
	}

	//dvRHW (D3DVALUE)
	g_Vertex[0].rhw = 1.0f;
	g_Vertex[1].rhw = 1.0f;
	g_Vertex[2].rhw = 1.0f;
	g_Vertex[3].rhw = 1.0f;

	//Base dcColor (D3DCOLOR) -- alpha (last) value can be altered
	g_Vertex[0].color = RGBA_MAKE(255, 255, 255, 127);//RGBA
	g_Vertex[1].color = RGBA_MAKE(255, 255, 255, 127);
	g_Vertex[2].color = RGBA_MAKE(255, 255, 255, 127);
	g_Vertex[3].color = RGBA_MAKE(255, 255, 255, 127);

	//dcSpecular (D3DCOLOR) 
	g_Vertex[0].specular = D3DRGB(1.0,1.0,1.0);
	g_Vertex[1].specular = D3DRGB(1.0,1.0,1.0);
	g_Vertex[2].specular = D3DRGB(1.0,1.0,1.0);
	g_Vertex[3].specular = D3DRGB(1.0,1.0,1.0);

	//Texture alignment (D3DVALUE). Since we are using 2D
	//sprites, we don't change this.
	g_Vertex[0].tu = 0.0f; g_Vertex[0].tv = 0.0f;
	g_Vertex[1].tu = 1.0f; g_Vertex[1].tv = 0.0f;
	g_Vertex[2].tu = 0.0f; g_Vertex[2].tv = 1.0f;
	g_Vertex[3].tu = 1.0f; g_Vertex[3].tv = 1.0f;

	return pImage;//return pointer pImage
}



//-----------------------------------------------------------------------------
// Name: LoadImage()
// Desc: Create a Direct Draw Surface from a bitmap resource.
//	- Example = DrawImage demo
//-----------------------------------------------------------------------------
cImage* LoadImage (char* szBitmap, char loadTo=0)
{
	HRESULT                 hr; //variable to receive results from DD commands

	//Try to load the bitmap from disk
	HBITMAP                 hbm;  //handle of the loaded bitmap
	BITMAP              

⌨️ 快捷键说明

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