diff options
author | sanine <sanine.not@pm.me> | 2023-02-07 20:17:19 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-07 20:17:19 -0600 |
commit | 7ba4ac76789b4be03789f5e3ebe4785aab2425d6 (patch) | |
tree | 61b049b4816532576fd6f999388433ce2e109ca0 /src | |
parent | 7743fd1feb81e1659584491ca88d50bd629cccef (diff) |
refactor: use KAI_FILL_ARRAY_FROM_TAGS
Diffstat (limited to 'src')
-rw-r--r-- | src/geometry/geometry.c | 159 |
1 files changed, 49 insertions, 110 deletions
diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 7d2e6c2..54be1c7 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -97,26 +97,17 @@ int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src) dest->source = source; dest->stride = kai_tag_attr_to_long(src, "stride", 1); - dest->param_count = kai_tag_num_children(src); - dest->param = kai_alloc( - dest->param_count * sizeof(struct ka_param_t), - "accessor tag param array" + int result; + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->param, dest->param_count, struct ka_param_t, + src, "param", + kai_read_param, kai_release_param ); - if (dest->param == NULL) { + if (result != 0) { free(source); return -1; } - struct kai_tag_t *t = src->children; - int i; - for (i=0; i<dest->param_count; i++) { - int result = kai_read_param(dest->param + i, t); - if (result != 0) { - free(dest->param); - free(dest->source); - return -1; - } - t = t->next; - } return 0; } @@ -246,31 +237,19 @@ int kai_read_vertices(struct ka_vertices_t *dest, struct kai_tag_t *src) dest->id = id; dest->name = kai_tag_attr_to_dup(src, "name"); - /* count input children */ - int count = 0; - struct kai_tag_t *child; - FOR_CHILD_OF_TYPE(src, child, "input") { - count += 1; - } + int result; + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->input, dest->input_count, struct ka_input_t, + src, "input", + kai_read_input_unshared, kai_release_input + ); - dest->input_count = count; - dest->input = malloc(count * sizeof(struct ka_input_t)); - if (dest->input == NULL) { + if (result != 0) { kai_release_vertices(*dest); return -1; } - int i=0; - FOR_CHILD_OF_TYPE(src, child, "input") { - int result = kai_read_input_unshared(dest->input + i, child); - if (result != 0) { - dest->input_count = i; - kai_release_vertices(*dest); - return -1; - } - i += 1; - } - return 0; } @@ -307,31 +286,19 @@ int kai_read_triangles(struct ka_triangles_t *dest, struct kai_tag_t *src) dest->p = NULL; /* load the child inputs into the structure */ - int input_count = 0; - struct kai_tag_t *child; - FOR_CHILD_OF_TYPE(src, child, "input") { - input_count += 1; - } + int result; + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->input, dest->input_count, struct ka_input_t, + src, "input", + kai_read_input_shared, kai_release_input + ); - dest->input = kai_alloc(input_count * sizeof(struct ka_input_t), "triangles input array"); - if (dest->input == NULL) { + if (result != 0) { kai_release_triangles(*dest); return -1; } - int i=0; - FOR_CHILD_OF_TYPE(src, child, "input") { - int result = kai_read_input_shared(dest->input + i, child); - if (result != 0) { - dest->input_count = i; - kai_release_triangles(*dest); - return -1; - } - i += 1; - } - dest->input_count = input_count; - - /* load the child p tag */ struct kai_tag_t *p = kai_tag_get_first_child_with_type(src, "p"); if (p != NULL) { @@ -381,46 +348,31 @@ int kai_read_mesh(struct ka_mesh_t *dest, struct kai_tag_t *src) if (result != 0) { return -1; } /* load sources */ - unsigned int source_count = 0; - FOR_CHILD_OF_TYPE(src, child, "source") { source_count += 1; } - - dest->source = kai_alloc(source_count * sizeof(struct ka_source_t), "mesh source array"); - if (dest->source == NULL) { return -1; } - - int i = 0; - FOR_CHILD_OF_TYPE(src, child, "source") { - result = kai_read_source(dest->source + i, child); - if (result != 0) { - dest->source_count = i; - kai_release_mesh(*dest); - return -1; - } - i += 1; + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->source, dest->source_count, struct ka_source_t, + src, "source", + kai_read_source, kai_release_source + ); + + if (result != 0) { + kai_release_mesh(*dest); + return -1; } - dest->source_count = source_count; /* load triangles */ - unsigned int triangles_count = 0; - FOR_CHILD_OF_TYPE(src, child, "triangles") { triangles_count += 1; } + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->triangles, dest->triangles_count, struct ka_triangles_t, + src, "triangles", + kai_read_triangles, kai_release_triangles + ); - dest->triangles = kai_alloc(triangles_count * sizeof(struct ka_triangles_t), "mesh triangles array"); - if (dest->triangles == NULL) { + if (result != 0) { kai_release_mesh(*dest); return -1; } - i = 0; - FOR_CHILD_OF_TYPE(src, child, "triangles") { - result = kai_read_triangles(dest->triangles + i, child); - if (result != 0) { - dest->triangles_count = i; - kai_release_mesh(*dest); - return -1; - } - i += 1; - } - dest->triangles_count = triangles_count; - return 0; } @@ -479,31 +431,18 @@ int kai_read_library_geometries(struct ka_library_geometries_t *dest, struct kai dest->id = kai_tag_attr_to_dup(src, "id"); dest->name = kai_tag_attr_to_dup(src, "name"); - struct kai_tag_t *child; - unsigned int geometry_count = 0; - FOR_CHILD_OF_TYPE(src, child, "geometry") { geometry_count += 1; } - - dest->geometry = kai_alloc( - geometry_count * sizeof(struct ka_geometry_t), - "library_geometries geometry array" + int result; + KAI_FILL_ARRAY_FROM_TAGS( + result, + dest->geometry, dest->geometry_count, struct ka_geometry_t, + src, "geometry", + kai_read_geometry, kai_release_geometry ); - if (dest->geometry == NULL) { - free(dest->id); - free(dest->name); - return -1; - } - int i=0; - FOR_CHILD_OF_TYPE(src, child, "geometry") { - int result = kai_read_geometry(dest->geometry + i, child); - if (result != 0) { - dest->geometry_count = i; - kai_release_library_geometries(*dest); - return -1; - } - i += 1; + if (result != 0) { + kai_release_library_geometries(*dest); + return -1; } - dest->geometry_count = geometry_count; return 0; } |