📄 dxutmisc.cpp
字号:
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
lstrcpyW( NewEntry.wszSource, pSrcFile );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.Depth = Depth;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_VOLUMETEXTURE;
(*ppTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateVolumeTextureFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, LPDIRECT3DVOLUMETEXTURE9 *ppVolumeTexture )
{
return CreateVolumeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppVolumeTexture );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::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 )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_TextureCache.GetSize(); ++i )
{
DXUTCache_Texture &Entry = m_TextureCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.Width == Width &&
Entry.Height == Height &&
Entry.Depth == Depth &&
Entry.MipLevels == MipLevels &&
Entry.Usage == Usage &&
Entry.Format == Format &&
Entry.Pool == Pool &&
Entry.Type == D3DRTYPE_VOLUMETEXTURE )
{
// A match is found. Obtain the IDirect3DVolumeTexture9 interface and return that.
return Entry.pTexture->QueryInterface( IID_IDirect3DVolumeTexture9, (LPVOID*)ppVolumeTexture );
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateVolumeTextureFromResourceEx( pDevice, hSrcModule, pSrcResource, Width, Height, Depth, MipLevels, Usage,
Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette, ppVolumeTexture );
if( FAILED( hr ) )
return hr;
DXUTCache_Texture NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
lstrcpyW( NewEntry.wszSource, pSrcResource );
NewEntry.Width = Width;
NewEntry.Height = Height;
NewEntry.Depth = Depth;
NewEntry.MipLevels = MipLevels;
NewEntry.Usage = Usage;
NewEntry.Format = Format;
NewEntry.Pool = Pool;
NewEntry.Type = D3DRTYPE_VOLUMETEXTURE;
(*ppVolumeTexture)->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&NewEntry.pTexture );
m_TextureCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::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 )
{
D3DXFONT_DESCW Desc;
Desc.Height = Height;
Desc.Width = Width;
Desc.Weight = Weight;
Desc.MipLevels = MipLevels;
Desc.Italic = Italic;
Desc.CharSet = (BYTE)CharSet;
Desc.OutputPrecision = (BYTE)OutputPrecision;
Desc.Quality = (BYTE)Quality;
Desc.PitchAndFamily = (BYTE)PitchAndFamily;
wcsncpy( Desc.FaceName, pFacename, LF_FACESIZE );
Desc.FaceName[LF_FACESIZE - 1] = L'\0';
return CreateFontIndirect( pDevice, &Desc, ppFont );
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateFontIndirect( LPDIRECT3DDEVICE9 pDevice, CONST D3DXFONT_DESC *pDesc, LPD3DXFONT *ppFont )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_FontCache.GetSize(); ++i )
{
DXUTCache_Font &Entry = m_FontCache[i];
if( Entry.Width == pDesc->Width &&
Entry.Height == pDesc->Height &&
Entry.Weight == pDesc->Weight &&
Entry.MipLevels == pDesc->MipLevels &&
Entry.Italic == pDesc->Italic &&
Entry.CharSet == pDesc->CharSet &&
Entry.OutputPrecision == pDesc->OutputPrecision &&
Entry.Quality == pDesc->Quality &&
Entry.PitchAndFamily == pDesc->PitchAndFamily &&
CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
Entry.FaceName, -1,
pDesc->FaceName, -1 ) == CSTR_EQUAL )
{
// A match is found. Increment the reference and return the ID3DXFont object.
Entry.pFont->AddRef();
*ppFont = Entry.pFont;
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateFontIndirect( pDevice, pDesc, ppFont );
if( FAILED( hr ) )
return hr;
DXUTCache_Font NewEntry;
(D3DXFONT_DESC &)NewEntry = *pDesc;
NewEntry.pFont = *ppFont;
NewEntry.pFont->AddRef();
m_FontCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateEffectFromFile( LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
{
DXUTCache_Effect &Entry = m_EffectCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_FILE &&
!lstrcmpW( Entry.wszSource, pSrcFile ) &&
Entry.dwFlags == Flags )
{
// A match is found. Increment the ref coutn and return the ID3DXEffect object.
*ppEffect = Entry.pEffect;
(*ppEffect)->AddRef();
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateEffectFromFile( pDevice, pSrcFile, pDefines, pInclude, Flags, pPool, ppEffect, ppCompilationErrors );
if( FAILED( hr ) )
return hr;
DXUTCache_Effect NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_FILE;
lstrcpyW( NewEntry.wszSource, pSrcFile );
NewEntry.dwFlags = Flags;
NewEntry.pEffect = *ppEffect;
NewEntry.pEffect->AddRef();
m_EffectCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::CreateEffectFromResource( LPDIRECT3DDEVICE9 pDevice, HMODULE hSrcModule, LPCTSTR pSrcResource, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXEFFECTPOOL pPool, LPD3DXEFFECT *ppEffect, LPD3DXBUFFER *ppCompilationErrors )
{
// Search the cache for a matching entry.
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
{
DXUTCache_Effect &Entry = m_EffectCache[i];
if( Entry.Location == DXUTCACHE_LOCATION_RESOURCE &&
Entry.hSrcModule == hSrcModule &&
!lstrcmpW( Entry.wszSource, pSrcResource ) &&
Entry.dwFlags == Flags )
{
// A match is found. Increment the ref coutn and return the ID3DXEffect object.
*ppEffect = Entry.pEffect;
(*ppEffect)->AddRef();
return S_OK;
}
}
HRESULT hr;
// No matching entry. Load the resource and create a new entry.
hr = D3DXCreateEffectFromResource( pDevice, hSrcModule, pSrcResource, pDefines, pInclude, Flags,
pPool, ppEffect, ppCompilationErrors );
if( FAILED( hr ) )
return hr;
DXUTCache_Effect NewEntry;
NewEntry.Location = DXUTCACHE_LOCATION_RESOURCE;
NewEntry.hSrcModule = hSrcModule;
lstrcpyW( NewEntry.wszSource, pSrcResource );
NewEntry.dwFlags = Flags;
NewEntry.pEffect = *ppEffect;
NewEntry.pEffect->AddRef();
m_EffectCache.Add( NewEntry );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Device event callbacks
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnCreateDevice( IDirect3DDevice9 *pd3dDevice )
{
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnResetDevice( IDirect3DDevice9 *pd3dDevice )
{
// Call OnResetDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnResetDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnResetDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnLostDevice()
{
// Call OnLostDevice on all effect and font objects
for( int i = 0; i < m_EffectCache.GetSize(); ++i )
m_EffectCache[i].pEffect->OnLostDevice();
for( int i = 0; i < m_FontCache.GetSize(); ++i )
m_FontCache[i].pFont->OnLostDevice();
// Release all the default pool textures
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
if( m_TextureCache[i].Pool == D3DPOOL_DEFAULT )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i ); // Remove the entry
}
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTResourceCache::OnDestroyDevice()
{
// Release all resources
for( int i = m_EffectCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_EffectCache[i].pEffect );
m_EffectCache.Remove( i );
}
for( int i = m_FontCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_FontCache[i].pFont );
m_FontCache.Remove( i );
}
for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i )
{
SAFE_RELEASE( m_TextureCache[i].pTexture );
m_TextureCache.Remove( i );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
CD3DArcBall::CD3DArcBall()
{
Reset();
m_vDownPt = D3DXVECTOR3(0,0,0);
m_vCurrentPt = D3DXVECTOR3(0,0,0);
m_Offset.x = m_Offset.y = 0;
RECT rc;
GetClientRect( GetForegroundWindow(), &rc );
SetWindow( rc.right, rc.bottom );
}
//--------------------------------------------------------------------------------------
void CD3DArcBall::Reset()
{
D3DXQuaternionIdentity( &m_qDown );
D3DXQuaternionIdentity( &m_qNow );
D3DXMatrixIdentity( &m_mRotation );
D3DXMatrixIdentity( &m_mTranslation );
D3DXMatrixIdentity( &m_mTranslationDelta );
m_bDrag = FALSE;
m_fRadiusTranslation = 1.0f;
m_fRadius = 1.0f;
}
//--------------------------------------------------------------------------------------
D3DXVECTOR3 CD3DArcBall::ScreenToVector( float fScreenPtX, float fScreenPtY )
{
// Scale to screen
FLOAT x = -(fScreenPtX - m_Offset.x - m_nWidth/2) / (m_fRadius*m_nWidth/2);
FLOAT y = (fScreenPtY - m_Offset.y - m_nHeight/2) / (m_fRadius*m_nHeight/2);
FLOAT z = 0.0f;
FLOAT mag = x*x + y*y;
if( mag > 1.0f )
{
FLOAT scale = 1.0f/sqrtf(mag);
x *= scale;
y *= scale;
}
else
z = sqrtf( 1.0f - mag );
// Return vector
return D3DXVECTOR3( x, y, z );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -