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) {
PtkComponent *ret = malloc(sizeof(PtkComponent));
ret->type = PTK_COMPONENT_TYPE_BOX;
ret->children = PTK_LIST_NEW(PtkHandle, child_count);
*ret = (PtkComponent){
.type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
return (PtkHandle)ret;
@ -143,21 +145,23 @@ PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle));
ret->type = PTK_COMPONENT_TYPE_TRIANGLE;
ret->children.size = 0;
*ret = (PtkTriangle){
.type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
ret->color = color;
return (PtkHandle)ret;
}
PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) {
PtkRect *ret = malloc(sizeof(PtkRect));
ret->type = PTK_COMPONENT_TYPE_RECT;
ret->children.size = 0;
ret->top_left = top_left;
ret->size = size;
ret->color = color;
*ret = (PtkRect){
.type = PTK_COMPONENT_TYPE_RECT,
.top_left = top_left,
.size = size,
.color = color,
};
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) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse));
ret->type = PTK_COMPONENT_TYPE_ELLIPSE;
ret->children.size = 0;
ret->center = center;
ret->radii = radii;
ret->color = color;
*ret = (PtkEllipse){
.type = PTK_COMPONENT_TYPE_ELLIPSE,
.center = center,
.radii = radii,
.color = color,
};
return (PtkHandle)ret;
}