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)
{
// 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)
mpRes->Release();
CResource *pRes = (CResource*) pPtr;
mpRes = dynamic_cast<ResType*>((CResource*) pRes);
if (mpRes)
if (ResType::StaticType() == pRes->Type())
{
mpRes = (ResType*) pRes;
mpRes->Lock();
}
else
mpRes = nullptr;
return *this;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,6 +18,7 @@ class CRenderer;
class CFont : public CResource
{
DECLARE_RESOURCE_TYPE(eFont)
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 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:
CFont();
~CFont();
EResType Type();
CResource* MakeCopy(CResCache *pCopyCache);
CVector2f RenderString(const TString& String, CRenderer *pRenderer, float AspectRatio,
CVector2f Position = CVector2f(0,0),

View File

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

View File

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

View File

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

View File

@ -9,8 +9,25 @@
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
{
DECLARE_RESOURCE_TYPE(eResource)
friend class CResCache;
TString mResSource;
@ -20,7 +37,6 @@ class CResource
public:
CResource();
virtual ~CResource();
virtual EResType Type();
TString Source();
TString FullSource();
CUniqueID ResID();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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