improve component initialization

compound literals zero-initialize all omitted fields including
children.size, now we don't have to explicitly do that in each function
This commit is contained in:
jacekpoz 2024-08-13 01:22:45 +02:00
parent 913286e9e7
commit 29f0d9f56a
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -134,8 +134,10 @@ bool ptk_init(size_t width, size_t height, const char *title, PtkVersion applica
PtkHandle ptk_box(size_t child_count, PtkHandle *children) { PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent)); PtkComponent *ret = malloc(sizeof(PtkComponent));
ret->type = PTK_COMPONENT_TYPE_BOX; *ret = (PtkComponent){
ret->children = PTK_LIST_NEW(PtkHandle, child_count); .type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count); PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
return (PtkHandle)ret; return (PtkHandle)ret;
@ -143,21 +145,23 @@ PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) { PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle)); PtkTriangle *ret = malloc(sizeof(PtkTriangle));
ret->type = PTK_COMPONENT_TYPE_TRIANGLE; *ret = (PtkTriangle){
ret->children.size = 0; .type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3); memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
ret->color = color;
return (PtkHandle)ret; return (PtkHandle)ret;
} }
PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) { PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) {
PtkRect *ret = malloc(sizeof(PtkRect)); PtkRect *ret = malloc(sizeof(PtkRect));
ret->type = PTK_COMPONENT_TYPE_RECT; *ret = (PtkRect){
ret->children.size = 0; .type = PTK_COMPONENT_TYPE_RECT,
ret->top_left = top_left; .top_left = top_left,
ret->size = size; .size = size,
ret->color = color; .color = color,
};
return (PtkHandle)ret; return (PtkHandle)ret;
} }
@ -168,11 +172,12 @@ PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color) {
PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) { PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse)); PtkEllipse *ret = malloc(sizeof(PtkEllipse));
ret->type = PTK_COMPONENT_TYPE_ELLIPSE; *ret = (PtkEllipse){
ret->children.size = 0; .type = PTK_COMPONENT_TYPE_ELLIPSE,
ret->center = center; .center = center,
ret->radii = radii; .radii = radii,
ret->color = color; .color = color,
};
return (PtkHandle)ret; return (PtkHandle)ret;
} }