Skip to content

Commit

Permalink
refactor(*): remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Jan 2, 2024
1 parent 0f4d242 commit 1344e39
Show file tree
Hide file tree
Showing 19 changed files with 520 additions and 2,286 deletions.
53 changes: 9 additions & 44 deletions src/Boxes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,41 @@

ZkVec3f ZkOrientedBoundingBox_getCenter(ZkOrientedBoundingBox const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_getCenter");
return {};
}

ZKC_CHECK_NULL(slf);
return slf->center;
}

ZkVec3f ZkOrientedBoundingBox_getAxis(ZkOrientedBoundingBox const* slf, ZkSize i) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_getAxis");
return {};
}

if (i >= 3) {
ZKC_LOG_ERROR("ZkOrientedBoundingBox_getAxis() failed: index out of range (0..2)");
return {};
}

ZKC_CHECK_NULL(slf);
ZKC_CHECK_LENA(3, i);
return slf->axes[i];
}

ZkVec3f ZkOrientedBoundingBox_getHalfWidth(ZkOrientedBoundingBox const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_getHalfWidth");
return {};
}

ZKC_CHECK_NULL(slf);
return slf->half_width;
}

ZkSize ZkOrientedBoundingBox_getChildCount(ZkOrientedBoundingBox const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_getChildCount");
return {};
}

ZKC_CHECK_NULL(slf);
return slf->children.size();
}

ZkOrientedBoundingBox const* ZkOrientedBoundingBox_getChild(ZkOrientedBoundingBox const* slf, ZkSize i) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_getChildCount");
return {};
}

if (i >= slf->children.size()) {
ZKC_LOG_ERROR("ZkOrientedBoundingBox_getChild() failed: index out of range");
return {};
}

ZKC_CHECK_NULL(slf);
ZKC_CHECK_LEN(slf->children, i);
return &slf->children[i];
}

void ZkOrientedBoundingBox_enumerateChildren(ZkOrientedBoundingBox const* slf,
ZkOrientedBoundingBoxEnumerator cb,
void* ctx) {
ZKC_TRACE_FN();
if (slf == nullptr || cb == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_enumerateChildren");
return;
}
ZKC_CHECK_NULLV(slf, cb);

for (auto& child : slf->children) {
if (cb(ctx, &child)) break;
Expand All @@ -80,10 +49,6 @@ void ZkOrientedBoundingBox_enumerateChildren(ZkOrientedBoundingBox const* slf,

ZkAxisAlignedBoundingBox ZkOrientedBoundingBox_toAabb(ZkOrientedBoundingBox const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkOrientedBoundingBox_toAabb");
return {};
}

ZKC_CHECK_NULL(slf);
return slf->as_bbox();
}
99 changes: 11 additions & 88 deletions src/CutsceneLibrary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,20 @@

#include "Internal.hh"

ZkCutsceneLibrary* ZkCutsceneLibrary_load(ZkRead* buf) {
ZKC_TRACE_FN();
if (buf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneLibrary_load");
return nullptr;
}

try {
ZkCutsceneLibrary obj {};
obj.load(buf);
return ZKC_WRAP_NEW(obj);
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkCutsceneLibrary_load() failed: %s", exc.what());
return nullptr;
}
}

ZkCutsceneLibrary* ZkCutsceneLibrary_loadPath(ZkString path) {
ZKC_TRACE_FN();
if (path == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneLibrary_loadPath");
return nullptr;
}

try {
auto buf = zenkit::Read::from(path);

ZkCutsceneLibrary obj {};
obj.load(buf.get());
return ZKC_WRAP_NEW(obj);
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkCutsceneLibrary_loadPath() failed: %s", exc.what());
return nullptr;
}
}

ZkCutsceneLibrary* ZkCutsceneLibrary_loadVfs(ZkVfs const* vfs, ZkString name) {
ZKC_TRACE_FN();
if (vfs == nullptr || name == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneLibrary_loadVfs");
return nullptr;
}

auto node = vfs->find(name);
if (node == nullptr) return nullptr;

auto rd = node->open_read();
return ZkCutsceneLibrary_load(rd.get());
}

void ZkCutsceneLibrary_del(ZkCutsceneLibrary* slf) {
ZKC_TRACE_FN();
delete slf;
}
ZKC_LOADER(ZkCutsceneLibrary);
ZKC_PATH_LOADER(ZkCutsceneLibrary);
ZKC_VFS_LOADER(ZkCutsceneLibrary);
ZKC_DELETER(ZkCutsceneLibrary);

ZkCutsceneBlock const* ZkCutsceneLibrary_getBlock(ZkCutsceneLibrary const* slf, ZkString name) {
ZKC_TRACE_FN();
if (slf == nullptr || name == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneLibrary_getBlock");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return slf->block_by_name(name);
}

void ZkCutsceneLibrary_enumerateBlocks(ZkCutsceneLibrary const* slf, ZkCutsceneBlockEnumerator cb, void* ctx) {
ZKC_TRACE_FN();
if (slf == nullptr || cb == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneLibrary_enumerateBlocks");
return;
}
ZKC_CHECK_NULLV(slf, cb);

for (auto& block : slf->blocks) {
if (cb(ctx, &block)) break;
Expand All @@ -83,50 +26,30 @@ void ZkCutsceneLibrary_enumerateBlocks(ZkCutsceneLibrary const* slf, ZkCutsceneB

ZkString ZkCutsceneBlock_getName(ZkCutsceneBlock const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneBlock_getName");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return slf->name.c_str();
}

ZkCutsceneMessage const* ZkCutsceneBlock_getMessage(ZkCutsceneBlock const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneBlock_getMessage");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return &slf->message;
}

uint32_t ZkCutsceneMessage_getType(ZkCutsceneMessage const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneMessage_getType");
return 0;
}

ZKC_CHECK_NULL(slf);
return slf->type;
}

ZkString ZkCutsceneMessage_getText(ZkCutsceneMessage const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneMessage_getText");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return slf->text.c_str();
}

ZkString ZkCutsceneMessage_getName(ZkCutsceneMessage const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkCutsceneMessage_getName");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return slf->name.c_str();
}
92 changes: 10 additions & 82 deletions src/Font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,41 @@

#include "Internal.hh"

ZkFont* ZkFont_load(ZkRead* buf) {
ZKC_TRACE_FN();
if (buf == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_load");
return nullptr;
}

try {
ZkFont obj {};
obj.load(buf);
return ZKC_WRAP_NEW(obj);
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkFont_load() failed: %s", exc.what());
return nullptr;
}
}

ZkFont* ZkFont_loadPath(ZkString path) {
ZKC_TRACE_FN();
if (path == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_loadPath");
return nullptr;
}

try {
auto buf = zenkit::Read::from(path);

ZkFont obj {};
obj.load(buf.get());
return ZKC_WRAP_NEW(obj);
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkFont_loadPath() failed: %s", exc.what());
return nullptr;
}
}

ZkFont* ZkFont_loadVfs(ZkVfs* vfs, ZkString name) {
ZKC_TRACE_FN();
if (vfs == nullptr || name == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_loadVfs");
return nullptr;
}

auto node = vfs->find(name);
if (node == nullptr) return nullptr;

auto rd = node->open_read();
return ZkFont_load(rd.get());
}

void ZkFont_del(ZkFont* slf) {
ZKC_TRACE_FN();
delete slf;
}
ZKC_LOADER(ZkFont);
ZKC_PATH_LOADER(ZkFont);
ZKC_VFS_LOADER(ZkFont);
ZKC_DELETER(ZkFont);

ZkString ZkFont_getName(ZkFont const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_getName");
return nullptr;
}

ZKC_CHECK_NULL(slf);
return slf->name.c_str();
}

uint32_t ZkFont_getHeight(ZkFont const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_getHeight");
return 0;
}

ZKC_CHECK_NULL(slf);
return slf->height;
}

ZkSize ZkFont_getGlyphCount(ZkFont const* slf) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_getGlyphCount");
return 0;
}

ZKC_CHECK_NULL(slf);
return slf->glyphs.size();
}

ZkFontGlyph ZkFont_getGlyph(ZkFont const* slf, ZkSize i) {
ZKC_TRACE_FN();
if (slf == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_getGlyph");
return {0, {}, {}};
}

if (i >= slf->glyphs.size()) {
ZKC_LOG_ERROR("ZkFont_getGlyph() failed: index out of range");
return {0, {}, {}};
}
ZKC_CHECK_NULL(slf);
ZKC_CHECK_LEN(slf->glyphs, i);

auto& glyph = slf->glyphs[i];
return {glyph.width, glyph.uv[0], glyph.uv[1]};
}

void ZkFont_enumerateGlyphs(ZkFont const* slf, ZkFontGlyphEnumerator cb, void* ctx) {
ZKC_TRACE_FN();
if (slf == nullptr || cb == nullptr) {
ZKC_LOG_WARN_NULL("ZkFont_enumerateGlyphs");
return;
}
ZKC_CHECK_NULLV(slf, cb);

ZkFontGlyph fg {0, {}, {}};
for (auto& glyph : slf->glyphs) {
Expand Down
Loading

0 comments on commit 1344e39

Please sign in to comment.