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 |
1.1 --- a/src/ascension/ui/text.h Fri Apr 12 22:03:15 2024 +0200 1.2 +++ b/src/ascension/ui/text.h Fri Apr 12 22:22:07 2024 +0200 1.3 @@ -53,20 +53,46 @@ 1.4 #define ASC_TEXT_ALIGNMENT_MASK 0x03 1.5 #define ASC_TEXT_CENTERED_FLAG 0x04 1.6 1.7 +struct asc_text_create_args { 1.8 + int x; 1.9 + int y; 1.10 + char const *text; 1.11 + enum asc_text_alignment alignment; 1.12 + unsigned short max_width; 1.13 +}; 1.14 + 1.15 /** 1.16 * Creates a text node. 1.17 * 1.18 * The current context ink and font will be used. 1.19 * 1.20 - * @param x the position where to draw the text 1.21 - * @param y the position where to draw the text 1.22 - * @param text the text to draw 1.23 + * @param args initial arguments for creating the node 1.24 * @return the scene node 1.25 * 1.26 * @see asc_ink() 1.27 * @see asc_font() 1.28 */ 1.29 -AscSceneNode *asc_text(int x, int y, char const* text); 1.30 +AscSceneNode *asc_text_create(struct asc_text_create_args args); 1.31 + 1.32 +/** 1.33 + * Creates a text node. 1.34 + * 1.35 + * The current context ink and font will be used. 1.36 + * 1.37 + * This is a convenience macro that lets you use the arguments 1.38 + * as named parameters. Usage example: 1.39 + * @code 1.40 + * AscSceneNode *mytext = asc_text( .x = 10, .y = 15 ); 1.41 + * @endcode 1.42 + * 1.43 + * @param ... initial arguments for creating the node 1.44 + * @return the scene node 1.45 + * 1.46 + * @see asc_ink() 1.47 + * @see asc_font() 1.48 + */ 1.49 +#define asc_text(...) \ 1.50 + asc_text_create((struct asc_text_create_args) { __VA_ARGS__ }) 1.51 1.52 /** 1.53 * Sets the text alignment.
2.1 --- a/src/text.c Fri Apr 12 22:03:15 2024 +0200 2.2 +++ b/src/text.c Fri Apr 12 22:22:07 2024 +0200 2.3 @@ -89,8 +89,7 @@ 2.4 SDL_FreeSurface(surface); 2.5 } 2.6 2.7 -AscSceneNode *asc_text(int x, int y, char const *text) { 2.8 - // TODO: implement fancy struct "named param" initialization 2.9 +AscSceneNode *asc_text_create(struct asc_text_create_args args) { 2.10 AscText *node = calloc(1, sizeof(AscText)); 2.11 2.12 node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; 2.13 @@ -98,14 +97,16 @@ 2.14 node->base.update_func = (asc_scene_update_func) asc_text_update; 2.15 node->base.draw_func = (asc_scene_draw_func) asc_text_draw; 2.16 2.17 - node->base.position.x = (float) x; 2.18 - node->base.position.y = (float) y; 2.19 + node->base.flags = args.alignment; 2.20 + node->base.position.x = (float) args.x; 2.21 + node->base.position.y = (float) args.y; 2.22 + node->max_width = args.max_width; 2.23 node->font = asc_context.active_font; 2.24 node->color = asc_context.ink; 2.25 - if (text == NULL) { 2.26 + if (args.text == NULL) { 2.27 node->text = cx_mutstr(strdup(" ")); 2.28 } else { 2.29 - node->text = cx_mutstr(strdup(text)); 2.30 + node->text = cx_mutstr(strdup(args.text)); 2.31 } 2.32 2.33 // initialize
3.1 --- a/test/snake.c Fri Apr 12 22:03:15 2024 +0200 3.2 +++ b/test/snake.c Fri Apr 12 22:22:07 2024 +0200 3.3 @@ -49,7 +49,7 @@ 3.4 static void create_fps_counter(void) { 3.5 asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); 3.6 asc_ink_rgb(255, 0, 0); 3.7 - AscSceneNode* node = asc_text(10, 10, NULL); 3.8 + AscSceneNode* node = asc_text( .x = 10, .y = 10 ); 3.9 asc_scene_add_behavior(node, update_fps_counter); 3.10 asc_add_ui_node(node); 3.11 } 3.12 @@ -70,7 +70,7 @@ 3.13 static void create_score_counter(void) { 3.14 asc_set_font(asc_font(ASC_FONT_BOLD, 14)); 3.15 asc_ink_rgb(0, 255, 0); 3.16 - AscSceneNode* node = asc_text(0, 0, "Score: 0"); 3.17 + AscSceneNode* node = asc_text( .x = 0, .y = 0, .text = "Score: 0" ); 3.18 asc_scene_add_behavior(node, update_score_counter); 3.19 asc_add_ui_node(node); 3.20 }