mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-06-07 14:06:36 +00:00
Removed the old SHAPE_ASSET macros Implements AssetRef struct that acts as a universal wrapper for an templated AssetPtr and AssetId pair Adds Type handling for AssetRef for ShapeAsset to unify handling in classes that utilize a shapeAsset, so assigning an assetPtr or an assetId will keep a record of the assignment in the event the assetPtr is invalid. Update all classes that utilized the old SHAPE_ASSET macros to utilize the AssetRef struct and updated the class code to utilize it to provide much more clean and concise code that isn't blocked behind macro definitions Added a new example class: shapeDatablockExample which allows render of a simple shape object utilizing a simple example datablock.
235 lines
7.6 KiB
C++
235 lines
7.6 KiB
C++
//-----------------------------------------------------------------------------
|
|
// Copyright (c) 2013 GarageGames, LLC
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to
|
|
// deal in the Software without restriction, including without limitation the
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
// IN THE SOFTWARE.
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#ifndef _ASSET_PTR_H_
|
|
#define _ASSET_PTR_H_
|
|
|
|
#ifndef _ASSET_MANAGER_H_
|
|
#include "assetManager.h"
|
|
#endif
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
class AssetPtrCallback
|
|
{
|
|
friend class AssetManager;
|
|
|
|
protected:
|
|
virtual void onAssetRefreshed( AssetPtrBase* pAssetPtrBase ) = 0;
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
class AssetPtrBase
|
|
{
|
|
public:
|
|
AssetPtrBase() {};
|
|
virtual ~AssetPtrBase()
|
|
{
|
|
// Un-register any notifications.
|
|
unregisterRefreshNotify();
|
|
};
|
|
|
|
/// Referencing.
|
|
virtual void clear( void ) = 0;
|
|
virtual void setAssetId( const char* pAssetId ) = 0;
|
|
virtual StringTableEntry getAssetId( void ) const = 0;
|
|
virtual StringTableEntry getAssetType( void ) const = 0;
|
|
virtual bool isAssetId( const char* pAssetId ) const = 0;
|
|
|
|
/// Validity.
|
|
virtual bool isNull( void ) const = 0;
|
|
virtual bool notNull( void ) const = 0;
|
|
|
|
/// Notification.
|
|
inline void registerRefreshNotify( AssetPtrCallback* pCallback )
|
|
{
|
|
// Sanity!
|
|
AssertFatal( AssetDatabase.isProperlyAdded(), "AssetPtrBase::registerRefreshNotify() - Cannot register an asset pointer with the asset system." );
|
|
|
|
// register refresh notify.
|
|
AssetDatabase.registerAssetPtrRefreshNotify( this, pCallback );
|
|
}
|
|
|
|
void unregisterRefreshNotify( void )
|
|
{
|
|
// Un-register the refresh notify if the asset system is available.
|
|
if ( AssetDatabase.isProperlyAdded() )
|
|
AssetDatabase.unregisterAssetPtrRefreshNotify( this );
|
|
}
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T> class AssetPtr : public AssetPtrBase
|
|
{
|
|
private:
|
|
SimObjectPtr<T> mpAsset;
|
|
|
|
public:
|
|
AssetPtr() {}
|
|
AssetPtr( const char* pAssetId )
|
|
{
|
|
// Finish if this is an invalid asset Id.
|
|
if ( pAssetId == NULL || *pAssetId == 0 )
|
|
return;
|
|
|
|
// Acquire asset.
|
|
mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
|
|
}
|
|
AssetPtr( const AssetPtr<T>& assetPtr )
|
|
{
|
|
// Does the asset pointer have an asset?
|
|
if ( assetPtr.notNull() )
|
|
{
|
|
// Yes, so acquire the asset.
|
|
mpAsset = AssetDatabase.acquireAsset<T>( assetPtr->getAssetId() );
|
|
}
|
|
}
|
|
virtual ~AssetPtr()
|
|
{
|
|
// Do we have an asset?
|
|
if ( notNull() )
|
|
{
|
|
// Yes, so release it.
|
|
AssetDatabase.releaseAsset( mpAsset->getAssetId() );
|
|
}
|
|
}
|
|
|
|
/// Assignment.
|
|
AssetPtr<T>& operator=( const char* pAssetId )
|
|
{
|
|
// Do we have an asset?
|
|
if ( notNull() )
|
|
{
|
|
// Yes, so finish if the asset Id is already assigned.
|
|
if ( isAssetId( pAssetId ) )
|
|
return *this;
|
|
|
|
// No, so release it.
|
|
AssetDatabase.releaseAsset( mpAsset->getAssetId() );
|
|
}
|
|
|
|
// Is the asset Id at least okay to attempt to acquire the asset?
|
|
if ( pAssetId != NULL && *pAssetId != 0 )
|
|
{
|
|
// Yes, so acquire the asset.
|
|
mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
|
|
}
|
|
else
|
|
{
|
|
// No, so remove reference.
|
|
mpAsset = NULL;
|
|
}
|
|
|
|
// Return Reference.
|
|
return *this;
|
|
}
|
|
|
|
AssetPtr<T>& operator=( const AssetPtr<T>& assetPtr )
|
|
{
|
|
// Set asset pointer.
|
|
*this = assetPtr->getAssetId();
|
|
|
|
// Return Reference.
|
|
return *this;
|
|
}
|
|
|
|
/// Referencing.
|
|
void clear( void ) override
|
|
{
|
|
// Do we have an asset?
|
|
if ( notNull() )
|
|
{
|
|
// Yes, so release it.
|
|
AssetDatabase.releaseAsset( mpAsset->getAssetId() );
|
|
}
|
|
|
|
// Reset the asset reference.
|
|
mpAsset = NULL;
|
|
}
|
|
|
|
T* operator->( void ) const { return mpAsset; }
|
|
T& operator*( void ) const { return *mpAsset; }
|
|
operator T*( void ) const { return mpAsset; }
|
|
void setAssetId( const char* pAssetId ) override { *this = pAssetId; }
|
|
StringTableEntry getAssetId( void ) const override { return isNull() ? StringTable->EmptyString() : mpAsset->getAssetId(); }
|
|
StringTableEntry getAssetType(void) const override { return isNull() ? StringTable->EmptyString() : mpAsset->getClassName(); }
|
|
bool isAssetId( const char* pAssetId ) const override { return pAssetId == NULL ? isNull() : getAssetId() == StringTable->insert(pAssetId); }
|
|
|
|
/// Validity.
|
|
bool isNull( void ) const override { return mpAsset.isNull(); }
|
|
bool notNull( void ) const override { return !mpAsset.isNull(); }
|
|
bool isValid(void) const { return notNull() && static_cast<AssetBase*>(mpAsset.getObject())->isAssetValid(); }
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// AssetRef is a simple struct that contains an asset Id and an AssetPtr.
|
|
// This is used in cases where we want to be able to track the asset Id even if the asset pointer is null (e.g. when the asset fails to load and we want to keep the asset Id around so that we can retry loading it later).
|
|
//-----------------------------------------------------------------------------
|
|
template <typename T>
|
|
struct AssetRef
|
|
{
|
|
StringTableEntry assetId;
|
|
AssetPtr<T> assetPtr;
|
|
|
|
AssetRef()
|
|
{
|
|
assetId = StringTable->EmptyString();
|
|
}
|
|
AssetRef<T>& operator=(const char* pAssetId)
|
|
{
|
|
assetId = StringTable->insert(pAssetId);
|
|
assetPtr = pAssetId;
|
|
|
|
// Return Reference.
|
|
return *this;
|
|
}
|
|
AssetRef<T>& operator=(const AssetPtr<T>& pAssetPtr)
|
|
{
|
|
if (pAssetPtr.notNull())
|
|
assetId = StringTable->insert(pAssetPtr->getAssetId());
|
|
|
|
assetPtr = pAssetPtr;
|
|
|
|
// Return Reference.
|
|
return *this;
|
|
}
|
|
AssetRef<T>& operator=(const AssetRef<T>& pAssetRef)
|
|
{
|
|
assetId = pAssetRef.assetId;
|
|
assetPtr = pAssetRef.assetPtr;
|
|
|
|
// Return Reference.
|
|
return *this;
|
|
}
|
|
|
|
StringTableEntry getAssetId(void) const { return assetId; }
|
|
bool isAssetId(const char* pAssetId) const { return assetId == StringTable->insert(pAssetId); }
|
|
bool hasAssetId() const { return assetId != StringTable->EmptyString(); }
|
|
|
|
/// Validity.
|
|
bool isNull(void) const { return assetPtr.isNull(); }
|
|
bool notNull(void) const { return !assetPtr.isNull(); }
|
|
bool isValid(void) const { return notNull() && static_cast<AssetBase*>(assetPtr)->isAssetValid(); }
|
|
};
|
|
#endif // _ASSET_PTR_H_
|