📄 hddrawsurf.cpp
字号:
// HDDrawSurf.cpp: implementation of the CDDrawSurf class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HDDrawSurf.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDDrawSurf::CDDrawSurf()
{
memset(&m_Dat,0,sizeof(_SURFACE_DATA));
m_Dib=0;
lpSourceSurf=0;
Buffer=0;
}
CDDrawSurf::~CDDrawSurf()
{
if(m_Dib)
{
delete m_Dib;
}
}
//在该函数中创建一个表面:
LPDIRECTDRAWSURFACE7 CDDrawSurf::Create(_SURFACE_DATA * dat)
{
//1. 赋值:
memcpy(&m_Dat,dat,sizeof(m_Dat));
DWORD bytes=m_Dat.Width*m_Dat.Height*(m_Dat.BPP/8);
if(m_Dat.BPP<8||m_Dat.BPP>32||m_Dat.DisplayBPP<8||m_Dat.DisplayBPP>32)
{
return 0;
}
if(m_Dat.lpDD==0)
{
return 0;
}
//2. 创建一个LPDIRECTDRAWSURFACE7表面:
DDSURFACEDESC2 desc;
ZeroMemory( &desc,sizeof(desc));
desc.dwSize = sizeof(desc);
desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
desc.dwWidth = m_Dat.Width;
desc.dwHeight = m_Dat.Height;
desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
HRESULT r=m_Dat.lpDD->CreateSurface(&desc,&lpSourceSurf,NULL);
if (r!=DD_OK)
{
return 0;
}
//3. 获取位图数据缓冲区首地址:
if(m_Dat.m_bFromFile)
{
m_Dib=new CDib();
LoadBmpFromFile();
}
else
{
AssignBuffer();
}
if(Buffer==0)
{
lpSourceSurf=0;
return 0;
}
//4.将位图数据传送到表面对应的内存:
if(m_Dat.BPP==24&&m_Dat.DisplayBPP==32)
{
if(!CopyPixels24to32())
{
SaveInfo("拷贝24未位图信息到32位表面错误.txt","CopyPixels()==0");
lpSourceSurf=0;
return 0;
}
}
else
{
if(!CopyPixels())
{
SaveInfo("拷贝位图信息错误.txt","CopyPixels()==0");
lpSourceSurf=0;
return 0;
}
}
//5.设置色键:
if(!SetColorKey(m_Dat.Colorkey))
{
SaveInfo("设置色键错误.txt","CDDrawSurf::SetColorKey()=FALSE");
lpSourceSurf=0;
return 0;
}
return lpSourceSurf;
}
//问题:应该创建一个函数,即可以用单独的位图文件
// 创建表面,又可以从一幅大位图中提取子画面;
void CDDrawSurf::LoadBmpFromFile()
{
if(!m_Dat.m_bFromFile)
{
Buffer=0;
return;
}
if(m_Dib==0)
{
Buffer=0;
return;
}
m_Dib->LoadBmp(m_Dat.fn);//根据位图文件名载入位图;
m_Dat.BPP=m_Dib->BPP; //读取位图色彩深度;
m_Dat.Bpl=m_Dib->Bpl;
//找到欲提取的数据:
Buffer=FindPos();//找到(xt,yt)像素在内存中的位置;
}
void CDDrawSurf::DestroySurface()
{
}
void CDDrawSurf::AssignBuffer()
{
//如果没有指定位图色彩深度或使用了不支持的位图色彩深度:
if(m_Dat.BPP!=8||m_Dat.BPP!=24)
{
Buffer=0;
return;
}
if(m_Dat.Bpl==0)
{
Buffer=0;
return;
}
Buffer=m_Dat.buf;
}
BYTE * CDDrawSurf::FindPos()
{
//获取位置:
BYTE * pos;
pos=m_Dib->GetBuffer();
pos=pos+m_Dib->Bpl*(m_Dib->Height-1-m_Dat.YS)+m_Dat.XS*m_Dat.BPP/8;
return pos;
}
BOOL CDDrawSurf::CopyPixels()
{
if (lpSourceSurf==0 || Buffer==0)
{
return FALSE;
}
//首先检查给出的坐标和需要截取的位图是否有越界的情况:
DWORD t1=m_Dat.XS+m_Dat.Width;
DWORD t2=m_Dat.YS+m_Dat.Height;
if(t1>m_Dib->Width)
{
m_Dat.Width=m_Dib->Width-m_Dat.XS;
}
if(t2>m_Dib->Height)
{
m_Dat.Height=m_Dib->Height-m_Dat.YS;
}
DWORD len,offset;
len=((m_Dat.Width*m_Dat.BPP)>>3);//截取的子画面的每一行字节宽度;
offset=((m_Dib->Width*m_Dat.BPP)>>3);//pos指针的偏移量,为位图每一行的字节宽度;
DDSURFACEDESC2 desc;
ZeroMemory( &desc, sizeof(desc) );
desc.dwSize = sizeof(desc);
//开始写数据:
HRESULT r=lpSourceSurf->Lock( 0, &desc, DDLOCK_WAIT | DDLOCK_WRITEONLY, 0 );
if (r!=DD_OK)
{
return FALSE;
}
//找到目的地址:
BYTE* surfbuf = (BYTE*)desc.lpSurface;
BYTE * pos = Buffer;
//逐行写入位图数据:
for( unsigned i=0;i<m_Dat.Height;i++)
{
memset(surfbuf,0,desc.lPitch);
memcpy(surfbuf,pos,len);
pos -= offset;
surfbuf += desc.lPitch;
}
//结束写数据:
lpSourceSurf->Unlock( 0 );
return TRUE;
}
//将24位位图拷贝到32位表面:
BOOL CDDrawSurf::CopyPixels24to32()
{
if (lpSourceSurf==0 || Buffer==0)
{
return FALSE;
}
if(m_Dat.BPP!=24)
{
return FALSE;
}
//首先检查给出的坐标和需要截取的位图是否有越界的情况:
DWORD t1=m_Dat.XS+m_Dat.Width;
DWORD t2=m_Dat.YS+m_Dat.Height;
if(t1>m_Dib->Width)
{
m_Dat.Width=m_Dib->Width-m_Dat.XS;
}
if(t2>m_Dib->Height)
{
m_Dat.Height=m_Dib->Height-m_Dat.YS;
}
DDSURFACEDESC2 desc;
ZeroMemory( &desc, sizeof(desc) );
desc.dwSize = sizeof(desc);
DWORD offset;
offset=((m_Dib->Width*m_Dat.BPP)>>3);//pos指针的偏移量,为位图每一行的字节宽度;
//开始写数据:
HRESULT r=lpSourceSurf->Lock( 0, &desc, DDLOCK_WAIT | DDLOCK_WRITEONLY, 0 );
if (r!=DD_OK)
{
return FALSE;
}
//找到目的地址:
BYTE * surfbuf = (BYTE*)desc.lpSurface;
BYTE * pos = Buffer;
BYTE * posline;
//逐行逐列写入位图数据:
for( unsigned i=0;i<m_Dat.Height;i++)
{
memset(surfbuf,1,desc.lPitch);
posline=pos;
for(unsigned j=0;j<m_Dat.Width;j++)
{
memcpy(surfbuf,posline,3);
posline+=3;
surfbuf+=4;
}
pos-=offset;
}
//结束写数据:
lpSourceSurf->Unlock( 0 );
return TRUE;
}
BOOL CDDrawSurf::Blit(int x,int y,unsigned uck)
{
if(lpSourceSurf==0||m_Dat.lpDestSurf==0)
{
return 0;
}
BOOL bfastblt=TRUE;
DDSURFACEDESC2 destdesc;
ZeroMemory( &destdesc, sizeof(destdesc) );
destdesc.dwSize = sizeof(destdesc);
m_Dat.lpDestSurf->GetSurfaceDesc( &destdesc );
RECT destrect;
destrect.left=0;
destrect.top=0;
destrect.right=destdesc.dwWidth;
destrect.bottom=destdesc.dwHeight;
DDSURFACEDESC2 srcdesc;
ZeroMemory( &srcdesc, sizeof(srcdesc) );
srcdesc.dwSize = sizeof(srcdesc);
lpSourceSurf->GetSurfaceDesc( &srcdesc );
RECT srcrect;
srcrect.left=0;
srcrect.top=0;
srcrect.right=srcdesc.dwWidth;
srcrect.bottom=srcdesc.dwHeight;
//保证贴图在目标表面之内:
if (x+srcrect.left>=destrect.right)
{
return FALSE;
}
if (y+srcrect.top>=destrect.bottom)
{
return FALSE;
}
if (x+srcrect.right<=destrect.left)
{
return FALSE;
}
if (y+srcrect.bottom<=destrect.top)
{
return FALSE;
}
//如果源表面有超出目标表面的情况,则进行裁剪:
if (x+srcrect.right>destrect.right)
{
srcrect.right-=x+srcrect.right-destrect.right;
}
if (y+srcrect.bottom>destrect.bottom)
{
srcrect.bottom-=y+srcrect.bottom-destrect.bottom;
}
RECT dr;
if (x<0)
{
srcrect.left=-x;
x=0;
dr.left=x;
dr.top=y;
dr.right=x+(srcrect.right-srcrect.left);
dr.bottom=y+(srcrect.bottom-srcrect.top);
bfastblt=FALSE;
}
if (y<0)
{
srcrect.top=-y;
y=0;
dr.left=x;
dr.top=y;
dr.right=x+(srcrect.left-srcrect.left);
dr.bottom=y+(srcrect.bottom-srcrect.top);
bfastblt=FALSE;
}
DWORD flags;
HRESULT re;
if (bfastblt)
{
flags=DDBLTFAST_WAIT;
if (uck)
{
flags |= DDBLTFAST_SRCCOLORKEY;
}
re=m_Dat.lpDestSurf->BltFast( x, y, lpSourceSurf, &srcrect, flags );
if(re!=DD_OK)
{
SaveBltError(re);
return FALSE;
}
}
else
{
flags=DDBLT_WAIT;
if (uck)
{
flags |= DDBLT_KEYSRC;
}
re=m_Dat.lpDestSurf->Blt( &dr, lpSourceSurf, &srcrect, flags, 0 );
if(re!=DD_OK)
{
SaveBltError(re);
return FALSE;
}
}
return TRUE;
}
BOOL CDDrawSurf::SetColorKey(COLORREF clr)
{
COLORREF rgbT;
HDC hdc;
DWORD dw = CLR_INVALID;
DDSURFACEDESC2 ddsd;
HRESULT hres;
if (clr != CLR_INVALID && lpSourceSurf->GetDC(&hdc) == DD_OK)
{
rgbT = GetPixel(hdc, 0, 0); //保存旧的像素点颜色
SetPixel(hdc, 0, 0, clr); //设置COLORREF指定的颜色
lpSourceSurf->ReleaseDC(hdc);
}
ddsd.dwSize = sizeof(ddsd);
while ((hres = lpSourceSurf->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
;
if (hres == DD_OK)
{
dw = *(DWORD *) ddsd.lpSurface;//读取颜色
if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;
lpSourceSurf->Unlock(NULL);
}
if (clr != CLR_INVALID && lpSourceSurf->GetDC(&hdc) == DD_OK)
{
SetPixel(hdc, 0, 0, rgbT);
lpSourceSurf->ReleaseDC(hdc);
}
//设置色键:
DDCOLORKEY ddck;
ddck.dwColorSpaceLowValue = dw;
ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
if(lpSourceSurf->SetColorKey(DDCKEY_SRCBLT, &ddck)!=DD_OK)
{
return FALSE;
}
return TRUE;
}
BOOL CDDrawSurf::Restore()
{
//先恢复表面:
if(lpSourceSurf->Restore()!=DD_OK)
{
SaveInfo("恢复表面失败.txt","Restore()失败!");
return FALSE;
}
//再将位图数据从Buffer中传送到表面对应的内存:
if(m_Dat.BPP==24&&m_Dat.DisplayBPP==32)
{
if(!CopyPixels24to32())
{
SaveInfo("拷贝24位位图信息到32位表面错误.txt","CopyPixels()==0");
return FALSE;
}
}
else
{
if(!CopyPixels())
{
SaveInfo("拷贝位图信息错误.txt","CopyPixels()==0");
return FALSE;
}
}
return TRUE;
}
void CDDrawSurf::SaveBltError(HRESULT re)
{
switch(re)
{
case DDERR_GENERIC:
{
SaveInfo("带色键帖表面失败.txt","DDERR_GENERIC");
break;
}
case DDERR_INVALIDCLIPLIST:
{
SaveInfo("带色键帖表面失败.txt","DDERR_INVALIDCLIPLIST");
break;
}
case DDERR_INVALIDOBJECT:
{
SaveInfo("带色键帖表面失败.txt","DDERR_INVALIDOBJECT ");
break;
}
case DDERR_INVALIDPARAMS:
{
SaveInfo("带色键帖表面失败.txt","DDERR_INVALIDPARAMS ");
break;
}
case DDERR_INVALIDRECT :
{
SaveInfo("带色键帖表面失败.txt","DDERR_INVALIDRECT ");
break;
}
case DDERR_NOALPHAHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOALPHAHW ");
break;
}
case DDERR_NOBLTHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOBLTHW ");
break;
}
case DDERR_NOCLIPLIST :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOCLIPLIST ");
break;
}
case DDERR_NODDROPSHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NODDROPSHW ");
break;
}
case DDERR_NOMIRRORHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOMIRRORHW ");
break;
}
case DDERR_NORASTEROPHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NORASTEROPHW ");
break;
}
case DDERR_NOROTATIONHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOROTATIONHW ");
break;
}
case DDERR_NOSTRETCHHW :
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOSTRETCHHW ");
break;
}
case DDERR_NOZBUFFERHW:
{
SaveInfo("带色键帖表面失败.txt","DDERR_NOZBUFFERHW");
break;
}
case DDERR_SURFACEBUSY :
{
SaveInfo("带色键帖表面失败.txt","DDERR_SURFACEBUSY ");
break;
}
case DDERR_SURFACELOST :
{
SaveInfo("带色键帖表面失败.txt","DDERR_SURFACELOST ");
break;
}
case DDERR_UNSUPPORTED :
{
SaveInfo("带色键帖表面失败.txt","DDERR_UNSUPPORTED ");
break;
}
case DDERR_WASSTILLDRAWING:
{
SaveInfo("带色键帖表面失败.txt","DDERR_WASSTILLDRAWING");
break;
}
};
}
void CDDrawSurf::Delete()
{
if(lpSourceSurf!=0)
{
lpSourceSurf->Release();
lpSourceSurf=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -