143 lines
3.1 KiB
C++
143 lines
3.1 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "KResource.h"
|
|
#include "KTextCacheManager.h"
|
|
#include "KSmartPtr.h"
|
|
#include <toolkit/SafeTickCount.h>
|
|
|
|
//#include "SDebug_Util.h"
|
|
|
|
DECL_SPTR(K3DTexture)
|
|
|
|
struct KCacheItem
|
|
{
|
|
KCacheItem(K3DTexture* _pTex, DWORD _dwTime)
|
|
{
|
|
_spTex = _pTex;
|
|
_dwLastUseTime = _dwTime;
|
|
}
|
|
~KCacheItem()
|
|
{
|
|
}
|
|
|
|
K3DTextureSPtr _spTex;
|
|
DWORD _dwLastUseTime;
|
|
};
|
|
|
|
|
|
struct KCacheIterItem
|
|
{
|
|
KCacheIterItem(KCacheItem* pItem, const hashPr_TextCache::Key& prKey)
|
|
{
|
|
_pItem = pItem; _prKey = prKey;
|
|
}
|
|
~KCacheIterItem()
|
|
{
|
|
SAFE_DELETE( _pItem );
|
|
}
|
|
|
|
|
|
KCacheItem* _pItem;
|
|
hashPr_TextCache::Key _prKey;
|
|
};
|
|
|
|
struct PrCacheIterItem
|
|
{
|
|
bool operator()(const KCacheIterItem* pIterItem1, const KCacheIterItem* pIterItem2) const
|
|
{
|
|
return pIterItem1->_pItem->_dwLastUseTime > pIterItem2->_pItem->_dwLastUseTime;
|
|
}
|
|
};
|
|
|
|
|
|
namespace
|
|
{
|
|
const int MAX_CACHE_SIZE = 100;
|
|
}
|
|
KTextCacheManager::KTextCacheManager()
|
|
{
|
|
m_dwMaxCacheSize = MAX_CACHE_SIZE;
|
|
}
|
|
KTextCacheManager::~KTextCacheManager()
|
|
{
|
|
DestroyAll();
|
|
}
|
|
|
|
void KTextCacheManager::DestroyAll()
|
|
{
|
|
m_hashTextCache.clear();
|
|
|
|
SAFE_DELETE_VECTOR( m_vtTextCache );
|
|
}
|
|
|
|
K3DTexture* KTextCacheManager::FindCache(LPCSTR _lpszText, KColor _color, LPCSTR _lpszFont, int _nSize,
|
|
DWORD _dwWidth, DWORD _dwHeight, DWORD _dwFlag, KColor _color_fx)
|
|
{
|
|
hashPr_TextCache::Key key( _lpszText, _color, _lpszFont, _nSize, _dwWidth, _dwHeight, _dwFlag, _color_fx);
|
|
|
|
KCacheItem* pItem;
|
|
|
|
static DWORD TOTAL_FIND = 0;
|
|
static DWORD TOTAL_HIT = 0;
|
|
static DWORD HIT_RATIO = 100;
|
|
|
|
TOTAL_FIND++;
|
|
|
|
if( m_hashTextCache.lookup( key, pItem) )
|
|
{
|
|
TOTAL_HIT++;
|
|
pItem->_dwLastUseTime = GetSafeTickCount();
|
|
HIT_RATIO = DWORD( (float)TOTAL_HIT / (float) TOTAL_FIND * 100.f );
|
|
// _oprint("Cache Hit! Text Cache Hit Ratio is %d%%\n", HIT_RATIO);
|
|
return pItem->_spTex;
|
|
}
|
|
|
|
HIT_RATIO = DWORD( (float)TOTAL_HIT / (float) TOTAL_FIND * 100.f );
|
|
// _oprint("Cache Miss! Text Cache Hit Ratio is %d%%\n", HIT_RATIO);
|
|
return NULL;
|
|
}
|
|
|
|
bool KTextCacheManager::RegisterCache(LPCSTR _lpszText, KColor _color, LPCSTR _lpszFont, int _nSize,
|
|
DWORD _dwWidth, DWORD _dwHeight, DWORD _dwFlag, KColor _color_fx, K3DTexture* pTex)
|
|
{
|
|
if( m_vtTextCache.size() > m_dwMaxCacheSize )
|
|
_RemoveCacheItem();
|
|
|
|
|
|
hashPr_TextCache::Key key( _lpszText, _color, _lpszFont, _nSize, _dwWidth, _dwHeight, _dwFlag, _color_fx);
|
|
|
|
KCacheItem* pItem = NULL;
|
|
|
|
if( m_hashTextCache.has( key ) )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
pItem = new KCacheItem(pTex, GetSafeTickCount() );
|
|
|
|
m_hashTextCache.add( key, pItem);
|
|
|
|
KCacheIterItem* pIterItem = new KCacheIterItem( pItem, key);
|
|
m_vtTextCache.push_back( pIterItem);
|
|
|
|
return true;
|
|
}
|
|
|
|
void KTextCacheManager::_RemoveCacheItem()
|
|
{
|
|
// Random Access 로 10% 정도 지워볼까 -_-
|
|
DWORD dwDeleteCount = DWORD( (float)m_dwMaxCacheSize * 0.1f );
|
|
|
|
std::sort( m_vtTextCache.begin(), m_vtTextCache.end(), PrCacheIterItem() );
|
|
|
|
for(DWORD i = 0; i < dwDeleteCount; ++i)
|
|
{
|
|
KCacheIterItem* pIterItem = m_vtTextCache.back();
|
|
m_vtTextCache.erase( m_vtTextCache.end() - 1);
|
|
m_hashTextCache.erase( pIterItem->_prKey);
|
|
|
|
SAFE_DELETE( pIterItem );
|
|
}
|
|
}
|
|
|