My Project
d3dapp.h
1 #pragma once
2 //-----------------------------------------------------------------------------
3 // File: D3DApp.h
4 //
5 // Desc: Base application class based on the Direct3D samples framework library.
6 //
7 //-----------------------------------------------------------------------------
8 #include "IParaEngineApp.h"
9 
14 {
15 public:
16  virtual ~CD3DApplication() { }
17 
18 protected:
19  // Internal error handling function
20  HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
21 
22  // Internal functions to manage and render the 3D scene
23  static bool ConfirmDeviceHelper( D3DCAPS9* pCaps, VertexProcessingType vertexProcessingType, D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat );
24  bool FindBestWindowedMode( bool bRequireHAL, bool bRequireREF );
25  bool FindBestFullscreenMode( bool bRequireHAL, bool bRequireREF );
26  HRESULT LaunchReadme();
27  HRESULT ChooseInitialD3DSettings();
28  HRESULT Initialize3DEnvironment();
32  HRESULT HandlePossibleSizeChange(bool bUpdateSizeOnly = false);
33  HRESULT Reset3DEnvironment();
34  HRESULT ToggleFullscreen();
35  HRESULT ForceWindowed();
36  HRESULT UserSelectNewDevice();
37  void Cleanup3DEnvironment();
41  HRESULT Render3DEnvironment(bool bForceRender=false);
42  virtual void BuildPresentParamsFromSettings();
43  virtual HRESULT AdjustWindowForChange();
44  virtual void UpdateStats();
45 
46  // Overridable functions for the 3D scene created by the app
47  virtual HRESULT ConfirmDevice(D3DCAPS9*,DWORD,D3DFORMAT,D3DFORMAT) { return S_OK; }
48  virtual HRESULT OneTimeSceneInit() { return S_OK; }
49  virtual HRESULT InitDeviceObjects() { return S_OK; }
50  virtual HRESULT RestoreDeviceObjects() { return S_OK; }
51  virtual HRESULT FrameMove() { return S_OK; }
52  virtual HRESULT Render() { return S_OK; }
53  virtual HRESULT InvalidateDeviceObjects() { return S_OK; }
54  virtual HRESULT DeleteDeviceObjects() { return S_OK; }
55  virtual HRESULT FinalCleanup() { return S_OK; }
57  virtual bool UpdateViewPort();
58 
62  virtual HRESULT DoWork();
63 
64 
65 public:
66  virtual void Pause( bool bPause );
67  virtual bool IsPaused();
68 
70  HRESULT PresentScene();
71 
72  void EnablePassiveRendering( bool bEnable ) { m_bPassiveRendering = bEnable;};
73  bool IsPassiveRenderingEnabled( ) { return m_bPassiveRendering; };
74 
79  void Enable3DRendering(bool bEnable) {m_bEnable3DRendering = bEnable;};
80 
84 
91  void SetRefreshTimer(float fTimeInterval, int nFrameRateControl = 0);
92 
95  float GetRefreshTimer() const;
96 
97  // Functions to create, run, pause, and clean up the application
98  virtual HRESULT Create();
99  virtual HRESULT CreateFromD3D9Device(IDirect3DDevice9* pD3dDevice, IDirect3DSwapChain9* apSwapChain);
100 
101  virtual int Run(HINSTANCE hInstance);
102 
103  ParaEngine::PEAppState GetAppState() {return m_nAppState;}
104  void SetAppState(ParaEngine::PEAppState state) {m_nAppState = state;}
105 
107  bool IsExternalD3DDevice(){return m_bIsExternalD3DDevice;}
108 protected:
109  // Internal constructor
110  CD3DApplication();
111 
112  CD3DEnumeration m_d3dEnumeration;
113  CD3DSettings m_d3dSettings;
114 
115  // Internal variables for the state of the app
116  bool m_bWindowed;
117  bool m_bActive;
118  bool m_bDeviceLost;
119  bool m_bMinimized;
120  bool m_bMaximized;
121  bool m_bIgnoreSizeChange;
122  bool m_bDeviceObjectsInited;
123  bool m_bDeviceObjectsRestored;
124 
125  // Internal variables used for timing
126  bool m_bFrameMoving;
127  bool m_bSingleStep;
132 
135 
138 
139  // Main objects used for creating and rendering the 3D scene
140  D3DPRESENT_PARAMETERS m_d3dpp; // Parameters for CreateDevice/Reset
141  HWND m_hWnd; // The main app window
142  HWND m_hWndFocus; // The D3D focus window (usually same as m_hWnd)
143  HMENU m_hMenu; // App menu bar (stored here when fullscreen)
144  LPDIRECT3D9 m_pD3D; // The main D3D object
145  LPDIRECT3DDEVICE9 m_pd3dDevice; // The D3D rendering device
146  IDirect3DSwapChain9* m_pd3dSwapChain;
147  D3DCAPS9 m_d3dCaps; // Caps for the device
148  D3DSURFACE_DESC m_d3dsdBackBuffer; // Surface desc of the backbuffer
149  DWORD m_dwCreateFlags; // Indicate sw or hw vertex processing
150  DWORD m_dwWindowStyle; // Saved window style for mode switches
151  RECT m_rcWindowBounds; // Saved window bounds for mode switches
152  RECT m_rcWindowClient; // Saved client area size for mode switches
153  int m_nClientWidth;
154  int m_nClientHeight;
155 
156  // Variables for timing
157  double m_fTime; // Current time in seconds
158  double m_fElapsedTime; // Time elapsed since last frame
159  FLOAT m_fFPS; // Instanteous frame rate
160  TCHAR m_strDeviceStats[90];// string to hold D3D device stats
161  TCHAR m_strFrameStats[90]; // string to hold frame stats
162  float m_fRefreshTimerInterval; // in seconds.
163  int m_nFrameRateControl;
164 
165  // Overridable variables for the app
166  TCHAR* m_strWindowTitle; // Title for the app's window
167  DWORD m_dwCreationWidth; // Width used to create window
168  DWORD m_dwCreationHeight; // Height used to create window
169  bool m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
170  bool m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
171  bool m_bStartFullscreen; // Whether to start up the app in fullscreen mode
172  bool m_bCreateMultithreadDevice; // Whether to create a multithreaded device
173  bool m_bAllowDialogBoxMode; // If enabled the framework will try to enable GDI dialogs while in fullscreen
174 
177 
178  bool m_bIsExternalD3DDevice;
181 
182 };
bool m_bEnable3DRendering
whether to render 3d scene and present to screen.
Definition: d3dapp.h:137
ParaEngine::PEAppState m_nAppState
application state
Definition: d3dapp.h:180
bool m_bPassiveRendering
passive rendering, it will not render the scene, but simulation and time remains the same...
Definition: d3dapp.h:134
Definition: ManagedDef.h:18
void SetRefreshTimer(float fTimeInterval, int nFrameRateControl=0)
Set the frame rate timer interval.
Definition: d3dapp.cpp:1703
HRESULT Render3DEnvironment(bool bForceRender=false)
frame move and render
Definition: d3dapp.cpp:284
HRESULT PresentScene()
force present the scene.
Definition: d3dapp.cpp:435
virtual bool UpdateViewPort()
update view port by backbuffer size.
Definition: d3dapp.cpp:862
float GetRefreshTimer() const
get the refresh timer.
Definition: d3dapp.cpp:1710
Definition: d3denumeration.h:98
bool m_bDisableD3D
if this is true, the directX is not needed to run the application.
Definition: d3dapp.h:131
A base class for creating D3D device based application.
Definition: d3dapp.h:13
void Enable3DRendering(bool bEnable)
disable 3D rendering, do not present the scene.
Definition: d3dapp.h:79
bool IsExternalD3DDevice()
whether the d3d device is managed externally
Definition: d3dapp.h:107
virtual HRESULT DoWork()
this function is called per frame, in most cases, it will render the 3d scene and frame move...
Definition: d3dapp.cpp:1808
bool m_bIsExternalWindow
whether the main rendering window is an externally provided window or not.
Definition: d3dapp.h:176
PEAppState
ParaEngine application state.
Definition: IParaEngineApp.h:40
bool Is3DRenderingEnabled()
whether 3D rendering is enabled, do not present the scene.
Definition: d3dapp.h:83
Definition: d3dsettings.h:17
HRESULT HandlePossibleSizeChange(bool bUpdateSizeOnly=false)
this function is called whenever the main window size changes.
Definition: d3dapp.cpp:787