From bdc78a99d865fbb1870890d547053309d2a4c75e Mon Sep 17 00:00:00 2001 From: Luis Michaelis Date: Sat, 24 Feb 2024 16:33:58 +0100 Subject: [PATCH] feat(ZkTexture): implement `ZkTextureBuilder` API --- include/zenkit-capi/Texture.h | 7 +++++++ src/Texture.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/zenkit-capi/Texture.h b/include/zenkit-capi/Texture.h index 95a197f..5b83ae1 100644 --- a/include/zenkit-capi/Texture.h +++ b/include/zenkit-capi/Texture.h @@ -8,6 +8,7 @@ #ifdef __cplusplus #include using ZkTexture = zenkit::Texture; +using ZkTextureBuilder = zenkit::TextureBuilder; #else typedef struct ZkInternal_Texture ZkTexture; #endif @@ -56,3 +57,9 @@ ZKC_API ZkSize ZkTexture_getMipmapRgba(ZkTexture const* slf, ZkSize level, uint8 ZKC_API void ZkTexture_enumerateRawMipmaps(ZkTexture const* slf, ZkTextureMipmapEnumerator cb, void* ctx); ZKC_API void ZkTexture_enumerateRgbaMipmaps(ZkTexture const* slf, ZkTextureMipmapEnumerator cb, void* ctx); + +ZKC_API ZkTextureBuilder* ZkTextureBuilder_new(ZkSize width, ZkSize height); +ZKC_API void ZkTextureBuilder_del(ZkTextureBuilder* slf); + +ZKC_API bool ZkTextureBuilder_addMipmap(ZkTextureBuilder* slf, ZkByte* buf, ZkSize len, ZkTextureFormat fmt); +ZKC_API ZkTexture* ZkTextureBuilder_build(ZkTextureBuilder* slf, ZkTextureFormat fmt); diff --git a/src/Texture.cc b/src/Texture.cc index 86325b4..fc313b2 100644 --- a/src/Texture.cc +++ b/src/Texture.cc @@ -128,3 +128,34 @@ void ZkTexture_enumerateRgbaMipmaps(ZkTexture const* slf, ZkTextureMipmapEnumera if (cb(ctx, i, rgba.data(), rgba.size())) break; } } + + +ZkTextureBuilder* ZkTextureBuilder_new(ZkSize width, ZkSize height) { + ZKC_TRACE_FN(); + + return new ZkTextureBuilder {static_cast(width), static_cast(height)}; +} + +void ZkTextureBuilder_del(ZkTextureBuilder* slf) { + delete[] slf; +} + +bool ZkTextureBuilder_addMipmap(ZkTextureBuilder* slf, ZkByte* buf, ZkSize len, ZkTextureFormat fmt) { + ZKC_TRACE_FN(); + ZKC_CHECK_NULL(slf, buf); + + try { + std::vector dta; + dta.assign(buf, buf + len); + slf->add_mipmap(std::move(dta), static_cast(fmt)); + return true; + } catch (zenkit::Error const& exc) { + ZKC_LOG_ERROR("ZkTextureBuilder_addMipmap() failed: %s", exc.what()); + return false; + } +} + +ZkTexture* ZkTextureBuilder_build(ZkTextureBuilder* slf, ZkTextureFormat fmt) { + auto tex = slf->build(static_cast(fmt)); + return ZKC_WRAP_NEW(tex); +}