// ---------------------------------------------------------------------------------------------- // This is the async thread. It will continue to check for pending resources to load until s_exitRequested // is set to true, at which point it will exit. DWORD WINAPI ResourceManager::ThreadProc (void* arg) { ResourceManager* mgr = (ResourceManager*)arg; while(!mgr->m_exitRequested) { WaitForSingleObject(mgr->m_EventHandle, INFINITE); if(mgr->m_exitRequested) { break; } std::wstring filename; Resource * res = NULL; bool pending = true; while(pending) { { // CRITICAL SECTION - BEGIN AutoSync sync(&mgr->m_criticalSection); // just grab the 1st one from the map, not worrying about queue order or anything like that. ResourceInfoMap::iterator it = mgr->m_pending.begin(); if(it == mgr->m_pending.end() ) { pending = false; continue; } assert(it != mgr->m_pending.end() ); // make a copy on purpose since we are going to leave the critical section very soon. filename = it->first; // we can use the info pointer here because the info must not be deleted // clients have access to the pointer. So, all we are going to do is move the // pointer from the pending map to the loaded map once we load the underlying resource. res = it->second; } // CRITICAL SECTION - END // do the actual 'load' here.....this could take some time....better not be in a critical section mgr->LoadResource(res, filename.c_str()); { // CRITICAL SECTION - BEGIN AutoSync sync(&mgr->m_criticalSection); mgr->m_loaded[filename] = res; mgr->m_pending.erase(filename); //imporntant to clear pending only after added to loading. } // CRITICAL SECTION - END for(auto it = mgr->m_listeners.begin(); it != mgr->m_listeners.end(); ++it) { ResourceListener * listener = (*it); listener->OnResourceLoaded(res); } } } return 0; }
int main() { ResourceManager *resman = ResourceManager::GetInstance(); int res = resman->LoadResource("Resources/Test.txt")->ID; //Resource* res2 = resman->LoadResource("Resources/bossMusic.wav"); Resource* res3 = resman->LoadResource("Resources/test.png"); Resource* res4 = resman->LoadResource("Resources/test.obj"); //res2->getAudioData()->audioPlay(); std::cout << res << std::endl; std::cout << res << std::endl; std::cout << resman->GetResourceUsers(res) << std::endl; resman->UnLoadResource(res); std::cout << resman->GetResourceUsers(res) << std::endl; std::vector<unsigned char> resvec = res3->getImageData(); resman->UnLoadResource(res); std::cout << resman->GetResourceUsers(res) << std::endl; system("pause"); return 0; }