//-------------------------------------------------------------------------------------- // File: DXUTMisc.h // // Helper functions for Direct3D programming. // // Copyright (c) Microsoft Corporation. All rights reserved //-------------------------------------------------------------------------------------- #pragma once #ifndef DXUT_MISC_H #define DXUT_MISC_H //-------------------------------------------------------------------------------------- // A growable array //-------------------------------------------------------------------------------------- template< typename TYPE > class CGrowableArray { public: CGrowableArray() { m_pData = NULL; m_nSize = 0; m_nMaxSize = 0; } CGrowableArray( const CGrowableArray& a ) { for( int i=0; i < a.m_nSize; i++ ) Add( a.m_pData[i] ); } ~CGrowableArray() { RemoveAll(); } const TYPE& operator[]( int nIndex ) const { return GetAt( nIndex ); } TYPE& operator[]( int nIndex ) { return GetAt( nIndex ); } CGrowableArray& operator=( const CGrowableArray& a ) { if( this == &a ) return *this; RemoveAll(); for( int i=0; i < a.m_nSize; i++ ) Add( a.m_pData[i] ); return *this; } HRESULT SetSize( int nNewMaxSize ); HRESULT Add( const TYPE& value ); HRESULT Insert( int nIndex, const TYPE& value ); HRESULT SetAt( int nIndex, const TYPE& value ); TYPE& GetAt( int nIndex ) { assert( nIndex >= 0 && nIndex < m_nSize ); return m_pData[nIndex]; } int GetSize() const { return m_nSize; } TYPE* GetData() { return m_pData; } bool Contains( const TYPE& value ){ return ( -1 != IndexOf( value ) ); } int IndexOf( const TYPE& value ) { return ( m_nSize > 0 ) ? IndexOf( value, 0, m_nSize ) : -1; } int IndexOf( const TYPE& value, int iStart ) { return IndexOf( value, iStart, m_nSize - iStart ); } int IndexOf( const TYPE& value, int nIndex, int nNumElements ); int LastIndexOf( const TYPE& value ) { return ( m_nSize > 0 ) ? LastIndexOf( value, m_nSize-1, m_nSize ) : -1; } int LastIndexOf( const TYPE& value, int nIndex ) { return LastIndexOf( value, nIndex, nIndex+1 ); } int LastIndexOf( const TYPE& value, int nIndex, int nNumElements ); HRESULT Remove( int nIndex ); void RemoveAll() { SetSize(0); } protected: TYPE* m_pData; // the actual array of data int m_nSize; // # of elements (upperBound - 1) int m_nMaxSize; // max allocated HRESULT SetSizeInternal( int nNewMaxSize ); // This version doesn't call ctor or dtor. }; //-------------------------------------------------------------------------------------- // Performs timer operations // Use DXUTGetGlobalTimer() to get the global instance //-------------------------------------------------------------------------------------- class CDXUTTimer { public: CDXUTTimer(); void Reset(); // resets the timer void Start(); // starts the timer void Stop(); // stop (or pause) the timer void Advance(); // advance the timer by 0.1 seconds double GetAbsoluteTime(); // get the absolute system time double GetTime(); // get the current time double GetElapsedTime(); // get the time that elapsed between GetElapsedTime() calls bool IsStopped(); // returns true if timer stopped protected: bool m_bUsingQPF; bool m_bTimerStopped; LONGLONG m_llQPFTicksPerSec; LONGLONG m_llStopTime; LONGLONG m_llLastElapsedTime; LONGLONG m_llBaseTime; }; //----------------------------------------------------------------------------- // Resource cache for textures, fonts, meshs, and effects. // Use DXUTGetGlobalResourceCache() to access the global cache //----------------------------------------------------------------------------- enum DXUTCACHE_SOURCELOCATION { DXUTCACHE_LOCATION_FILE, DXUTCACHE_LOCATION_RESOURCE }; struct DXUTCache_Texture { DXUTCACHE_SOURCELOCATION Location; TCHAR wszSource[MAX_PATH]; HMODULE hSrcModule; UINT Width; UINT Height; UINT Depth; UINT MipLevels; DWORD Usage; D3DFORMAT Format; D3DPOOL Pool; D3DRESOURCETYPE Type; IDirect3DBaseTexture9 *pTexture; }; struct DXUTCache_Font : public D3DXFONT_DESC { ID3DXFont *pFont; }; struct DXUTCache_Effect { DXUTCACHE_SOURCELOCATION Location; TCHAR wszSource[MAX_PATH]; HMODULE hSrcModule; DWORD dwFlags; ID3DXEffect *pEffect; }; class CDXUTResourceCache { public: ~CDXUTResourceCache(); public: HRESULT OnResetDevice( IDirect3DDevice9 *pd3dDevice ); HRESULT OnLostDevice(); HRESULT OnDestroyDevice(); protected: friend CDXUTResourceCache& DXUTGetGlobalResourceCache(); friend HRESULT DXUTInitialize3DEnvironment(); friend HRESULT DXUTReset3DEnvironment(); friend void DXUTCleanup3DEnvironment( bool bReleaseSettings ); CDXUTResourceCache() { } CGrowableArray< DXUTCache_Texture > m_TextureCache; CGrowableArray< DXUTCache_Effect > m_EffectCache; CGrowableArray< DXUTCache_Font > m_FontCache; }; CDXUTResourceCache& DXUTGetGlobalResourceCache(); //-------------------------------------------------------------------------------------- // Implementation of CGrowableArray //-------------------------------------------------------------------------------------- // This version doesn't call ctor or dtor. template< typename TYPE > HRESULT CGrowableArray::SetSizeInternal( int nNewMaxSize ) { if( nNewMaxSize < 0 ) { assert( false ); return E_INVALIDARG; } if( nNewMaxSize == 0 ) { // Shrink to 0 size & cleanup if( m_pData ) { free( m_pData ); m_pData = NULL; } m_nMaxSize = 0; m_nSize = 0; } else if( m_pData == NULL || nNewMaxSize > m_nMaxSize ) { // Grow array int nGrowBy = ( m_nMaxSize == 0 ) ? 16 : m_nMaxSize; nNewMaxSize = __max( nNewMaxSize, m_nMaxSize + nGrowBy ); TYPE* pDataNew = (TYPE*) realloc( m_pData, nNewMaxSize * sizeof(TYPE) ); if( pDataNew == NULL ) return E_OUTOFMEMORY; m_pData = pDataNew; m_nMaxSize = nNewMaxSize; } return S_OK; } //-------------------------------------------------------------------------------------- template< typename TYPE > HRESULT CGrowableArray::SetSize( int nNewMaxSize ) { int nOldSize = m_nSize; if( nOldSize > nNewMaxSize ) { // Removing elements. Call dtor. for( int i = nNewMaxSize; i < nOldSize; ++i ) m_pData[i].~TYPE(); } // Adjust buffer. Note that there's no need to check for error // since if it happens, nOldSize == nNewMaxSize will be true.) HRESULT hr = SetSizeInternal( nNewMaxSize ); if( nOldSize < nNewMaxSize ) { // Adding elements. Call ctor. for( int i = nOldSize; i < nNewMaxSize; ++i ) ::new (&m_pData[i]) TYPE; } return hr; } //-------------------------------------------------------------------------------------- template< typename TYPE > HRESULT CGrowableArray::Add( const TYPE& value ) { HRESULT hr; if( FAILED( hr = SetSizeInternal( m_nSize + 1 ) ) ) return hr; // Construct the new element ::new (&m_pData[m_nSize]) TYPE; // Assign m_pData[m_nSize] = value; ++m_nSize; return S_OK; } //-------------------------------------------------------------------------------------- template< typename TYPE > HRESULT CGrowableArray::Insert( int nIndex, const TYPE& value ) { HRESULT hr; // Validate index if( nIndex < 0 || nIndex > m_nSize ) { assert( false ); return E_INVALIDARG; } // Prepare the buffer if( FAILED( hr = SetSizeInternal( m_nSize + 1 ) ) ) return hr; // Shift the array MoveMemory( &m_pData[nIndex+1], &m_pData[nIndex], sizeof(TYPE) * (m_nSize - nIndex) ); // Construct the new element ::new (&m_pData[nIndex]) TYPE; // Set the value and increase the size m_pData[nIndex] = value; ++m_nSize; return S_OK; } //-------------------------------------------------------------------------------------- template< typename TYPE > HRESULT CGrowableArray::SetAt( int nIndex, const TYPE& value ) { // Validate arguments if( nIndex < 0 || nIndex >= m_nSize ) { assert( false ); return E_INVALIDARG; } m_pData[nIndex] = value; return S_OK; } //-------------------------------------------------------------------------------------- // Searches for the specified value and returns the index of the first occurrence // within the section of the data array that extends from iStart and contains the // specified number of elements. Returns -1 if value is not found within the given // section. //-------------------------------------------------------------------------------------- template< typename TYPE > int CGrowableArray::IndexOf( const TYPE& value, int iStart, int nNumElements ) { // Validate arguments if( iStart < 0 || iStart >= m_nSize || nNumElements < 0 || iStart + nNumElements > m_nSize ) { assert( false ); return -1; } // Search for( int i = iStart; i < (iStart + nNumElements); i++ ) { if( value == m_pData[i] ) return i; } // Not found return -1; } //-------------------------------------------------------------------------------------- // Searches for the specified value and returns the index of the last occurrence // within the section of the data array that contains the specified number of elements // and ends at iEnd. Returns -1 if value is not found within the given section. //-------------------------------------------------------------------------------------- template< typename TYPE > int CGrowableArray::LastIndexOf( const TYPE& value, int iEnd, int nNumElements ) { // Validate arguments if( iEnd < 0 || iEnd >= m_nSize || nNumElements < 0 || iEnd - nNumElements < 0 ) { assert( false ); return -1; } // Search for( int i = iEnd; i > (iEnd - nNumElements); i-- ) { if( value == m_pData[i] ) return i; } // Not found return -1; } //-------------------------------------------------------------------------------------- template< typename TYPE > HRESULT CGrowableArray::Remove( int nIndex ) { if( nIndex < 0 || nIndex >= m_nSize ) { assert( false ); return E_INVALIDARG; } // Destruct the element to be removed m_pData[nIndex].~TYPE(); // Compact the array and decrease the size MoveMemory( &m_pData[nIndex], &m_pData[nIndex+1], sizeof(TYPE) * (m_nSize - (nIndex+1)) ); --m_nSize; return S_OK; } #endif