AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

用MFC构造DirectX应用框架

51自学网 2015-08-30 http://www.wanshiok.com

 

附录:

文件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:

LPDERECTDRAW lpDD; //DirectDraw对象指针

LPDERECTDRAWSURFACE lpFrontBuffer;//DirectDraw主缓冲区

LPDERECTDRAWSURFACE lpBacdBuffer;//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(CderectXWnd)

afx-msg void OnActivateApp(BOOL bActive, HTASK hTask);

//}}AFX-MSG

DECLARE-MESSAGE-MAP()

};

////////////////////////

//{{AFX-INSERT-LOCATION}}

#endif//!defined(DERECTXWND-H)

文件DirectXWnd.CPP

//DirectXWnd.cpp:implementation file

#include”stdafx.h”

#include”DirectX.h”

#include”DirectXWnd.h”

#ifdef-DEUG

#define new DEBUG-NEW

#undef THIS –FILE

static char THIS –FILE[]=--FILE--;

#endif

CDirectXWnd::CdirectWXnd()

{

lpDD=NULL;

lpFrontBuffer=NULL;

lpBackBuffer=NULL;

nBufferCount=0;

blsActive=TRUE;

}

CDirectXWnd::~CdirectXWnd()

{

if(lpDD){

CleanSurface();

lpDD->Release();

lpDD=NULL;

}

}

BOOL CdirectXWnd::Create(LPCSTR IpszAppName)

{

CString className=AfxRegisterWndClass(CS-DBLCLKS,::LoadCursor(NULL,IDC-ARRWINDOW, className,IpszAppName,

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;

}

ddral=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”,ddrval0;

return FALSE;

}

//check capabilites

DDCAPS ddcaps;

ddcaps.dwSize=sizeof(ddcaps);

ddrval=lpDD->GetCaps(&ddcaps,NULL);

if(ddrval!=DD-OK){

Msg(“SetDisplayMode 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;

}

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 CDirectSWnd: :UpdateFrame()

{

}

BEGIN_MESSAGE_MAP(CdirectXWnd,CWnd)

//{{AFX_MSG_MAP(CdirectXWnd)

ON_WM_KEYDOWN()

ON_WM_ACTIVATEAPP()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////

//CDirectXWnd message gandlers

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;

}

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:DirectDraw&nbsp;游戏编程基础(4)  下一篇:用DirectDraw编写动画程序