Renderer/Texture: Add static helpers

This commit is contained in:
Jérôme Leclercq
2021-05-25 15:37:55 +02:00
parent 59cfc74ab4
commit 335bb82be1
5 changed files with 106 additions and 127 deletions

View File

@@ -3,9 +3,72 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
Texture::~Texture() = default;
bool TextureParams::IsValid() const
{
if (!ImageParams::IsValid())
return false;
if (!renderDevice)
{
NazaraError("no render device set");
return false;
}
if (!usageFlags)
{
NazaraError("a texture should have at least one usage flag");
return false;
}
return true;
}
std::shared_ptr<Texture> Texture::CreateFromImage(const Image& image, const TextureParams& params)
{
NazaraAssert(params.IsValid(), "Invalid TextureParams");
Nz::TextureInfo texParams;
texParams.depth = image.GetDepth();
texParams.height = image.GetHeight();
texParams.pixelFormat = image.GetFormat();
texParams.type = image.GetType();
texParams.width = image.GetWidth();
texParams.usageFlags = params.usageFlags;
std::shared_ptr<Nz::Texture> texture = params.renderDevice->InstantiateTexture(texParams);
if (!texture->Update(image.GetConstPixels()))
{
NazaraError("failed to update texture");
return {};
}
return texture;
}
std::shared_ptr<Texture> Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, params);
return CreateFromImage(*image, params);
}
std::shared_ptr<Texture> Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, params);
return CreateFromImage(*image, params);
}
std::shared_ptr<Texture> Texture::LoadFromStream(Stream& stream, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromStream(stream, params);
return CreateFromImage(*image, params);
}
}