Fri, 12 Apr 2024 22:22:07 +0200
use fancy named parameters for asc_text()
src/ascension/ui/text.h | file | annotate | diff | comparison | revisions | |
src/text.c | file | annotate | diff | comparison | revisions | |
test/snake.c | file | annotate | diff | comparison | revisions |
--- a/src/ascension/ui/text.h Fri Apr 12 22:03:15 2024 +0200 +++ b/src/ascension/ui/text.h Fri Apr 12 22:22:07 2024 +0200 @@ -53,20 +53,46 @@ #define ASC_TEXT_ALIGNMENT_MASK 0x03 #define ASC_TEXT_CENTERED_FLAG 0x04 +struct asc_text_create_args { + int x; + int y; + char const *text; + enum asc_text_alignment alignment; + unsigned short max_width; +}; + /** * Creates a text node. * * The current context ink and font will be used. * - * @param x the position where to draw the text - * @param y the position where to draw the text - * @param text the text to draw + * @param args initial arguments for creating the node * @return the scene node * * @see asc_ink() * @see asc_font() */ -AscSceneNode *asc_text(int x, int y, char const* text); +AscSceneNode *asc_text_create(struct asc_text_create_args args); + +/** + * Creates a text node. + * + * The current context ink and font will be used. + * + * This is a convenience macro that lets you use the arguments + * as named parameters. Usage example: + * @code + * AscSceneNode *mytext = asc_text( .x = 10, .y = 15 ); + * @endcode + * + * @param ... initial arguments for creating the node + * @return the scene node + * + * @see asc_ink() + * @see asc_font() + */ +#define asc_text(...) \ + asc_text_create((struct asc_text_create_args) { __VA_ARGS__ }) /** * Sets the text alignment.
--- a/src/text.c Fri Apr 12 22:03:15 2024 +0200 +++ b/src/text.c Fri Apr 12 22:22:07 2024 +0200 @@ -89,8 +89,7 @@ SDL_FreeSurface(surface); } -AscSceneNode *asc_text(int x, int y, char const *text) { - // TODO: implement fancy struct "named param" initialization +AscSceneNode *asc_text_create(struct asc_text_create_args args) { AscText *node = calloc(1, sizeof(AscText)); node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; @@ -98,14 +97,16 @@ node->base.update_func = (asc_scene_update_func) asc_text_update; node->base.draw_func = (asc_scene_draw_func) asc_text_draw; - node->base.position.x = (float) x; - node->base.position.y = (float) y; + node->base.flags = args.alignment; + node->base.position.x = (float) args.x; + node->base.position.y = (float) args.y; + node->max_width = args.max_width; node->font = asc_context.active_font; node->color = asc_context.ink; - if (text == NULL) { + if (args.text == NULL) { node->text = cx_mutstr(strdup(" ")); } else { - node->text = cx_mutstr(strdup(text)); + node->text = cx_mutstr(strdup(args.text)); } // initialize
--- a/test/snake.c Fri Apr 12 22:03:15 2024 +0200 +++ b/test/snake.c Fri Apr 12 22:22:07 2024 +0200 @@ -49,7 +49,7 @@ static void create_fps_counter(void) { asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); asc_ink_rgb(255, 0, 0); - AscSceneNode* node = asc_text(10, 10, NULL); + AscSceneNode* node = asc_text( .x = 10, .y = 10 ); asc_scene_add_behavior(node, update_fps_counter); asc_add_ui_node(node); } @@ -70,7 +70,7 @@ static void create_score_counter(void) { asc_set_font(asc_font(ASC_FONT_BOLD, 14)); asc_ink_rgb(0, 255, 0); - AscSceneNode* node = asc_text(0, 0, "Score: 0"); + AscSceneNode* node = asc_text( .x = 0, .y = 0, .text = "Score: 0" ); asc_scene_add_behavior(node, update_score_counter); asc_add_ui_node(node); }