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

📄 dxlibrary.h

📁 A Star 算法 的C++实现, 有很好的类实现
💻 H
📖 第 1 页 / 共 5 页
字号:
		pSurfaceNew = NULL;
		return 0;
	}

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

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

	return pImageNew;//return pointer pImageNew
}


//-----------------------------------------------------------------------------
// Name: CopyRect
// Desc: Copies a portion of one image to another, or to the current
//	drawing buffer
//	Example = CopyRect demo
//-----------------------------------------------------------------------------
int CopyRect (int x1, int y1, int width, int height, int x2, int y2,
			  cImage* pImage1, cImage* pImage2, 
			  int pointer1=-1, int pointer2=-1)
{
	//Get source image pointer
	LPDIRECTDRAWSURFACE7 pSourceImage=NULL;
	if (pointer1 == 0)
		pSourceImage = g_pCurrentSurface;
	else if (pointer1 == 1)
		pSourceImage = g_pFrontBuffer;
	else if (pointer1 == 2)
		pSourceImage = g_pBackBuffer;
	else
		pSourceImage = pImage1->pSurface;

	//Get destination image pointer
	LPDIRECTDRAWSURFACE7 pDestImage=NULL;
	if (pointer2 == 0)
		pDestImage = g_pCurrentSurface;
	else if (pointer2 == 1)
		pDestImage = g_pFrontBuffer;
	else if (pointer2 == 2)
		pDestImage = g_pBackBuffer;
	else
		pDestImage = pImage2->pSurface;

	//Compute source rectangle
	RECT rectSource;
	rectSource.left=x1;
	rectSource.right=x1 + width;
	rectSource.top=y1;
	rectSource.bottom=y1 + height;

	//Compute destination rectangle
	RECT rectDest;
	rectDest.left=x2;
	rectDest.right=x2 + width;
	rectDest.top=y2;
	rectDest.bottom=y2 + height;

	//Blit to the destination image
	HRESULT hr = pDestImage->Blt(
	&rectDest, //Destination RECT
	pSourceImage,  //lpDDSrcSurface
	&rectSource, //Source RECT or NULL for entire surface
	DDBLT_WAIT, //flags
	NULL ); //special effects

	if (hr == DD_OK) 
		return 1;
	else
		return hr;
}

//-----------------------------------------------------------------------------
// Name: CopyRect
// Desc: Overloaded version of above function.
//-----------------------------------------------------------------------------
int CopyRect (int x1, int y1, int width, int height, int x2, int y2,
			  int pointer1, int pointer2)
{
	return CopyRect (x1, y1, width, height, x2, y2,
			  NULL,NULL, pointer1, pointer2);
}


//-----------------------------------------------------------------------------
// Name: CopyRect
// Desc: Overloaded version of above function.
//-----------------------------------------------------------------------------
int CopyRect (int x1, int y1, int width, int height, int x2, int y2,
			  int pointer1, cImage* image2)
{
	return CopyRect (x1, y1, width, height, x2, y2,
			  NULL,image2, pointer1, -1);
}


//-----------------------------------------------------------------------------
// Name: CopyRect
// Desc: Overloaded version of above function.
//-----------------------------------------------------------------------------
int CopyRect (int x1, int y1, int width, int height, int x2, int y2,
			  cImage* image1, int pointer2)
{
	return CopyRect (x1, y1, width, height, x2, y2,
			  image1,NULL, -1, pointer2);
}


//-----------------------------------------------------------------------------
// Name: CreateImage
// Desc: Creates an image of the specified dimensions, ready for 
//	writing or copying.
//-----------------------------------------------------------------------------
cImage* CreateImage (int width, int height, int loadTo=0)
{
	//Set up the Direct Draw surface description
	DDSURFACEDESC2 ddsd; //surface description data structure
	ZeroMemory(&ddsd, sizeof(ddsd));// zero-out the memory area
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | //ddsCaps structure is valid (see below
                 DDSD_HEIGHT | //dwHeight member is valid
                 DDSD_WIDTH;   //dwWidth member is valid

	//Specify type of surface (plain offscreen) and location (VRAM/RAM)
	if (loadTo == 0) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
	if (loadTo == 1) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN 
		| DDSCAPS_SYSTEMMEMORY; //load to RAM if loadTo == 1

	//Record image dimensions.
	ddsd.dwWidth = width;
	ddsd.dwHeight = height;

	//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;

	//Fill the surface with the color black.
	DDBLTFX ddbltfx;
	ZeroMemory( &ddbltfx, sizeof(ddbltfx));
	ddbltfx.dwSize      = sizeof(ddbltfx);
	ddbltfx.dwFillColor = 0;
	HRESULT hr = pSurface->Blt(
	NULL, //destination rectangle of blit (0 = blit entire surface)
	NULL, //address of source surface blitted from (0 used here)
	NULL, //source rectangle of blit (0 = blit from entire surface)
	DDBLT_COLORFILL | DDBLT_WAIT, //blit flags; DDBLT_COLORFILL = use dwFillColor member
	// of the DDBLTFX structure as the RGB color that fills the destination
	&ddbltfx );//address of special effects ddbltfx structure created above

	//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 = loadTo;
	pImage->imageIs3D = false;
	pImage->xHandle = 0;
	pImage->yHandle = 0;

	//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
	}

	return pImage;//return pointer pImage
}

//-----------------------------------------------------------------------------
// Name: Draw3DImage()
// Desc: Draws a 3D-enabled image, which supports real-time rotations, scaling
//	and alpha blending. Not currently supported in 8 bit (paletted) or 24 bit 
//	display mode.
//	Example = Draw3DImage demo
//-----------------------------------------------------------------------------
int Draw3DImage (cImage* pImage, int x, int y, int alpha=255,
                  int scalePercent=100,int angle=0, int flipped=0)
{
	//Not supported in 8 or 24 bit screen mode
	if (g_screenDepth == 8 || g_screenDepth == 24) return 0; 

	//Error check
	if(pImage == NULL) return E_INVALIDARG;
	if(pImage->pSurface == NULL) return E_INVALIDARG;
	if (alpha <= 0 || scalePercent <= 0) return 1;//don't draw invisible images

	x = x-pImage->xHandle;
	y = y-pImage->yHandle;

	//Set location of 3D image by adjusting image vertices
	if (scalePercent == 100 && angle == 0 && flipped == 0) //simple (no effects)
	{
		    g_Vertex[0].sx = (float) x;
		    g_Vertex[0].sy = (float) y;

		    g_Vertex[1].sx = (float) x+pImage->imageWidth;
		    g_Vertex[1].sy = (float) y;

		    g_Vertex[2].sx = (float) x;
		    g_Vertex[2].sy = (float) y+pImage->imageHeight;

		    g_Vertex[3].sx = (float) x+pImage->imageWidth;
		    g_Vertex[3].sy = (float) y+pImage->imageHeight;
	}

	else if (flipped == 0) //if it is not flipped
	{
		//This rotation method avoids the D3D T&L pipeline. We do our own 
		//transformations and send the transformed vertices to the video card.
		int xRadius = pImage->imageWidth*scalePercent/200;
		int yRadius = pImage->imageHeight*scalePercent/200;
		int midPointX = x+pImage->imageWidth/2;
		int midPointY = y+pImage->imageHeight/2;
		double multiple = 3.14159265/180;

	    g_Vertex[0].sx = (float) (midPointX+1.41*cos((135+angle)*multiple)*xRadius);
	    g_Vertex[0].sy = (float) (midPointY-1.41*sin((135+angle)*multiple)*yRadius);

	    g_Vertex[1].sx = (float) (midPointX+1.41*cos((45+angle)*multiple)*xRadius);
	    g_Vertex[1].sy = (float) (midPointY-1.41*sin((45+angle)*multiple)*yRadius);

	    g_Vertex[2].sx = (float) (midPointX+1.41*cos((225+angle)*multiple)*xRadius);
	    g_Vertex[2].sy = (float) (midPointY-1.41*sin((225+angle)*multiple)*yRadius);

	    g_Vertex[3].sx = (float) (midPointX+1.41*cos((315+angle)*multiple)*xRadius);
	    g_Vertex[3].sy = (float) (midPointY-1.41*sin((315+angle)*multiple)*yRadius);
	}

	else //if image is flipped
	{
		if (scalePercent == 100 && angle == 0) //if it is not rotated or scaled
		{
		    g_Vertex[0].sx = (float) x+pImage->imageWidth;
		    g_Vertex[0].sy = (float) y;

		    g_Vertex[1].sx = (float) x;
		    g_Vertex[1].sy = (float) y;

		    g_Vertex[2].sx = (float) x+pImage->imageWidth;
		    g_Vertex[2].sy = (float) y+pImage->imageHeight;

		    g_Vertex[3].sx = (float) x;
		    g_Vertex[3].sy = (float) y+pImage->imageHeight;
		}

		else //if it is rotated and/or scaled
		{
			//This rotation method avoids the D3D T&L pipeline. We do our own 
			//transformations and send the transformed vertices to the video card.
			int xRadius = pImage->imageWidth*scalePercent/200;
			int yRadius = pImage->imageHeight*scalePercent/200;
			int midPointX = x+pImage->imageWidth/2;
			int midPointY = y+pImage->imageHeight/2;
			double multiple = 3.14159265/180;

		    g_Vertex[0].sx = (float) (midPointX+1.41*cos((315+angle)*multiple)*xRadius);
		    g_Vertex[0].sy = (float) (midPointY-1.41*sin((135+angle)*multiple)*yRadius);

		    g_Vertex[1].sx = (float) (midPointX+1.41*cos((225+angle)*multiple)*xRadius);
		    g_Vertex[1].sy = (float) (midPointY-1.41*sin((45+angle)*multiple)*yRadius);

		    g_Vertex[2].sx = (float) (midPointX+1.41*cos((45+angle)*multiple)*xRadius);
		    g_Vertex[2].sy = (float) (midPointY-1.41*sin((225+angle)*multiple)*yRadius);

		    g_Vertex[3].sx = (float) (midPointX+1.41*cos((135+angle)*multiple)*xRadius);
		    g_Vertex[3].sy = (float) (midPointY-1.41*sin((315+angle)*multiple)*yRadius);
		}
	}

	//Set render state
    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_CLIPPING,false);
    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_LIGHTING,false);
    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_CULLMODE,D3DCULL_NONE);
	if (pImage->isMasked == true)
		g_pD3DDevice->SetRenderState(D3DRENDERSTATE_COLORKEYENABLE, TRUE);
	if (alpha != 255) //if image is alpha blended
	{
	    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE,true);
	    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND,D3DBLEND_SRCALPHA );
	    g_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND,D3DBLEND_INVSRCALPHA);
	    g_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTA_TFACTOR);
	}
	g_Vertex[0].color = RGBA_MAKE(255, 255, 255, alpha);
	g_Vertex[1].color = RGBA_MAKE(255, 255, 255, alpha);
	g_Vertex[2].color = RGBA_MAKE(255, 255, 255, alpha);
	g_Vertex[3].color = RGBA_MAKE(255, 255, 255, alpha);

	//Set texture
	g_pD3DDevice->SetTexture(0,pImage->pSurface);

    // Begin the scene
    if(FAILED(g_pD3DDevice->BeginScene())) return -1;

    //DrawPrimitive to render the quad
    g_pD3DDevice->DrawPrimitive(
        D3DPT_TRIANGLESTRIP,
		//D3DFVF_TLVERTEX, //which is the following
		D3DFVF_XYZRHW | 
		D3DFVF_DIFFUSE | 
		D3DFVF_SPECULAR |
        D3DFVF_TEX1, 
		g_Vertex,
		4,
		0);

    // End the scene.
    g_pD3DDevice->EndScene();

	//Free the texture from the 3D device, thereby avoiding memory leaks
	g_pD3DDevice->SetTexture(0,NULL);

	if (alpha != 255)  
		g_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE,false);

	return 1;
}


//-----------------------------------------------------------------------------
// Name: DrawBlock
// Desc: This function blits an unmasked version of an image on the screen
//	Example = DrawImage demo
//-----------------------------------------------------------------------------
int DrawBlock (cImage* pImage, int x, int y)
{
	//Get incoming info
	if(pImage == NULL) return E_INVALIDARG;
	if(pImage->pSurface == NULL) return E_INVALIDARG;
	x = x-pImage->xHandle; //x position to draw image
	y = y-pImage->yHandle; //y position to draw image

	//Compute destination rectangle
	RECT rectDest;
	rectDest.left=x;
	rectDest.right=x + pImage->imageWidth;
	rectDest.top=y;
	rectDest.bottom=y + pImage->imageHeight;

	//Blit to the current buffer
	HRESULT hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_WAIT, //flags
	NULL ); //special effects

	//Return result (should be DD_OK, which is 0, if drawn ok)
	return hr;
}


//-----------------------------------------------------------------------------
// Name: DrawFlipped
// Desc: This function blits a mirror image of an image on the screen
//	- Mode 0 = horizontal mirror; 1 = vertical; 2 = both (180 degree rotate)
// This command may be somewhat slower than Draw3DImage in flipped mode.
//-----------------------------------------------------------------------------
int DrawFlipped (cImage* pImage, int x, int y, int mode=0)
{
	HRESULT                     hr;

⌨️ 快捷键说明

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