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 |
--- 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.
--- 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. *
--- 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) {
--- 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();