DirectDraw版的AlphaBlend
程序的结果是把离屏表面lps1和lps2的混合结果写到lps3上面
sfa是混合区域
AlphaValue是Alpha指值
几乎就是传统Alpha混合的标准方法了,所以具体不做介绍了,知道DirectX和Alpha混合原理的都可以看得明白。
这个程序比上一个版本速度快多了,图片稍大一点就会感觉“卡”了。
看过汇编版的代码,看不懂不说,还不知道怎么用
是不是Alpha混合只有汇编一条路呢?
void AlphaBlend(LPDIRECTDRAWSURFACE7 lps1,
LPDIRECTDRAWSURFACE7 lps2,
LPDIRECTDRAWSURFACE7 &lps3,
RECT sfa,
float AlphaValue)
{
DDSURFACEDESC2 sppp;
DDSURFACEDESC2 sppp2;
DDSURFACEDESC2 sppp3;
memset(&sppp,0,sizeof(sppp));
memset(&sppp2,0,sizeof(sppp2));
memset(&sppp3,0,sizeof(sppp3));
sppp.dwSize=sizeof(sppp);
sppp2.dwSize=sizeof(sppp2);
sppp3.dwSize=sizeof(sppp3);
//锁定表面
lps1->Lock(NULL, &sppp, DDLOCK_WAIT, NULL);
lps2->Lock(NULL, &sppp2, DDLOCK_WAIT, NULL);
lps3->Lock(NULL, &sppp3, DDLOCK_WAIT, NULL);
//获取像素
USHORT* pM3 = (USHORT*)sppp3.lpSurface;
USHORT* pM2 = (USHORT*)sppp2.lpSurface;
USHORT* pM = (USHORT*)sppp.lpSurface;
int lp3=(int)sppp3.lPitch/2;
int lp2=(int)sppp2.lPitch/2;
int lp=(int)sppp.lPitch/2;
for(int t=0;t<sfa.bottom-sfa.top;++t)
for(int j=0;j<sfa.right-sfa.left;++j)
{
R1= pM[j+t*lp] >> 11;
G1= (pM[j+t*lp] & 2016)>>5 ;
B1= pM[j+t*lp] & 31;
R2= pM2[j+sfa.left+(t+sfa.top)*lp2] >> 11;
G2= (pM2[j+sfa.left+(t+sfa.top)*lp2] & 2016)>>5 ;
B2= pM2[j+sfa.left+(t+sfa.top)*lp2] & 31;
//计算16位565格式位图的混合结果
R=( AlphaValue * ( R2 - R1 ) /255) + R1;
G=( AlphaValue * ( G2 - G1 ) /255) + G1;
B=( AlphaValue * ( B2 - B1 ) /255) + B1;
USHORT st = (B & 31) + ((G & 63) << 5) + ((R & 31) << 11);
pM3[j+sfa.left+(t+sfa.top)*lp3]=st;
}
//释放表面
lps3->Unlock(0);
lps2->Unlock(0);
lps1->Unlock(0);
}
LPDIRECTDRAWSURFACE7 lps2,
LPDIRECTDRAWSURFACE7 &lps3,
RECT sfa,
float AlphaValue)
{
DDSURFACEDESC2 sppp;
DDSURFACEDESC2 sppp2;
DDSURFACEDESC2 sppp3;
memset(&sppp,0,sizeof(sppp));
memset(&sppp2,0,sizeof(sppp2));
memset(&sppp3,0,sizeof(sppp3));
sppp.dwSize=sizeof(sppp);
sppp2.dwSize=sizeof(sppp2);
sppp3.dwSize=sizeof(sppp3);
//锁定表面
lps1->Lock(NULL, &sppp, DDLOCK_WAIT, NULL);
lps2->Lock(NULL, &sppp2, DDLOCK_WAIT, NULL);
lps3->Lock(NULL, &sppp3, DDLOCK_WAIT, NULL);
//获取像素
USHORT* pM3 = (USHORT*)sppp3.lpSurface;
USHORT* pM2 = (USHORT*)sppp2.lpSurface;
USHORT* pM = (USHORT*)sppp.lpSurface;
int lp3=(int)sppp3.lPitch/2;
int lp2=(int)sppp2.lPitch/2;
int lp=(int)sppp.lPitch/2;
for(int t=0;t<sfa.bottom-sfa.top;++t)
for(int j=0;j<sfa.right-sfa.left;++j)
{
R1= pM[j+t*lp] >> 11;
G1= (pM[j+t*lp] & 2016)>>5 ;
B1= pM[j+t*lp] & 31;
R2= pM2[j+sfa.left+(t+sfa.top)*lp2] >> 11;
G2= (pM2[j+sfa.left+(t+sfa.top)*lp2] & 2016)>>5 ;
B2= pM2[j+sfa.left+(t+sfa.top)*lp2] & 31;
//计算16位565格式位图的混合结果
R=( AlphaValue * ( R2 - R1 ) /255) + R1;
G=( AlphaValue * ( G2 - G1 ) /255) + G1;
B=( AlphaValue * ( B2 - B1 ) /255) + B1;
USHORT st = (B & 31) + ((G & 63) << 5) + ((R & 31) << 11);
pM3[j+sfa.left+(t+sfa.top)*lp3]=st;
}
//释放表面
lps3->Unlock(0);
lps2->Unlock(0);
lps1->Unlock(0);
}