📄 dd.cpp
字号:
#include "stdafx.h"
#include "DD.h"
#include "ddutil.h"
//////////////////////////////////////////////////////////////////////////
//
CDD::CDD()
{
//add some special code here?
}
CDD::~CDD()
{
Destroy();
}
//////////////////////////////////////////////////////////////////////////
//
CDD::CDD(HWND hwnd,UINT uScreenWidth,UINT uScreenHeight,UINT uColorDepth,LPCSTR szSourceBitmap,COLORREF rgbTran,COLORREF rgbBK)
{
Initialize(hwnd,uScreenWidth,uScreenHeight,uColorDepth,szSourceBitmap,rgbTran,rgbBK);
}
/////////////////////////////////////////////////////////////////////////////
//DirectDraw object initilize:
bool CDD::Initialize(HWND hwnd,UINT uScreenWidth,UINT uScreenHeight,UINT uColorDepth,LPCSTR szSourceBitmap,COLORREF rgbTran,COLORREF rgbBK)
{
HRESULT ddrval;
m_nScreenHeight=uScreenHeight;//全屏幕的长宽;
m_nScreenWidth=uScreenWidth;
m_szSourceBitmap=szSourceBitmap;
//(1)Now Create the main DirectDraw object:
ddrval=DirectDrawCreate(NULL,&m_lpDD,NULL); //创建主DD对象在m_lpDD中;
if(ddrval!=DD_OK)goto Destroy;
//(2)Get EXCLUSIVE mode--(2):独占、全屏模式:
ddrval=m_lpDD->SetCooperativeLevel(hwnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN);
if(ddrval!=DD_OK)goto Destroy;
//(3)Set Display mode:
ddrval=m_lpDD->SetDisplayMode(uScreenWidth,uScreenHeight,uColorDepth);
if(ddrval!=DD_OK)goto Destroy;
//(4.1)Create the Primary Surface with 1 back buffer:
DDSURFACEDESC ddsd;
ddsd.dwSize=sizeof(ddsd);
//ddsd.dwRefreshRate=0;
ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount=1; //一个后备表面;
ddrval=m_lpDD->CreateSurface(&ddsd,&m_lpDDS_Primary,NULL);//创建主表面对象在m_lpDD_Primary;
if(ddrval!=DD_OK)goto Destroy;
//(4.2)Get a pointer to the buffer:
DDSCAPS ddscaps;
ddscaps.dwCaps=DDSCAPS_BACKBUFFER;
ddrval=m_lpDDS_Primary->GetAttachedSurface(&ddscaps,&m_lpDDS_Back);//创建主表面的后备表面在m_lpDD_Back;
if(ddrval!=DD_OK)goto Destroy;
//(5)load the image:
m_lpDDS_Source=DDLoadBitmap(m_lpDD,m_szSourceBitmap,0,0);
if(m_lpDDS_Source==NULL)goto Destroy;
//(6)Set Color RGB(0,0,0) as the color key:
ddrval=DDSetColorKey(m_lpDDS_Source,rgbTran);
if(ddrval!=DD_OK)goto Destroy;
if(!SetBkColor(rgbBK))goto Destroy;
return true;
Destroy:
Destroy();
DestroyWindow(hwnd);
ErrorCheck(ddrval);
return false;
}
///////////////////////////////////////////////////////////////////////////
//Destroy Direct Draw object:
void CDD::Destroy(void)
{
if(m_lpDD!=NULL)
{
if(m_lpDDS_Primary!=NULL)
{
m_lpDDS_Primary->Release();
m_lpDDS_Primary=NULL;
}
m_lpDD->Release();
m_lpDD=NULL;
}
}
///////////////////////////////////////////////////////////////////////////
//Restore All DirectDraw Objects:
HRESULT CDD::RestoreSurface(void)
{
HRESULT ddrval;
ddrval=m_lpDDS_Primary->Restore();
if(ddrval==DD_OK)
{
ddrval=m_lpDDS_Source->Restore();
if(ddrval==DD_OK)
{
DDReLoadBitmap(m_lpDDS_Source,m_szSourceBitmap);
}
}
return ddrval;
}
///////////////////////////////////////////////////////////////////////////
//Restore All DirectDraw Objects:
HRESULT CDD::ReLoadSourceBitmap(LPCSTR szSourceBitmap)
{
HRESULT ddrval=DD_OK;
m_szSourceBitmap=szSourceBitmap;
//ddrval=m_lpDDS_Primary->Restore();
//if(ddrval==DD_OK)
{
//ddrval=m_lpDDS_Source->Restore();
//if(ddrval==DD_OK)
{
DDReLoadBitmap(m_lpDDS_Source,m_szSourceBitmap);
}
}
return ddrval;
}
/////////////////////////////////////////////////////////////////////////////
//Blit:
bool CDD::BlitSourceToBack(int x,int y,RECT rcRect,int style)
{
bool bBlit=false;
int nShowX=x;
int nShowY=y;
int nHeight=rcRect.bottom-rcRect.top+1;
int nWidth =rcRect.right-rcRect.left+1;
RECT rcShow=rcRect;
////先判断x,y,rect,与屏幕的关系,以确定确实要BLIT的区域及位置:
////X方向;
if(x<=(-nWidth+1))
return true;
if(((-nWidth+1)<x)&&(x<0))//(-nWidth<x<0)
{
nShowX=0;
rcShow.left=rcRect.left-x;
rcShow.right=rcRect.right;
}
if((0<=x)&&(x<=m_nScreenWidth-nWidth))
{
nShowX=x;
rcShow.left=rcRect.left;
rcShow.right=rcRect.right;
}
if((m_nScreenWidth-nWidth<x)&&(x<m_nScreenWidth-1))
{
nShowX=x;
rcShow.left=rcRect.left;
rcShow.right=rcRect.left+(m_nScreenWidth-x)-1;
}
if(x>=m_nScreenWidth-1)
return true;
////Y方向;
if(y<=(-nHeight+1))
return true;
if(((-nHeight+1)<y)&&(y<0))//(-nWidth<x<0)
{
nShowY=0;
rcShow.top=rcRect.top-y;
rcShow.bottom=rcRect.bottom;
goto BLIT;
}
if((0<=y)&&(y<=m_nScreenHeight-nHeight))
{
nShowY=y;
rcShow.top=rcRect.top;
rcShow.bottom=rcRect.bottom;
goto BLIT;
}
if((m_nScreenHeight-nHeight<y)&&(y<m_nScreenHeight-1))
{
nShowY=y;
rcShow.top=rcRect.top;
rcShow.bottom=rcRect.top+(m_nScreenHeight-y)-1;
goto BLIT;
}
if(y>=m_nScreenHeight-1)
return true;
BLIT:;
HRESULT ddrval;
ddrval=m_lpDDS_Back->BltFast(nShowX,nShowY,
m_lpDDS_Source,&rcShow,
style);
while(true)
{
if(ddrval==DD_OK)return true;
if(ddrval==DDERR_SURFACELOST)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -