My Project
AssetEntity.h
1 #pragma once
2 #include "IAttributeFields.h"
3 #include "VertexFVF.h"
4 
5 #include <set>
6 #include <map>
7 
8 namespace ParaEngine
9 {
13  typedef std::string AssetKey;
14 
25  struct AssetEntity : public IAttributeFields
26  {
27  public:
28  AssetEntity();
29  AssetEntity(const AssetKey& key);
30  virtual ~AssetEntity();
31  ATTRIBUTE_DEFINE_CLASS(AssetEntity);
32 
34  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
35 
36  ATTRIBUTE_METHOD1(AssetEntity, GetLocalFileName_s, const char**) { *p1 = cls->GetLocalFileName().c_str(); return S_OK; }
37  ATTRIBUTE_METHOD1(AssetEntity, SetLocalFileName_s, const char*) { cls->SetLocalFileName(p1); return S_OK; }
38 
39  ATTRIBUTE_METHOD1(AssetEntity, GetRemoteAssetURL_s, const char**) { static std::string s; s = cls->GetRemoteAssetURL().c_str(); *p1 = s.c_str(); return S_OK; }
40 
41  ATTRIBUTE_METHOD1(AssetEntity, IsValid_s, bool*) { *p1 = cls->IsValid(); return S_OK; }
42  ATTRIBUTE_METHOD1(AssetEntity, IsLoaded_s, bool*) { *p1 = cls->IsLoaded(); return S_OK; }
43  ATTRIBUTE_METHOD1(AssetEntity, IsLocked_s, bool*) { *p1 = cls->IsLocked(); return S_OK; }
44  ATTRIBUTE_METHOD1(AssetEntity, IsInitialized_s, bool*) { *p1 = cls->IsInitialized(); return S_OK; }
45 
46  ATTRIBUTE_METHOD1(AssetEntity, GetState_s, int*) { *p1 = (int)cls->GetState(); return S_OK; }
47  ATTRIBUTE_METHOD1(AssetEntity, SetState_s, int) { cls->SetState((AssetState)p1); return S_OK; }
48 
49  public:
50 
55  {
79  };
80 
82  enum AssetType
83  {
84  base=0,
85  texture,
86  mesh,
87  multianimation,
88  spritevertex,
89  font,
90  sound,
91  mdx,
92  parax,
93  database,
94  effectfile,
95  dllplugin,
96  datagrid,
97  cadMesh,
98  image,
99  };
100  virtual AssetType GetType(){return base;};
101 
102  public:
103  virtual const std::string& GetIdentifier();
104 
110  virtual bool AutoSync(bool bForceSync = false, bool bLazyLoading=true);
111 
118  virtual void Refresh(const char* sFilename=NULL,bool bLazyLoad = false);
119 
124  virtual const string& GetLocalFileName();
125  virtual void SetLocalFileName(const char* sFileName);
126 
128  virtual string GetRemoteAssetURL();
129 
133  virtual CParameterBlock* GetParamBlock(bool bCreateIfNotExist = false){return NULL;};
134 
135 
137  bool IsValid() {return m_bIsValid;};
138 
140  bool IsInitialized();
141 
146  virtual bool IsLoaded();
147 
153  virtual int Release();
154 
158  static const string& GetAssetServerUrl();
159 
163  static void SetAssetServerUrl(const char* pStr);
164 
165  public:
167  AssetKey& GetKey()
168  {
169  return m_key;
170  }
173  {
174  return m_assetState;
175  }
177  void SetState(AssetState state)
178  {
179  m_assetState = state;
180  }
183 
184  virtual HRESULT InitDeviceObjects(){m_bIsInitialized =true;return S_OK;};
185  virtual HRESULT RestoreDeviceObjects(){return S_OK;};
186  virtual HRESULT InvalidateDeviceObjects(){return S_OK;};
187  virtual HRESULT DeleteDeviceObjects(){m_bIsInitialized =false;return S_OK;};
191  virtual HRESULT RendererRecreated() { return S_OK; };
192 
195  virtual void Cleanup(){};
196 
204  void LoadAsset(){
205  if(!m_bIsInitialized)
206  {
207  InitDeviceObjects();
208  RestoreDeviceObjects();
209  //m_bIsInitialized = true;
210  }
211  };
215  void UnloadAsset(){
216  if(m_bIsInitialized)
217  {
218  InvalidateDeviceObjects();
219  DeleteDeviceObjects();
220  //m_bIsInitialized = false;
221  }
222  }
229  if(m_refcount<=0)
230  {
231  UnloadAsset();
232  m_refcount = 0;
233  }
234  }
235 
237  inline bool IsLocked() {return m_bIsLocked;}
238 
243  virtual bool Lock(int nMethod=0);
244 
247  virtual void UnLock();
248 
250  virtual bool GetBoundingBox(Vector3* pMin, Vector3* pMax) { return false; };
251  public:
253  AssetKey m_key;
254 
257 
260 
261  private:
263  AssetState m_assetState;
264 
266  bool m_bIsLocked;
267 
271  string m_localfilename;
272  };
273 }
274 
275 #include "AssetManager.h"
AssetState GetState()
return the asset state.
Definition: AssetEntity.h:172
static const string & GetAssetServerUrl()
get the asset server url.
Definition: AssetEntity.cpp:53
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
the asset is always remote.
Definition: AssetEntity.h:62
virtual CParameterBlock * GetParamBlock(bool bCreateIfNotExist=false)
get effect parameter block with this object.
Definition: AssetEntity.h:133
bool IsInitialized()
whether initialized.
Definition: AssetEntity.cpp:48
void SetState(AssetState state)
set the asset state.
Definition: AssetEntity.h:177
virtual bool GetBoundingBox(Vector3 *pMin, Vector3 *pMax)
Get AABB bounding box of the asset object.
Definition: AssetEntity.h:250
virtual int Release()
call this function to safely release this asset.
Definition: AssetEntity.cpp:254
virtual void Refresh(const char *sFilename=NULL, bool bLazyLoad=false)
refresh the asset from local disk.
Definition: AssetEntity.cpp:230
AssetType
each asset type has a unique asset type number
Definition: AssetEntity.h:82
virtual HRESULT RendererRecreated()
callback of listening the event that renderer was recreated on Android/WP8 all opengl related id has ...
Definition: AssetEntity.h:191
different physics engine has different winding order.
Definition: EventBinding.h:32
if a remote file can not be downloaded.
Definition: AssetEntity.h:75
the asset is local, which means that we have found a local disk version of the file.
Definition: AssetEntity.h:60
if file can not be found in local disk or in local cache, we will put it in sync start state...
Definition: AssetEntity.h:69
virtual bool IsLoaded()
most assets are loaded asynchronously.
Definition: AssetEntity.cpp:43
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
AssetKey m_key
this is the unique key object.
Definition: AssetEntity.h:250
virtual const string & GetLocalFileName()
the actual local file name.
Definition: AssetEntity.cpp:239
bool m_bIsInitialized
whether this entity is initialized;Entity is by default lazily initialized
Definition: AssetEntity.h:256
virtual string GetRemoteAssetURL()
get the remote server url of this asset.
Definition: AssetEntity.cpp:81
bool IsValid()
whether this is a valid resource object.
Definition: AssetEntity.h:137
virtual int InstallFields(CAttributeClass *pClass, bool bOverride)
this class should be implemented if one wants to add new attribute.
Definition: AssetEntity.cpp:265
if a remote file is downloaded to cache and loaded.
Definition: AssetEntity.h:72
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
the file is not found or failed to load
Definition: AssetEntity.h:78
virtual void UnLock()
UnLock the object.
Definition: AssetEntity.cpp:76
virtual void Cleanup()
Clean up additional resources.
Definition: AssetEntity.h:195
virtual bool AutoSync(bool bForceSync=false, bool bLazyLoading=true)
call this function to automatically sync with the remote server according to current asset state ...
Definition: AssetEntity.cpp:165
virtual IAttributeFields * GetAttributeObject()
get the attribute object of this asset.
Definition: AssetEntity.cpp:154
std::string AssetKey
the unique key object for asset entity.
Definition: AssetEntity.h:13
virtual bool Lock(int nMethod=0)
Lock the object.
Definition: AssetEntity.cpp:66
AssetKey & GetKey()
return the key object.
Definition: AssetEntity.h:167
bool m_bIsValid
whether this is a valid resource object.
Definition: AssetEntity.h:259
void GarbageCollectMe()
if its reference count is zero, unload this asset object.
Definition: AssetEntity.h:228
void UnloadAsset()
unload asset.
Definition: AssetEntity.h:215
void LoadAsset()
load asset.
Definition: AssetEntity.h:204
static void SetAssetServerUrl(const char *pStr)
set the asset server url.
Definition: AssetEntity.cpp:58
asset is from the local cache.
Definition: AssetEntity.h:65
Base class for managed asset entity in ParaEngine.
Definition: AssetEntity.h:25
asset is always created in normal state.
Definition: AssetEntity.h:57
a list of CParameter{name, value} pairs of anything.
Definition: ParameterBlock.h:108
bool IsLocked()
whether this is a locked resource resource.
Definition: AssetEntity.h:237
AssetState
the asset state.
Definition: AssetEntity.h:54