2001-11-03 10:52:42 +00:00
|
|
|
#include "stdafx.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
2002-05-19 01:59:48 +00:00
|
|
|
Class: RageTextureManager
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Desc: See header.
|
2001-11-04 19:34:28 +00:00
|
|
|
|
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-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
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-07-27 19:29:51 +00:00
|
|
|
#include "RageException.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
RageTextureManager* TEXTUREMAN = NULL;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// constructor/destructor
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-11-11 04:53:31 +00:00
|
|
|
RageTextureManager::RageTextureManager( int iTextureColorDepth, int iSecondsBeforeUnload )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-11-11 04:53:31 +00:00
|
|
|
SetPrefs( iTextureColorDepth, iSecondsBeforeUnload );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageTextureManager::~RageTextureManager()
|
|
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
for( std::map<CString, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); ++i)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
RageTexture* pTexture = i->second;
|
2002-10-12 01:15:39 +00:00
|
|
|
if( pTexture->m_iRefCount )
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", i->first.GetString(), pTexture->m_iRefCount );
|
2002-03-30 20:00:13 +00:00
|
|
|
SAFE_DELETE( pTexture );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Load/Unload textures from disk
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-11-11 04:53:31 +00:00
|
|
|
RageTexture* RageTextureManager::LoadTexture( CString sTexturePath )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
sTexturePath.MakeLower();
|
|
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "RageTextureManager::LoadTexture(%s).", sTexturePath.GetString() );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
// holder for the new texture
|
2002-03-30 20:00:13 +00:00
|
|
|
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" ).
|
|
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
std::map<CString, RageTexture*>::iterator p = m_mapPathToTexture.find(sTexturePath);
|
|
|
|
|
if(p != m_mapPathToTexture.end()) {
|
|
|
|
|
pTexture = p->second;
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
pTexture->m_iRefCount++;
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "RageTextureManager: '%s' now has %d references.", sTexturePath.GetString(), pTexture->m_iRefCount );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
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;
|
2002-03-30 20:00:13 +00:00
|
|
|
splitpath( false, sTexturePath, sDrive, sDir, sFName, sExt );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-11-11 04:53:31 +00:00
|
|
|
// if( sExt == "avi" || sExt == "mpg" || sExt == "mpeg" )
|
|
|
|
|
// pTexture = new RageMovieTexture( sTexturePath );
|
|
|
|
|
// else
|
|
|
|
|
pTexture = new RageBitmapTexture( sTexturePath );
|
2002-01-24 08:01:24 +00:00
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "RageTextureManager: Finished loading '%s'.", sTexturePath.GetString() );
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
m_mapPathToTexture[sTexturePath] = pTexture;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-08-03 18:40:09 +00:00
|
|
|
// LOG->Trace( "Display: %.2f KB video memory left", DISPLAY->GetDevice()->GetAvailableTextureMem()/1000000.0f );
|
2002-06-29 11:59:09 +00:00
|
|
|
|
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-03-30 20:00:13 +00:00
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
return m_mapPathToTexture.find(sTexturePath) != m_mapPathToTexture.end();
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-19 19:18:40 +00:00
|
|
|
void RageTextureManager::GCTextures()
|
|
|
|
|
{
|
|
|
|
|
/* If m_iSecondsBeforeUnload is 0, then we'll unload textures immediately when
|
|
|
|
|
* they're no longer needed, to use as little extra memory as possible, so don't
|
|
|
|
|
* bother checking for expired textures here. */
|
|
|
|
|
if(!m_iSecondsBeforeUnload) return;
|
|
|
|
|
|
|
|
|
|
static int timeLastGarbageCollect = time(NULL);
|
|
|
|
|
if( timeLastGarbageCollect+m_iSecondsBeforeUnload/2 > time(NULL) )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Search for old textures with refcount==0 to unload
|
|
|
|
|
LOG->Trace("Performing texture garbage collection");
|
|
|
|
|
timeLastGarbageCollect = time(NULL);
|
|
|
|
|
|
|
|
|
|
for( std::map<CString, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); )
|
|
|
|
|
{
|
|
|
|
|
std::map<CString, RageTexture*>::iterator j = i;
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
CString sPath = j->first;
|
|
|
|
|
RageTexture* pTexture = j->second;
|
|
|
|
|
if( pTexture->m_iRefCount==0 &&
|
|
|
|
|
pTexture->m_iTimeOfLastUnload+m_iSecondsBeforeUnload < time(NULL) )
|
|
|
|
|
{
|
|
|
|
|
SAFE_DELETE( pTexture ); // free the texture
|
|
|
|
|
m_mapPathToTexture.erase(j); // and remove the key in the map
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-01-16 10:01:32 +00:00
|
|
|
void RageTextureManager::UnloadTexture( CString sTexturePath )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
sTexturePath.MakeLower();
|
|
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "RageTextureManager::UnloadTexture(%s).", sTexturePath.GetString() );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
if( sTexturePath == "" )
|
|
|
|
|
{
|
2002-07-31 19:40:40 +00:00
|
|
|
//LOG->Trace( "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
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
std::map<CString, RageTexture*>::iterator p = m_mapPathToTexture.find(sTexturePath);
|
|
|
|
|
if(p == m_mapPathToTexture.end())
|
2002-10-29 07:58:44 +00:00
|
|
|
throw RageException( "Tried to Unload texture '%s' that wasn't loaded.", sTexturePath.GetString() );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-19 19:18:40 +00:00
|
|
|
RageTexture* pTexture = p->second;
|
2002-09-07 08:20:10 +00:00
|
|
|
pTexture->m_iRefCount--;
|
2002-09-21 16:29:14 +00:00
|
|
|
pTexture->m_iTimeOfLastUnload = time(NULL);
|
2002-09-18 20:42:33 +00:00
|
|
|
ASSERT( pTexture->m_iRefCount >= 0 );
|
2002-10-19 19:18:40 +00:00
|
|
|
|
|
|
|
|
/* Always unload movies, so we don't waste time decoding.
|
|
|
|
|
*
|
|
|
|
|
* Actually, multiple refs to a movie won't work; they should play independently,
|
|
|
|
|
* but they'll actually share settings. Not worth fixing, since we don't currently
|
|
|
|
|
* using movies for anything except BGAs (though we could).
|
|
|
|
|
*
|
|
|
|
|
* Also, if texture caching is off, just remove it now instead of doing
|
|
|
|
|
* garbage collection. */
|
|
|
|
|
if( pTexture->m_iRefCount == 0 &&
|
|
|
|
|
(pTexture->IsAMovie() || !m_iSecondsBeforeUnload ))
|
2002-09-07 08:20:10 +00:00
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "RageTextureManager: '%s' will be deleted. It has %d references.", sTexturePath.GetString(), pTexture->m_iRefCount );
|
2002-09-21 16:29:14 +00:00
|
|
|
SAFE_DELETE( pTexture ); // free the texture
|
|
|
|
|
m_mapPathToTexture.erase(p); // and remove the key in the map
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-19 19:18:40 +00:00
|
|
|
GCTextures();
|
2002-09-21 16:29:14 +00:00
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
//LOG->Trace( "RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath.GetString(), pTexture->m_iRefCount );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
2002-04-28 20:42:32 +00:00
|
|
|
|
|
|
|
|
void RageTextureManager::ReloadAll()
|
|
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
for( std::map<CString, RageTexture*>::iterator i = m_mapPathToTexture.begin();
|
|
|
|
|
i != m_mapPathToTexture.end(); ++i)
|
2002-04-28 20:42:32 +00:00
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
RageTexture* pTexture = i->second;
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2002-11-11 04:53:31 +00:00
|
|
|
pTexture->Reload(); // this is not entirely correct. Hints are lost! XXX
|
2002-04-28 20:42:32 +00:00
|
|
|
}
|
2002-09-07 20:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-11 04:53:31 +00:00
|
|
|
void RageTextureManager::SetPrefs( int iTextureColorDepth, int iSecondsBeforeUnload )
|
2002-09-07 20:32:50 +00:00
|
|
|
{
|
2002-10-19 19:18:40 +00:00
|
|
|
m_iSecondsBeforeUnload = iSecondsBeforeUnload;
|
2002-09-18 20:42:33 +00:00
|
|
|
m_iTextureColorDepth = iTextureColorDepth;
|
2002-11-11 04:53:31 +00:00
|
|
|
ASSERT( m_iTextureColorDepth==16 || m_iTextureColorDepth==32 );
|
2002-09-07 20:32:50 +00:00
|
|
|
ReloadAll();
|
|
|
|
|
}
|