用MFC构造DIRECTX应用框架
2010-07-01 20:42:58 来源:WEB开发网4、编译并运行该程序,屏幕上会出现一个白色球在移动。
附录:
文件:DirectXWnd.H
#if!defined(DIRECTXWND_H)
#define DIRECTXWND_H
//DirectXWnd.h:header file
#include<ddraw.h>
#pragma comment(lib,"ddraw.lib")//链接时加入ddraw.lib库
class CDirectXWnd:public CWnd
{
//Construction
public:
CDirectXWnd();
//Attributes
public:
BOOL blsActive;//应用程序是否被激活
protected:
LPDIRECTDRAW lpDD;//DirectDraw对象指针
LPDIRECTDRAWSURFACE lpFrontBuffer;//DirectDraw主缓冲区
LPDIRECTDRAWSURFACE lpBackBuffer;//DirectDraw后台缓冲区
int nBufferCount;//后备缓冲区个数
//Operations
protected:
void Msg(LPSTR fmt,...);
public:
BOOL Create(LPCSTR lpszAppName);//创建窗体
//Overrides
virtual BOOL InitializeGame(UINT GModex,UINT GModeY,UINT GBPP);
virtual BOOL CleanSurface();
virtual void UpdateFrame();
virtual BOOL RestoreSurfaces(void);
//{{AFX_VIRTUAL(CDirectXWnd)
//}}AFX_VIRTUAL
//implementation
public:
virtual ~CDirectXWnd();
//Generated message map functions
protected:
//{{AFX_MSG(CDirectXWnd)
afx_msg void OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags);
afx_msg void OnActivateApp(BOOL bActive,HTASK hTask);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
////////////////////{{AFX_INSERT_LOCATION}}
#endif//!define(DIRECTXWND_H)
文件:DirectXWnd.CPP
//DirectXWnd.cpp:implementation file
#include "stdafx.h"
#include "DirectX.h"
#include "DirectXWnd.h"
#ifdef _DUBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
CDirectXWnd::CDirectXWnd()
{
lpDD=NULL;
lpFrontBuffer=NULL;
lpBackBuffer=NULL;
nBufferCount=0;
blsActive=TRUE;
}
CDirectXWnd::~CDirectXWnd()
{
if(lpDD)
{
CleanSurface();
lpDD->Release();
lpDD=NULL;
}
}
BOOL CDirectXWnd::Create(LPCSTR lpszAppName)
{
CString className=AfxRegisterWndClass(CS_DBLCLKS,::LoadCursor(NULL,IDC_ARROW),
NULL,AfxGetApp()->LoadIcon(IDR_MAINFRAME));
return(CWnd::CreateEx(WS_EX_APPWINDOW,className,lpszAppName,
WS_VISIBLE|WS_SYSMENU|WS_POPUP,0,0,GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),NULL,NULL));
}
BOOL CDirectXWnd::InitializeGame(UINT GModeX,UINT GModeY,UINT GBPP)
{
HRESULT ddrval;
ddrval=DirectDrawCreate(NULL,&lpDD,NULL);
if(ddrval!=DD_OK)
{
Msg("DirectDrawCreate failed err=%d",ddrval);
return FALSE;
}
ddrval=lpDD->SetCooperativeLevel(m_hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN);
if(ddrval!=DD_OK)
{
Msg("SetCooperativeLevel failed err=%d",ddrval);
return FALSE;
}
ddrval=lpDD->SetDisplayMode(GModeX,GModeY,GBPP);
if(ddrval!=DD_OK)
{
Msg("SetDisplayMode failed err=%d",ddrval);
return FALSE;
}
//check capabilites
DDCAPS ddcaps;
ddcaps.dwSize=sizeof(ddcaps);
ddrval=lpDD->GetCaps(&ddcaps,NULL);
if(ddrval!=DD_OK)
{
Msg("GetCaps failed err=%d",ddrval);
return FALSE;
}
if(ddcaps.dwCaps&DDCAPS_NOHARDWARE)
{
Msg("No hardware support at all");
}
//default to double buffered on 1mb,triple buffered
if(nBufferCount==0)
{
if(ddcaps.dwVidMemTotal<=1024L*1024L*(GBPP/8)||GModeX>640)
{
nBufferCount=2;
}
else
{
nBufferCount=3;
}
}
DDSURFACEDESC ddsd;
::ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount=nBufferCount-1;
ddrval=lpDD->CreateSurface(&ddsd,&lpFrontBuffer,NULL);
if(ddrval!=DD_OK)
{
Msg("CreateSurface failed err=%d",ddrval);
return FALSE;
}
else Msg("显示内存允许建立的缓冲区数=%d(最多需要3)",nBufferCount);
DDSCAPS ddscaps;
ddscaps.dwCaps=DDSCAPS_BACKBUFFER;
ddrval=lpFrontBuffer->GetAttachedSurface(&ddscaps,&lpBackBuffer);
if(ddrval!=DD_OK)
{
Msg("GetAttachedsurface failed err=%d",ddrval);
return FALSE;
}
return TRUE;
}
void CDirectXWnd::Msg(LPSTR fmt,...)
{
char buff[256];
va_list va;
lstrcpy(buff,"DirectxWnd:");
va_start(va,fmt);
wvsprintf(&buff[lstrlen(buff)],fmt,va);
va_end(va);
lstrcat(buff,"\r\n");
AfxMessageBox(buff);
}
////////////////////////Virtual Function
BOOL CDirectXWnd::RestoreSurfaces()
{
HRESULT ddrval;
ddrval=lpFrontBuffer->Restore();
if(ddrval!=DD_OK)
return FALSE;
return TRUE;
}
BOOL CDirectXWnd::CleanSurface()
{
if(lpBackBuffer)
{
lpBackBuffer->Release();
lpBackBuffer=NULL;
}
if(lpFrontBuffer)
{
lpFrontBuffer->Release();
lpFrontBuffer=NULL;
}
return TRUE;
}
void CDirectXWnd::UpdateFrame()
{
}
BEGIN_MESSAGE_MAP(CDirectXWnd,CWnd)
//{{AFX_MSG_MAP(CDirectXWnd,Cwnd)
ON_WM_KEYDOWN()
ON_WM_ACTIVATEAPP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////CDirectXWnd message handlers
void CDirectXWnd::OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
{
switch(nChar)
{
case VK_ESCAPE:
case VK_F12:
PostMessage(WM_CLOSE);
break;
}
CWnd::OnKeyDown(nChar,nRepCnt,nFlags);
}
void CDirectXWnd::OnActivateApp(BOOL bActive,HTASK hTask)
{
CWnd::OnActivateApp(bActive,hTask);
blsActive=bActive;
return;
}
更多精彩
赞助商链接