Added a macro for declaring resource types to allow TResPtr to work without dynamic_cast

This commit is contained in:
parax0 2015-12-13 14:06:24 -07:00
parent aeb6cd08d6
commit b4b134d55b
21 changed files with 33 additions and 64 deletions

View File

@ -60,16 +60,19 @@ public:
TResPtr<ResType>& operator=(void *pPtr) TResPtr<ResType>& operator=(void *pPtr)
{ {
// todo: this probably crashes if you try to pass a non-CResource-derived pointer. is there a safer way? // todo: this probably crashes if you try to pass a non-CResource-derived pointer. is there a safer way?
// dynamic cast may be slow, but it's the only way I can think of to do type-checking without making a
// zillion redundant static functions on every resource class
if (mpRes) if (mpRes)
mpRes->Release(); mpRes->Release();
CResource *pRes = (CResource*) pPtr; CResource *pRes = (CResource*) pPtr;
mpRes = dynamic_cast<ResType*>((CResource*) pRes);
if (mpRes) if (ResType::StaticType() == pRes->Type())
{
mpRes = (ResType*) pRes;
mpRes->Lock(); mpRes->Lock();
}
else
mpRes = nullptr;
return *this; return *this;
} }

View File

@ -9,11 +9,6 @@ CAnimSet::~CAnimSet()
{ {
} }
EResType CAnimSet::Type()
{
return eAnimSet;
}
u32 CAnimSet::getNodeCount() u32 CAnimSet::getNodeCount()
{ {
return nodes.size(); return nodes.size();

View File

@ -10,6 +10,7 @@
// will expand later! this is where animation support will come in // will expand later! this is where animation support will come in
class CAnimSet : public CResource class CAnimSet : public CResource
{ {
DECLARE_RESOURCE_TYPE(eAnimSet)
friend class CAnimSetLoader; friend class CAnimSetLoader;
struct SNode struct SNode
@ -26,7 +27,6 @@ class CAnimSet : public CResource
public: public:
CAnimSet(); CAnimSet();
~CAnimSet(); ~CAnimSet();
EResType Type();
u32 getNodeCount(); u32 getNodeCount();
TString getNodeName(u32 node); TString getNodeName(u32 node);

View File

@ -10,11 +10,6 @@ CCollisionMeshGroup::~CCollisionMeshGroup()
delete *it; delete *it;
} }
EResType CCollisionMeshGroup::Type()
{
return eCollisionMeshGroup;
}
u32 CCollisionMeshGroup::NumMeshes() u32 CCollisionMeshGroup::NumMeshes()
{ {
return mMeshes.size(); return mMeshes.size();

View File

@ -8,12 +8,12 @@
class CCollisionMeshGroup : public CResource class CCollisionMeshGroup : public CResource
{ {
DECLARE_RESOURCE_TYPE(eCollisionMeshGroup)
std::vector<CCollisionMesh*> mMeshes; std::vector<CCollisionMesh*> mMeshes;
public: public:
CCollisionMeshGroup(); CCollisionMeshGroup();
~CCollisionMeshGroup(); ~CCollisionMeshGroup();
EResType Type();
u32 NumMeshes(); u32 NumMeshes();
CCollisionMesh* MeshByIndex(u32 index); CCollisionMesh* MeshByIndex(u32 index);

View File

@ -16,11 +16,6 @@ CFont::~CFont()
{ {
} }
EResType CFont::Type()
{
return eFont;
}
inline float PtsToFloat(s32 pt) inline float PtsToFloat(s32 pt)
{ {
// This is a bit of an arbitrary number but it works // This is a bit of an arbitrary number but it works

View File

@ -18,6 +18,7 @@ class CRenderer;
class CFont : public CResource class CFont : public CResource
{ {
DECLARE_RESOURCE_TYPE(eFont)
friend class CFontLoader; friend class CFontLoader;
static CDynamicVertexBuffer smGlyphVertices; // This is the vertex buffer used to draw glyphs. It has two attributes - Pos and Tex0. Tex0 should be updated for each glyph. static CDynamicVertexBuffer smGlyphVertices; // This is the vertex buffer used to draw glyphs. It has two attributes - Pos and Tex0. Tex0 should be updated for each glyph.
static CIndexBuffer smGlyphIndices; // This is the index buffer used to draw glyphs. It uses a triangle strip. static CIndexBuffer smGlyphIndices; // This is the index buffer used to draw glyphs. It uses a triangle strip.
@ -59,7 +60,6 @@ class CFont : public CResource
public: public:
CFont(); CFont();
~CFont(); ~CFont();
EResType Type();
CResource* MakeCopy(CResCache *pCopyCache); CResource* MakeCopy(CResCache *pCopyCache);
CVector2f RenderString(const TString& String, CRenderer *pRenderer, float AspectRatio, CVector2f RenderString(const TString& String, CRenderer *pRenderer, float AspectRatio,
CVector2f Position = CVector2f(0,0), CVector2f Position = CVector2f(0,0),

View File

@ -27,11 +27,6 @@ CGameArea::~CGameArea()
delete mLightLayers[lyr][lit]; delete mLightLayers[lyr][lit];
} }
EResType CGameArea::Type()
{
return eArea;
}
void CGameArea::AddWorldModel(CModel *mdl) void CGameArea::AddWorldModel(CModel *mdl)
{ {
mTerrainModels.push_back(mdl); mTerrainModels.push_back(mdl);

View File

@ -16,6 +16,7 @@ class CScriptObject;
class CGameArea : public CResource class CGameArea : public CResource
{ {
DECLARE_RESOURCE_TYPE(eArea)
friend class CAreaLoader; friend class CAreaLoader;
u32 mVertexCount; u32 mVertexCount;
@ -41,7 +42,6 @@ class CGameArea : public CResource
public: public:
CGameArea(); CGameArea();
~CGameArea(); ~CGameArea();
EResType Type();
void AddWorldModel(CModel *mdl); void AddWorldModel(CModel *mdl);
void MergeTerrain(); void MergeTerrain();

View File

@ -11,11 +11,6 @@ CResource::~CResource()
{ {
} }
EResType CResource::Type()
{
return eResource;
}
TString CResource::Source() TString CResource::Source()
{ {
return mResSource.GetFileName(); return mResSource.GetFileName();

View File

@ -9,8 +9,25 @@
class CResCache; class CResCache;
// This macro creates functions that allow us to easily identify this resource type.
// Must be included on every CResource subclass.
#define DECLARE_RESOURCE_TYPE(ResTypeEnum) \
public: \
virtual EResType Type() const \
{ \
return ResTypeEnum; \
} \
\
static EResType StaticType() \
{ \
return ResTypeEnum; \
} \
\
private: \
class CResource class CResource
{ {
DECLARE_RESOURCE_TYPE(eResource)
friend class CResCache; friend class CResCache;
TString mResSource; TString mResSource;
@ -20,7 +37,6 @@ class CResource
public: public:
CResource(); CResource();
virtual ~CResource(); virtual ~CResource();
virtual EResType Type();
TString Source(); TString Source();
TString FullSource(); TString FullSource();
CUniqueID ResID(); CUniqueID ResID();

View File

@ -13,11 +13,6 @@ CScan::~CScan()
{ {
} }
EResType CScan::Type()
{
return eScan;
}
EGame CScan::Version() EGame CScan::Version()
{ {
return mVersion; return mVersion;

View File

@ -8,6 +8,7 @@
class CScan : public CResource class CScan : public CResource
{ {
DECLARE_RESOURCE_TYPE(eScan)
friend class CScanLoader; friend class CScanLoader;
public: public:
@ -32,7 +33,6 @@ private:
public: public:
CScan(); CScan();
~CScan(); ~CScan();
EResType Type();
EGame Version(); EGame Version();
CStringTable* ScanText(); CStringTable* ScanText();
bool IsImportant(); bool IsImportant();

View File

@ -8,11 +8,6 @@ CStringTable::~CStringTable()
{ {
} }
EResType CStringTable::Type()
{
return eStringTable;
}
CResource* CStringTable::MakeCopy(CResCache*) CResource* CStringTable::MakeCopy(CResCache*)
{ {
// Not using parameter 1 (CResCache* - pResCache) // Not using parameter 1 (CResCache* - pResCache)

View File

@ -9,6 +9,7 @@
class CStringTable : public CResource class CStringTable : public CResource
{ {
DECLARE_RESOURCE_TYPE(eStringTable)
friend class CStringLoader; friend class CStringLoader;
std::vector<TString> mStringNames; std::vector<TString> mStringNames;
@ -24,7 +25,6 @@ class CStringTable : public CResource
public: public:
CStringTable(); CStringTable();
~CStringTable(); ~CStringTable();
EResType Type();
CResource* MakeCopy(CResCache *pCopyCache); CResource* MakeCopy(CResCache *pCopyCache);
// Getters // Getters

View File

@ -53,11 +53,6 @@ CTexture::~CTexture()
DeleteBuffers(); DeleteBuffers();
} }
EResType CTexture::Type()
{
return eTexture;
}
bool CTexture::BufferGL() bool CTexture::BufferGL()
{ {
glGenTextures(1, &mTextureID); glGenTextures(1, &mTextureID);

View File

@ -10,6 +10,7 @@
class CTexture : public CResource class CTexture : public CResource
{ {
DECLARE_RESOURCE_TYPE(eTexture)
friend class CTextureDecoder; friend class CTextureDecoder;
friend class CTextureEncoder; friend class CTextureEncoder;
@ -31,7 +32,6 @@ public:
CTexture(const CTexture& Source); CTexture(const CTexture& Source);
CTexture(u32 Width, u32 Height); CTexture(u32 Width, u32 Height);
~CTexture(); ~CTexture();
EResType Type();
bool BufferGL(); bool BufferGL();
void Bind(u32 GLTextureUnit); void Bind(u32 GLTextureUnit);

View File

@ -16,11 +16,6 @@ CWorld::~CWorld()
{ {
} }
EResType CWorld::Type()
{
return eWorld;
}
void CWorld::SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex) void CWorld::SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex)
{ {
// The AreaIndex parameter is a placeholder until an improved world loader is implemented. // The AreaIndex parameter is a placeholder until an improved world loader is implemented.

View File

@ -10,6 +10,7 @@
class CWorld : public CResource class CWorld : public CResource
{ {
DECLARE_RESOURCE_TYPE(eWorld)
friend class CWorldLoader; friend class CWorldLoader;
// Instances of CResource pointers are placeholders for unimplemented resource types (eg CMapWorld) // Instances of CResource pointers are placeholders for unimplemented resource types (eg CMapWorld)
@ -82,7 +83,6 @@ class CWorld : public CResource
public: public:
CWorld(); CWorld();
~CWorld(); ~CWorld();
EResType Type();
void SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex); void SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex);

View File

@ -16,11 +16,6 @@ CBasicModel::~CBasicModel()
delete mSurfaces[iSurf]; delete mSurfaces[iSurf];
} }
EResType CBasicModel::Type()
{
return eModel;
}
u32 CBasicModel::GetVertexCount() u32 CBasicModel::GetVertexCount()
{ {
return mVertexCount; return mVertexCount;

View File

@ -8,6 +8,7 @@
class CBasicModel : public CResource class CBasicModel : public CResource
{ {
DECLARE_RESOURCE_TYPE(eModel)
protected: protected:
CAABox mAABox; CAABox mAABox;
u32 mVertexCount; u32 mVertexCount;
@ -22,7 +23,6 @@ protected:
public: public:
CBasicModel(); CBasicModel();
~CBasicModel(); ~CBasicModel();
EResType Type();
u32 GetVertexCount(); u32 GetVertexCount();
u32 GetTriangleCount(); u32 GetTriangleCount();