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: //-----------------------------------------------------------------------------
0010: // Error codes
0011: //-----------------------------------------------------------------------------
0012: enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
0013: 
0014: #define D3DAPPERR_NODIRECT3D          0x82000001
0015: #define D3DAPPERR_NOWINDOW            0x82000002
0016: #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
0017: #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
0018: #define D3DAPPERR_NOHARDWAREDEVICE    0x82000005
0019: #define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006
0020: #define D3DAPPERR_NOWINDOWEDHAL       0x82000007
0021: #define D3DAPPERR_NODESKTOPHAL        0x82000008
0022: #define D3DAPPERR_NOHALTHISMODE       0x82000009
0023: #define D3DAPPERR_NONZEROREFCOUNT     0x8200000a
0024: #define D3DAPPERR_MEDIANOTFOUND       0x8200000b
0025: #define D3DAPPERR_RESETFAILED         0x8200000c
0026: #define D3DAPPERR_NULLREFDEVICE       0x8200000d
0027: 
0028: 
0029: 
0030: 
0031: //-----------------------------------------------------------------------------
0032: // Name: class CD3DApplication
0033: // Desc: A base class for creating sample D3D9 applications. To create a simple
0034: //       Direct3D application, simply derive this class into a class (such as
0035: //       class CMyD3DApplication) and override the following functions, as 
0036: //       needed:
0037: //          OneTimeSceneInit()    - To initialize app data (alloc mem, etc.)
0038: //          InitDeviceObjects()   - To initialize the 3D scene objects
0039: //          FrameMove()           - To animate the scene
0040: //          Render()              - To render the scene
0041: //          DeleteDeviceObjects() - To cleanup the 3D scene objects
0042: //          FinalCleanup()        - To cleanup app data (for exitting the app)
0043: //          MsgProc()             - To handle Windows messages
0044: //-----------------------------------------------------------------------------
0045: class CD3DApplication
0046: {
0047: protected:
0048:     CD3DEnumeration   m_d3dEnumeration;
0049:     CD3DSettings      m_d3dSettings;
0050: 
0051:     // Internal variables for the state of the app
0052:     bool              m_bWindowed;
0053:     bool              m_bActive;
0054:     bool              m_bDeviceLost;
0055:     bool              m_bMinimized;
0056:     bool              m_bMaximized;
0057:     bool              m_bIgnoreSizeChange;
0058:     bool              m_bDeviceObjectsInited;
0059:     bool              m_bDeviceObjectsRestored;
0060: 
0061:     // Internal variables used for timing
0062:     bool              m_bFrameMoving;
0063:     bool              m_bSingleStep;
0064: 
0065:     // Internal error handling function
0066:     HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
0067: 
0068:     // Internal functions to manage and render the 3D scene
0069:     static bool ConfirmDeviceHelper( D3DCAPS9* pCaps, 
0070:         VertexProcessingType vertexProcessingType, D3DFORMAT backBufferFormat );
0071:     void    BuildPresentParamsFromSettings();
0072:     bool    FindBestWindowedMode( bool bRequireHAL, bool bRequireREF );
0073:     bool    FindBestFullscreenMode( bool bRequireHAL, bool bRequireREF );
0074:     HRESULT ChooseInitialD3DSettings();
0075:     HRESULT Initialize3DEnvironment();
0076:     HRESULT HandlePossibleSizeChange();
0077:     HRESULT Reset3DEnvironment();
0078:     HRESULT ToggleFullscreen();
0079:     HRESULT ForceWindowed();
0080:     HRESULT UserSelectNewDevice();
0081:     void    Cleanup3DEnvironment();
0082:     HRESULT Render3DEnvironment();
0083:     virtual HRESULT AdjustWindowForChange();
0084:     virtual void UpdateStats();
0085: 
0086: protected:
0087:     // Main objects used for creating and rendering the 3D scene
0088:     D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset
0089:     HWND              m_hWnd;              // The main app window
0090:     HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)
0091:     HMENU             m_hMenu;             // App menu bar (stored here when fullscreen)
0092:     LPDIRECT3D9       m_pD3D;              // The main D3D object
0093:     LPDIRECT3DDEVICE9 m_pd3dDevice;        // The D3D rendering device
0094:     D3DCAPS9          m_d3dCaps;           // Caps for the device
0095:     D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer
0096:     DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing
0097:     DWORD             m_dwWindowStyle;     // Saved window style for mode switches
0098:     RECT              m_rcWindowBounds;    // Saved window bounds for mode switches
0099:     RECT              m_rcWindowClient;    // Saved client area size for mode switches
0100: 
0101:     // Variables for timing
0102:     FLOAT             m_fTime;             // Current time in seconds
0103:     FLOAT             m_fElapsedTime;      // Time elapsed since last frame
0104:     FLOAT             m_fFPS;              // Instanteous frame rate
0105:     TCHAR             m_strDeviceStats[90];// String to hold D3D device stats
0106:     TCHAR             m_strFrameStats[90]; // String to hold frame stats
0107: 
0108:     // Overridable variables for the app
0109:     TCHAR*            m_strWindowTitle;    // Title for the app's window
0110:     DWORD             m_dwCreationWidth;   // Width used to create window
0111:     DWORD             m_dwCreationHeight;  // Height used to create window
0112:     bool              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
0113:     bool              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
0114:     bool              m_bStartFullscreen;  // Whether to start up the app in fullscreen mode
0115: 
0116:     // Overridable functions for the 3D scene created by the app
0117:     virtual HRESULT ConfirmDevice(D3DCAPS9*,DWORD,D3DFORMAT)   { return S_OK; }
0118:     virtual HRESULT OneTimeSceneInit()                         { return S_OK; }
0119:     virtual HRESULT InitDeviceObjects()                        { return S_OK; }
0120:     virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }
0121:     virtual HRESULT FrameMove()                                { return S_OK; }
0122:     virtual HRESULT Render()                                   { return S_OK; }
0123:     virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }
0124:     virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }
0125:     virtual HRESULT FinalCleanup()                             { return S_OK; }
0126: 
0127: public:
0128:     // Functions to create, run, pause, and clean up the application
0129:     virtual HRESULT Create( HINSTANCE hInstance );
0130:     virtual INT     Run();
0131:     virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
0132:     virtual void    Pause( bool bPause );
0133:     virtual         ~CD3DApplication()                         { }
0134: 
0135:     // Internal constructor
0136:     CD3DApplication();
0137: };
0138: 
0139: 
0140: 
0141: 
0142: #endif
0143: 
0144: 
0145: 
0146: