Files
itgmania212121/stepmania/src/RageTextureManager.cpp
T

146 lines
4.5 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
File: RageTextureManager.cpp
Desc: Interface for loading and releasing textures.
Copyright (c) 2001 Chris Danford. All rights reserved.
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "RageTextureManager.h"
#include "RageBitmapTexture.h"
#include "RageMovieTexture.h"
#include "RageUtil.h"
#include "RageHelper.h"
2001-11-03 10:52:42 +00:00
RageTextureManager* TEXTURE = NULL;
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// constructor/destructor
//-----------------------------------------------------------------------------
RageTextureManager::RageTextureManager( RageScreen* pScreen )
2001-11-03 10:52:42 +00:00
{
assert( pScreen != NULL );
m_pScreen = pScreen;
m_dwMaxTextureSize = 2048; // infinite size
m_dwTextureColorDepth = 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 );
HELPER.Log( "TEXTURE 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
//-----------------------------------------------------------------------------
RageTexture* RageTextureManager::LoadTexture( CString sTexturePath, const DWORD dwHints, const bool bForceReload )
2001-11-03 10:52:42 +00:00
{
sTexturePath.MakeLower();
// HELPER.Log( "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++;
HELPER.Log( ssprintf("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" )
pTexture = (RageTexture*) new RageMovieTexture( m_pScreen, sTexturePath );
2001-11-03 10:52:42 +00:00
else
pTexture = (RageTexture*) new RageBitmapTexture( m_pScreen, sTexturePath, m_dwMaxTextureSize, m_dwTextureColorDepth, dwHints );
2001-11-03 10:52:42 +00:00
HELPER.Log( "RageTextureManager: Loading '%s' (%ux%u) from disk. It has %d references",
sTexturePath,
pTexture->GetTextureWidth(),
pTexture->GetTextureHeight(),
pTexture->m_iRefCount
);
2001-11-03 10:52:42 +00:00
m_mapPathToTexture.SetAt( sTexturePath, pTexture );
}
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();
// HELPER.Log( "RageTextureManager::UnloadTexture(%s).", sTexturePath );
2001-11-03 10:52:42 +00:00
if( sTexturePath == "" )
{
HELPER.Log( "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
{
HELPER.Log( ssprintf("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
{
HELPER.Log( ssprintf("RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath, pTexture->m_iRefCount) );
}
}
else // texture not found
{
HELPER.FatalError( ssprintf("Tried to Unload texture '%s' that wasn't loaded.", sTexturePath) );
2001-11-03 10:52:42 +00:00
}
}