Files
itgmania212121/stepmania/src/RageTextureManager.cpp
T

259 lines
8.5 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-19 23:53:19 +00:00
#if !defined(LINUX)
# include "RageMovieTexture.h"
#endif
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-01-03 05:29:45 +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-01-11 20:15:00 +00:00
RageTextureManager::RageTextureManager( int iTextureColorDepth, int iSecondsBeforeUnload, int iMaxTextureResolution )
2001-11-03 10:52:42 +00:00
{
2003-01-11 20:15:00 +00:00
SetPrefs( iTextureColorDepth, iSecondsBeforeUnload, iMaxTextureResolution );
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 )
LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", i->first.filename.GetString(), 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-02-15 00:12:31 +00:00
/* Don't do this; just get case right to begin with. */
// ID.filename.MakeLower();
LOG->Trace( "RageTextureManager::LoadTexture(%s).", ID.filename.GetString() );
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" ).
/* This will return a texture that is equivalent to ID; here, that means it
* has the same filename. (see RageTextureID::operator<). Once we have that,
* we need to search through textures that are equivalent, looking for one
* that's equal. */
std::map<RageTextureID, RageTexture*>::iterator p = m_mapPathToTexture.find(ID);
while(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. */
p->second->m_iRefCount++;
return p->second;
}
2003-01-11 20:15: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 );
2001-11-03 10:52:42 +00:00
RageTexture* pTexture;
2003-02-19 18:13:06 +00:00
#ifndef LINUX
2003-01-25 08:11:03 +00:00
if( sExt == ".avi" || sExt == ".mpg" || sExt == ".mpeg" )
pTexture = new RageMovieTexture( ID );
else
pTexture = new RageBitmapTexture( ID );
2003-02-19 18:13:06 +00:00
#else
pTexture = new RageBitmapTexture(ID);
#endif
// LOG->Trace( "RageTextureManager: Loaded '%s'.", ID.filename.GetString() );
2002-04-28 20:42:32 +00:00
m_mapPathToTexture[ID] = pTexture;
2001-11-03 10:52:42 +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-09-07 08:20:10 +00:00
return m_mapPathToTexture.find(sTexturePath) != m_mapPathToTexture.end();
2002-01-16 10:01:32 +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<RageTextureID, RageTexture*>::iterator 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 &&
pTexture->m_iTimeOfLastUnload+m_iSecondsBeforeUnload < time(NULL) )
{
SAFE_DELETE( pTexture ); // free the texture
m_mapPathToTexture.erase(j); // and remove the key in the map
}
}
}
void RageTextureManager::UnloadTexture( RageTextureID ID )
2001-11-03 10:52:42 +00:00
{
ID.filename.MakeLower();
2001-11-03 10:52:42 +00:00
if( ID.filename == "" )
2001-11-03 10:52:42 +00:00
{
//LOG->Trace( "RageTextureManager::UnloadTexture(): tried to Unload a blank texture." );
2001-11-03 10:52:42 +00:00
return;
}
LOG->Trace( "RageTextureManager::UnloadTexture(%s).", ID.filename.GetString() );
std::map<RageTextureID, RageTexture*>::iterator p = m_mapPathToTexture.find(ID);
2002-09-07 08:20:10 +00:00
if(p == m_mapPathToTexture.end())
RageException::Throw( "Tried to Unload texture '%s' that wasn't loaded.", ID.filename.GetString() );
2001-11-03 10:52:42 +00:00
RageTexture* pTexture = p->second;
UnloadTexture(p->second);
//LOG->Trace( "RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath.GetString(), pTexture->m_iRefCount );
}
void RageTextureManager::UnloadTexture( RageTexture *t )
{
t->m_iRefCount--;
t->m_iTimeOfLastUnload = time(NULL);
ASSERT( t->m_iRefCount >= 0 );
/* 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( t->m_iRefCount == 0 &&
(t->IsAMovie() || !m_iSecondsBeforeUnload ))
2002-09-07 08:20:10 +00:00
{
// LOG->Trace( "RageTextureManager: '%s' will be deleted. It has %d references.", sTexturePath.GetString(), pTexture->m_iRefCount );
m_mapPathToTexture.erase(t->GetID()); // and remove the key in the map
SAFE_DELETE( t ); // free the texture
}
GCTextures();
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-01-11 20:15:00 +00:00
bool RageTextureManager::SetPrefs( int iTextureColorDepth, int iSecondsBeforeUnload, int iMaxTextureResolution )
{
bool need_reload = false;
if(m_iSecondsBeforeUnload != iSecondsBeforeUnload ||
2003-01-11 20:15:00 +00:00
m_iTextureColorDepth != iTextureColorDepth ||
m_iMaxTextureResolution != iMaxTextureResolution )
need_reload = true;
m_iSecondsBeforeUnload = iSecondsBeforeUnload;
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
}