DirectX学习笔记(1)

 1.DirectX Graphics的基本环境的建立

文件组织:

DirectXApp.cpp与DirectXApp.h  用于程序的进入点与窗口建立

DirectXFrame.cpp与DirectXFrame.h 窗口框架程序

myd3d.cpp与myd3d.h DirectX Graphics函数库

(1)DirectXApp.h文件内容

// DirectxApp.h: interface for the DirectxApp class.
//
//

#if !defined(AFX_DIRECTXAPP_H__A4754641_A88A_11D5_BD53_0050BAE96245__INCLUDED_)
#define AFX_DIRECTXAPP_H__A4754641_A88A_11D5_BD53_0050BAE96245__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class DirectxApp : public CWinApp 
{
public:
 BOOL InitInstance();
 DirectxApp();
 virtual ~DirectxApp();
};

#endif // !defined(AFX_DIRECTXAPP_H__A4754641_A88A_11D5_BD53_0050BAE96245__INCLUDED_)

(2)DirectXApp.cpp文件内容

// DirectXApp.cpp: implementation of the DirectXApp class.
//
//

#include "stdafx.h"
#include "DirectXr.h"
#include "DirectXApp.h"
#include "DirectXFrame.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//
DirectXApp DirectX;
DirectXApp::DirectXApp()
{

}

DirectXApp::~DirectXApp()
{

}

DirectXApp::InitInstance()
{
 m_pMainWnd = new DirectXFrame;
 m_pMainWnd->ShowWindow( m_nCmdShow );
 m_pMainWnd->UpdateWindow();
 return true;
}

(3)DirectXFrame.h文件内容

#if !defined(AFX_DIRECTXFRAME_H__FB577E7A_A821_11D5_BD53_0050BAE96245__INCLUDED_)
#define AFX_DIRECTXFRAME_H__FB577E7A_A821_11D5_BD53_0050BAE96245__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// DirectXFrame.h : header file
//

/
// DirectXFrame frame

class DirectXFrame : public CFrameWnd
{
 DECLARE_DYNCREATE(DirectXFrame)
public:
 DirectXFrame();           // protected constructor used by dynamic creation
   
// Attributes
public:

// Operations
public:
 void Render();

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(DirectXFrame)
 protected:
 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
 //}}AFX_VIRTUAL

// Implementation
protected:
 virtual ~DirectXFrame();

 // Generated message map functions
 //{{AFX_MSG(DirectXFrame)
 afx_msg void OnPaint();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DIRECTXFRAME_H__FB577E7A_A821_11D5_BD53_0050BAE96245__INCLUDED_)
(4)DirectXFrame.cpp文件内容

/ DirectXFrame.cpp : implementation file
//

#include "stdafx.h"
#include "DirectXr.h"
#include "DirectXFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// DirectXFrame

IMPLEMENT_DYNCREATE(DirectXFrame, CFrameWnd)

DirectXFrame::DirectXFrame()
{
 RECT rect;
 Create(NULL,"ch07-1:DirectGraphics");
 CClientDC dc(this);
 int width = dc.GetDeviceCaps(HORZRES);
 int height = dc.GetDeviceCaps(VERTRES);
 GetWindowRect( &rect );
 width = ( width - ( rect.right - rect.left ))/2 ;
 height = (height - (rect.bottom - rect.top ))/2 ;
 MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);

}

DirectXFrame::~DirectXFrame()
{
}


BEGIN_MESSAGE_MAP(DirectXFrame, CFrameWnd)
 //{{AFX_MSG_MAP(DirectXFrame)
 ON_WM_PAINT()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// DirectXFrame message handlers


LRESULT DirectXFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 // TODO: Add your specialized code here and/or call the base class
 switch( message )
 {
 case WM_CREATE :
  if( !d3dCreate( m_hWnd , 640 , 480 , true ))
   PostMessage( WM_CLOSE );
 case WM_DESTROY :
  d3dRelease();
  return 0 ;
 }
 return CFrameWnd::WindowProc(message, wParam, lParam);
}

void DirectXFrame::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 Render();
 
}

void DirectXFrame::Render()
{

        d3d_Device->BeginScene();
 d3d_Device->EndScene();
 d3d_Device->Present( NULL , NULL , NULL , NULL );
}
(5)myd3d.h文件内容


#if !defined(__MYD3D_H___)
#define __MYD3D_H___


#include "d3d9.h"

#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"d3d9.lib")


/*//

  全局的d3d函数

/*//
extern LPDIRECT3D9   d3d_3D ;
extern LPDIRECT3DDEVICE9 d3d_Device ;
//初始化函数
void d3dInit();
//清理现场函数
void d3dRelease();
//创建设备函数
BOOL d3dCreate( HWND hWnd , int width , int height , BOOL IsWindow  );
//设置显示设备函数
BOOL d3dSetDisplaySize( HWND hWnd , int width , int height );
//创建3D设备函数
BOOL d3dDeviceCreate( HWND hWnd , BOOL IsWindow );
//将绘图页清空函数
void d3dClear( UINT color = 0 );

#endif
(6)myd3d.cpp文件内容

#include "windows.h"
#include "myd3d.h"

LPDIRECT3D9   d3d_3D ;
LPDIRECT3DDEVICE9 d3d_Device ;


void d3dInit()
{
 d3d_3D = NULL ;
 d3d_Device = NULL ;
}


void d3dRelease()
{
 if( d3d_3D ) d3d_3D->Release();
 if( d3d_Device ) d3d_Device->Release();
}


BOOL d3dCreate( HWND hWnd , int width , int height , BOOL IsWindow )
{

 if( !d3dSetDisplaySize( hWnd , width , height ))
  return false ;

 if( !d3dDeviceCreate( hWnd , IsWindow ))
  return false ;

 d3dClear();

 return true ;
}



BOOL d3dSetDisplaySize( HWND hWnd , int width , int height )
{

 RECT rect ;
 HDC hdc ;

 hdc = GetDC( hWnd );
 rect.left = (GetDeviceCaps( hdc , HORZRES ) - width )/2;
 rect.top = (GetDeviceCaps( hdc , VERTRES ) - height )/2;
 ReleaseDC( hWnd , hdc );
 rect.right = rect.left + width ;
 rect.bottom = rect.top + height ;

 AdjustWindowRectEx( &rect , GetWindowLong( hWnd , GWL_STYLE ) , (GetMenu( hWnd)!=NULL) ,
  GetWindowLong( hWnd , GWL_EXSTYLE) );

 MoveWindow( hWnd , rect.left , rect.top , rect.right - rect.left , rect.bottom - rect.top , true );
 return true ;
}

BOOL d3dDeviceCreate( HWND hWnd , BOOL IsWindow  )
{

 d3d_3D =  Direct3DCreate9( D3D_SDK_VERSION );
 if( !d3d_3D )
  return false ;

 D3DDISPLAYMODE d3ddm ;
 if( d3d_3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT , &d3ddm ) != D3D_OK )
  return false ;
 D3DPRESENT_PARAMETERS d3dpp ;
 memset( &d3dpp , 0 , sizeof( d3dpp ));
 d3dpp.Windowed = IsWindow ;
 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;
 d3dpp.BackBufferFormat = d3ddm.Format ;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.EnableAutoDepthStencil = TRUE;
 d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER ;

 if( d3d_3D->CreateDevice( D3DADAPTER_DEFAULT ,
  D3DDEVTYPE_HAL , hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING ,
  &d3dpp , &d3d_Device ) != D3D_OK )
  return false ;
 return true ;
}

void d3dClear( UINT color )
{
 d3d_Device->Clear( 0 , 0 ,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER , color , 1.0f , 0 );
}

以上就搭建好了DirectX Graphics的框架了。要在其中显示图像什么的,只要添加相应的类就可以了。

转载于:https://www.cnblogs.com/jh0262/archive/2007/11/28/2946813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值