1 | //--------------------------------------------------------------------------------------
|
---|
2 | // File: DXUTMisc.h
|
---|
3 | //
|
---|
4 | // Helper functions for Direct3D programming.
|
---|
5 | //
|
---|
6 | // Copyright (c) Microsoft Corporation. All rights reserved
|
---|
7 | //--------------------------------------------------------------------------------------
|
---|
8 | #pragma once
|
---|
9 | #ifndef DXUT_MISC_H
|
---|
10 | #define DXUT_MISC_H
|
---|
11 |
|
---|
12 |
|
---|
13 | //--------------------------------------------------------------------------------------
|
---|
14 | // A growable array
|
---|
15 | //--------------------------------------------------------------------------------------
|
---|
16 | template< typename TYPE >
|
---|
17 | class CGrowableArray
|
---|
18 | {
|
---|
19 | public:
|
---|
20 | CGrowableArray() { m_pData = NULL; m_nSize = 0; m_nMaxSize = 0; }
|
---|
21 | CGrowableArray( const CGrowableArray<TYPE>& a ) { for( int i=0; i < a.m_nSize; i++ ) Add( a.m_pData[i] ); }
|
---|
22 | ~CGrowableArray() { RemoveAll(); }
|
---|
23 |
|
---|
24 | const TYPE& operator[]( int nIndex ) const { return GetAt( nIndex ); }
|
---|
25 | TYPE& operator[]( int nIndex ) { return GetAt( nIndex ); }
|
---|
26 |
|
---|
27 | CGrowableArray& operator=( const CGrowableArray<TYPE>& a ) { if( this == &a ) return *this; RemoveAll(); for( int i=0; i < a.m_nSize; i++ ) Add( a.m_pData[i] ); return *this; }
|
---|
28 |
|
---|
29 | HRESULT SetSize( int nNewMaxSize );
|
---|
30 | HRESULT Add( const TYPE& value );
|
---|
31 | HRESULT Insert( int nIndex, const TYPE& value );
|
---|
32 | HRESULT SetAt( int nIndex, const TYPE& value );
|
---|
33 | TYPE& GetAt( int nIndex ) { assert( nIndex >= 0 && nIndex < m_nSize ); return m_pData[nIndex]; }
|
---|
34 | int GetSize() const { return m_nSize; }
|
---|
35 | TYPE* GetData() { return m_pData; }
|
---|
36 | bool Contains( const TYPE& value ){ return ( -1 != IndexOf( value ) ); }
|
---|
37 |
|
---|
38 | int IndexOf( const TYPE& value ) { return ( m_nSize > 0 ) ? IndexOf( value, 0, m_nSize ) : -1; }
|
---|
39 | int IndexOf( const TYPE& value, int iStart ) { return IndexOf( value, iStart, m_nSize - iStart ); }
|
---|
40 | int IndexOf( const TYPE& value, int nIndex, int nNumElements );
|
---|
41 |
|
---|
42 | int LastIndexOf( const TYPE& value ) { return ( m_nSize > 0 ) ? LastIndexOf( value, m_nSize-1, m_nSize ) : -1; }
|
---|
43 | int LastIndexOf( const TYPE& value, int nIndex ) { return LastIndexOf( value, nIndex, nIndex+1 ); }
|
---|
44 | int LastIndexOf( const TYPE& value, int nIndex, int nNumElements );
|
---|
45 |
|
---|
46 | HRESULT Remove( int nIndex );
|
---|
47 | void RemoveAll() { SetSize(0); }
|
---|
48 |
|
---|
49 | protected:
|
---|
50 | TYPE* m_pData; // the actual array of data
|
---|
51 | int m_nSize; // # of elements (upperBound - 1)
|
---|
52 | int m_nMaxSize; // max allocated
|
---|
53 |
|
---|
54 | HRESULT SetSizeInternal( int nNewMaxSize ); // This version doesn't call ctor or dtor.
|
---|
55 | };
|
---|
56 |
|
---|
57 |
|
---|
58 | //--------------------------------------------------------------------------------------
|
---|
59 | // Performs timer operations
|
---|
60 | // Use DXUTGetGlobalTimer() to get the global instance
|
---|
61 | //--------------------------------------------------------------------------------------
|
---|
62 | class CDXUTTimer
|
---|
63 | {
|
---|
64 | public:
|
---|
65 | CDXUTTimer();
|
---|
66 |
|
---|
67 | void Reset(); // resets the timer
|
---|
68 | void Start(); // starts the timer
|
---|
69 | void Stop(); // stop (or pause) the timer
|
---|
70 | void Advance(); // advance the timer by 0.1 seconds
|
---|
71 | double GetAbsoluteTime(); // get the absolute system time
|
---|
72 | double GetTime(); // get the current time
|
---|
73 | double GetElapsedTime(); // get the time that elapsed between GetElapsedTime() calls
|
---|
74 | bool IsStopped(); // returns true if timer stopped
|
---|
75 |
|
---|
76 | protected:
|
---|
77 | bool m_bUsingQPF;
|
---|
78 | bool m_bTimerStopped;
|
---|
79 | LONGLONG m_llQPFTicksPerSec;
|
---|
80 |
|
---|
81 | LONGLONG m_llStopTime;
|
---|
82 | LONGLONG m_llLastElapsedTime;
|
---|
83 | LONGLONG m_llBaseTime;
|
---|
84 | };
|
---|
85 |
|
---|
86 | CDXUTTimer* DXUTGetGlobalTimer();
|
---|
87 |
|
---|
88 |
|
---|
89 | //-----------------------------------------------------------------------------
|
---|
90 | // Resource cache for textures, fonts, meshs, and effects.
|
---|
91 | // Use DXUTGetGlobalResourceCache() to access the global cache
|
---|
92 | //-----------------------------------------------------------------------------
|
---|
93 |
|
---|
94 | enum DXUTCACHE_SOURCELOCATION { DXUTCACHE_LOCATION_FILE, DXUTCACHE_LOCATION_RESOURCE };
|
---|
95 |
|
---|
96 | struct DXUTCache_Texture
|
---|
97 | {
|
---|
98 | DXUTCACHE_SOURCELOCATION Location;
|
---|
99 | WCHAR wszSource[MAX_PATH];
|
---|
100 | HMODULE hSrcModule;
|
---|
101 | UINT Width;
|
---|
102 | UINT Height;
|
---|
103 | UINT Depth;
|
---|
104 | UINT MipLevels;
|
---|
105 | DWORD Usage;
|
---|
106 | D3DFORMAT Format;
|
---|
107 | D3DPOOL Pool;
|
---|
108 | D3DRESOURCETYPE Type;
|
---|
109 | IDirect3DBaseTexture9 *pTexture;
|
---|
110 | };
|
---|
111 |
|
---|
112 | struct DXUTCache_Font : public D3DXFONT_DESC
|
---|
113 | {
|
---|
114 | ID3DXFont *pFont;
|
---|
115 | };
|
---|
116 |
|
---|
117 | struct DXUTCache_Effect
|
---|
118 | {
|
---|
119 | DXUTCACHE_SOURCELOCATION Location;
|
---|
120 | WCHAR wszSource[MAX_PATH];
|
---|
121 | HMODULE hSrcModule;
|
---|
122 | DWORD dwFlags;
|
---|
123 | ID3DXEffect *pEffect;
|
---|
124 | };
|
---|
125 |
|
---|
126 |
|
---|
127 | class CDXUTResourceCache
|
---|
128 | {
|
---|
129 | public:
|
---|
130 | ~CDXUTResourceCache();
|
---|
131 |
|
---|
132 | HRESULT CreateTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DTEXTURE9 *ppTexture );
|
---|
133 | HRESULT CreateTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture );
|
---|
134 | HRESULT CreateTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DTEXTURE9 *ppTexture );
|
---|
135 | HRESULT CreateTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture );
|
---|
136 | HRESULT CreateCubeTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture );
|
---|
137 | HRESULT CreateCubeTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Size, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture );
|
---|
138 | HRESULT CreateCubeTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture );
|
---|
139 | HRESULT CreateCubeTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Size, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DCUBETEXTURE9 *ppCubeTexture );
|
---|
140 | HRESULT CreateVolumeTextureFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture );
|
---|
141 | HRESULT CreateVolumeTextureFromFileEx( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, UINT Width, UINT Height, UINT Depth, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DVOLUMETEXTURE9 *ppTexture );
|
---|
142 | HRESULT CreateVolumeTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture );
|
---|
143 | HRESULT CreateVolumeTextureFromResourceEx( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, UINT Width, UINT Height, UINT Depth, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture );
|
---|
144 | HRESULT CreateFont( LPDIRECT3DDEVICE9 pDevice, UINT Height, UINT Width, UINT Weight, UINT MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, LPCTSTR pFacename, LPD3DXFONT *ppFont );
|
---|
145 | HRESULT CreateFontIndirect( LPDIRECT3DDEVICE9 pDevice, CONST D3DXFONT_DESC *pDesc, LPD3DXFONT *ppFont );
|
---|
146 | HRESULT CreateEffectFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors );
|
---|
147 | HRESULT CreateEffectFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors );
|
---|
148 |
|
---|
149 | public:
|
---|
150 | HRESULT OnCreateDevice( IDirect3DDevice9 *pd3dDevice );
|
---|
151 | HRESULT OnResetDevice( IDirect3DDevice9 *pd3dDevice );
|
---|
152 | HRESULT OnLostDevice();
|
---|
153 | HRESULT OnDestroyDevice();
|
---|
154 |
|
---|
155 | protected:
|
---|
156 | friend CDXUTResourceCache& DXUTGetGlobalResourceCache();
|
---|
157 | friend HRESULT DXUTInitialize3DEnvironment();
|
---|
158 | friend HRESULT DXUTReset3DEnvironment();
|
---|
159 | friend void DXUTCleanup3DEnvironment( bool bReleaseSettings );
|
---|
160 |
|
---|
161 | CDXUTResourceCache() { }
|
---|
162 |
|
---|
163 | CGrowableArray< DXUTCache_Texture > m_TextureCache;
|
---|
164 | CGrowableArray< DXUTCache_Effect > m_EffectCache;
|
---|
165 | CGrowableArray< DXUTCache_Font > m_FontCache;
|
---|
166 | };
|
---|
167 |
|
---|
168 | CDXUTResourceCache& DXUTGetGlobalResourceCache();
|
---|
169 |
|
---|
170 |
|
---|
171 | //--------------------------------------------------------------------------------------
|
---|
172 | class CD3DArcBall
|
---|
173 | {
|
---|
174 | public:
|
---|
175 | CD3DArcBall();
|
---|
176 |
|
---|
177 | // Functions to change behavior
|
---|
178 | void Reset();
|
---|
179 | void SetTranslationRadius( FLOAT fRadiusTranslation ) { m_fRadiusTranslation = fRadiusTranslation; }
|
---|
180 | void SetWindow( INT nWidth, INT nHeight, FLOAT fRadius = 0.9f ) { m_nWidth = nWidth; m_nHeight = nHeight; m_fRadius = fRadius; m_vCenter = D3DXVECTOR2(m_nWidth/2.0f,m_nHeight/2.0f); }
|
---|
181 | void SetOffset( INT nX, INT nY ) { m_Offset.x = nX; m_Offset.y = nY; }
|
---|
182 |
|
---|
183 | // Call these from client and use GetRotationMatrix() to read new rotation matrix
|
---|
184 | void OnBegin( int nX, int nY ); // start the rotation (pass current mouse position)
|
---|
185 | void OnMove( int nX, int nY ); // continue the rotation (pass current mouse position)
|
---|
186 | void OnEnd(); // end the rotation
|
---|
187 |
|
---|
188 | // Or call this to automatically handle left, middle, right buttons
|
---|
189 | LRESULT HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
|
---|
190 |
|
---|
191 | // Functions to get/set state
|
---|
192 | D3DXMATRIX* GetRotationMatrix() { return D3DXMatrixRotationQuaternion(&m_mRotation, &m_qNow); };
|
---|
193 | D3DXMATRIX* GetTranslationMatrix() { return &m_mTranslation; }
|
---|
194 | D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_mTranslationDelta; }
|
---|
195 | bool IsBeingDragged() { return m_bDrag; }
|
---|
196 | D3DXQUATERNION GetQuatNow() { return m_qNow; }
|
---|
197 | void SetQuatNow( D3DXQUATERNION q ) { m_qNow = q; }
|
---|
198 |
|
---|
199 | static D3DXQUATERNION QuatFromBallPoints( const D3DXVECTOR3 &vFrom, const D3DXVECTOR3 &vTo );
|
---|
200 |
|
---|
201 |
|
---|
202 | protected:
|
---|
203 | D3DXMATRIXA16 m_mRotation; // Matrix for arc ball's orientation
|
---|
204 | D3DXMATRIXA16 m_mTranslation; // Matrix for arc ball's position
|
---|
205 | D3DXMATRIXA16 m_mTranslationDelta; // Matrix for arc ball's position
|
---|
206 |
|
---|
207 | POINT m_Offset; // window offset, or upper-left corner of window
|
---|
208 | INT m_nWidth; // arc ball's window width
|
---|
209 | INT m_nHeight; // arc ball's window height
|
---|
210 | D3DXVECTOR2 m_vCenter; // center of arc ball
|
---|
211 | FLOAT m_fRadius; // arc ball's radius in screen coords
|
---|
212 | FLOAT m_fRadiusTranslation; // arc ball's radius for translating the target
|
---|
213 |
|
---|
214 | D3DXQUATERNION m_qDown; // Quaternion before button down
|
---|
215 | D3DXQUATERNION m_qNow; // Composite quaternion for current drag
|
---|
216 | bool m_bDrag; // Whether user is dragging arc ball
|
---|
217 |
|
---|
218 | POINT m_ptLastMouse; // position of last mouse point
|
---|
219 | D3DXVECTOR3 m_vDownPt; // starting point of rotation arc
|
---|
220 | D3DXVECTOR3 m_vCurrentPt; // current point of rotation arc
|
---|
221 |
|
---|
222 | D3DXVECTOR3 ScreenToVector( float fScreenPtX, float fScreenPtY );
|
---|
223 | };
|
---|
224 |
|
---|
225 |
|
---|
226 | //--------------------------------------------------------------------------------------
|
---|
227 | // used by CCamera to map WM_KEYDOWN keys
|
---|
228 | //--------------------------------------------------------------------------------------
|
---|
229 | enum D3DUtil_CameraKeys
|
---|
230 | {
|
---|
231 | CAM_STRAFE_LEFT = 0,
|
---|
232 | CAM_STRAFE_RIGHT,
|
---|
233 | CAM_MOVE_FORWARD,
|
---|
234 | CAM_MOVE_BACKWARD,
|
---|
235 | CAM_MOVE_UP,
|
---|
236 | CAM_MOVE_DOWN,
|
---|
237 | CAM_RESET,
|
---|
238 | CAM_CONTROLDOWN,
|
---|
239 | CAM_MAX_KEYS,
|
---|
240 | CAM_UNKNOWN = 0xFF
|
---|
241 | };
|
---|
242 |
|
---|
243 | #define KEY_WAS_DOWN_MASK 0x80
|
---|
244 | #define KEY_IS_DOWN_MASK 0x01
|
---|
245 |
|
---|
246 | #define MOUSE_LEFT_BUTTON 0x01
|
---|
247 | #define MOUSE_MIDDLE_BUTTON 0x02
|
---|
248 | #define MOUSE_RIGHT_BUTTON 0x04
|
---|
249 | #define MOUSE_WHEEL 0x08
|
---|
250 |
|
---|
251 |
|
---|
252 | //--------------------------------------------------------------------------------------
|
---|
253 | // Simple base camera class that moves and rotates. The base class
|
---|
254 | // records mouse and keyboard input for use by a derived class, and
|
---|
255 | // keeps common state.
|
---|
256 | //--------------------------------------------------------------------------------------
|
---|
257 | class CBaseCamera
|
---|
258 | {
|
---|
259 | public:
|
---|
260 | CBaseCamera();
|
---|
261 |
|
---|
262 | // Call these from client and use Get*Matrix() to read new matrices
|
---|
263 | virtual LRESULT HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
|
---|
264 | virtual void FrameMove( FLOAT fElapsedTime ) = 0;
|
---|
265 |
|
---|
266 | // Functions to change camera matrices
|
---|
267 | virtual void Reset();
|
---|
268 | virtual void SetViewParams( D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt );
|
---|
269 | virtual void SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane, FLOAT fFarPlane );
|
---|
270 |
|
---|
271 | // Functions to change behavior
|
---|
272 | virtual void SetDragRect( RECT &rc ) { m_rcDrag = rc; }
|
---|
273 | void SetInvertPitch( bool bInvertPitch ) { m_bInvertPitch = bInvertPitch; }
|
---|
274 | void SetDrag( bool bMovementDrag, FLOAT fTotalDragTimeToZero = 0.25f ) { m_bMovementDrag = bMovementDrag; m_fTotalDragTimeToZero = fTotalDragTimeToZero; }
|
---|
275 | void SetEnableYAxisMovement( bool bEnableYAxisMovement ) { m_bEnableYAxisMovement = bEnableYAxisMovement; }
|
---|
276 | void SetEnablePositionMovement( bool bEnablePositionMovement ) { m_bEnablePositionMovement = bEnablePositionMovement; }
|
---|
277 | void SetClipToBoundary( bool bClipToBoundary, D3DXVECTOR3* pvMinBoundary, D3DXVECTOR3* pvMaxBoundary ) { m_bClipToBoundary = bClipToBoundary; if( pvMinBoundary ) m_vMinBoundary = *pvMinBoundary; if( pvMaxBoundary ) m_vMaxBoundary = *pvMaxBoundary; }
|
---|
278 | void SetScalers( FLOAT fRotationScaler = 0.01f, FLOAT fMoveScaler = 5.0f ) { m_fRotationScaler = fRotationScaler; m_fMoveScaler = fMoveScaler; }
|
---|
279 | void SetNumberOfFramesToSmoothMouseData( int nFrames ) { if( nFrames > 0 ) m_fFramesToSmoothMouseData = (float)nFrames; }
|
---|
280 | void SetResetCursorAfterMove( bool bResetCursorAfterMove ) { m_bResetCursorAfterMove = bResetCursorAfterMove; }
|
---|
281 |
|
---|
282 | // Functions to get state
|
---|
283 | const D3DXMATRIX* GetViewMatrix() const { return &m_mView; }
|
---|
284 | const D3DXMATRIX* GetProjMatrix() const { return &m_mProj; }
|
---|
285 | const D3DXVECTOR3* GetEyePt() const { return &m_vEye; }
|
---|
286 | const D3DXVECTOR3* GetLookAtPt() const { return &m_vLookAt; }
|
---|
287 |
|
---|
288 | bool IsBeingDragged() { return (m_bMouseLButtonDown || m_bMouseMButtonDown || m_bMouseRButtonDown); }
|
---|
289 | bool IsMouseLButtonDown() { return m_bMouseLButtonDown; }
|
---|
290 | bool IsMouseMButtonDown() { return m_bMouseMButtonDown; }
|
---|
291 | bool IsMouseRButtonDown() { return m_bMouseRButtonDown; }
|
---|
292 |
|
---|
293 | protected:
|
---|
294 | // Functions to map a WM_KEYDOWN key to a D3DUtil_CameraKeys enum
|
---|
295 | virtual D3DUtil_CameraKeys MapKey( UINT nKey );
|
---|
296 | bool IsKeyDown( BYTE key ) { return( (key & KEY_IS_DOWN_MASK) == KEY_IS_DOWN_MASK ); }
|
---|
297 | bool WasKeyDown( BYTE key ) { return( (key & KEY_WAS_DOWN_MASK) == KEY_WAS_DOWN_MASK ); }
|
---|
298 |
|
---|
299 | void ConstrainToBoundary( D3DXVECTOR3* pV );
|
---|
300 | void UpdateMouseDelta( float fElapsedTime );
|
---|
301 | void UpdateVelocity( float fElapsedTime );
|
---|
302 |
|
---|
303 | D3DXMATRIX m_mView; // View matrix
|
---|
304 | D3DXMATRIX m_mProj; // Projection matrix
|
---|
305 |
|
---|
306 | BYTE m_aKeys[CAM_MAX_KEYS]; // State of input - KEY_WAS_DOWN_MASK|KEY_IS_DOWN_MASK
|
---|
307 | POINT m_ptLastMousePosition; // Last absolute position of mouse cursor
|
---|
308 | bool m_bMouseLButtonDown; // True if left button is down
|
---|
309 | bool m_bMouseMButtonDown; // True if middle button is down
|
---|
310 | bool m_bMouseRButtonDown; // True if right button is down
|
---|
311 | int m_nCurrentButtonMask; // mask of which buttons are down
|
---|
312 | int m_nMouseWheelDelta; // Amount of middle wheel scroll (+/-)
|
---|
313 | D3DXVECTOR2 m_vMouseDelta; // Mouse relative delta smoothed over a few frames
|
---|
314 | float m_fFramesToSmoothMouseData; // Number of frames to smooth mouse data over
|
---|
315 |
|
---|
316 | D3DXVECTOR3 m_vDefaultEye; // Default camera eye position
|
---|
317 | D3DXVECTOR3 m_vDefaultLookAt; // Default LookAt position
|
---|
318 | D3DXVECTOR3 m_vEye; // Camera eye position
|
---|
319 | D3DXVECTOR3 m_vLookAt; // LookAt position
|
---|
320 | float m_fCameraYawAngle; // Yaw angle of camera
|
---|
321 | float m_fCameraPitchAngle; // Pitch angle of camera
|
---|
322 |
|
---|
323 | RECT m_rcDrag; // Rectangle within which a drag can be initiated.
|
---|
324 | D3DXVECTOR3 m_vVelocity; // Velocity of camera
|
---|
325 | bool m_bMovementDrag; // If true, then camera movement will slow to a stop otherwise movement is instant
|
---|
326 | D3DXVECTOR3 m_vVelocityDrag; // Velocity drag force
|
---|
327 | FLOAT m_fDragTimer; // Countdown timer to apply drag
|
---|
328 | FLOAT m_fTotalDragTimeToZero; // Time it takes for velocity to go from full to 0
|
---|
329 | D3DXVECTOR2 m_vRotVelocity; // Velocity of camera
|
---|
330 |
|
---|
331 | float m_fFOV; // Field of view
|
---|
332 | float m_fAspect; // Aspect ratio
|
---|
333 | float m_fNearPlane; // Near plane
|
---|
334 | float m_fFarPlane; // Far plane
|
---|
335 |
|
---|
336 | float m_fRotationScaler; // Scaler for rotation
|
---|
337 | float m_fMoveScaler; // Scaler for movement
|
---|
338 |
|
---|
339 | bool m_bInvertPitch; // Invert the pitch axis
|
---|
340 | bool m_bEnablePositionMovement; // If true, then the user can translate the camera/model
|
---|
341 | bool m_bEnableYAxisMovement; // If true, then camera can move in the y-axis
|
---|
342 |
|
---|
343 | bool m_bClipToBoundary; // If true, then the camera will be clipped to the boundary
|
---|
344 | D3DXVECTOR3 m_vMinBoundary; // Min point in clip boundary
|
---|
345 | D3DXVECTOR3 m_vMaxBoundary; // Max point in clip boundary
|
---|
346 |
|
---|
347 | bool m_bResetCursorAfterMove;// If true, the class will reset the cursor position so that the cursor always has space to move
|
---|
348 | };
|
---|
349 |
|
---|
350 |
|
---|
351 | //--------------------------------------------------------------------------------------
|
---|
352 | // Simple first person camera class that moves and rotates.
|
---|
353 | // It allows yaw and pitch but not roll. It uses WM_KEYDOWN and
|
---|
354 | // GetCursorPos() to respond to keyboard and mouse input and updates the
|
---|
355 | // view matrix based on input.
|
---|
356 | //--------------------------------------------------------------------------------------
|
---|
357 | class CFirstPersonCamera : public CBaseCamera
|
---|
358 | {
|
---|
359 | public:
|
---|
360 | CFirstPersonCamera();
|
---|
361 |
|
---|
362 | // Call these from client and use Get*Matrix() to read new matrices
|
---|
363 | virtual void FrameMove( FLOAT fElapsedTime );
|
---|
364 |
|
---|
365 | // Functions to change behavior
|
---|
366 | void SetRotateButtons( bool bLeft, bool bMiddle, bool bRight );
|
---|
367 |
|
---|
368 | // Functions to get state
|
---|
369 | D3DXMATRIX* GetWorldMatrix() { return &m_mCameraWorld; }
|
---|
370 |
|
---|
371 | const D3DXVECTOR3* GetWorldRight() const { return (D3DXVECTOR3*)&m_mCameraWorld._11; }
|
---|
372 | const D3DXVECTOR3* GetWorldUp() const { return (D3DXVECTOR3*)&m_mCameraWorld._21; }
|
---|
373 | const D3DXVECTOR3* GetWorldAhead() const { return (D3DXVECTOR3*)&m_mCameraWorld._31; }
|
---|
374 | const D3DXVECTOR3* GetEyePt() const { return (D3DXVECTOR3*)&m_mCameraWorld._41; }
|
---|
375 |
|
---|
376 | protected:
|
---|
377 | D3DXMATRIX m_mCameraWorld; // World matrix of the camera (inverse of the view matrix)
|
---|
378 |
|
---|
379 | int m_nActiveButtonMask; // Mask to determine which button to enable for rotation
|
---|
380 | };
|
---|
381 |
|
---|
382 |
|
---|
383 | //--------------------------------------------------------------------------------------
|
---|
384 | // Simple model viewing camera class that rotates around the object.
|
---|
385 | //--------------------------------------------------------------------------------------
|
---|
386 | class CModelViewerCamera : public CBaseCamera
|
---|
387 | {
|
---|
388 | public:
|
---|
389 | CModelViewerCamera();
|
---|
390 |
|
---|
391 | // Call these from client and use Get*Matrix() to read new matrices
|
---|
392 | virtual LRESULT HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
|
---|
393 | virtual void FrameMove( FLOAT fElapsedTime );
|
---|
394 |
|
---|
395 |
|
---|
396 | // Functions to change behavior
|
---|
397 | virtual void SetDragRect( RECT &rc );
|
---|
398 | void Reset();
|
---|
399 | void SetViewParams( D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt );
|
---|
400 | void SetButtonMasks( int nRotateModelButtonMask = MOUSE_LEFT_BUTTON, int nZoomButtonMask = MOUSE_WHEEL, int nRotateCameraButtonMask = MOUSE_RIGHT_BUTTON ) { m_nRotateModelButtonMask = nRotateModelButtonMask, m_nZoomButtonMask = nZoomButtonMask; m_nRotateCameraButtonMask = nRotateCameraButtonMask; }
|
---|
401 | void SetAttachCameraToModel( bool bEnable = false ) { m_bAttachCameraToModel = bEnable; }
|
---|
402 | void SetWindow( int nWidth, int nHeight, float fArcballRadius=0.9f ) { m_WorldArcBall.SetWindow( nWidth, nHeight, fArcballRadius ); m_ViewArcBall.SetWindow( nWidth, nHeight, fArcballRadius ); }
|
---|
403 | void SetRadius( float fDefaultRadius=5.0f, float fMinRadius=1.0f, float fMaxRadius=FLT_MAX ) { m_fDefaultRadius = m_fRadius = fDefaultRadius; m_fMinRadius = fMinRadius; m_fMaxRadius = fMaxRadius; }
|
---|
404 | void SetModelCenter( D3DXVECTOR3 vModelCenter ) { m_vModelCenter = vModelCenter; }
|
---|
405 | void SetLimitPitch( bool bLimitPitch ) { m_bLimitPitch = bLimitPitch; }
|
---|
406 | void SetViewQuat( D3DXQUATERNION q ) { m_ViewArcBall.SetQuatNow( q ); m_bDragSinceLastUpdate = true; }
|
---|
407 | void SetWorldQuat( D3DXQUATERNION q ) { m_WorldArcBall.SetQuatNow( q ); m_bDragSinceLastUpdate = true; }
|
---|
408 |
|
---|
409 | // Functions to get state
|
---|
410 | D3DXMATRIX* GetWorldMatrix() { return &m_mWorld; }
|
---|
411 |
|
---|
412 | protected:
|
---|
413 | CD3DArcBall m_WorldArcBall;
|
---|
414 | CD3DArcBall m_ViewArcBall;
|
---|
415 | D3DXVECTOR3 m_vModelCenter;
|
---|
416 | D3DXMATRIX m_mModelLastRot; // Last arcball rotation matrix for model
|
---|
417 | D3DXMATRIX m_mModelRot; // Rotation matrix of model
|
---|
418 | D3DXMATRIX m_mWorld; // World matrix of model
|
---|
419 |
|
---|
420 | int m_nRotateModelButtonMask;
|
---|
421 | int m_nZoomButtonMask;
|
---|
422 | int m_nRotateCameraButtonMask;
|
---|
423 |
|
---|
424 | bool m_bAttachCameraToModel;
|
---|
425 | bool m_bLimitPitch;
|
---|
426 | float m_fRadius; // Distance from the camera to model
|
---|
427 | float m_fDefaultRadius; // Distance from the camera to model
|
---|
428 | float m_fMinRadius; // Min radius
|
---|
429 | float m_fMaxRadius; // Max radius
|
---|
430 | bool m_bDragSinceLastUpdate; // True if mouse drag has happened since last time FrameMove is called.
|
---|
431 |
|
---|
432 | D3DXMATRIX m_mCameraRotLast;
|
---|
433 |
|
---|
434 | };
|
---|
435 |
|
---|
436 |
|
---|
437 | //--------------------------------------------------------------------------------------
|
---|
438 | // Manages the intertion point when drawing text
|
---|
439 | //--------------------------------------------------------------------------------------
|
---|
440 | class CDXUTTextHelper
|
---|
441 | {
|
---|
442 | public:
|
---|
443 | CDXUTTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight );
|
---|
444 |
|
---|
445 | void SetInsertionPos( int x, int y ) { m_pt.x = x; m_pt.y = y; }
|
---|
446 | void SetForegroundColor( D3DXCOLOR clr ) { m_clr = clr; }
|
---|
447 |
|
---|
448 | void Begin();
|
---|
449 | HRESULT DrawFormattedTextLine( const WCHAR* strMsg, ... );
|
---|
450 | HRESULT DrawTextLine( const WCHAR* strMsg );
|
---|
451 | HRESULT DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... );
|
---|
452 | HRESULT DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg );
|
---|
453 | void End();
|
---|
454 |
|
---|
455 | protected:
|
---|
456 | ID3DXFont* m_pFont;
|
---|
457 | ID3DXSprite* m_pSprite;
|
---|
458 | D3DXCOLOR m_clr;
|
---|
459 | POINT m_pt;
|
---|
460 | int m_nLineHeight;
|
---|
461 | };
|
---|
462 |
|
---|
463 |
|
---|
464 | //--------------------------------------------------------------------------------------
|
---|
465 | // Manages a persistent list of lines and draws them using ID3DXLine
|
---|
466 | //--------------------------------------------------------------------------------------
|
---|
467 | class CDXUTLineManager
|
---|
468 | {
|
---|
469 | public:
|
---|
470 | CDXUTLineManager();
|
---|
471 | ~CDXUTLineManager();
|
---|
472 |
|
---|
473 | HRESULT OnCreatedDevice( IDirect3DDevice9* pd3dDevice );
|
---|
474 | HRESULT OnResetDevice();
|
---|
475 | HRESULT OnRender();
|
---|
476 | HRESULT OnLostDevice();
|
---|
477 | HRESULT OnDeletedDevice();
|
---|
478 |
|
---|
479 | HRESULT AddLine( int* pnLineID, D3DXVECTOR2* pVertexList, DWORD dwVertexListCount, D3DCOLOR Color, float fWidth, float fScaleRatio, bool bAntiAlias );
|
---|
480 | HRESULT AddRect( int* pnLineID, RECT rc, D3DCOLOR Color, float fWidth, float fScaleRatio, bool bAntiAlias );
|
---|
481 | HRESULT RemoveLine( int nLineID );
|
---|
482 | HRESULT RemoveAllLines();
|
---|
483 |
|
---|
484 | protected:
|
---|
485 | struct LINE_NODE
|
---|
486 | {
|
---|
487 | int nLineID;
|
---|
488 | D3DCOLOR Color;
|
---|
489 | float fWidth;
|
---|
490 | bool bAntiAlias;
|
---|
491 | float fScaleRatio;
|
---|
492 | D3DXVECTOR2* pVertexList;
|
---|
493 | DWORD dwVertexListCount;
|
---|
494 | };
|
---|
495 |
|
---|
496 | CGrowableArray<LINE_NODE*> m_LinesList;
|
---|
497 | IDirect3DDevice9* m_pd3dDevice;
|
---|
498 | ID3DXLine* m_pD3DXLine;
|
---|
499 | };
|
---|
500 |
|
---|
501 |
|
---|
502 | //--------------------------------------------------------------------------------------
|
---|
503 | // Manages the mesh, direction, mouse events of a directional arrow that
|
---|
504 | // rotates around a radius controlled by an arcball
|
---|
505 | //--------------------------------------------------------------------------------------
|
---|
506 | class CDXUTDirectionWidget
|
---|
507 | {
|
---|
508 | public:
|
---|
509 | CDXUTDirectionWidget();
|
---|
510 |
|
---|
511 | static HRESULT StaticOnCreateDevice( IDirect3DDevice9* pd3dDevice );
|
---|
512 | HRESULT OnResetDevice( const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
|
---|
513 | HRESULT OnRender( D3DXCOLOR color, const D3DXMATRIX* pmView, const D3DXMATRIX* pmProj, const D3DXVECTOR3* pEyePt );
|
---|
514 | LRESULT HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
|
---|
515 | static void StaticOnLostDevice();
|
---|
516 | static void StaticOnDestroyDevice();
|
---|
517 |
|
---|
518 | D3DXVECTOR3 GetLightDirection() { return m_vCurrentDir; };
|
---|
519 | void SetLightDirection( D3DXVECTOR3 vDir ) { m_vDefaultDir = m_vCurrentDir = vDir; };
|
---|
520 | void SetButtonMask( int nRotate = MOUSE_RIGHT_BUTTON ) { m_nRotateMask = nRotate; }
|
---|
521 |
|
---|
522 | float GetRadius() { return m_fRadius; };
|
---|
523 | void SetRadius( float fRadius ) { m_fRadius = fRadius; };
|
---|
524 |
|
---|
525 | bool IsBeingDragged() { return m_ArcBall.IsBeingDragged(); };
|
---|
526 |
|
---|
527 | protected:
|
---|
528 | HRESULT UpdateLightDir();
|
---|
529 |
|
---|
530 | static IDirect3DDevice9* s_pd3dDevice;
|
---|
531 | static ID3DXEffect* s_pEffect;
|
---|
532 | static ID3DXMesh* s_pMesh;
|
---|
533 |
|
---|
534 | float m_fRadius;
|
---|
535 | int m_nRotateMask;
|
---|
536 | CD3DArcBall m_ArcBall;
|
---|
537 | D3DXVECTOR3 m_vDefaultDir;
|
---|
538 | D3DXVECTOR3 m_vCurrentDir;
|
---|
539 | D3DXMATRIX m_mView;
|
---|
540 | D3DXMATRIXA16 m_mRot;
|
---|
541 | D3DXMATRIXA16 m_mRotSnapshot;
|
---|
542 | };
|
---|
543 |
|
---|
544 |
|
---|
545 | //--------------------------------------------------------------------------------------
|
---|
546 | // Tries to finds a media file by searching in common locations
|
---|
547 | //--------------------------------------------------------------------------------------
|
---|
548 | HRESULT DXUTFindDXSDKMediaFileCch( WCHAR* strDestPath, int cchDest, LPCWSTR strFilename );
|
---|
549 | HRESULT DXUTSetMediaSearchPath( LPCWSTR strPath );
|
---|
550 | LPCWSTR DXUTGetMediaSearchPath();
|
---|
551 |
|
---|
552 |
|
---|
553 | //--------------------------------------------------------------------------------------
|
---|
554 | // Returns the string for the given D3DFORMAT.
|
---|
555 | // bWithPrefix determines whether the string should include the "D3DFMT_"
|
---|
556 | //--------------------------------------------------------------------------------------
|
---|
557 | LPCWSTR DXUTD3DFormatToString( D3DFORMAT format, bool bWithPrefix );
|
---|
558 |
|
---|
559 |
|
---|
560 | //--------------------------------------------------------------------------------------
|
---|
561 | // Builds and sets a cursor for the D3D device based on hCursor.
|
---|
562 | //--------------------------------------------------------------------------------------
|
---|
563 | HRESULT DXUTSetDeviceCursor( IDirect3DDevice9* pd3dDevice, HCURSOR hCursor, bool bAddWatermark );
|
---|
564 |
|
---|
565 |
|
---|
566 | //--------------------------------------------------------------------------------------
|
---|
567 | // Returns a view matrix for rendering to a face of a cubemap.
|
---|
568 | //--------------------------------------------------------------------------------------
|
---|
569 | D3DXMATRIX DXUTGetCubeMapViewMatrix( DWORD dwFace );
|
---|
570 |
|
---|
571 |
|
---|
572 | //--------------------------------------------------------------------------------------
|
---|
573 | // Debug printing support
|
---|
574 | // See dxerr9.h for more debug printing support
|
---|
575 | //--------------------------------------------------------------------------------------
|
---|
576 | void DXUTOutputDebugStringW( LPCWSTR strMsg, ... );
|
---|
577 | void DXUTOutputDebugStringA( LPCSTR strMsg, ... );
|
---|
578 | HRESULT WINAPI DXUTTrace( const CHAR* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, bool bPopMsgBox );
|
---|
579 |
|
---|
580 | #ifdef UNICODE
|
---|
581 | #define DXUTOutputDebugString DXUTOutputDebugStringW
|
---|
582 | #else
|
---|
583 | #define DXUTOutputDebugString DXUTOutputDebugStringA
|
---|
584 | #endif
|
---|
585 |
|
---|
586 | // These macros are very similar to dxerr9's but it special cases the HRESULT defined
|
---|
587 | // by the sample framework pop better message boxes.
|
---|
588 | #if defined(DEBUG) | defined(_DEBUG)
|
---|
589 | #define DXUT_ERR(str,hr) DXUTTrace( __FILE__, (DWORD)__LINE__, hr, str, false )
|
---|
590 | #define DXUT_ERR_MSGBOX(str,hr) DXUTTrace( __FILE__, (DWORD)__LINE__, hr, str, true )
|
---|
591 | #define DXUTTRACE DXUTOutputDebugString
|
---|
592 | #else
|
---|
593 | #define DXUT_ERR(str,hr) (hr)
|
---|
594 | #define DXUT_ERR_MSGBOX(str,hr) (hr)
|
---|
595 | #define DXUTTRACE (__noop)
|
---|
596 | #endif
|
---|
597 |
|
---|
598 |
|
---|
599 | //--------------------------------------------------------------------------------------
|
---|
600 | // Direct3D9 dynamic linking support -- calls top-level D3D9 APIs with graceful
|
---|
601 | // failure if APIs are not present.
|
---|
602 | //--------------------------------------------------------------------------------------
|
---|
603 |
|
---|
604 | IDirect3D9 * WINAPI DXUT_Dynamic_Direct3DCreate9(UINT SDKVersion);
|
---|
605 | int WINAPI DXUT_Dynamic_D3DPERF_BeginEvent( D3DCOLOR col, LPCWSTR wszName );
|
---|
606 | int WINAPI DXUT_Dynamic_D3DPERF_EndEvent( void );
|
---|
607 | void WINAPI DXUT_Dynamic_D3DPERF_SetMarker( D3DCOLOR col, LPCWSTR wszName );
|
---|
608 | void WINAPI DXUT_Dynamic_D3DPERF_SetRegion( D3DCOLOR col, LPCWSTR wszName );
|
---|
609 | BOOL WINAPI DXUT_Dynamic_D3DPERF_QueryRepeatFrame( void );
|
---|
610 | void WINAPI DXUT_Dynamic_D3DPERF_SetOptions( DWORD dwOptions );
|
---|
611 | DWORD WINAPI DXUT_Dynamic_D3DPERF_GetStatus( void );
|
---|
612 |
|
---|
613 |
|
---|
614 | //--------------------------------------------------------------------------------------
|
---|
615 | // Profiling/instrumentation support
|
---|
616 | //--------------------------------------------------------------------------------------
|
---|
617 |
|
---|
618 | //--------------------------------------------------------------------------------------
|
---|
619 | // Some D3DPERF APIs take a color that can be used when displaying user events in
|
---|
620 | // performance analysis tools. The following constants are provided for your
|
---|
621 | // convenience, but you can use any colors you like.
|
---|
622 | //--------------------------------------------------------------------------------------
|
---|
623 | const D3DCOLOR DXUT_PERFEVENTCOLOR = D3DCOLOR_XRGB(200,100,100);
|
---|
624 | const D3DCOLOR DXUT_PERFEVENTCOLOR2 = D3DCOLOR_XRGB(100,200,100);
|
---|
625 | const D3DCOLOR DXUT_PERFEVENTCOLOR3 = D3DCOLOR_XRGB(100,100,200);
|
---|
626 |
|
---|
627 | //--------------------------------------------------------------------------------------
|
---|
628 | // The following macros provide a convenient way for your code to call the D3DPERF
|
---|
629 | // functions only when PROFILE is defined. If PROFILE is not defined (as for the final
|
---|
630 | // release version of a program), these macros evaluate to nothing, so no detailed event
|
---|
631 | // information is embedded in your shipping program. It is recommended that you create
|
---|
632 | // and use three build configurations for your projects:
|
---|
633 | // Debug (nonoptimized code, asserts active, PROFILE defined to assist debugging)
|
---|
634 | // Profile (optimized code, asserts disabled, PROFILE defined to assist optimization)
|
---|
635 | // Release (optimized code, asserts disabled, PROFILE not defined)
|
---|
636 | //--------------------------------------------------------------------------------------
|
---|
637 | #ifdef PROFILE
|
---|
638 | // PROFILE is defined, so these macros call the D3DPERF functions
|
---|
639 | #define DXUT_BeginPerfEvent( color, pstrMessage ) DXUT_Dynamic_D3DPERF_BeginEvent( color, pstrMessage )
|
---|
640 | #define DXUT_EndPerfEvent() DXUT_Dynamic_D3DPERF_EndEvent()
|
---|
641 | #define DXUT_SetPerfMarker( color, pstrMessage ) DXUT_Dynamic_D3DPERF_SetMarker( color, pstrMessage )
|
---|
642 | #else
|
---|
643 | // PROFILE is not defined, so these macros do nothing
|
---|
644 | #define DXUT_BeginPerfEvent( color, pstrMessage ) (__noop)
|
---|
645 | #define DXUT_EndPerfEvent() (__noop)
|
---|
646 | #define DXUT_SetPerfMarker( color, pstrMessage ) (__noop)
|
---|
647 | #endif
|
---|
648 |
|
---|
649 | //--------------------------------------------------------------------------------------
|
---|
650 | // CDXUTPerfEventGenerator is a helper class that makes it easy to attach begin and end
|
---|
651 | // events to a block of code. Simply define a CDXUTPerfEventGenerator variable anywhere
|
---|
652 | // in a block of code, and the class's constructor will call DXUT_BeginPerfEvent when
|
---|
653 | // the block of code begins, and the class's destructor will call DXUT_EndPerfEvent when
|
---|
654 | // the block ends.
|
---|
655 | //--------------------------------------------------------------------------------------
|
---|
656 | class CDXUTPerfEventGenerator
|
---|
657 | {
|
---|
658 | public:
|
---|
659 | CDXUTPerfEventGenerator( D3DCOLOR color, LPCWSTR pstrMessage ) { DXUT_BeginPerfEvent( color, pstrMessage ); }
|
---|
660 | ~CDXUTPerfEventGenerator( void ) { DXUT_EndPerfEvent(); }
|
---|
661 | };
|
---|
662 |
|
---|
663 | //--------------------------------------------------------------------------------------
|
---|
664 | // Implementation of CGrowableArray
|
---|
665 | //--------------------------------------------------------------------------------------
|
---|
666 |
|
---|
667 | // This version doesn't call ctor or dtor.
|
---|
668 | template< typename TYPE >
|
---|
669 | HRESULT CGrowableArray<TYPE>::SetSizeInternal( int nNewMaxSize )
|
---|
670 | {
|
---|
671 | if( nNewMaxSize < 0 )
|
---|
672 | {
|
---|
673 | assert( false );
|
---|
674 | return E_INVALIDARG;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if( nNewMaxSize == 0 )
|
---|
678 | {
|
---|
679 | // Shrink to 0 size & cleanup
|
---|
680 | if( m_pData )
|
---|
681 | {
|
---|
682 | free( m_pData );
|
---|
683 | m_pData = NULL;
|
---|
684 | }
|
---|
685 |
|
---|
686 | m_nMaxSize = 0;
|
---|
687 | m_nSize = 0;
|
---|
688 | }
|
---|
689 | else if( m_pData == NULL || nNewMaxSize > m_nMaxSize )
|
---|
690 | {
|
---|
691 | // Grow array
|
---|
692 | int nGrowBy = ( m_nMaxSize == 0 ) ? 16 : m_nMaxSize;
|
---|
693 | nNewMaxSize = max( nNewMaxSize, m_nMaxSize + nGrowBy );
|
---|
694 |
|
---|
695 | TYPE* pDataNew = (TYPE*) realloc( m_pData, nNewMaxSize * sizeof(TYPE) );
|
---|
696 | if( pDataNew == NULL )
|
---|
697 | return E_OUTOFMEMORY;
|
---|
698 |
|
---|
699 | m_pData = pDataNew;
|
---|
700 | m_nMaxSize = nNewMaxSize;
|
---|
701 | }
|
---|
702 |
|
---|
703 | return S_OK;
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 | //--------------------------------------------------------------------------------------
|
---|
708 | template< typename TYPE >
|
---|
709 | HRESULT CGrowableArray<TYPE>::SetSize( int nNewMaxSize )
|
---|
710 | {
|
---|
711 | int nOldSize = m_nSize;
|
---|
712 |
|
---|
713 | if( nOldSize > nNewMaxSize )
|
---|
714 | {
|
---|
715 | // Removing elements. Call dtor.
|
---|
716 |
|
---|
717 | for( int i = nNewMaxSize; i < nOldSize; ++i )
|
---|
718 | m_pData[i].~TYPE();
|
---|
719 | }
|
---|
720 |
|
---|
721 | // Adjust buffer. Note that there's no need to check for error
|
---|
722 | // since if it happens, nOldSize == nNewMaxSize will be true.)
|
---|
723 | HRESULT hr = SetSizeInternal( nNewMaxSize );
|
---|
724 |
|
---|
725 | if( nOldSize < nNewMaxSize )
|
---|
726 | {
|
---|
727 | // Adding elements. Call ctor.
|
---|
728 |
|
---|
729 | for( int i = nOldSize; i < nNewMaxSize; ++i )
|
---|
730 | ::new (&m_pData[i]) TYPE;
|
---|
731 | }
|
---|
732 |
|
---|
733 | return hr;
|
---|
734 | }
|
---|
735 |
|
---|
736 |
|
---|
737 | //--------------------------------------------------------------------------------------
|
---|
738 | template< typename TYPE >
|
---|
739 | HRESULT CGrowableArray<TYPE>::Add( const TYPE& value )
|
---|
740 | {
|
---|
741 | HRESULT hr;
|
---|
742 | if( FAILED( hr = SetSizeInternal( m_nSize + 1 ) ) )
|
---|
743 | return hr;
|
---|
744 |
|
---|
745 | // Construct the new element
|
---|
746 | ::new (&m_pData[m_nSize]) TYPE;
|
---|
747 |
|
---|
748 | // Assign
|
---|
749 | m_pData[m_nSize] = value;
|
---|
750 | ++m_nSize;
|
---|
751 |
|
---|
752 | return S_OK;
|
---|
753 | }
|
---|
754 |
|
---|
755 |
|
---|
756 | //--------------------------------------------------------------------------------------
|
---|
757 | template< typename TYPE >
|
---|
758 | HRESULT CGrowableArray<TYPE>::Insert( int nIndex, const TYPE& value )
|
---|
759 | {
|
---|
760 | HRESULT hr;
|
---|
761 |
|
---|
762 | // Validate index
|
---|
763 | if( nIndex < 0 ||
|
---|
764 | nIndex > m_nSize )
|
---|
765 | {
|
---|
766 | assert( false );
|
---|
767 | return E_INVALIDARG;
|
---|
768 | }
|
---|
769 |
|
---|
770 | // Prepare the buffer
|
---|
771 | if( FAILED( hr = SetSizeInternal( m_nSize + 1 ) ) )
|
---|
772 | return hr;
|
---|
773 |
|
---|
774 | // Shift the array
|
---|
775 | MoveMemory( &m_pData[nIndex+1], &m_pData[nIndex], sizeof(TYPE) * (m_nSize - nIndex) );
|
---|
776 |
|
---|
777 | // Construct the new element
|
---|
778 | ::new (&m_pData[nIndex]) TYPE;
|
---|
779 |
|
---|
780 | // Set the value and increase the size
|
---|
781 | m_pData[nIndex] = value;
|
---|
782 | ++m_nSize;
|
---|
783 |
|
---|
784 | return S_OK;
|
---|
785 | }
|
---|
786 |
|
---|
787 |
|
---|
788 | //--------------------------------------------------------------------------------------
|
---|
789 | template< typename TYPE >
|
---|
790 | HRESULT CGrowableArray<TYPE>::SetAt( int nIndex, const TYPE& value )
|
---|
791 | {
|
---|
792 | // Validate arguments
|
---|
793 | if( nIndex < 0 ||
|
---|
794 | nIndex >= m_nSize )
|
---|
795 | {
|
---|
796 | assert( false );
|
---|
797 | return E_INVALIDARG;
|
---|
798 | }
|
---|
799 |
|
---|
800 | m_pData[nIndex] = value;
|
---|
801 | return S_OK;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | //--------------------------------------------------------------------------------------
|
---|
806 | // Searches for the specified value and returns the index of the first occurrence
|
---|
807 | // within the section of the data array that extends from iStart and contains the
|
---|
808 | // specified number of elements. Returns -1 if value is not found within the given
|
---|
809 | // section.
|
---|
810 | //--------------------------------------------------------------------------------------
|
---|
811 | template< typename TYPE >
|
---|
812 | int CGrowableArray<TYPE>::IndexOf( const TYPE& value, int iStart, int nNumElements )
|
---|
813 | {
|
---|
814 | // Validate arguments
|
---|
815 | if( iStart < 0 ||
|
---|
816 | iStart >= m_nSize ||
|
---|
817 | nNumElements < 0 ||
|
---|
818 | iStart + nNumElements > m_nSize )
|
---|
819 | {
|
---|
820 | assert( false );
|
---|
821 | return -1;
|
---|
822 | }
|
---|
823 |
|
---|
824 | // Search
|
---|
825 | for( int i = iStart; i < (iStart + nNumElements); i++ )
|
---|
826 | {
|
---|
827 | if( value == m_pData[i] )
|
---|
828 | return i;
|
---|
829 | }
|
---|
830 |
|
---|
831 | // Not found
|
---|
832 | return -1;
|
---|
833 | }
|
---|
834 |
|
---|
835 |
|
---|
836 | //--------------------------------------------------------------------------------------
|
---|
837 | // Searches for the specified value and returns the index of the last occurrence
|
---|
838 | // within the section of the data array that contains the specified number of elements
|
---|
839 | // and ends at iEnd. Returns -1 if value is not found within the given section.
|
---|
840 | //--------------------------------------------------------------------------------------
|
---|
841 | template< typename TYPE >
|
---|
842 | int CGrowableArray<TYPE>::LastIndexOf( const TYPE& value, int iEnd, int nNumElements )
|
---|
843 | {
|
---|
844 | // Validate arguments
|
---|
845 | if( iEnd < 0 ||
|
---|
846 | iEnd >= m_nSize ||
|
---|
847 | nNumElements < 0 ||
|
---|
848 | iEnd - nNumElements < 0 )
|
---|
849 | {
|
---|
850 | assert( false );
|
---|
851 | return -1;
|
---|
852 | }
|
---|
853 |
|
---|
854 | // Search
|
---|
855 | for( int i = iEnd; i > (iEnd - nNumElements); i-- )
|
---|
856 | {
|
---|
857 | if( value == m_pData[i] )
|
---|
858 | return i;
|
---|
859 | }
|
---|
860 |
|
---|
861 | // Not found
|
---|
862 | return -1;
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 |
|
---|
867 | //--------------------------------------------------------------------------------------
|
---|
868 | template< typename TYPE >
|
---|
869 | HRESULT CGrowableArray<TYPE>::Remove( int nIndex )
|
---|
870 | {
|
---|
871 | if( nIndex < 0 ||
|
---|
872 | nIndex >= m_nSize )
|
---|
873 | {
|
---|
874 | assert( false );
|
---|
875 | return E_INVALIDARG;
|
---|
876 | }
|
---|
877 |
|
---|
878 | // Destruct the element to be removed
|
---|
879 | m_pData[nIndex].~TYPE();
|
---|
880 |
|
---|
881 | // Compact the array and decrease the size
|
---|
882 | MoveMemory( &m_pData[nIndex], &m_pData[nIndex+1], sizeof(TYPE) * (m_nSize - (nIndex+1)) );
|
---|
883 | --m_nSize;
|
---|
884 |
|
---|
885 | return S_OK;
|
---|
886 | }
|
---|
887 |
|
---|
888 |
|
---|
889 | #endif
|
---|