From 29f0d9f56a7439f8f224cc18be73d99b0b73705f Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Tue, 13 Aug 2024 01:22:45 +0200 Subject: [PATCH] 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 --- src/ptk.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/ptk.c b/src/ptk.c index fd4bf4e..f34359d 100644 --- a/src/ptk.c +++ b/src/ptk.c @@ -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; }