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

📄 dxlibrary.h

📁 A Star 算法 的C++实现, 有很好的类实现
💻 H
📖 第 1 页 / 共 5 页
字号:
	//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

	//Set up Mirrored Blit stuff
	int effect = 0;
	if (mode == 0) effect = DDBLTFX_MIRRORLEFTRIGHT;
	if (mode == 1) effect = DDBLTFX_MIRRORUPDOWN;
	if (mode == 2) effect = DDBLTFX_MIRRORLEFTRIGHT | DDBLTFX_MIRRORUPDOWN;
	DDBLTFX  ddbltfx;  
	ZeroMemory(&ddbltfx, sizeof(ddbltfx));
	ddbltfx.dwSize = sizeof(ddbltfx);
	ddbltfx.dwDDFX = effect;

	//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
	//If image is not masked, draw it as-is
	if (pImage->isMasked == false)
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_WAIT | DDBLT_DDFX,//flags
	&ddbltfx ); //special effects
	}

	//If image is masked, draw it masked.
	else
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_KEYSRC | DDBLT_WAIT | DDBLT_DDFX, //flags
	&ddbltfx ); //special effects
	}

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


//-----------------------------------------------------------------------------
// Name: DrawImage
// Desc: This function blits a masked image on the screen.
//	Example = DrawImage demo
//-----------------------------------------------------------------------------
int DrawImage (cImage* pImage, int x, int y)
{
	HRESULT                     hr;
	//Error check
	if(pImage == NULL) return E_INVALIDARG;
	if(pImage->pSurface == NULL) return E_INVALIDARG;

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

	//Blit to the backbuffer
	//If image is not masked, draw it as-is
	if (pImage->isMasked == false)
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_WAIT, //DDBLT_WAIT = wait until blitter is free
	NULL ); //special effects
	}

	//If image is masked, draw it masked.
	else
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_KEYSRC | DDBLT_WAIT, //DDBLT_KEYSRC = use source color key
	NULL ); //special effects
	}

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


//-----------------------------------------------------------------------------
// Name: DrawImageRect
// Desc: Draws a portion of an image to the backbuffer. If the original
//	is masked, this will be too. Handle information is ignored.
//  - Example = CopyRect demo.
//-----------------------------------------------------------------------------
int DrawImageRect (cImage* pImage, int x2, int y2, int x1, int y1,
					 int width, int height)
{
	//Get incoming info
	if(pImage == NULL) return -1;
	if(pImage->pSurface == NULL) return -1;

	//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 backbuffer
	//If image is not masked, draw it as-is
	HRESULT hr;
	if (pImage->isMasked == false)
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	&rectSource, //Source RECT or NULL for entire surface
	DDBLT_WAIT, //DDBLT_WAIT = wait until blitter is free
	NULL ); //special effects
	}

	//If image is masked, draw it masked.
	else
	{
	hr = g_pCurrentSurface->Blt(
	&rectDest, //Destination RECT
	pImage->pSurface,  //lpDDSrcSurface
	&rectSource, //Source RECT or NULL for entire surface
	DDBLT_KEYSRC | DDBLT_WAIT, //DDBLT_KEYSRC = use source color key
	NULL ); //special effects
	}

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


//-----------------------------------------------------------------------------
// Name: EndGraphics
// Desc: Releases all Direct X objects. Required at the end of each
//	program.
//-----------------------------------------------------------------------------
int EndGraphics (void)
{
	return ReleaseObjects();
}


//-----------------------------------------------------------------------------
// Name: Flip
// Desc: This function flips stuff drawn on the back buffer to the front buffer
//	It also updates the mouse position.
//-----------------------------------------------------------------------------
int Flip (void)
{
	HRESULT                     hr;
	while (TRUE)
	{
        hr = g_pFrontBuffer->Flip(NULL, 0);
        if (hr == DD_OK)
            break;
        if (hr == DDERR_SURFACELOST)
        {
            hr = g_pDirectDraw->RestoreAllSurfaces(); //Reload images
            if (hr != DD_OK) break; 
			hr = g_pMouse->Acquire(); //reaquire mouse for Direct Input
			ShowCursor(FALSE);
			g_MouseX = g_screenWidth/2;
			g_MouseY = g_screenHeight/2;
        }
        if (hr != DDERR_WASSTILLDRAWING)
            break;
	}
    UpdateMouse();
	return (1);
}



//-----------------------------------------------------------------------------
// Name: FlipImage
// Desc: This function flips an image horizontally, vertically, or both.
//	Unlike DrawFlipped, which draws a flipped version of the original
//	image on screen, this function flips the original image itself.
//	Example = Scale & Rotate demo
//-----------------------------------------------------------------------------
int FlipImage (cImage* pImage, int mode=0)
{
	//Get incoming info
	if(pImage == NULL) return 0;
	if(pImage->pSurface == NULL) return 0;	
	char loadTo = pImage->memLocation;

	//Create a temporary image to copy flipped image to
	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
	if (loadTo == 0) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
	if (loadTo == 1) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN 
		| DDSCAPS_SYSTEMMEMORY; //load to RAM if loadTo == 1
	ddsd.dwWidth = pImage->imageWidth;
	ddsd.dwHeight = pImage->imageHeight;
	LPDIRECTDRAWSURFACE7 pTempImage; //pointer to Direct Draw surface
	HRESULT hr = g_pDirectDraw->CreateSurface(
	&ddsd, //address of above DDSURFACEDESC2 structure that describes surface
	&pTempImage, //address of the variable pointing to the new image
	NULL);  //advanced COM stuff
	if (hr != DD_OK) return hr;

	//Set up Mirrored Blit stuff
	int effect = 0;
	if (mode == 0) effect = DDBLTFX_MIRRORLEFTRIGHT;
	if (mode == 1) effect = DDBLTFX_MIRRORUPDOWN;
	if (mode == 2) effect = DDBLTFX_MIRRORLEFTRIGHT | DDBLTFX_MIRRORUPDOWN;
	DDBLTFX  ddbltfx;  
	ZeroMemory(&ddbltfx, sizeof(ddbltfx));
	ddbltfx.dwSize = sizeof(ddbltfx);
	ddbltfx.dwDDFX = effect;

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

	//Blit it to the temporary image
	pTempImage->Blt(
	    &rectDest, //Destination RECT
		pImage->pSurface,  //lpDDSrcSurface
	    NULL, //Source RECT or NULL for entire surface
	    DDBLT_WAIT | DDBLT_DDFX,//flags
	    &ddbltfx ); //special effects

	//Now delete the original image and put the new image
	//in its place
	pImage->pSurface->Release();
	pImage->pSurface = pTempImage;

	//If appropriate, mask the new image the same as the original
	if (pImage->isMasked == true)
	{
	DDCOLORKEY ddck;
	ddck.dwColorSpaceLowValue = pImage->convertedMaskColor;
	ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
	pImage->pSurface->SetColorKey(DDCKEY_SRCBLT, &ddck);
	}

	return 1;
}


//-----------------------------------------------------------------------------
// Name: FPS() 
// Desc: Returns the current frames per second. Should be called every
//	frame to ensure accuracy.
//	- Example = DrawImage demo
//-----------------------------------------------------------------------------
int FPS (void)
{
	static int time = 0, FPS = 0, frames = 0, frames0 = 0;
	if (timeGetTime() >= time) 
	{
		FPS = frames-frames0;
		time = timeGetTime()+1000;
		frames0 = frames;
	}
	frames = frames+1;
	return FPS;
}


//-----------------------------------------------------------------------------
// Name: FreeImage 
// Desc: Frees the VRAM associated with a previously created image.
//  The command does not delete the image's container class, however, which 
//	consumes only a few bytes of memory. This provides a certain level of
//	error protection against subsequent DrawImage calls using
//	a pointer to an image that no longer exists. If a DrawImage or
//	similar command is called on an image that no longer exists,
//	it will do nothing. Unfortunately, this does not protect against
//	DrawImage calls on images that were never created in the first 
//	place.
//  - Example = FreeImage demo
//-----------------------------------------------------------------------------
int FreeImage (cImage* pImage)
{
	if(pImage == NULL) return 0;
	if(pImage->pSurface == NULL) return 0;

	//Release the image associated with this class pointer
	if (pImage->pSurface != NULL)
	{
	pImage->pSurface->Release();//release VRAM
	pImage->pSurface = NULL;//set pointer to NULL
	}

	return DD_OK;
}


//-----------------------------------------------------------------------------
// Name: FrontBuffer
// Desc: Returns a "pointer" to the front buffer.
//-----------------------------------------------------------------------------
int FrontBuffer (void)
{
	return 1;
}


//-----------------------------------------------------------------------------
// Name: Get16BitFormat
// Desc: Returns the particular 16 bit format used when in 16 bit mode
//-----------------------------------------------------------------------------
int Get16BitFormat (void)
{ 	
	return g_screenFormat16Bit;
}


//-----------------------------------------------------------------------------
// Name: GetMouseSpeed
// Desc: Returns the current mouse speed. This number does not correlate
//	with Windows system mouse speed values.
//-----------------------------------------------------------------------------
int GetMouseSpeed (void)
{ 	
	return g_MouseSpeed;
}


//-----------------------------------------------------------------------------
// Name: GetR
// Desc: Returns the r component of an RGB value.
//-----------------------------------------------------------------------------
int GetR(unsigned int rgb)
{
	return rgb >> 16 & 0xff;
}


//-----------------------------------------------------------------------------
// Name: GetG
// Desc: Returns the r component of an RGB value.
//-----------------------------------------------------------------------------
int GetG(unsigned int rgb)
{
	return rgb >> 8 & 0xff;
}


//-----------------------------------------------------------------------------
// Name: GetB
// Desc: Returns the r component of an RGB value.
//-----------------------------------------------------------------------------
int GetB(unsigned int rgb)
{
	return rgb & 0xff;
}


⌨️ 快捷键说明

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