📄 dxmember.cpp
字号:
/********************************************************************
created: 2001/05/14
created: 14:5:2001 21:55
filename: .\dxmember.cpp
file path: ..\stonepic
file base: dxmember
file ext: cpp
author: 程式浪人
purpose:
*********************************************************************/
#include "stdafx.h"
//本程序使用的可能会重复包含的内容
#include "ddraw.h"
#include "ddutil.h"
#include "DXMember.h"
#define MULTI_TO_WIDE( x,y ) MultiByteToWideChar( CP_ACP, \
MB_PRECOMPOSED, y, -1, x, _MAX_PATH );
//////////////////////////////////////////////////////////
//------------------------数据开始----------------------//
//DDRAW
/*定制三个后台缓冲页,一个放主地图,一个小地图,一个放纹理*/
LPDIRECTDRAW7 g_lpDD7=NULL;
LPDIRECTDRAWSURFACE7 g_lpFrontBuffer=NULL;//前台页面
LPDIRECTDRAWSURFACE7 g_lpBackBuffer=NULL;//后台缓冲
LPDIRECTDRAWCLIPPER g_lpDDClipper=NULL;// DirectDraw clipper.
DWORD g_dwSurfaceBits=0;//当前颜色格式(8位,16位,24位或是32位)
BOOL g_bCanRotation90=FALSE;//显卡是否支持90度图面旋转
//////////////////////////////////////////////////////////////
//-------------------------DDRAW部分------------------------//
//////////////////////////////////////////////////////////////
//-------------------------------------------------------------------
// Name: InitDDraw
//
// Note: Create the DirectDraw object,primary surface,offscreen
// surfaces and the clipper
//
// In: hWnd handle of the main window
// dwX width of the offscreen surface
// dwY height of the offscreen surface
//
// Return: return false when failed in create the objects
//-------------------------------------------------------------------
extern BOOL InitDDraw(HWND hWnd, const DWORD dwX, const DWORD dwY)
{
HRESULT ddrval;
//创建之前检查分辩率
HDC hdc;
hdc=::GetDC(NULL);//获得屏幕DC
//检查分辨率,如果太小就退出
if(800>GetDeviceCaps(hdc,HORZRES)||600>GetDeviceCaps(hdc,VERTRES))
{
// Msg("程序需要至少800*600的桌面下运行,请先调整分辨率再运行此程序");
// return FALSE;
}
g_dwSurfaceBits = GetDeviceCaps(hdc, BITSPIXEL);
// if(g_dwSurfaceBits!=16)
// {
// Msg("程序至少需要在16位色桌面环境运行,请先调整颜色数再运行此程序");
// return FALSE;
// }
::ReleaseDC (NULL, hdc);
///////////第一步:创建DDRAW对象///////////////
ddrval=DirectDrawCreateEx(NULL,(VOID**)&g_lpDD7,IID_IDirectDraw7,NULL);
if(ddrval!=DD_OK)
{
Msg("Fail to create DirectDraw!");
return FALSE;
}
////第三步:将窗口行为模式设置为NORMAL模式,也就是窗口模式////
ddrval=g_lpDD7->SetCooperativeLevel(hWnd,DDSCL_NORMAL);
if(ddrval!=DD_OK)
{
Msg("Fail to set cooperation level!");
return FALSE;
}
////////////////第四步:创建一个主页面///////////////////
DDSURFACEDESC2 ddsd;//DDSURFACE4需要使用DDSURFACEDESC2结构
::ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
//表示准备使用DDSD_CAPS成员,NORMAL模式不能创建后台缓冲页进行FLIP交换
ddsd.dwFlags=DDSD_CAPS;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
ddrval=g_lpDD7->CreateSurface(&ddsd,&g_lpFrontBuffer,NULL);
if(ddrval!=DD_OK)
{
Msg("Fail to create primary surface,error No.=%d",ddrval);
return FALSE;
}
//////接下来:创建一个后台缓冲/////////////////
ZeroMemory(&ddsd,sizeof(DDSURFACEDESC2));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth = dwX;//后缓冲的大小
ddsd.dwHeight =dwY;
ddrval = g_lpDD7->CreateSurface(&ddsd, &g_lpBackBuffer, NULL);
if (ddrval != DD_OK)
{
Msg("Fail to create offscreen surface, error No.=%d",ddrval);
return FALSE;
}
CRect rect(0, 0 ,dwX, dwY);
FillColor (g_lpBackBuffer, RGB(0, 0, 0), rect);
//////////////////////////////////////////////////////////////////
///最后,创建剪切块对象与窗口相关,这样用BLT时不会覆盖其他窗口
//////////////////////////////////////////////////////////////////
if ( FAILED( g_lpDD7->CreateClipper( 0, &g_lpDDClipper, NULL ) ) )
{
Msg("Fail to create the clipper!"); // 将它与主页面相联
return FALSE;
}
if ( FAILED( g_lpDDClipper->SetHWnd( 0, hWnd ) ) )
{
Msg("Fail to set window handle to the clipper");
return FALSE;
}
if ( FAILED( g_lpFrontBuffer->SetClipper( g_lpDDClipper ) ) )
{
Msg("Fail to set the clipper to primary surface");
return FALSE;
}
g_lpDDClipper->Release();
//最后检测显卡是否支持90读图面旋转
// DDCAPS HALCaps;
// HALCaps.dwSize = sizeof(DDCAPS);
// g_lpDD7->GetCaps(&HALCaps, NULL);
// if (HALCaps.dwFXCaps & DDFXCAPS_BLTROTATION90)
// g_bCanRotation90 = TRUE;
// else
// g_bCanRotation90 = FALSE;
return TRUE;//全部通过
}
extern BOOL ReleaseDDraw()
{
if(NULL!=g_lpBackBuffer) //释放后缓冲
{
g_lpBackBuffer->Release();
}
if(NULL!=g_lpFrontBuffer) //释放前缓冲
g_lpFrontBuffer->Release();
if (NULL!=g_lpDD7)
g_lpDD7->Release();
return TRUE;
}
//////////////////////////////////////////////////////////////////
//-----------------------------OTHERS----------------------------
//////////////////////////////////////////////////////////////////
//---------这个函数制作一个简单的错误信息显示函数----------//
void Msg(LPSTR fmt,...)
{
char buff[256];
va_list va;
lstrcpy(buff,"DirectX:");
va_start(va,fmt);
wvsprintf(&buff[lstrlen(buff)],fmt,va);
va_end(va);
lstrcat(buff,"\r\n");
AfxMessageBox(buff);
}
//为矩形填充颜色
BOOL FillColor(IDirectDrawSurface7 *lpSurface,COLORREF color,CRect &rcFill)
{
if(NULL==lpSurface)//查错
return FALSE;
//初始化数据结构,至少要将第一个成员填入
DDBLTFX ddbltfx;
HRESULT ddrval;
ddbltfx.dwSize=sizeof(ddbltfx);
ddbltfx.dwFillColor=color;
ddrval=lpSurface->Blt(
&rcFill,//dest rect
NULL,//src surface
&rcFill,//src rect
DDBLT_WAIT|DDBLT_COLORFILL,
&ddbltfx);
if(ddrval!=DD_OK)
return FALSE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -