0001: //-----------------------------------------------------------------------------
0002: // File: D3DApp.h
0003: //
0004: // Desc: Application class for the Direct3D samples framework library.
0005: //-----------------------------------------------------------------------------
0006: #ifndef D3DAPP_H
0007: #define D3DAPP_H
0008: 
0009: // basetsd.h defines INT_PTR (used below).  It is not included by default
0010: // under VC 5.0.  If you are using VC6 or later, it is included via Windows.h.
0011: #include <basetsd.h> 
0012: 
0013: 
0014: //-----------------------------------------------------------------------------
0015: // Error codes
0016: //-----------------------------------------------------------------------------
0017: enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
0018: 
0019: #define D3DAPPERR_NODIRECT3D          0x82000001
0020: #define D3DAPPERR_NOWINDOW            0x82000002
0021: #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
0022: #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
0023: #define D3DAPPERR_NOHARDWAREDEVICE    0x82000005
0024: #define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006
0025: #define D3DAPPERR_NOWINDOWEDHAL       0x82000007
0026: #define D3DAPPERR_NODESKTOPHAL        0x82000008
0027: #define D3DAPPERR_NOHALTHISMODE       0x82000009
0028: #define D3DAPPERR_NONZEROREFCOUNT     0x8200000a
0029: #define D3DAPPERR_MEDIANOTFOUND       0x8200000b
0030: #define D3DAPPERR_RESIZEFAILED        0x8200000c
0031: #define D3DAPPERR_NULLREFDEVICE       0x8200000d
0032: 
0033: 
0034: 
0035: 
0036: //-----------------------------------------------------------------------------
0037: // Name: struct D3DModeInfo
0038: // Desc: Structure for holding information about a display mode
0039: //-----------------------------------------------------------------------------
0040: struct D3DModeInfo
0041: {
0042:     DWORD      Width;      // Screen width in this mode
0043:     DWORD      Height;     // Screen height in this mode
0044:     D3DFORMAT  Format;     // Pixel format in this mode
0045:     DWORD      dwBehavior; // Hardware / Software / Mixed vertex processing
0046:     D3DFORMAT  DepthStencilFormat; // Which depth/stencil format to use with this mode
0047: };
0048: 
0049: 
0050: 
0051: 
0052: //-----------------------------------------------------------------------------
0053: // Name: struct D3DDeviceInfo
0054: // Desc: Structure for holding information about a Direct3D device, including
0055: //       a list of modes compatible with this device
0056: //-----------------------------------------------------------------------------
0057: struct D3DDeviceInfo
0058: {
0059:     // Device data
0060:     D3DDEVTYPE   DeviceType;      // Reference, HAL, etc.
0061:     D3DCAPS8     d3dCaps;         // Capabilities of this device
0062:     const TCHAR* strDesc;         // Name of this device
0063:     BOOL         bCanDoWindowed;  // Whether this device can work in windowed mode
0064: 
0065:     // Modes for this device
0066:     DWORD        dwNumModes;
0067:     D3DModeInfo  modes[150];
0068: 
0069:     // Current state
0070:     DWORD        dwCurrentMode;
0071:     BOOL         bWindowed;
0072:     D3DMULTISAMPLE_TYPE MultiSampleTypeWindowed;
0073:     D3DMULTISAMPLE_TYPE MultiSampleTypeFullscreen;
0074: };
0075: 
0076: 
0077: 
0078: 
0079: //-----------------------------------------------------------------------------
0080: // Name: struct D3DAdapterInfo
0081: // Desc: Structure for holding information about an adapter, including a list
0082: //       of devices available on this adapter
0083: //-----------------------------------------------------------------------------
0084: struct D3DAdapterInfo
0085: {
0086:     // Adapter data
0087:     D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
0088:     D3DDISPLAYMODE d3ddmDesktop;      // Desktop display mode for this adapter
0089: 
0090:     // Devices for this adapter
0091:     DWORD          dwNumDevices;
0092:     D3DDeviceInfo  devices[5];
0093: 
0094:     // Current state
0095:     DWORD          dwCurrentDevice;
0096: };
0097: 
0098: 
0099: 
0100: 
0101: //-----------------------------------------------------------------------------
0102: // Name: class CD3DApplication
0103: // Desc: A base class for creating sample D3D8 applications. To create a simple
0104: //       Direct3D application, simply derive this class into a class (such as
0105: //       class CMyD3DApplication) and override the following functions, as 
0106: //       needed:
0107: //          OneTimeSceneInit()    - To initialize app data (alloc mem, etc.)
0108: //          InitDeviceObjects()   - To initialize the 3D scene objects
0109: //          FrameMove()           - To animate the scene
0110: //          Render()              - To render the scene
0111: //          DeleteDeviceObjects() - To cleanup the 3D scene objects
0112: //          FinalCleanup()        - To cleanup app data (for exitting the app)
0113: //          MsgProc()             - To handle Windows messages
0114: //-----------------------------------------------------------------------------
0115: class CD3DApplication
0116: {
0117: protected:
0118:     // Internal variables for the state of the app
0119:     D3DAdapterInfo    m_Adapters[10];
0120:     DWORD             m_dwNumAdapters;
0121:     DWORD             m_dwAdapter;
0122:     BOOL              m_bWindowed;
0123:     BOOL              m_bActive;
0124:     BOOL              m_bReady;
0125:     BOOL              m_bHasFocus;
0126: 
0127:     // Internal variables used for timing
0128:     BOOL              m_bFrameMoving;
0129:     BOOL              m_bSingleStep;
0130: 
0131:     // Internal error handling function
0132:     HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
0133: 
0134:     // Internal functions to manage and render the 3D scene
0135:     HRESULT BuildDeviceList();
0136:     BOOL    FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
0137:                 D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
0138:     HRESULT Initialize3DEnvironment();
0139:     HRESULT Resize3DEnvironment();
0140:     HRESULT ToggleFullscreen();
0141:     HRESULT ForceWindowed();
0142:     HRESULT UserSelectNewDevice();
0143:     VOID    Cleanup3DEnvironment();
0144:     HRESULT Render3DEnvironment();
0145:     virtual HRESULT AdjustWindowForChange();
0146:     static INT_PTR CALLBACK SelectDeviceProc( HWND hDlg, UINT msg, 
0147:                 WPARAM wParam, LPARAM lParam );
0148: 
0149: protected:
0150:     // Main objects used for creating and rendering the 3D scene
0151:     D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset
0152:     HWND              m_hWnd;              // The main app window
0153:     HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)
0154:     LPDIRECT3D8       m_pD3D;              // The main D3D object
0155:     LPDIRECT3DDEVICE8 m_pd3dDevice;        // The D3D rendering device
0156:     D3DCAPS8          m_d3dCaps;           // Caps for the device
0157:     D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer
0158:     DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing
0159:     DWORD             m_dwWindowStyle;     // Saved window style for mode switches
0160:     RECT              m_rcWindowBounds;    // Saved window bounds for mode switches
0161:     RECT              m_rcWindowClient;    // Saved client area size for mode switches
0162: 
0163:     // Variables for timing
0164:     FLOAT             m_fTime;             // Current time in seconds
0165:     FLOAT             m_fElapsedTime;      // Time elapsed since last frame
0166:     FLOAT             m_fFPS;              // Instanteous frame rate
0167:     TCHAR             m_strDeviceStats[90];// String to hold D3D device stats
0168:     TCHAR             m_strFrameStats[90]; // String to hold frame stats
0169: 
0170:     // Overridable variables for the app
0171:     TCHAR*            m_strWindowTitle;    // Title for the app's window
0172:     BOOL              m_bUseDepthBuffer;   // Whether to autocreate depthbuffer
0173:     DWORD             m_dwMinDepthBits;    // Minimum number of bits needed in depth buffer
0174:     DWORD             m_dwMinStencilBits;  // Minimum number of bits needed in stencil buffer
0175:     DWORD             m_dwCreationWidth;   // Width used to create window
0176:     DWORD             m_dwCreationHeight;  // Height used to create window
0177:     BOOL              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
0178:     BOOL              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
0179: 
0180:     // Overridable functions for the 3D scene created by the app
0181:     virtual HRESULT ConfirmDevice(D3DCAPS8*,DWORD,D3DFORMAT)   { return S_OK; }
0182:     virtual HRESULT OneTimeSceneInit()                         { return S_OK; }
0183:     virtual HRESULT InitDeviceObjects()                        { return S_OK; }
0184:     virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }
0185:     virtual HRESULT FrameMove()                                { return S_OK; }
0186:     virtual HRESULT Render()                                   { return S_OK; }
0187:     virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }
0188:     virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }
0189:     virtual HRESULT FinalCleanup()                             { return S_OK; }
0190: 
0191: public:
0192:     // Functions to create, run, pause, and clean up the application
0193:     virtual HRESULT Create( HINSTANCE hInstance );
0194:     virtual INT     Run();
0195:     virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
0196:     virtual VOID    Pause( BOOL bPause );
0197: 
0198:     // Internal constructor
0199:     CD3DApplication();
0200: };
0201: 
0202: 
0203: 
0204: 
0205: #endif
0206: 
0207: 
0208: 
0209: