From 44aec8d5a56ee1d98c280891d47266708818e4d5 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Tue, 4 Apr 2023 08:24:52 +0200 Subject: [PATCH] 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 --- .../Nazara/Core/AppFilesystemComponent.hpp | 12 +++--- .../Nazara/Core/AppFilesystemComponent.inl | 36 ++++++++--------- include/Nazara/Renderer/Texture.hpp | 12 +++--- include/Nazara/Utility/Image.hpp | 20 +++++----- src/Nazara/Renderer/Texture.cpp | 24 +++++------ src/Nazara/Utility/Image.cpp | 40 +++++++++---------- 6 files changed, 72 insertions(+), 72 deletions(-) diff --git a/include/Nazara/Core/AppFilesystemComponent.hpp b/include/Nazara/Core/AppFilesystemComponent.hpp index 945a00722..42c198aea 100644 --- a/include/Nazara/Core/AppFilesystemComponent.hpp +++ b/include/Nazara/Core/AppFilesystemComponent.hpp @@ -37,16 +37,16 @@ namespace Nz template const typename T::Params* GetDefaultResourceParameters() const; - template std::shared_ptr Load(std::string_view assetPath); - template std::shared_ptr Load(std::string_view assetPath, typename T::Params params); + template std::shared_ptr Load(std::string_view assetPath, ExtraArgs&&... args); + template std::shared_ptr 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 std::shared_ptr Open(std::string_view assetPath); - template std::shared_ptr Open(std::string_view assetPath, typename T::Params params); + template std::shared_ptr Open(std::string_view assetPath, ExtraArgs&&... args); + template std::shared_ptr Open(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args); template void SetDefaultResourceParameters(typename T::Params params); @@ -56,8 +56,8 @@ namespace Nz static inline void RegisterResourceTypes(); private: - template std::shared_ptr LoadImpl(std::string_view assetPath, const typename T::Params& params); - template std::shared_ptr OpenImpl(std::string_view assetPath, const typename T::Params& params); + template std::shared_ptr LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args); + template std::shared_ptr OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args); std::vector> m_defaultParameters; VirtualDirectoryPtr m_rootDirectory; diff --git a/include/Nazara/Core/AppFilesystemComponent.inl b/include/Nazara/Core/AppFilesystemComponent.inl index 00c01534c..96a71c3ec 100644 --- a/include/Nazara/Core/AppFilesystemComponent.inl +++ b/include/Nazara/Core/AppFilesystemComponent.inl @@ -34,14 +34,14 @@ namespace Nz return static_cast(m_defaultParameters[resourceIndex].get()); } - template - std::shared_ptr AppFilesystemComponent::Load(std::string_view assetPath) + template + std::shared_ptr AppFilesystemComponent::Load(std::string_view assetPath, ExtraArgs&&... args) { - return Load(assetPath, typename T::Params{}); + return Load(assetPath, typename T::Params{}, std::forward(args)...); } - template - std::shared_ptr AppFilesystemComponent::Load(std::string_view assetPath, typename T::Params params) + template + std::shared_ptr AppFilesystemComponent::Load(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args) { if constexpr (Detail::ResourceParameterHasMerge::value) { @@ -49,17 +49,17 @@ namespace Nz params.Merge(*defaultParams); } - return LoadImpl(assetPath, params); + return LoadImpl(assetPath, params, std::forward(args)...); } - template - std::shared_ptr AppFilesystemComponent::Open(std::string_view assetPath) + template + std::shared_ptr AppFilesystemComponent::Open(std::string_view assetPath, ExtraArgs&&... args) { - return Open(assetPath, typename T::Params{}); + return Open(assetPath, typename T::Params{}, std::forward(args)...); } - template - std::shared_ptr AppFilesystemComponent::Open(std::string_view assetPath, typename T::Params params) + template + std::shared_ptr AppFilesystemComponent::Open(std::string_view assetPath, typename T::Params params, ExtraArgs&&... args) { if constexpr (Detail::ResourceParameterHasMerge::value) { @@ -67,7 +67,7 @@ namespace Nz params.Merge(*defaultParams); } - return OpenImpl(assetPath, params); + return OpenImpl(assetPath, params, std::forward(args)...); } template @@ -112,8 +112,8 @@ namespace Nz throw std::runtime_error("Texture has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); } - template - std::shared_ptr AppFilesystemComponent::LoadImpl(std::string_view assetPath, const typename T::Params& params) + template + std::shared_ptr AppFilesystemComponent::LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args) { std::shared_ptr resource; if (!m_rootDirectory) @@ -131,7 +131,7 @@ namespace Nz } else if constexpr (std::is_same_v) { - resource = T::LoadFromStream(*arg.stream, params); + resource = T::LoadFromStream(*arg.stream, params, std::forward(args)...); return true; } else @@ -143,8 +143,8 @@ namespace Nz return resource; } - template - std::shared_ptr AppFilesystemComponent::OpenImpl(std::string_view assetPath, const typename T::Params& params) + template + std::shared_ptr AppFilesystemComponent::OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args) { std::shared_ptr resource; if (!m_rootDirectory) @@ -162,7 +162,7 @@ namespace Nz } else if constexpr (std::is_same_v) { - resource = T::OpenFromStream(*arg.stream, params); + resource = T::OpenFromStream(*arg.stream, params, std::forward(args)...); return true; } else diff --git a/include/Nazara/Renderer/Texture.hpp b/include/Nazara/Renderer/Texture.hpp index d0938b506..b5e388eea 100644 --- a/include/Nazara/Renderer/Texture.hpp +++ b/include/Nazara/Renderer/Texture.hpp @@ -90,14 +90,14 @@ namespace Nz static std::shared_ptr LoadFromStream(Stream& stream, const TextureParams& params); // LoadArray - static std::shared_ptr LoadArrayFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2)); - static std::shared_ptr LoadArrayFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2)); - static std::shared_ptr LoadArrayFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize = Vector2ui(2, 2)); + static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize); + static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize); + static std::shared_ptr LoadFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize); // LoadCubemap - static std::shared_ptr LoadCubemapFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams()); - static std::shared_ptr LoadCubemapFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams()); - static std::shared_ptr LoadCubemapFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams = CubemapParams()); + static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams); + static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams); + static std::shared_ptr LoadFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams); }; } diff --git a/include/Nazara/Utility/Image.hpp b/include/Nazara/Utility/Image.hpp index a221ca4f4..d76971ac5 100644 --- a/include/Nazara/Utility/Image.hpp +++ b/include/Nazara/Utility/Image.hpp @@ -118,17 +118,17 @@ namespace Nz static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const ImageParams& params = ImageParams()); static std::shared_ptr LoadFromStream(Stream& stream, const ImageParams& params = ImageParams()); - // LoadArray - static std::shared_ptr LoadArrayFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2)); - static std::shared_ptr LoadArrayFromImage(const Image& image, const Vector2ui& atlasSize = Vector2ui(2, 2)); - static std::shared_ptr LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2)); - static std::shared_ptr LoadArrayFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), const Vector2ui& atlasSize = Vector2ui(2, 2)); + // Load (array) + static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize); + static std::shared_ptr LoadFromImage(const Image& image, const Vector2ui& atlasSize); + static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize); + static std::shared_ptr LoadFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize); - // LoadCubemap - static std::shared_ptr LoadCubemapFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams()); - static std::shared_ptr LoadCubemapFromImage(const Image& image, const CubemapParams& params = CubemapParams()); - static std::shared_ptr LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams()); - static std::shared_ptr LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), const CubemapParams& cubemapParams = CubemapParams()); + // Load (cubemap) + static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams); + static std::shared_ptr LoadFromImage(const Image& image, const CubemapParams& params); + static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams); + static std::shared_ptr LoadFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams); struct SharedImage { diff --git a/src/Nazara/Renderer/Texture.cpp b/src/Nazara/Renderer/Texture.cpp index 426ccc98c..12ce02acf 100644 --- a/src/Nazara/Renderer/Texture.cpp +++ b/src/Nazara/Renderer/Texture.cpp @@ -114,54 +114,54 @@ namespace Nz return CreateFromImage(*image, params); } - std::shared_ptr Texture::LoadArrayFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize) + std::shared_ptr Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const Vector2ui& atlasSize) { - std::shared_ptr image = Image::LoadArrayFromFile(filePath, textureParams, atlasSize); + std::shared_ptr image = Image::LoadFromFile(filePath, textureParams, atlasSize); if (!image) return {}; return CreateFromImage(*image, textureParams); } - std::shared_ptr Texture::LoadArrayFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize) + std::shared_ptr Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const Vector2ui& atlasSize) { - std::shared_ptr image = Image::LoadArrayFromMemory(data, size, textureParams, atlasSize); + std::shared_ptr image = Image::LoadFromMemory(data, size, textureParams, atlasSize); if (!image) return {}; return CreateFromImage(*image, textureParams); } - std::shared_ptr Texture::LoadArrayFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize) + std::shared_ptr Texture::LoadFromStream(Stream& stream, const TextureParams& textureParams, const Vector2ui& atlasSize) { - std::shared_ptr image = Image::LoadArrayFromStream(stream, textureParams, atlasSize); + std::shared_ptr image = Image::LoadFromStream(stream, textureParams, atlasSize); if (!image) return {}; return CreateFromImage(*image, textureParams); } - std::shared_ptr Texture::LoadCubemapFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams) + std::shared_ptr Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& textureParams, const CubemapParams& cubemapParams) { - std::shared_ptr image = Image::LoadCubemapFromFile(filePath, textureParams, cubemapParams); + std::shared_ptr image = Image::LoadFromFile(filePath, textureParams, cubemapParams); if (!image) return {}; return CreateFromImage(*image, textureParams); } - std::shared_ptr Texture::LoadCubemapFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams) + std::shared_ptr Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& textureParams, const CubemapParams& cubemapParams) { - std::shared_ptr image = Image::LoadCubemapFromMemory(data, size, textureParams, cubemapParams); + std::shared_ptr image = Image::LoadFromMemory(data, size, textureParams, cubemapParams); if (!image) return {}; return CreateFromImage(*image, textureParams); } - std::shared_ptr Texture::LoadCubemapFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams) + std::shared_ptr Texture::LoadFromStream(Stream& stream, const TextureParams& textureParams, const CubemapParams& cubemapParams) { - std::shared_ptr image = Image::LoadCubemapFromStream(stream, textureParams, cubemapParams); + std::shared_ptr image = Image::LoadFromStream(stream, textureParams, cubemapParams); if (!image) return {}; diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index ce178480e..6d2f79a53 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -854,7 +854,7 @@ namespace Nz } // LoadArray - std::shared_ptr Image::LoadArrayFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize) + std::shared_ptr Image::LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize) { std::shared_ptr 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::LoadArrayFromImage(const Image& image, const Vector2ui& atlasSize) + std::shared_ptr 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::LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize) + std::shared_ptr Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize) { std::shared_ptr 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::LoadArrayFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize) + std::shared_ptr Image::LoadFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize) { std::shared_ptr 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::LoadCubemapFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams) + std::shared_ptr Image::LoadFromFile(const std::filesystem::path& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams) { std::shared_ptr 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::LoadCubemapFromImage(const Image& image, const CubemapParams& params) + std::shared_ptr 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::LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams) + std::shared_ptr Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams) { std::shared_ptr 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::LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams) + std::shared_ptr Image::LoadFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams) { std::shared_ptr 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)