Files
itgmania212121/stepmania/src/RageTextureManager.cpp
T

161 lines
5.1 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
2002-05-19 01:59:48 +00:00
Class: RageTextureManager
2002-05-19 01:59:48 +00:00
Desc: See header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "RageTextureManager.h"
#include "RageBitmapTexture.h"
#include "RageMovieTexture.h"
#include "RageUtil.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-06-14 22:25:22 +00:00
2001-11-03 10:52:42 +00:00
2002-05-19 01:59:48 +00:00
RageTextureManager* TEXTUREMAN = NULL;
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// constructor/destructor
//-----------------------------------------------------------------------------
2002-05-19 01:59:48 +00:00
RageTextureManager::RageTextureManager( RageDisplay* pScreen )
2001-11-03 10:52:42 +00:00
{
assert( pScreen != NULL );
m_pScreen = pScreen;
2002-05-20 08:59:37 +00:00
m_iMaxTextureSize = 2048; // infinite size
m_iTextureColorDepth = 16;
2001-11-03 10:52:42 +00:00
}
RageTextureManager::~RageTextureManager()
{
// delete all textures
POSITION pos = m_mapPathToTexture.GetStartPosition();
CString sPath;
RageTexture* pTexture;
2001-11-03 10:52:42 +00:00
while( pos != NULL ) // iterate over all k/v pairs in map
{
m_mapPathToTexture.GetNextAssoc( pos, sPath, pTexture );
2002-05-19 01:59:48 +00:00
LOG->WriteLine( "TEXTUREMAN LEAK: '%s', RefCount = %d.", sPath, pTexture->m_iRefCount );
SAFE_DELETE( pTexture );
2001-11-03 10:52:42 +00:00
}
m_mapPathToTexture.RemoveAll();
}
//-----------------------------------------------------------------------------
// Load/Unload textures from disk
//-----------------------------------------------------------------------------
2002-05-19 01:59:48 +00:00
RageTexture* RageTextureManager::LoadTexture( CString sTexturePath, bool bForceReload, int iMipMaps, int iAlphaBits, bool bDither, bool bStretch )
2001-11-03 10:52:42 +00:00
{
sTexturePath.MakeLower();
2002-05-01 19:14:55 +00:00
// LOG->WriteLine( "RageTextureManager::LoadTexture(%s).", sTexturePath );
2001-11-03 10:52:42 +00:00
// holder for the new texture
RageTexture* pTexture;
2001-11-03 10:52:42 +00:00
// Convert the path to lowercase so that we don't load duplicates.
// Really, this does not solve the duplicate problem. We could have to copies
// of the same bitmap if there are equivalent but different paths
// (e.g. "Bitmaps\me.bmp" and "..\Rage PC Edition\Bitmaps\me.bmp" ).
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture already exists in the map
{
pTexture->m_iRefCount++;
2002-04-28 20:42:32 +00:00
if( bForceReload )
2002-07-02 00:27:58 +00:00
pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
2002-04-28 20:42:32 +00:00
2002-05-01 19:14:55 +00:00
LOG->WriteLine( "RageTextureManager: '%s' now has %d references.", sTexturePath, pTexture->m_iRefCount );
2001-11-03 10:52:42 +00:00
}
else // the texture is not already loaded
2001-11-03 10:52:42 +00:00
{
CString sDrive, sDir, sFName, sExt;
splitpath( false, sTexturePath, sDrive, sDir, sFName, sExt );
2001-11-03 10:52:42 +00:00
if( sExt == "avi" || sExt == "mpg" || sExt == "mpeg" )
2002-05-20 08:59:37 +00:00
pTexture = (RageTexture*) new RageMovieTexture( m_pScreen, sTexturePath, m_iMaxTextureSize, m_iTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
2001-11-03 10:52:42 +00:00
else
2002-05-20 08:59:37 +00:00
pTexture = (RageTexture*) new RageBitmapTexture( m_pScreen, sTexturePath, m_iMaxTextureSize, m_iTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
2001-11-03 10:52:42 +00:00
2002-05-19 01:59:48 +00:00
LOG->WriteLine( "RageTextureManager: Finished loading '%s' - %d references.", sTexturePath, pTexture->m_iRefCount );
2002-04-28 20:42:32 +00:00
2001-11-03 10:52:42 +00:00
m_mapPathToTexture.SetAt( sTexturePath, pTexture );
}
2002-06-29 11:59:09 +00:00
LOG->WriteLine( "Display: %u MB video memory left", DISPLAY->GetDevice()->GetAvailableTextureMem() );
2001-11-03 10:52:42 +00:00
return pTexture;
}
2002-01-16 10:01:32 +00:00
bool RageTextureManager::IsTextureLoaded( CString sTexturePath )
{
sTexturePath.MakeLower();
2002-01-16 10:01:32 +00:00
RageTexture* pTexture;
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture exists in the map
return true;
else
return false;
}
void RageTextureManager::UnloadTexture( CString sTexturePath )
2001-11-03 10:52:42 +00:00
{
sTexturePath.MakeLower();
2002-05-01 19:14:55 +00:00
// LOG->WriteLine( "RageTextureManager::UnloadTexture(%s).", sTexturePath );
2001-11-03 10:52:42 +00:00
if( sTexturePath == "" )
{
2002-06-29 11:59:09 +00:00
//LOG->WriteLine( "RageTextureManager::UnloadTexture(): tried to Unload a blank texture." );
2001-11-03 10:52:42 +00:00
return;
}
2002-01-16 10:01:32 +00:00
RageTexture* pTexture;
2001-11-03 10:52:42 +00:00
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture exists in the map
{
pTexture->m_iRefCount--;
if( pTexture->m_iRefCount == 0 ) // there are no more references to this texture
{
2002-06-29 11:59:09 +00:00
LOG->WriteLine( "RageTextureManager: '%s' will be deleted. It has %d references.", sTexturePath, pTexture->m_iRefCount );
2001-11-03 10:52:42 +00:00
SAFE_DELETE( pTexture ); // free the texture
m_mapPathToTexture.RemoveKey( sTexturePath ); // and remove the key in the map
}
else
{
2002-05-01 19:14:55 +00:00
LOG->WriteLine( "RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath, pTexture->m_iRefCount );
}
}
else // texture not found
{
2002-06-14 22:25:22 +00:00
throw RageException( "Tried to Unload texture '%s' that wasn't loaded.", sTexturePath );
2001-11-03 10:52:42 +00:00
}
}
2002-04-28 20:42:32 +00:00
void RageTextureManager::ReloadAll()
{
for( POSITION pos = m_mapPathToTexture.GetStartPosition(); pos != NULL; )
{
CString sPath;
RageTexture* pTexture;
m_mapPathToTexture.GetNextAssoc( pos, sPath, pTexture );
2002-05-20 08:59:37 +00:00
pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, 0 ); // this not entirely correct. Hints are lost!
2002-04-28 20:42:32 +00:00
}
}