2001-11-03 10:52:42 +00:00
|
|
|
#include "stdafx.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
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 <assert.h>
|
|
|
|
|
|
|
|
|
|
LPRageTextureManager TM = NULL;
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// constructor/destructor
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
RageTextureManager::RageTextureManager( LPRageScreen pScreen )
|
|
|
|
|
{
|
|
|
|
|
assert( pScreen != NULL );
|
|
|
|
|
m_pScreen = pScreen;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageTextureManager::~RageTextureManager()
|
|
|
|
|
{
|
|
|
|
|
// delete all textures
|
|
|
|
|
POSITION pos = m_mapPathToTexture.GetStartPosition();
|
|
|
|
|
CString key;
|
|
|
|
|
LPRageTexture value;
|
|
|
|
|
|
|
|
|
|
while( pos != NULL ) // iterate over all k/v pairs in map
|
|
|
|
|
{
|
|
|
|
|
m_mapPathToTexture.GetNextAssoc( pos, key, value );
|
|
|
|
|
SAFE_DELETE( value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_mapPathToTexture.RemoveAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Load/Unload textures from disk
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-03 20:45:08 +00:00
|
|
|
LPRageTexture RageTextureManager::LoadTexture( CString sTexturePath, DWORD dwHints, bool bForceReload )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
// RageLog( "RageTextureManager::LoadTexture(%s).", sTexturePath );
|
|
|
|
|
|
|
|
|
|
// holder for the new texture
|
|
|
|
|
LPRageTexture pTexture;
|
|
|
|
|
|
|
|
|
|
// 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" ).
|
|
|
|
|
sTexturePath.MakeLower();
|
|
|
|
|
|
|
|
|
|
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture already exists in the map
|
|
|
|
|
{
|
|
|
|
|
// RageLog( ssprintf("Found that '%s' is already loaded. Using the loaded copy.", sTexturePath) );
|
|
|
|
|
pTexture->m_iRefCount++;
|
|
|
|
|
}
|
2002-01-24 08:01:24 +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 );
|
|
|
|
|
|
|
|
|
|
if( sExt == "avi" || sExt == "mpg" || sExt == "mpeg" )
|
|
|
|
|
pTexture = (LPRageTexture) new RageMovieTexture( m_pScreen, sTexturePath );
|
|
|
|
|
else
|
2002-02-03 20:45:08 +00:00
|
|
|
pTexture = (LPRageTexture) new RageBitmapTexture( m_pScreen, sTexturePath, dwHints );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-01-24 08:01:24 +00:00
|
|
|
|
|
|
|
|
RageLog( "RageTextureManager: allocating space for '%s' (%ux%u).", sTexturePath, pTexture->GetTextureWidth(), pTexture->GetTextureHeight() );
|
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
// RageLog( "RageTextureManager::UnloadTexture(%s).", sTexturePath );
|
|
|
|
|
|
|
|
|
|
if( sTexturePath == "" )
|
|
|
|
|
{
|
2002-01-20 07:51:09 +00:00
|
|
|
RageLog( "RageTextureManager::UnloadTexture(): tried to Unload a blank" );
|
2001-11-03 10:52:42 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
|
|
|
|
if( !IsTextureLoaded( sTexturePath ) )
|
|
|
|
|
RageError( ssprintf("Tried to Unload a texture that wasn't loaded. '%s'", sTexturePath) );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
sTexturePath.MakeLower();
|
|
|
|
|
LPRageTexture pTexture;
|
|
|
|
|
|
|
|
|
|
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-01-20 07:51:09 +00:00
|
|
|
RageLog( ssprintf("RageTextureManager: deallocating '%s'.", sTexturePath) );
|
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
|
|
|
|
|
// RageLog( ssprintf("The texture '%s' has %d more references. It will not be deleted.",
|
|
|
|
|
// sTexturePath, pTexture->m_iRefCount) );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|