3 #include "AssetEntity.h" 12 template <
class IDTYPE,
class ClassImpType = IDTYPE,
class ETYPE = AssetEntity>
17 typedef std::map<std::string, ETYPE*> AssetItemsNameMap_t;
18 typedef std::map<AssetKey, ETYPE*> AssetItemsSet_t;
22 static_assert(std::is_convertible<IDTYPE*, AssetEntity*>::value,
"Invalid Type for AssetManager!");
42 if (nRowIndex < (
int)
m_items.size())
45 std::advance(iter, nRowIndex);
51 inline static MyType_t& GetInstance() {
52 static MyType_t s_instance;
56 virtual const std::string& GetIdentifier() {
59 virtual void SetIdentifier(
const std::string& sName){
70 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
71 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
74 ETYPE* pAsset =itCurCP->second;
75 if(pAsset->GetRefCount()>1){
76 OUTPUT_LOG(
"warning: asset <%s> exits with ref %d\n", pAsset->m_key.c_str(), pAsset->GetRefCount()-1);
80 itCurCP->second->Release();
98 const char A = (char)
'A';
99 const char Z = (char)
'Z';
100 const char diff = (char)
'a' - A;
101 int nLen = (int)key.size();
102 outNameLowered = key;
104 for (
int i=0; i<nLen; ++i)
108 outNameLowered[i] = c + diff;
124 const AssetKey& key = entity->GetKey();
127 std::string sNameLowered;
133 if(iter->second != entity)
135 OUTPUT_LOG(
"warning: DeleteEntity %s with multiple candidates\n", key.c_str());
141 typename AssetItemsSet_t::iterator itCur =
m_items.find(key);
148 if(entity->GetRefCount() > 1)
150 OUTPUT_LOG(
"warning: you are deleting an entity %s whose has unreleased external references\n", entity->GetKey().c_str());
153 entity->UnloadAsset();
168 typename AssetItemsNameMap_t::iterator iter =
m_names.find(name);
184 typename AssetItemsNameMap_t::iterator iter =
m_names.find(name);
186 return (IDTYPE*)((*iter).second);
198 typename AssetItemsSet_t::iterator iter =
m_items.find(key);
206 std::string sNameLowered;
212 return iter1->second;
225 IDTYPE* pEntity =
new ClassImpType(key);
230 bool AddEntity(
const std::string& name, IDTYPE* pEntity)
232 const AssetKey& key = pEntity->GetKey();
233 ETYPE* pEntityOld =
get(key);
236 if (pEntityOld == pEntity)
257 std::string sNameLowered;
279 ETYPE* pEntity =
get(key);
294 return pair<IDTYPE*, bool>((IDTYPE*)pEntity,
false);
308 std::string sNameLowered;
312 return pair<IDTYPE*, bool>((IDTYPE*)pEntity,
true);
322 return (IDTYPE*)
get(name);
327 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
328 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
330 itCurCP->second->LoadAsset();
335 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
336 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
338 itCurCP->second->UnloadAsset();
343 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
344 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
346 itCurCP->second->GarbageCollectMe();
375 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
376 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP,++i)
378 pOutputFile->
WriteString(itCurCP->second->GetKey());
386 virtual void InitDeviceObjects(){
387 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
388 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
390 itCurCP->second->InitDeviceObjects();
393 virtual void RestoreDeviceObjects(){
394 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
395 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
397 itCurCP->second->RestoreDeviceObjects();
400 virtual void InvalidateDeviceObjects(){
401 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
402 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
404 itCurCP->second->InvalidateDeviceObjects();
407 virtual void DeleteDeviceObjects(){
408 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
409 for( itCurCP =
m_items.begin(); itCurCP != itEndCP; ++ itCurCP)
411 itCurCP->second->DeleteDeviceObjects();
418 typename AssetItemsSet_t::iterator itCurCP, itEndCP =
m_items.end();
419 for (itCurCP =
m_items.begin(); itCurCP != itEndCP; ++itCurCP)
421 itCurCP->second->RendererRecreated();
virtual bool DeleteEntity(ETYPE *entity)
decrease the reference count on the entity, and if it is negative, the asset will be unloaded...
Definition: AssetManager.h:121
void LoadAsset()
initialize all assets created so far to accelerate loading during game play.
Definition: AssetManager.h:326
IDTYPE * GetByName(const std::string &name)
check if there is a object with a specified name, and return the pointer to it.
Definition: AssetManager.h:182
virtual int PrintToFile(CParaFile *pOutputFile)
print all asset file to a given file.
Definition: AssetManager.h:370
void UnloadAsset()
uninitialize all assets created so far to save some memory
Definition: AssetManager.h:334
different physics engine has different winding order.
Definition: EventBinding.h:32
virtual IAttributeFields * GetChildAttributeObject(const std::string &sName)
get attribute by child object.
Definition: AssetManager.h:34
virtual void Cleanup()
delete all asset item from memory.
Definition: AssetManager.h:67
bool CheckRefresh(const std::string &sEntityName)
check if the entity exist, if so call Refresh().
Definition: AssetManager.h:355
AssetManager manages a set of asset entities of a certain type.
Definition: AssetManager.h:13
IDTYPE * GetEntity(const string &name)
get the entity by its entity key name
Definition: AssetManager.h:320
AssetItemsSet_t m_items
A set of all asset entities.
Definition: AssetManager.h:434
AssetItemsNameMap_t m_lowercase_item_maps
mapping from lower cased AssetKey to asset entity.
Definition: AssetManager.h:438
IDTYPE * NewEntity(const AssetKey &key)
just create the entity instance without adding to the manager.
Definition: AssetManager.h:224
virtual int GetChildAttributeColumnCount()
we support multi-dimensional child object.
Definition: AssetManager.h:40
void GarbageCollectAll()
Garbage Collect(free resources of) all unused entity.
Definition: AssetManager.h:342
A common interface for all classes implementing IAttributeFields By implementing this class's virtual...
Definition: IAttributeFields.h:59
it presents a real or virtual file in ParaEngine.
Definition: ParaFile.h:31
bool AddEntity(const std::string &name, IDTYPE *pEntity)
insert entity to the manager.
Definition: AssetManager.h:230
void DeleteByName(const std::string &name)
delete the object by user specified name.
Definition: AssetManager.h:166
void addref() const
add reference count of the object.
Definition: PERef.h:15
std::string AssetKey
the unique key object for asset entity.
Definition: AssetEntity.h:13
PE_CORE_DECL int WriteString(const string &sStr)
write string to file.
Definition: ParaFile.cpp:1126
std::string m_sName
identifier
Definition: AssetManager.h:426
pair< IDTYPE *, bool > CreateEntity(const string &name, const AssetKey &key)
Create a new entity object and add it to the manager.
Definition: AssetManager.h:277
AssetItemsNameMap_t m_names
human readable name of asset object, which can be used as key to retrieve entity from its manager Int...
Definition: AssetManager.h:430
void GetLowerCaseString(std::string &outNameLowered, const AssetKey &key)
get the lower cased key string.
Definition: AssetManager.h:97
virtual int GetChildAttributeObjectCount(int nColumnIndex=0)
get the number of child objects (row count) in the given column.
Definition: AssetManager.h:38
virtual void RendererRecreated()
callback of listening the event that renderer was recreated on Android/WP8 all opengl related id has ...
Definition: AssetManager.h:417