# HG changeset patch # User Mike Becker # Date 1709764697 -3600 # Node ID e26b4ac1661c3d406d7674badf9622e796da2833 # Parent 674eb53c12f3e7880745c01fbb0ebee1ebe566c6 invert the logic of converting between specialized nodes and the generic interface diff -r 674eb53c12f3 -r e26b4ac1661c src/ascension/text.h --- a/src/ascension/text.h Wed Mar 06 23:13:06 2024 +0100 +++ b/src/ascension/text.h Wed Mar 06 23:38:17 2024 +0100 @@ -49,17 +49,24 @@ /** * Creates a text node. * - * The current context ink and font will be used and the - * node will be automatically added to the UI scene of the - * currently active window. + * 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 + * @return the scene node + * * @see asc_ink() * @see asc_font() */ -AscText *asc_text(int x, int y, char const* text); +AscSceneNode *asc_text(int x, int y, char const* text); + +/** + * Provides access to the text data fields. + * + * @param node scene node created by asc_text() + */ +#define asc_text_data(node) ((AscText*)node) /** * Releases all the memory of this node. diff -r 674eb53c12f3 -r e26b4ac1661c src/ascension/window.h --- a/src/ascension/window.h Wed Mar 06 23:13:06 2024 +0100 +++ b/src/ascension/window.h Wed Mar 06 23:38:17 2024 +0100 @@ -59,6 +59,8 @@ AscScene ui; } AscWindow; +#define asc_window_active_ui &(asc_context.active_window->ui) + /** * Initializes the settings structure with default values. * diff -r 674eb53c12f3 -r e26b4ac1661c src/text.c --- a/src/text.c Wed Mar 06 23:13:06 2024 +0100 +++ b/src/text.c Wed Mar 06 23:38:17 2024 +0100 @@ -99,7 +99,7 @@ SDL_FreeSurface(surface); } -AscText *asc_text(int x, int y, char const *text) { +AscSceneNode *asc_text(int x, int y, char const *text) { AscText *node = calloc(1, sizeof(AscText)); if (node == NULL) { asc_error("Out of memory."); @@ -118,9 +118,7 @@ node->text = strdup(text); } - asc_scene_add(&asc_context.active_window->ui, &node->base); - - return node; + return &node->base; } void asc_text_free(AscText *node) { diff -r 674eb53c12f3 -r e26b4ac1661c test/snake.c --- a/test/snake.c Wed Mar 06 23:13:06 2024 +0100 +++ b/test/snake.c Wed Mar 06 23:38:17 2024 +0100 @@ -40,7 +40,7 @@ fps /= asc_context.elapsed_millis; if (fps != last_fps) { last_fps = fps; - snprintf(((AscText*)node)->text, 9, "%u FPS", fps); + snprintf(asc_text_data(node)->text, 9, "%u FPS", fps); asc_node_update(node); } } @@ -49,8 +49,9 @@ static void create_fps_counter(void) { asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); asc_ink_rgb(255, 0, 0); - AscText* text = asc_text(10, 10, "XXXXX FPS"); - asc_scene_add_behavior(asc_node(text), update_fps_counter); + AscSceneNode* node = asc_text(10, 10, "XXXXX FPS"); + asc_scene_add_behavior(node, update_fps_counter); + asc_scene_add(asc_window_active_ui, node); } int main(int argc, char** argv) { @@ -63,7 +64,7 @@ AscWindowSettings settings; asc_window_settings_init_defaults(&settings); - settings.title = "Sandbox Application"; + settings.title = "Snake"; AscWindow *window = asc_window_initialize(0, &settings); asc_shader_initialize_predefined();