diff --git a/src/EterLib/ResourceManager.cpp b/src/EterLib/ResourceManager.cpp index 4c572f2..8c6a714 100644 --- a/src/EterLib/ResourceManager.cpp +++ b/src/EterLib/ResourceManager.cpp @@ -7,6 +7,8 @@ #include "ResourceManager.h" #include "GrpImage.h" +#include "TextureCache.h" +#include "DecodedImageData.h" int g_iLoadingDelayTime = 1; // Reduced from 20ms to 1ms for faster async loading @@ -68,7 +70,16 @@ void CResourceManager::ProcessBackgroundLoading() } //printf("REQ %s\n", stFileName.c_str()); - ms_loadingThread.Request(stFileName); + + if (m_pLoaderThreadPool) + { + m_pLoaderThreadPool->Request(stFileName); + } + else + { + ms_loadingThread.Request(stFileName); + } + m_WaitingMap.insert(TResourceRequestMap::value_type(dwFileCRC, stFileName)); itor = m_RequestMap.erase(itor); //break; // NOTE: 여기서 break 하면 천천히 로딩 된다. @@ -76,6 +87,44 @@ void CResourceManager::ProcessBackgroundLoading() DWORD dwCurrentTime = ELTimer_GetMSec(); + if (m_pLoaderThreadPool) + { + CFileLoaderThreadPool::TLoadResult result; + while (m_pLoaderThreadPool->Fetch(result)) + { + CResource * pResource = GetResourcePointer(result.stFileName.c_str()); + + if (pResource) + { + if (pResource->IsEmpty()) + { + if (result.hasDecodedImage) + { + CGraphicImage* pImage = dynamic_cast(pResource); + if (pImage) + { + pImage->OnLoadFromDecodedData(result.decodedImage); + } + else + { + pResource->OnLoad(result.File.size(), result.File.data()); + } + } + else + { + pResource->OnLoad(result.File.size(), result.File.data()); + } + + pResource->AddReferenceOnly(); + m_pResRefDecreaseWaitingMap.insert(TResourceRefDecreaseWaitingMap::value_type(dwCurrentTime, pResource)); + } + } + + m_WaitingMap.erase(GetCRC32(result.stFileName.c_str(), result.stFileName.size())); + } + } + + // Process old thread results CFileLoaderThread::TData * pData; while (ms_loadingThread.Fetch(&pData)) { @@ -528,12 +577,36 @@ void CResourceManager::ReserveDeletingResource(CResource * pResource) } CResourceManager::CResourceManager() + : m_pLoaderThreadPool(nullptr) + , m_pTextureCache(nullptr) { ms_loadingThread.Create(0); + + m_pLoaderThreadPool = new CFileLoaderThreadPool(); + if (!m_pLoaderThreadPool->Initialize()) + { + TraceError("CResourceManager: Failed to initialize FileLoaderThreadPool"); + delete m_pLoaderThreadPool; + m_pLoaderThreadPool = nullptr; + } + + m_pTextureCache = new CTextureCache(512); } CResourceManager::~CResourceManager() { Destroy(); ms_loadingThread.Shutdown(); + + if (m_pLoaderThreadPool) + { + delete m_pLoaderThreadPool; + m_pLoaderThreadPool = nullptr; + } + + if (m_pTextureCache) + { + delete m_pTextureCache; + m_pTextureCache = nullptr; + } } diff --git a/src/EterLib/ResourceManager.h b/src/EterLib/ResourceManager.h index 22308a5..71ee2b4 100644 --- a/src/EterLib/ResourceManager.h +++ b/src/EterLib/ResourceManager.h @@ -2,11 +2,14 @@ #include "Resource.h" #include "FileLoaderThread.h" +#include "FileLoaderThreadPool.h" #include #include #include +class CTextureCache; + class CResourceManager : public CSingleton { public: @@ -42,6 +45,9 @@ class CResourceManager : public CSingleton void ProcessBackgroundLoading(); void PushBackgroundLoadingSet(std::set & LoadingSet); + CTextureCache* GetTextureCache() { return m_pTextureCache; } + CFileLoaderThreadPool* GetLoaderThreadPool() { return m_pLoaderThreadPool; } + protected: void __DestroyDeletingResourceMap(); void __DestroyResourceMap(); @@ -68,6 +74,8 @@ class CResourceManager : public CSingleton TResourceRefDecreaseWaitingMap m_pResRefDecreaseWaitingMap; static CFileLoaderThread ms_loadingThread; + CFileLoaderThreadPool* m_pLoaderThreadPool; + CTextureCache* m_pTextureCache; }; extern int g_iLoadingDelayTime; \ No newline at end of file