Wed, 06 Mar 2024 23:38:17 +0100
invert the logic of converting between specialized nodes and the generic interface
src/ascension/text.h | file | annotate | diff | comparison | revisions | |
src/ascension/window.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/text.h Wed Mar 06 23:13:06 2024 +0100 1.2 +++ b/src/ascension/text.h Wed Mar 06 23:38:17 2024 +0100 1.3 @@ -49,17 +49,24 @@ 1.4 /** 1.5 * Creates a text node. 1.6 * 1.7 - * The current context ink and font will be used and the 1.8 - * node will be automatically added to the UI scene of the 1.9 - * currently active window. 1.10 + * The current context ink and font will be used. 1.11 * 1.12 * @param x the position where to draw the text 1.13 * @param y the position where to draw the text 1.14 * @param text the text to draw 1.15 + * @return the scene node 1.16 + * 1.17 * @see asc_ink() 1.18 * @see asc_font() 1.19 */ 1.20 -AscText *asc_text(int x, int y, char const* text); 1.21 +AscSceneNode *asc_text(int x, int y, char const* text); 1.22 + 1.23 +/** 1.24 + * Provides access to the text data fields. 1.25 + * 1.26 + * @param node scene node created by asc_text() 1.27 + */ 1.28 +#define asc_text_data(node) ((AscText*)node) 1.29 1.30 /** 1.31 * Releases all the memory of this node.
2.1 --- a/src/ascension/window.h Wed Mar 06 23:13:06 2024 +0100 2.2 +++ b/src/ascension/window.h Wed Mar 06 23:38:17 2024 +0100 2.3 @@ -59,6 +59,8 @@ 2.4 AscScene ui; 2.5 } AscWindow; 2.6 2.7 +#define asc_window_active_ui &(asc_context.active_window->ui) 2.8 + 2.9 /** 2.10 * Initializes the settings structure with default values. 2.11 *
3.1 --- a/src/text.c Wed Mar 06 23:13:06 2024 +0100 3.2 +++ b/src/text.c Wed Mar 06 23:38:17 2024 +0100 3.3 @@ -99,7 +99,7 @@ 3.4 SDL_FreeSurface(surface); 3.5 } 3.6 3.7 -AscText *asc_text(int x, int y, char const *text) { 3.8 +AscSceneNode *asc_text(int x, int y, char const *text) { 3.9 AscText *node = calloc(1, sizeof(AscText)); 3.10 if (node == NULL) { 3.11 asc_error("Out of memory."); 3.12 @@ -118,9 +118,7 @@ 3.13 node->text = strdup(text); 3.14 } 3.15 3.16 - asc_scene_add(&asc_context.active_window->ui, &node->base); 3.17 - 3.18 - return node; 3.19 + return &node->base; 3.20 } 3.21 3.22 void asc_text_free(AscText *node) {
4.1 --- a/test/snake.c Wed Mar 06 23:13:06 2024 +0100 4.2 +++ b/test/snake.c Wed Mar 06 23:38:17 2024 +0100 4.3 @@ -40,7 +40,7 @@ 4.4 fps /= asc_context.elapsed_millis; 4.5 if (fps != last_fps) { 4.6 last_fps = fps; 4.7 - snprintf(((AscText*)node)->text, 9, "%u FPS", fps); 4.8 + snprintf(asc_text_data(node)->text, 9, "%u FPS", fps); 4.9 asc_node_update(node); 4.10 } 4.11 } 4.12 @@ -49,8 +49,9 @@ 4.13 static void create_fps_counter(void) { 4.14 asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); 4.15 asc_ink_rgb(255, 0, 0); 4.16 - AscText* text = asc_text(10, 10, "XXXXX FPS"); 4.17 - asc_scene_add_behavior(asc_node(text), update_fps_counter); 4.18 + AscSceneNode* node = asc_text(10, 10, "XXXXX FPS"); 4.19 + asc_scene_add_behavior(node, update_fps_counter); 4.20 + asc_scene_add(asc_window_active_ui, node); 4.21 } 4.22 4.23 int main(int argc, char** argv) { 4.24 @@ -63,7 +64,7 @@ 4.25 4.26 AscWindowSettings settings; 4.27 asc_window_settings_init_defaults(&settings); 4.28 - settings.title = "Sandbox Application"; 4.29 + settings.title = "Snake"; 4.30 4.31 AscWindow *window = asc_window_initialize(0, &settings); 4.32 asc_shader_initialize_predefined();