-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResourceLibrary class and refactor TextureManager
We want to reuse the TextureManager logic in a few other places in our engine for handling assets. Create a ResourceLibrary template class and then refactor TextureManager to use it instead of code customized just for textures.
- Loading branch information
1 parent
1b8b2ea
commit 01bee96
Showing
9 changed files
with
165 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,19 @@ | ||
#ifndef GL_ADAGIO_TEXTURE2D_H | ||
#define GL_ADAGIO_TEXTURE2D_H | ||
|
||
#include "../resource/Asset.h" | ||
#include "TextureDimensions.h" | ||
|
||
namespace Adagio { | ||
typedef unsigned int TextureHandle; | ||
|
||
class Texture2D { | ||
class Texture2D : public Asset<TextureHandle, TextureDimensions> { | ||
public: | ||
explicit Texture2D(TextureHandle handle, TextureHandle secret, unsigned int width, unsigned int height); | ||
|
||
TextureHandle handle; | ||
|
||
unsigned int getWidth(); | ||
|
||
unsigned int getHeight(); | ||
|
||
TextureHandle getSecretId(); | ||
|
||
bool isValid(); | ||
|
||
private: | ||
TextureHandle secret; | ||
unsigned int width; | ||
unsigned int height; | ||
}; | ||
} | ||
#endif //GL_ADAGIO_TEXTURE2D_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef GL_ADAGIO_ASSET_H | ||
#define GL_ADAGIO_ASSET_H | ||
|
||
namespace Adagio { | ||
template<typename HandleType, typename MetadataType> | ||
class Asset { | ||
public: | ||
explicit Asset(HandleType handle, HandleType secret, const MetadataType &metadata) { | ||
this->handle = handle; | ||
this->secret = secret; | ||
this->metadata = metadata; | ||
} | ||
|
||
HandleType handle; | ||
|
||
HandleType getSecretId() const { | ||
return secret; | ||
} | ||
|
||
[[nodiscard]] bool isValid() const { | ||
return secret != 0; | ||
} | ||
|
||
MetadataType getMetadata() const { | ||
return metadata; | ||
} | ||
|
||
protected: | ||
HandleType secret; | ||
MetadataType metadata; | ||
}; | ||
} | ||
|
||
#endif //GL_ADAGIO_ASSET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef GL_ADAGIO_ASSETLOADER_H | ||
#define GL_ADAGIO_ASSETLOADER_H | ||
|
||
#include <utility> | ||
|
||
namespace Adagio { | ||
template<typename InternalAssetType, typename MetadataType> | ||
struct AssetLoader { | ||
virtual std::pair<InternalAssetType, MetadataType> load(const char *resource) = 0; | ||
|
||
virtual void unload(InternalAssetType asset) = 0; | ||
}; | ||
} | ||
|
||
#endif //GL_ADAGIO_ASSETLOADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef GL_ADAGIO_INTERNALASSETWITHSECRET_H | ||
#define GL_ADAGIO_INTERNALASSETWITHSECRET_H | ||
|
||
namespace Adagio { | ||
template<typename InternalAssetType, typename MetadataType, typename HandleType> | ||
struct InternalAssetWithSecret { | ||
HandleType secret; | ||
MetadataType metadata; | ||
InternalAssetType asset; | ||
}; | ||
} | ||
|
||
#endif //GL_ADAGIO_INTERNALASSETWITHSECRET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#ifndef GL_ADAGIO_RESOURCELIBRARY_H | ||
#define GL_ADAGIO_RESOURCELIBRARY_H | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include "Asset.h" | ||
#include "AssetLoader.h" | ||
#include "InternalAssetWithSecret.h" | ||
|
||
namespace Adagio { | ||
template<typename InternalAssetType, typename MetadataType, typename HandleType> | ||
class ResourceLibrary { | ||
typedef Asset<HandleType, MetadataType> ResourceType; | ||
typedef InternalAssetWithSecret<InternalAssetType, MetadataType, HandleType> ResourceWithSecret; | ||
typedef AssetLoader<InternalAssetType, MetadataType> LoaderType; | ||
|
||
public: | ||
explicit ResourceLibrary(AssetLoader<InternalAssetType, MetadataType> *loader) { | ||
this->loader = loader; | ||
library.reserve(64); | ||
nextSecret = 1; | ||
} | ||
|
||
ResourceType load(std::string resource) { | ||
if (loadedFilenames.find(resource) != loadedFilenames.end()) { | ||
HandleType handle = loadedFilenames[resource] - 1; | ||
ResourceWithSecret internalAsset = library[handle]; | ||
return Asset<HandleType, MetadataType>(handle + 1, | ||
internalAsset.secret, | ||
internalAsset.metadata); | ||
} | ||
auto internalTexture = loader->load(resource.c_str()); | ||
auto dimensions = internalTexture.second; | ||
HandleType secret = nextSecret++; | ||
HandleType id; | ||
if (!freeHandles.empty()) { | ||
id = freeHandles.back(); | ||
freeHandles.pop_back(); | ||
library[id - 1] = {secret, dimensions, internalTexture.first}; | ||
} else { | ||
id = library.size() + 1; | ||
library.push_back({secret, dimensions, internalTexture.first}); | ||
} | ||
loadedFilenames[resource] = id; | ||
Asset<HandleType, MetadataType> handle(id, secret, dimensions); | ||
return handle; | ||
} | ||
|
||
void unload(const ResourceType &asset) { | ||
ResourceWithSecret internalTexture = library[asset.handle - 1]; | ||
internalTexture.secret = 0; | ||
loader->unload(internalTexture.asset); | ||
freeHandles.push_back(asset.handle); | ||
for (auto it = loadedFilenames.begin(); it != loadedFilenames.end(); it++) { | ||
if (it->second == asset.handle) { | ||
loadedFilenames.erase(it); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
InternalAssetType useResource(const ResourceType &asset) { | ||
ResourceWithSecret internalTexture = library[asset.handle - 1]; | ||
if (asset.getSecretId() != internalTexture.secret) { | ||
throw std::invalid_argument("Unloaded texture used."); | ||
} | ||
return internalTexture.asset; | ||
} | ||
|
||
private: | ||
AssetLoader<InternalAssetType, MetadataType> *loader; | ||
std::vector<ResourceWithSecret> library; | ||
std::vector<HandleType> freeHandles; | ||
std::unordered_map<std::string, HandleType> loadedFilenames; | ||
|
||
HandleType nextSecret; | ||
}; | ||
} | ||
#endif //GL_ADAGIO_RESOURCELIBRARY_H |