From f547164540f4ac7f8436fef8e981b1fef503f08c Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 6 Feb 2023 21:46:34 -0600 Subject: add kai_read_geometry and fix kai_read_source error handling --- src/geometry/geometry.c | 69 +++++++++++++++++++++++++++++++++++++++----- src/geometry/geometry.h | 3 ++ src/geometry/geometry.test.c | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 72af92c..25b899b 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -143,14 +143,36 @@ int kai_read_source(struct ka_source_t *dest, struct kai_tag_t *src) dest->name = kai_tag_attr_to_dup(src, "name"); struct kai_tag_t *child; - for (child = src->children; child != NULL; child = child->next) { - if (strcmp(child->type, "float_array") == 0) { - kai_read_float_array(&(dest->float_array), child); - } - else if (strcmp(child->type, "technique_common") == 0) { - kai_read_accessor(&(dest->accessor), child->children); - } - /* ignore others */ + int result; + + child = kai_tag_get_first_child_with_type(src, "float_array"); + result = kai_read_float_array(&(dest->float_array), child); + if (result != 0) { + free(dest->id); + free(dest->name); + return -1; + } + + child = kai_tag_get_first_child_with_type(src, "technique_common"); + if (child == NULL) { + free(dest->id); + free(dest->name); + kai_release_float_array(dest->float_array); + return -1; + } + child = kai_tag_get_first_child_with_type(child, "accessor"); + if (child == NULL) { + free(dest->id); + free(dest->name); + kai_release_float_array(dest->float_array); + return -1; + } + result = kai_read_accessor(&(dest->accessor), child); + if (result != 0) { + free(dest->id); + free(dest->name); + kai_release_float_array(dest->float_array); + return -1; } return 0; @@ -417,3 +439,34 @@ void kai_release_mesh(struct ka_mesh_t m) } free(m.triangles); } + + +int kai_read_geometry(struct ka_geometry_t *dest, struct kai_tag_t *src) +{ + CHECK_TAG_TYPE("geometry"); + + dest->id = kai_tag_attr_to_dup(src, "id"); + dest->name = kai_tag_attr_to_dup(src, "name"); + + struct kai_tag_t *mesh = kai_tag_get_first_child_with_type(src, "mesh"); + if (mesh == NULL) { + free(dest->id); + free(dest->name); + return -1; + } + int result = kai_read_mesh(&(dest->mesh), mesh); + if (result != 0) { + free(dest->id); + free(dest->name); + return -1; + } + + return 0; +} + +void kai_release_geometry(struct ka_geometry_t g) +{ + free(g.id); + free(g.name); + kai_release_mesh(g.mesh); +} diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h index a46af87..c00704f 100644 --- a/src/geometry/geometry.h +++ b/src/geometry/geometry.h @@ -29,4 +29,7 @@ void kai_release_triangles(struct ka_triangles_t t); int kai_read_mesh(struct ka_mesh_t *dest, struct kai_tag_t *src); void kai_release_mesh(struct ka_mesh_t m); +int kai_read_geometry(struct ka_geometry_t *dest, struct kai_tag_t *src); +void kai_release_geometry(struct ka_geometry_t m); + #endif diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index 17be188..60d6698 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -538,6 +538,64 @@ LILY_TEST("read mesh tag") #include LILY_PUSH_TEST() +LILY_TEST("fail to read non-geometry tag") +{ + struct kai_tag_t *t = kai_parse_string( + "" + ); + + struct ka_geometry_t geometry; + int result = kai_read_geometry(&geometry, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("read geometry tag") +{ + struct kai_tag_t *t = kai_parse_string( + "" + " " + " " + " 0.0" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "

" + " 0 0 1 3 2 1" + " 0 0 2 1 3 2" + "

" + "
" + "
" + "
" + ); + + struct ka_geometry_t geometry; + int result = kai_read_geometry(&geometry, t); + kai_tag_destroy(t); + + REQUIRE_EQ(result, 0, "%d"); + + CHECK_EQS(geometry.id, "xxx"); + CHECK_EQ(geometry.mesh.source_count, 1, "%d"); + CHECK_EQ(geometry.mesh.triangles_count, 1, "%d"); + + kai_release_geometry(geometry); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() -- cgit v1.2.1