Archived

This topic is now archived and is closed to further replies.

Help With Direct X 8 Transparent Surfaces

This topic is 5710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a function that will copy one surface to another, pixel by pixel, the only reason I am doing it this way is so there can be transparancy, I.E. I will not draw a pixel if it is the selected transparent color, anyways when I run the function I get a really messed stuff on the screen. The picture is drawn about 5 times across the screen overwriting each other. I am a Dx8 newbie, and still haven''t fully grasped this whole concept of the surface being one dimensional. Anyways if you know what is wrong with this code, or a whole entire way around it (not BitBlt) it would be appreciated. Thanks, heres the function:
  

//------------------------------------------------------------------------------------

//Param 1 - Rectangle of the surface that is being copied

//Param 2 - The point at which the rectangle will be copied to the surface

//Param 3 - The source surface where the DC will come from

//Param 4 - The destination surface where the above will be copied to

//Param 5 - The color key that makes certain pixels invisible, -1 if no color key

int SurfaceToSurface(RECT* srcRect,
					 POINT* dstPoint,
					 LPDIRECT3DSURFACE8 srcSurface,
					 LPDIRECT3DSURFACE8 dstSurface,
					 D3DCOLOR colorKey)
{
	D3DLOCKED_RECT	lckSource;		//The source rectangle lock

	D3DLOCKED_RECT	lckDest;		//The destination rectangle lock

	RECT			rSrcRect;		//The source rectangle

	POINT			pDstPoint;		//The destination point

	D3DSURFACE_DESC	d3dDesc;		//The destription of the dest surface

	DWORD*			srcData;		//Points to the data in the source

	DWORD*			dstData;		//Points to the data on the destination

	int				srcOff;			//Source offset

	int				dstOff;			//Destination offset


	//Check the source and destination surface for validicity reasons

	if (!srcSurface || !dstSurface)
		return E_FAIL;

	//Fill up the DESC information

	dstSurface->GetDesc(&d3dDesc);

	//If the src rectangle was specified, i.e. NOT full rectangle

	if (srcRect)
	{
		//Copy the srcRect into the variable

		rSrcRect = *srcRect;
	}
	else
	{
		//Set the rect to the entire surface

		SetRect(&rSrcRect,
				0,
				0,
				d3dDesc.Width,
				d3dDesc.Height);
	}

	//Do the same for the destination

	//If the src rectangle was specified, i.e. NOT full rectangle

	if (dstPoint)
	{
		//Copy the dstRect into the variable

		pDstPoint = *dstPoint;
	}
	else
	{
		//Set the point to the entire surface

		pDstPoint.x = pDstPoint.y = 0;
	}
	
	//Lock the source

	if (FAILED(srcSurface->LockRect(&lckSource, 0, 0)))
		return E_FAIL;

	//Lock the destination, if falure quit and unlock the source

	if (FAILED(dstSurface->LockRect(&lckDest, 0, 0)))
	{
		srcSurface->UnlockRect();
		return E_FAIL;
	}

	//Change the pitch of the locked rects to be 32 bit

	lckSource.Pitch /= 4;
	lckDest.Pitch /= 4;
	
	//Set the pointers to the surface''s data

	srcData = (DWORD*)lckSource.pBits;
	dstData = (DWORD*)lckDest.pBits;

	//Get the offset ammount to the memory so pixels can be plotted

	srcOff = rSrcRect.top * lckSource.Pitch + rSrcRect.left;
	dstOff = pDstPoint.y * lckDest.Pitch + pDstPoint.x;

	//Go through every row

	for (int y = 0; y < rSrcRect.bottom ; y++)
	{
		//Go through every column

		for (int x = 0; x < rSrcRect.right; x++)
		{
			//If transparancy is on

			if (colorKey)
			{
				//See if the pixel were at should be transparent, if so do not draw

				if (srcData[srcOff] != colorKey)
					dstData[dstOff] = srcData[srcOff];
			}
			else
			{
				dstData[dstOff] = srcData[srcOff];
			}
			//Incriment to the next pixel

			srcOff++;
			dstOff++;
		}
		//Goto the next row

		srcOff += (lckSource.Pitch/4) - rSrcRect.right;
		dstOff += (lckDest.Pitch/4) - rSrcRect.right;
	}
	
	//Unlock the surfaces

	srcSurface->UnlockRect();
	dstSurface->UnlockRect();

	//It worked, return S_OK

	return S_OK;
}
  

Share this post


Link to post
Share on other sites