Files
itgmania212121/stepmania/src/RageTextureManager.cpp
T

260 lines
7.9 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.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"
2003-02-20 03:35:37 +00:00
#include "arch/MovieTexture/MovieTexture.h"
2001-11-03 10:52:42 +00:00
#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"
2003-02-20 03:36:57 +00:00
#include <time.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
//-----------------------------------------------------------------------------
2003-04-18 23:55:20 +00:00
RageTextureManager::RageTextureManager()
2001-11-03 10:52:42 +00:00
{
}
RageTextureManager::~RageTextureManager()
{
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
2002-09-07 08:20:10 +00:00
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 )
2003-04-25 00:01:35 +00:00
LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", i->first.filename.c_str(), pTexture->m_iRefCount );
SAFE_DELETE( pTexture );
2001-11-03 10:52:42 +00:00
}
}
void RageTextureManager::Update( float fDeltaTime )
{
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); ++i)
{
RageTexture* pTexture = i->second;
pTexture->Update( fDeltaTime );
}
}
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Load/Unload textures from disk
//-----------------------------------------------------------------------------
RageTexture* RageTextureManager::LoadTexture( RageTextureID ID )
2001-11-03 10:52:42 +00:00
{
2003-04-25 00:01:35 +00:00
Checkpoint( ssprintf( "RageTextureManager::LoadTexture(%s).", ID.filename.c_str() ) );
2001-11-03 10:52:42 +00:00
2003-05-22 05:28:37 +00:00
if( ID.iColorDepth == -1 )
ID.iColorDepth = m_iTextureColorDepth;
ID.iMaxSize = min( ID.iMaxSize, m_iMaxTextureResolution );
2003-04-19 22:37:00 +00:00
/* We could have two copies of the same bitmap if there are equivalent but
* different paths, e.g. "Bitmaps\me.bmp" and "..\Rage PC Edition\Bitmaps\me.bmp". */
std::map<RageTextureID, RageTexture*>::iterator p = m_mapPathToTexture.find(ID);
2003-05-22 05:28:37 +00:00
if(p != m_mapPathToTexture.end())
2001-11-03 10:52:42 +00:00
{
2003-02-12 22:32:04 +00:00
/* Found the texture. Just increase the refcount and return it. */
2003-05-22 05:28:37 +00:00
RageTexture* pTexture = p->second;
pTexture->m_iRefCount++;
2003-04-22 05:58:29 +00:00
LOG->UnmapLog( "LoadTexture" );
2003-05-22 05:28:37 +00:00
return pTexture;
}
2003-04-19 22:37:00 +00:00
// The texture is not already loaded. Load it.
2003-01-16 22:49:22 +00:00
CString sDir, sFName, sExt;
splitpath( ID.filename, sDir, sFName, sExt );
2003-03-11 19:07:03 +00:00
sExt.MakeLower();
2001-11-03 10:52:42 +00:00
RageTexture* pTexture;
2003-01-25 08:11:03 +00:00
if( sExt == ".avi" || sExt == ".mpg" || sExt == ".mpeg" )
2003-02-20 03:35:37 +00:00
pTexture = MakeRageMovieTexture( ID );
else
pTexture = new RageBitmapTexture( ID );
m_mapPathToTexture[ID] = pTexture;
2001-11-03 10:52:42 +00:00
2003-04-22 05:58:29 +00:00
LOG->UnmapLog( "LoadTexture" );
2001-11-03 10:52:42 +00:00
return pTexture;
}
2003-04-18 23:55:20 +00:00
void RageTextureManager::CacheTexture( RageTextureID ID )
{
RageTexture* pTexture = LoadTexture( ID );
pTexture->m_bCacheThis |= true;
UnloadTexture( pTexture );
}
2002-01-16 10:01:32 +00:00
2003-04-18 23:55:20 +00:00
void RageTextureManager::UnloadTexture( RageTexture *t )
{
2003-04-18 23:55:20 +00:00
t->m_iRefCount--;
ASSERT( t->m_iRefCount >= 0 );
2003-04-19 22:37:00 +00:00
if( t->m_iRefCount )
2003-04-19 22:38:10 +00:00
return; /* Can't unload textures that are still referenced. */
2003-04-19 22:37:00 +00:00
bool bDeleteThis = false;
2003-04-18 23:55:20 +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).
*/
2003-04-19 22:37:00 +00:00
if( t->IsAMovie() )
bDeleteThis = true;
/* If m_bDelayedDelete, keep all textures around until we GC. If m_bCacheThis,
* keep this individual texture, even if m_bDelayedDelete is off. (Would
* "m_bDelayedDelete" be clearer as "m_bCacheAll"?) */
if( !m_bDelayedDelete && !t->m_bCacheThis )
2003-04-19 22:37:00 +00:00
bDeleteThis = true;
if( bDeleteThis )
DeleteTexture( t );
2003-04-18 23:55:20 +00:00
}
void RageTextureManager::DeleteTexture( RageTexture *t )
{
ASSERT( t->m_iRefCount==0 );
2003-04-25 00:01:35 +00:00
LOG->Trace( "RageTextureManager: deleting '%s'.", t->GetID().filename.c_str() );
2003-05-22 05:28:37 +00:00
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); i++ )
{
if( i->second == t )
{
m_mapPathToTexture.erase( i ); // remove map entry
SAFE_DELETE( t ); // free the texture
return;
}
}
ASSERT(0); // we tried to delete a texture that wasn't loaded.
2003-04-18 23:55:20 +00:00
}
2003-04-18 23:55:20 +00:00
void RageTextureManager::GarbageCollect( GCType type )
{
// Search for old textures with refcount==0 to unload
2003-04-18 23:55:20 +00:00
LOG->Trace("Performing texture garbage collection.");
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); )
{
std::map<RageTextureID, RageTexture*>::iterator j = i;
i++;
CString sPath = j->first.filename;
2003-04-18 23:55:20 +00:00
RageTexture* t = j->second;
2003-04-19 22:37:00 +00:00
if( t->m_iRefCount )
continue; /* Can't unload textures that are still referenced. */
2003-04-19 22:37:00 +00:00
bool bDeleteThis = false;
if( type==cached_textures && !m_bDelayedDelete )
bDeleteThis = true;
if( type==delayed_delete )
bDeleteThis = true;
if( bDeleteThis )
DeleteTexture( t );
2001-11-03 10:52:42 +00:00
}
}
2002-04-28 20:42:32 +00:00
void RageTextureManager::ReloadAll()
{
for( std::map<RageTextureID, RageTexture*>::iterator i = m_mapPathToTexture.begin();
2002-09-07 08:20:10 +00:00
i != m_mapPathToTexture.end(); ++i)
2002-04-28 20:42:32 +00:00
{
i->second->Reload();
2002-04-28 20:42:32 +00:00
}
2002-09-07 20:32:50 +00:00
}
/* In some cases, changing the display mode will reset the rendering context,
* releasing all textures. We don't want to reload immediately if that happens,
* since we might be changing texture preferences too, which also may have to reload
* textures. Instead, tell all textures that their texture ID is invalid, so it
* doesn't try to free it later when we really do reload (since that ID might be
* associated with a different texture). Ack. */
void RageTextureManager::InvalidateTextures()
2002-09-07 20:32:50 +00:00
{
2003-01-21 10:32:08 +00:00
std::map<RageTextureID, RageTexture*>::iterator i;
for( i = m_mapPathToTexture.begin(); i != m_mapPathToTexture.end(); ++i)
{
RageTexture* pTexture = i->second;
pTexture->Invalidate();
}
2003-01-20 22:12:43 +00:00
/* We're going to have to reload all loaded textures. Let's get rid
* of all unreferenced textures, so we don't reload a ton of cached
2003-01-21 10:32:08 +00:00
* data that we're not necessarily going to use.
*
* This must be done *after* we Invalidate above, to let the texture
* know its OpenGL texture number is invalid. If we don't do that,
* it'll try to free it. */
2003-01-20 22:12:43 +00:00
for( i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); )
{
std::map<RageTextureID, RageTexture*>::iterator j = i;
i++;
CString sPath = j->first.filename;
RageTexture* pTexture = j->second;
if( pTexture->m_iRefCount==0 )
{
SAFE_DELETE( pTexture ); // free the texture
m_mapPathToTexture.erase(j); // and remove the key in the map
}
}
}
2003-04-18 23:55:20 +00:00
bool RageTextureManager::SetPrefs( int iTextureColorDepth, bool bDelayedDelete, int iMaxTextureResolution )
{
bool need_reload = false;
2003-04-18 23:55:20 +00:00
if( m_bDelayedDelete != bDelayedDelete ||
2003-01-11 20:15:00 +00:00
m_iTextureColorDepth != iTextureColorDepth ||
m_iMaxTextureResolution != iMaxTextureResolution )
need_reload = true;
2003-04-18 23:55:20 +00:00
m_bDelayedDelete = bDelayedDelete;
2002-09-18 20:42:33 +00:00
m_iTextureColorDepth = iTextureColorDepth;
2003-01-11 20:15:00 +00:00
m_iMaxTextureResolution = iMaxTextureResolution;
ASSERT( m_iTextureColorDepth==16 || m_iTextureColorDepth==32 );
return need_reload;
2002-09-07 20:32:50 +00:00
}
2003-04-24 19:51:27 +00:00
void RageTextureManager::DiagnosticOutput() const
{
unsigned cnt = distance(m_mapPathToTexture.begin(), m_mapPathToTexture.end());
LOG->Trace("%u textures loaded:", cnt);
for( std::map<RageTextureID, RageTexture*>::const_iterator i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); ++i )
{
const RageTextureID &ID = i->first;
const RageTexture *tex = i->second;
/* This could output much more, but I only need resolution now and I don't
* want it to be more than one line. */
LOG->Trace(" %3ix%3i (%2i) %s",
tex->GetTextureHeight(), tex->GetTextureWidth(),
tex->m_iRefCount, Basename(ID.filename).c_str() );
}
}