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
{

View File

@ -114,54 +114,54 @@ namespace Nz
return CreateFromImage(*image, params);
}
std::shared_ptr<Texture> Texture::LoadArrayFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize)
std::shared_ptr<Texture> Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadArrayFromFile(filePath, textureParams, atlasSize);
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, textureParams, atlasSize);
if (!image)
return {};
return CreateFromImage(*image, textureParams);
}
std::shared_ptr<Texture> Texture::LoadArrayFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize)
std::shared_ptr<Texture> Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadArrayFromMemory(data, size, textureParams, atlasSize);
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, textureParams, atlasSize);
if (!image)
return {};
return CreateFromImage(*image, textureParams);
}
std::shared_ptr<Texture> Texture::LoadArrayFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize)
std::shared_ptr<Texture> Texture::LoadFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadArrayFromStream(stream, textureParams, atlasSize);
std::shared_ptr<Image> image = Image::LoadFromStream(stream, textureParams, atlasSize);
if (!image)
return {};
return CreateFromImage(*image, textureParams);
}
std::shared_ptr<Texture> Texture::LoadCubemapFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams)
std::shared_ptr<Texture> Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadCubemapFromFile(filePath, textureParams, cubemapParams);
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, textureParams, cubemapParams);
if (!image)
return {};
return CreateFromImage(*image, textureParams);
}
std::shared_ptr<Texture> Texture::LoadCubemapFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams)
std::shared_ptr<Texture> Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadCubemapFromMemory(data, size, textureParams, cubemapParams);
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, textureParams, cubemapParams);
if (!image)
return {};
return CreateFromImage(*image, textureParams);
}
std::shared_ptr<Texture> Texture::LoadCubemapFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams)
std::shared_ptr<Texture> Texture::LoadFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadCubemapFromStream(stream, textureParams, cubemapParams);
std::shared_ptr<Image> image = Image::LoadFromStream(stream, textureParams, cubemapParams);
if (!image)
return {};

View File

@ -854,7 +854,7 @@ namespace Nz
}
// LoadArray
std::shared_ptr<Image> Image::LoadArrayFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize)
std::shared_ptr<Image> Image::LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, imageParams);
if (!image)
@ -863,10 +863,10 @@ namespace Nz
return nullptr;
}
return LoadArrayFromImage(*image, atlasSize);
return LoadFromImage(*image, atlasSize);
}
std::shared_ptr<Image> Image::LoadArrayFromImage(const Image& image, const Vector2ui& atlasSize)
std::shared_ptr<Image> Image::LoadFromImage(const Image& image, const Vector2ui& atlasSize)
{
NazaraAssert(image.IsValid(), "Invalid image");
@ -931,7 +931,7 @@ namespace Nz
return arrayImage;
}
std::shared_ptr<Image> Image::LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize)
std::shared_ptr<Image> Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, imageParams);
if (!image)
@ -940,10 +940,10 @@ namespace Nz
return nullptr;
}
return LoadArrayFromImage(*image, atlasSize);
return LoadFromImage(*image, atlasSize);
}
std::shared_ptr<Image> Image::LoadArrayFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize)
std::shared_ptr<Image> Image::LoadFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
std::shared_ptr<Image> image = Image::LoadFromStream(stream, imageParams);
if (!image)
@ -952,10 +952,10 @@ namespace Nz
return nullptr;
}
return LoadArrayFromImage(*image, atlasSize);
return LoadFromImage(*image, atlasSize);
}
std::shared_ptr<Image> Image::LoadCubemapFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams)
std::shared_ptr<Image> Image::LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, imageParams);
if (!image)
@ -964,10 +964,10 @@ namespace Nz
return nullptr;
}
return LoadCubemapFromImage(*image, cubemapParams);
return LoadFromImage(*image, cubemapParams);
}
std::shared_ptr<Image> Image::LoadCubemapFromImage(const Image& image, const CubemapParams& params)
std::shared_ptr<Image> Image::LoadFromImage(const Image& image, const CubemapParams& params)
{
NazaraAssert(image.IsValid(), "Invalid image");
@ -1044,17 +1044,17 @@ namespace Nz
return nullptr;
}
cubemap->Copy(image, Rectui(backPos.x, backPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeZ)));
cubemap->Copy(image, Rectui(downPos.x, downPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeY)));
cubemap->Copy(image, Rectui(forwardPos.x, forwardPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveZ)));
cubemap->Copy(image, Rectui(leftPos.x, leftPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeX)));
cubemap->Copy(image, Rectui(rightPos.x, rightPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveX)));
cubemap->Copy(image, Rectui(upPos.x, upPos.y, faceSize, faceSize), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveY)));
cubemap->Copy(image, Boxui(backPos.x, backPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeZ)));
cubemap->Copy(image, Boxui(downPos.x, downPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeY)));
cubemap->Copy(image, Boxui(forwardPos.x, forwardPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveZ)));
cubemap->Copy(image, Boxui(leftPos.x, leftPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::NegativeX)));
cubemap->Copy(image, Boxui(rightPos.x, rightPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveX)));
cubemap->Copy(image, Boxui(upPos.x, upPos.y, 0, faceSize, faceSize, 1), Vector3ui(0, 0, UnderlyingCast(CubemapFace::PositiveY)));
return cubemap;
}
std::shared_ptr<Image> Image::LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams)
std::shared_ptr<Image> Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, imageParams);
if (!image)
@ -1063,10 +1063,10 @@ namespace Nz
return nullptr;
}
return LoadCubemapFromImage(*image, cubemapParams);
return LoadFromImage(*image, cubemapParams);
}
std::shared_ptr<Image> Image::LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams)
std::shared_ptr<Image> Image::LoadFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
std::shared_ptr<Image> image = Image::LoadFromStream(stream, imageParams);
if (!image)
@ -1075,7 +1075,7 @@ namespace Nz
return nullptr;
}
return LoadCubemapFromImage(*image, cubemapParams);
return LoadFromImage(*image, cubemapParams);
}
bool Image::LoadFaceFromFile(CubemapFace face, const std::filesystem::path& filePath, const ImageParams& params)