www.pudn.com > terrainSimple.rar > d3dapp.h


//----------------------------------------------------------------------------- 
// File: D3DApp.h 
// 
// Desc: Application class for the Direct3D samples framework library. 
// 
// Copyright (c) Microsoft Corporation. All rights reserved. 
//----------------------------------------------------------------------------- 
#ifndef D3DAPP_H 
#define D3DAPP_H 
 
//----------------------------------------------------------------------------- 
//错误代码 
//----------------------------------------------------------------------------- 
enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF }; 
 
#define D3DAPPERR_NODIRECT3D          0x82000001 
#define D3DAPPERR_NOWINDOW            0x82000002 
#define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003 
#define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004 
#define D3DAPPERR_NOHARDWAREDEVICE    0x82000005 
#define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006 
#define D3DAPPERR_NOWINDOWEDHAL       0x82000007 
#define D3DAPPERR_NODESKTOPHAL        0x82000008 
#define D3DAPPERR_NOHALTHISMODE       0x82000009 
#define D3DAPPERR_NONZEROREFCOUNT     0x8200000a 
#define D3DAPPERR_MEDIANOTFOUND       0x8200000b 
#define D3DAPPERR_RESETFAILED         0x8200000c 
#define D3DAPPERR_NULLREFDEVICE       0x8200000d 
 
 
 
 
//----------------------------------------------------------------------------- 
// Name: class CD3DApplication 
// Desc: 一个创建D3D9应用程序的基础类。 要创建D3D9应用程序只要简单的从此类派生一个类 
//       (比如class CMyD3DApplication) 然后按需要覆盖以下函数: 
//          OneTimeSceneInit()    - 用于初始化应用程序数据 (alloc mem, etc.) 
//          InitDeviceObjects()   - 用于初始化场景对象 
//          FrameMove()           - 用于变换场景 
//          Render()              - 用于渲染场景 
//          DeleteDeviceObjects() - 用于清除3D场景对象 
//          FinalCleanup()        - 用于清除应用数据 (退出应用程序时) 
//          MsgProc()             - 捕获WINDOWS消息 
//----------------------------------------------------------------------------- 
class CD3DApplication 
{ 
protected: 
    CD3DEnumeration   m_d3dEnumeration; 
    CD3DSettings      m_d3dSettings; 
 
    //用于应用程序状态的内部属性 
    bool              m_bWindowed; 
    bool              m_bActive; 
    bool              m_bDeviceLost; 
    bool              m_bMinimized; 
    bool              m_bMaximized; 
    bool              m_bIgnoreSizeChange; 
    bool              m_bDeviceObjectsInited; 
    bool              m_bDeviceObjectsRestored; 
 
    // 用于调速的内部属性 
    bool              m_bFrameMoving; 
    bool              m_bSingleStep; 
 
    // 内部错误捕获函数 
    HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType ); 
 
    // 用于管理和渲染3D场景的内部函数 
    static bool ConfirmDeviceHelper( D3DCAPS9* pCaps, VertexProcessingType vertexProcessingType,  
		D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat ); 
    void    BuildPresentParamsFromSettings(); 
    bool    FindBestWindowedMode( bool bRequireHAL, bool bRequireREF ); 
    bool    FindBestFullscreenMode( bool bRequireHAL, bool bRequireREF ); 
    HRESULT ChooseInitialD3DSettings(); 
    HRESULT Initialize3DEnvironment(); 
    HRESULT HandlePossibleSizeChange(); 
    HRESULT Reset3DEnvironment(); 
    HRESULT ToggleFullscreen(); 
    HRESULT ForceWindowed(); 
    HRESULT UserSelectNewDevice(); 
    void    Cleanup3DEnvironment(); 
    HRESULT Render3DEnvironment(); 
    virtual HRESULT AdjustWindowForChange(); 
    virtual void UpdateStats(); 
 
protected: 
    //用于创建和渲染场景的一些主要对象 
    D3DPRESENT_PARAMETERS m_d3dpp;         // 用于创建设备/重置的属性 
    HWND              m_hWnd;              // The main app window 
    HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd) 
    HMENU             m_hMenu;             // App menu bar (stored here when fullscreen) 
    LPDIRECT3D9       m_pD3D;              // The main D3D object 
    LPDIRECT3DDEVICE9 m_pd3dDevice;        // The D3D rendering device 
    D3DCAPS9          m_d3dCaps;           // Caps for the device 
    D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer 
    DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing 
    DWORD             m_dwWindowStyle;     // Saved window style for mode switches 
    RECT              m_rcWindowBounds;    // Saved window bounds for mode switches 
    RECT              m_rcWindowClient;    // Saved client area size for mode switches 
 
    // 用于调速的变量 
    FLOAT             m_fTime;             // 以秒为单位的当前时间 
    FLOAT             m_fElapsedTime;      // 从上一帧到现在经过的时间 
    FLOAT             m_fFPS;              // Instanteous frame rate 
    TCHAR             m_strDeviceStats[90];// String to hold D3D device stats 
    TCHAR             m_strFrameStats[90]; // String to hold frame stats 
 
    // 可改写的应用程序变量 
    TCHAR*            m_strWindowTitle;    // Title for the app's window 
    DWORD             m_dwCreationWidth;   // Width used to create window 
    DWORD             m_dwCreationHeight;  // Height used to create window 
    bool              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen 
    bool              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen 
    bool              m_bStartFullscreen;  // Whether to start up the app in fullscreen mode 
 
    //可覆盖函数,用于APP创建3D场景 
    virtual HRESULT ConfirmDevice(D3DCAPS9*,DWORD,D3DFORMAT,D3DFORMAT) { return S_OK; } 
    virtual HRESULT OneTimeSceneInit()                         { return S_OK; } 
    virtual HRESULT InitDeviceObjects()                        { return S_OK; } 
    virtual HRESULT RestoreDeviceObjects()                     { return S_OK; } 
    virtual HRESULT FrameMove()                                { return S_OK; } 
    virtual HRESULT Render()                                   { return S_OK; } 
    virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; } 
    virtual HRESULT DeleteDeviceObjects()                      { return S_OK; } 
    virtual HRESULT FinalCleanup()                             { return S_OK; } 
 
public: 
    //用于创建、运行、暂停和清除应用程序的函数 
    virtual HRESULT Create( HINSTANCE hInstance ); 
    virtual INT     Run(); 
    virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); 
    virtual void    Pause( bool bPause ); 
    virtual         ~CD3DApplication()                         { } 
 
    // 构造函数 
    CD3DApplication(); 
}; 
 
 
 
 
#endif