Mon, 16 Jun 2025 20:24:47 +0200
add new ui.c where several UI functions are now moved to
src/Makefile | file | annotate | diff | comparison | revisions | |
src/ascension/ui.h | file | annotate | diff | comparison | revisions | |
src/ascension/window.h | file | annotate | diff | comparison | revisions | |
src/ui.c | file | annotate | diff | comparison | revisions | |
src/window.c | file | annotate | diff | comparison | revisions | |
test/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/src/Makefile Sun Jun 15 21:02:29 2025 +0200 +++ b/src/Makefile Mon Jun 16 20:24:47 2025 +0200 @@ -31,7 +31,7 @@ window.c shader.c mesh.c texture.c \ sprite.c 2d.c \ camera.c scene.c scene_node.c behavior.c \ - font.c text.c + ui.c font.c text.c OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o) @@ -160,6 +160,17 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +$(BUILD_DIR)/ui.o: ui.c ascension/ui.h ascension/ui/text.h \ + ascension/ui/font.h ascension/ui/../sprite.h \ + ascension/ui/../scene_node.h ascension/ui/../datatypes.h \ + ascension/ui/../transform.h ascension/ui/../mesh.h \ + ascension/ui/../texture.h ascension/ui/../camera.h ascension/context.h \ + ascension/datatypes.h ascension/window.h ascension/glcontext.h \ + ascension/scene.h ascension/scene_node.h ascension/camera.h \ + ascension/input.h ascension/ui/font.h ascension/scene.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + $(BUILD_DIR)/window.o: window.c ascension/error.h ascension/window.h \ ascension/datatypes.h ascension/glcontext.h ascension/scene.h \ ascension/scene_node.h ascension/transform.h ascension/camera.h \
--- a/src/ascension/ui.h Sun Jun 15 21:02:29 2025 +0200 +++ b/src/ascension/ui.h Mon Jun 16 20:24:47 2025 +0200 @@ -28,7 +28,38 @@ #ifndef ASCENSION_UI_H #define ASCENSION_UI_H +#ifndef ASCENSION_UI_IMPLEMENTATION #include "ui/text.h" +#endif + +/** + * Sets the UI scaling factor for the active window. + * + * @param scale the scaling factor + */ +void asc_ui_scale(float scale); + +/** + * Returns the current UI scaling factor for the active window. + * + * @return the scaling factor + */ +float asc_ui_get_scale(void); + +/** + * Automatically chooses a UI scaling factor depending on display resolution. + * + * @note you need to call this function again when the window changes the display + * // TODO: check if there is an SDL event when the window changes the display and implement a reaction + */ +void asc_ui_scale_auto(void); + +/** + * Adds a node to the active window's UI. + * + * @param node the node to add + */ +void asc_ui_add_node(AscSceneNode *node); #endif /* ASCENSION_UI_H */
--- a/src/ascension/window.h Sun Jun 15 21:02:29 2025 +0200 +++ b/src/ascension/window.h Mon Jun 16 20:24:47 2025 +0200 @@ -148,31 +148,5 @@ */ asc_vec2u asc_window_display_resolution(void); -// TODO: there is a ui.h - better move stuff there? - -/** - * Sets the UI scaling factor for the active window. - * - * @param scale the scaling factor - */ -void asc_ui_scale(float scale); - -/** - * Returns the current UI scaling factor for the active window. - * - * @return the scaling factor - */ -float asc_ui_get_scale(void); - -/** - * Automatically chooses a UI scaling factor depending on display resolution. - * - * @note you need to call this function again when the window changes the display - * // TODO: check if there is an SDL event when the window changes the display and implement a reaction - */ -void asc_ui_scale_auto(void); - -void asc_add_ui_node(AscSceneNode *node); - #endif /* ASCENSION_WINDOW_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ui.c Mon Jun 16 20:24:47 2025 +0200 @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2025 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define ASC_UI_IMPLEMENTATION +#include "ascension/ui.h" + +#include "ascension/context.h" +#include "ascension/scene.h" + +void asc_ui_add_node(AscSceneNode *node) { + asc_scene_add_node(&asc_active_window->ui, node); +} + +void asc_ui_scale(float scale) { + asc_active_window->ui_scale = scale; +} + +float asc_ui_get_scale(void) { + return asc_active_window->ui_scale; +} + +void asc_ui_scale_auto(void) { + asc_vec2u res = asc_window_display_resolution(); + if (res.width > 3100) { + asc_ui_scale(2.0f); + } else if (res.width > 2000) { + asc_ui_scale(1.5f); + } else { + return asc_ui_scale(1.f); + } +}
--- a/src/window.c Sun Jun 15 21:02:29 2025 +0200 +++ b/src/window.c Mon Jun 16 20:24:47 2025 +0200 @@ -194,10 +194,6 @@ return i; } -void asc_add_ui_node(AscSceneNode *node) { - asc_scene_add_node(&asc_active_window->ui, node); -} - AscScene *asc_window_scene(unsigned int index) { return &asc_active_window->scenes[index]; } @@ -212,23 +208,3 @@ } return asc_vec2u_new(dm.w, dm.h); } - -void asc_ui_scale(float scale) { - asc_active_window->ui_scale = scale; -} - -float asc_ui_get_scale(void) { - return asc_active_window->ui_scale; -} - -void asc_ui_scale_auto(void) { - asc_vec2u res = asc_window_display_resolution(); - // TODO: debug why this is wrong under GNOME or just throw GNOME away - if (res.width > 3000) { - asc_ui_scale(1.5f); - } else if (res.width > 2000) { - asc_ui_scale(1.2f); - } else { - return asc_ui_scale(1.f); - } -}
--- a/test/snake/snake.c Sun Jun 15 21:02:29 2025 +0200 +++ b/test/snake/snake.c Mon Jun 16 20:24:47 2025 +0200 @@ -104,7 +104,7 @@ AscSceneNode *node = asc_text(.name = "FPS Counter"); asc_behavior_add(node, .func = update_fps_counter, .interval = asc_seconds(1)); asc_behavior_add(node, .func = tie_fps_counter_to_corner); - asc_add_ui_node(node); + asc_ui_add_node(node); } static void create_score_counter(void) { @@ -115,7 +115,7 @@ .x = 10, .y = 10, .text = "Score: 0" ); - asc_add_ui_node(node); + asc_ui_add_node(node); } static void create_spaceship(void) {