Skip to content

Commit

Permalink
feat(ZkTexture): implement ZkTextureBuilder API
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Feb 24, 2024
1 parent 1d27df3 commit bdc78a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/zenkit-capi/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifdef __cplusplus
#include <zenkit/Texture.hh>
using ZkTexture = zenkit::Texture;
using ZkTextureBuilder = zenkit::TextureBuilder;
#else
typedef struct ZkInternal_Texture ZkTexture;
#endif
Expand Down Expand Up @@ -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);
31 changes: 31 additions & 0 deletions src/Texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int>(width), static_cast<unsigned int>(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<uint8_t> dta;
dta.assign(buf, buf + len);
slf->add_mipmap(std::move(dta), static_cast<zenkit::TextureFormat>(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<zenkit::TextureFormat>(fmt));
return ZKC_WRAP_NEW(tex);
}

0 comments on commit bdc78a9

Please sign in to comment.