Allow to load image and textures cubemap/arrays from file using the AppFilesystemComponent

Renames LoadCubemap and LoadArray to Load with additional parameters and allows AppFilesystemComponent to pass additional parameters to them
This commit is contained in:
SirLynix
2023-04-04 08:24:52 +02:00
parent a3f7c437f4
commit 44aec8d5a5
6 changed files with 72 additions and 72 deletions

View File

@@ -37,16 +37,16 @@ namespace Nz
template<typename T> const typename T::Params* GetDefaultResourceParameters() const;
template<typename T> std::shared_ptr<T> Load(std::string_view assetPath);
template<typename T> std::shared_ptr<T> Load(std::string_view assetPath, typename T::Params params);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> Load(std::string_view assetPath, ExtraArgs&&... args);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> Load(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args);
const VirtualDirectoryPtr& Mount(std::string_view name, std::filesystem::path filepath);
const VirtualDirectoryPtr& Mount(std::string_view name, VirtualDirectoryPtr directory);
void MountDefaultDirectories();
template<typename T> std::shared_ptr<T> Open(std::string_view assetPath);
template<typename T> std::shared_ptr<T> Open(std::string_view assetPath, typename T::Params params);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> Open(std::string_view assetPath, ExtraArgs&&... args);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> Open(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args);
template<typename T> void SetDefaultResourceParameters(typename T::Params params);
@@ -56,8 +56,8 @@ namespace Nz
static inline void RegisterResourceTypes();
private:
template<typename T> std::shared_ptr<T> LoadImpl(std::string_view assetPath, const typename T::Params& params);
template<typename T> std::shared_ptr<T> OpenImpl(std::string_view assetPath, const typename T::Params& params);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
template<typename T, typename... ExtraArgs> std::shared_ptr<T> OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
std::vector<std::unique_ptr<ResourceParameters>> m_defaultParameters;
VirtualDirectoryPtr m_rootDirectory;

View File

@@ -34,14 +34,14 @@ namespace Nz
return static_cast<const typename T::Params*>(m_defaultParameters[resourceIndex].get());
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::Load(std::string_view assetPath)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::Load(std::string_view assetPath, ExtraArgs&&... args)
{
return Load<T>(assetPath, typename T::Params{});
return Load<T>(assetPath, typename T::Params{}, std::forward<ExtraArgs>(args)...);
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::Load(std::string_view assetPath, typename T::Params params)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::Load(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args)
{
if constexpr (Detail::ResourceParameterHasMerge<typename T::Params>::value)
{
@@ -49,17 +49,17 @@ namespace Nz
params.Merge(*defaultParams);
}
return LoadImpl<T>(assetPath, params);
return LoadImpl<T>(assetPath, params, std::forward<ExtraArgs>(args)...);
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath, ExtraArgs&&... args)
{
return Open<T>(assetPath, typename T::Params{});
return Open<T>(assetPath, typename T::Params{}, std::forward<ExtraArgs>(args)...);
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath, typename T::Params params)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args)
{
if constexpr (Detail::ResourceParameterHasMerge<typename T::Params>::value)
{
@@ -67,7 +67,7 @@ namespace Nz
params.Merge(*defaultParams);
}
return OpenImpl<T>(assetPath, params);
return OpenImpl<T>(assetPath, params, std::forward<ExtraArgs>(args)...);
}
template<typename T>
@@ -112,8 +112,8 @@ namespace Nz
throw std::runtime_error("Texture has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::LoadImpl(std::string_view assetPath, const typename T::Params& params)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args)
{
std::shared_ptr<T> resource;
if (!m_rootDirectory)
@@ -131,7 +131,7 @@ namespace Nz
}
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileEntry>)
{
resource = T::LoadFromStream(*arg.stream, params);
resource = T::LoadFromStream(*arg.stream, params, std::forward<ExtraArgs>(args)...);
return true;
}
else
@@ -143,8 +143,8 @@ namespace Nz
return resource;
}
template<typename T>
std::shared_ptr<T> AppFilesystemComponent::OpenImpl(std::string_view assetPath, const typename T::Params& params)
template<typename T, typename... ExtraArgs>
std::shared_ptr<T> AppFilesystemComponent::OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args)
{
std::shared_ptr<T> resource;
if (!m_rootDirectory)
@@ -162,7 +162,7 @@ namespace Nz
}
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileEntry>)
{
resource = T::OpenFromStream(*arg.stream, params);
resource = T::OpenFromStream(*arg.stream, params, std::forward<ExtraArgs>(args)...);
return true;
}
else

View File

@@ -90,14 +90,14 @@ namespace Nz
static std::shared_ptr<Texture> LoadFromStream(Stream& stream, const TextureParams& params);
// LoadArray
static std::shared_ptr<Texture> LoadArrayFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Texture> LoadArrayFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Texture> LoadArrayFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Texture> LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize);
static std::shared_ptr<Texture> LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize);
static std::shared_ptr<Texture> LoadFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize);
// LoadCubemap
static std::shared_ptr<Texture> LoadCubemapFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams());
static std::shared_ptr<Texture> LoadCubemapFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams());
static std::shared_ptr<Texture> LoadCubemapFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams());
static std::shared_ptr<Texture> LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams);
static std::shared_ptr<Texture> LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams);
static std::shared_ptr<Texture> LoadFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams);
};
}

View File

@@ -118,17 +118,17 @@ namespace Nz
static std::shared_ptr<Image> LoadFromMemory(const void* data, std::size_t size, const ImageParams& params = ImageParams());
static std::shared_ptr<Image> LoadFromStream(Stream& stream, const ImageParams& params = ImageParams());
// LoadArray
static std::shared_ptr<Image> LoadArrayFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Image> LoadArrayFromImage(const Image& image, const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Image> LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2));
static std::shared_ptr<Image> LoadArrayFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2));
// Load (array)
static std::shared_ptr<Image> LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize);
static std::shared_ptr<Image> LoadFromImage(const Image& image, const Vector2ui& atlasSize);
static std::shared_ptr<Image> LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize);
static std::shared_ptr<Image> LoadFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize);
// LoadCubemap
static std::shared_ptr<Image> LoadCubemapFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams());
static std::shared_ptr<Image> LoadCubemapFromImage(const Image& image, const CubemapParams& params = CubemapParams());
static std::shared_ptr<Image> LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams());
static std::shared_ptr<Image> LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams());
// Load (cubemap)
static std::shared_ptr<Image> LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams);
static std::shared_ptr<Image> LoadFromImage(const Image& image, const CubemapParams& params);
static std::shared_ptr<Image> LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams);
static std::shared_ptr<Image> LoadFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams);
struct SharedImage
{