|
|
发表于 2004-11-29 21:09:00
|
显示全部楼层
Re:请教:alpha的问题
HRESULT CDirectDraw::AlphaBlt(int x, int y, LPDIRECTDRAWSURFACE7 pDDS, RECT *pSourRect, bool bColorKeyed, int iAlpha)
{
if(!pDDS)
return S_OK;
if(iAlpha>255 || iAlpha<0)
return BltFast(x,y,pDDS,pSourRect,bColorKeyed);
DDSURFACEDESC2 ddsd,ddsdthis;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ZeroMemory(&ddsdthis,sizeof(ddsdthis));
ddsdthis.dwSize=sizeof(ddsdthis);
pDDS->GetSurfaceDesc(&ddsd);//得到两个表面的描述
this->m_lpBackSurface->GetSurfaceDesc(&ddsdthis);
RECT rtSour,rtClip;//源表面,背景表面的矩形
if(!pSourRect)
{
rtSour.top=0;
rtSour.bottom=ddsd.dwHeight;
rtSour.left=0;
rtSour.right=ddsd.dwWidth;
}
else//裁剪源表面
{
rtSour.bottom=pSourRect->bottom;
rtSour.left=pSourRect->left;
rtSour.right=pSourRect->right;
rtSour.top=pSourRect->top;
if(rtSour.top<0)
rtSour.top=0;
if(rtSour.left<0)
rtSour.left=0;
if(rtSour.right>(int)ddsd.dwWidth)
rtSour.right=ddsd.dwWidth;
if(rtSour.bottom>(int)ddsd.dwHeight)
rtSour.bottom=ddsd.dwHeight;
}
rtClip.top=0;
rtClip.left=0;
rtClip.right=ddsdthis.dwWidth;
rtClip.bottom=ddsdthis.dwHeight;
if(x<rtClip.left)
{
rtSour.left-=x;//右移动
x=rtClip.left;
}
if(y<rtClip.top)
{
rtSour.top-=y;//下移动
y=rtClip.top;
}
if(x+rtSour.right-rtSour.left>rtClip.right)
{
rtSour.right=rtSour.left+rtClip.right-x;
}
if(y+rtSour.bottom-rtSour.top>rtClip.bottom)
{
rtSour.bottom=rtSour.top+rtClip.bottom-y;
}
DDSURFACEDESC2 ddsddest;
DDSURFACEDESC2 ddsdsour;
ZeroMemory(&ddsddest,sizeof(ddsddest));
ZeroMemory(&ddsdsour,sizeof(ddsdsour));
ddsddest.dwSize=sizeof(ddsddest);
ddsdsour.dwSize=sizeof(ddsdsour);
DDCOLORKEY ddck;
if(bColorKeyed)
{
pDDS->GetColorKey(DDCKEY_SRCBLT,&ddck);
}
HRESULT result;
while(1)
{
result=m_lpBackSurface->Lock(NULL,&ddsddest,DDLOCK_WAIT,NULL);
if(result==DD_OK)
break;
if(result==DDERR_SURFACELOST)
{
result = m_lpDD7->RestoreAllSurfaces();
}
else
return result;
}
while(1)
{
result=pDDS->Lock(NULL,&ddsdsour,DDLOCK_WAIT,NULL);
if(result==DD_OK)
break;
if(result==DDERR_SURFACELOST)
{
result = m_lpDD7->RestoreAllSurfaces();
}
else
return result;
}
UINT*DestBuf=(UINT*)ddsddest.lpSurface;
UINT*SourBuf=(UINT*)ddsdsour.lpSurface;
int SourWidth=ddsdsour.lPitch>>2;//32位色深,每个象素4个字节
int DestWidth=ddsddest.lPitch>>2;
int DestStart=DestWidth*y+x;
int SourStart=SourWidth*rtSour.top+rtSour.left;
int SourOff=SourWidth-rtSour.right+rtSour.left;
int DestOff=DestWidth-rtSour.right+rtSour.left;
int H=rtSour.bottom-rtSour.top;
int W=rtSour.right-rtSour.left;
if(!bColorKeyed)
{
for(int iy=0;iy<H;iy++)
{
for(int ix=0;ix<W;ix++)
{
DestBuf[DestStart]=Alpha(DestBuf[DestStart],SourBuf[SourStart],iAlpha);
SourStart++;DestStart++;
}
SourStart+=SourOff;
DestStart+=DestOff;
}
}
else
{
for(int iy=0;iy<H;iy++)
{
for(int ix=0;ix<W;ix++)
{
if(SourBuf[SourStart]!=ddck.dwColorSpaceHighValue)
DestBuf[DestStart]=Alpha(DestBuf[DestStart],SourBuf[SourStart],iAlpha);
SourStart++;DestStart++;
}
SourStart+=SourOff;
DestStart+=DestOff;
}
}
this->m_lpBackSurface->Unlock(NULL);
pDDS->Unlock(NULL);
return S_OK;
}
是我很久前写的,高手指点下。是32位色的混合函数,不知道怎么优化下,除了汇编方式 |
|