Sat, 19 Apr 2025 12:54:49 +0200
put snake test program into separate directory
Makefile | file | annotate | diff | comparison | revisions | |
configure | file | annotate | diff | comparison | revisions | |
make/project.xml | file | annotate | diff | comparison | revisions | |
test/Makefile | file | annotate | diff | comparison | revisions | |
test/snake.c | file | annotate | diff | comparison | revisions | |
test/snake/Makefile | file | annotate | diff | comparison | revisions | |
test/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/Makefile Sat Apr 19 12:18:43 2025 +0200 +++ b/Makefile Sat Apr 19 12:54:49 2025 +0200 @@ -23,17 +23,16 @@ # POSSIBILITY OF SUCH DAMAGE. # -all: build/test/sandbox +all: config.mk build/lib/libascension.a -build/test/sandbox: build/test build/lib/libascension.a config.mk FORCE - @cd test && $(MAKE) +test-snake: build/test/snake FORCE + +build/test/snake: all + @cd test/snake && $(MAKE) build/lib/libascension.a: build/lib config.mk FORCE @cd src && $(MAKE) -build/test: - mkdir -p build/test - build/lib: mkdir -p build/lib @@ -49,7 +48,7 @@ update-rules: make/update-rules.sh src - CFLAGS=-I../src make/update-rules.sh test + CFLAGS=-I`pwd`/src make/update-rules.sh test/snake FORCE:
--- a/configure Sat Apr 19 12:18:43 2025 +0200 +++ b/configure Sat Apr 19 12:54:49 2025 +0200 @@ -146,6 +146,10 @@ mandir= # custom variables +if true \ + ; then + SRCDIR=`pwd` +fi # features @@ -386,6 +390,9 @@ break fi + cat >> "$TEMP_DIR/make.mk" << __EOF__ +srcdir=$SRCDIR +__EOF__ break done break
--- a/make/project.xml Sat Apr 19 12:18:43 2025 +0200 +++ b/make/project.xml Sat Apr 19 12:54:49 2025 +0200 @@ -1,7 +1,11 @@ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://unixwork.de/uwproj" version="0.3"> + <config> + <var name="SRCDIR" exec="true">pwd</var> + </config> <dependency> <lang>c</lang> + <make>srcdir=$SRCDIR</make> </dependency> <dependency name="ucx">
--- a/test/Makefile Sat Apr 19 12:18:43 2025 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -# Copyright 2023 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. -# - -include ../config.mk - -BUILD_DIR=../build/test -LIB_ASCENSION=../build/lib/libascension.a -CFLAGS += -I../src - -all: $(BUILD_DIR)/snake FORCE - @echo "Demo game successfully built." - -$(BUILD_DIR)/snake: $(BUILD_DIR)/snake.o $(LIB_ASCENSION) - @echo "Linking snake..." - $(CC) $(LDFLAGS) -o $@ $^ - -$(LIB_ASCENSION): - test -f "$@" - -FORCE: - -$(BUILD_DIR)/snake.o: snake.c ../src/ascension/core.h \ - ../src/ascension/error.h ../src/ascension/context.h \ - ../src/ascension/datatypes.h ../src/ascension/window.h \ - ../src/ascension/glcontext.h ../src/ascension/primitives.h \ - ../src/ascension/mesh.h ../src/ascension/shader.h \ - ../src/ascension/scene.h ../src/ascension/scene_node.h \ - ../src/ascension/transform.h ../src/ascension/camera.h \ - ../src/ascension/input.h ../src/ascension/ui/font.h \ - ../src/ascension/ui.h ../src/ascension/ui/text.h \ - ../src/ascension/ui/font.h ../src/ascension/ui/../2d/sprite.h \ - ../src/ascension/ui/../2d/../scene_node.h \ - ../src/ascension/ui/../2d/../texture.h ../src/ascension/ui/../utils.h - @echo "Compiling $<" - $(CC) -o $@ $(CFLAGS) -c $< -
--- a/test/snake.c Sat Apr 19 12:18:43 2025 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * Copyright 2023 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. - */ - -#include "ascension/core.h" -#include <ascension/ui.h> - -#include <cx/printf.h> - -static void update_fps_counter(AscSceneNode *node) { - static uint64_t last_fps = 0; - static uint64_t debounce = ASC_NANOS_SECOND - 1; - debounce += asc_context.frame_nanos; - // only update text every second - if (debounce >= ASC_NANOS_SECOND) { - debounce = 0; - uint64_t fps = ASC_NANOS_SECOND; - fps /= asc_context.frame_nanos; - if (fps != last_fps) { - last_fps = fps; - asc_text_printf(node, "%"PRIu64" FPS", fps); - } - } - // tie to bottom right of the screen - if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) - || asc_active_window->resized) { - asc_vec2i bottom_right = asc_active_window->dimensions; - asc_vec2i scale = asc_get_scale2d(node); - asc_set_position2d( - node, - bottom_right.x - scale.width - 10, - bottom_right.y - scale.height - 10 - ); - } -} - -static void create_fps_counter(void) { - asc_font(ASC_FONT_REGULAR, 12); - asc_ink_rgba(128, 128, 128, 196); - AscSceneNode *node = asc_text(); - asc_scene_add_behavior(node, update_fps_counter); - asc_add_ui_node(node); -} - -static void create_score_counter(void) { - asc_font(ASC_FONT_BOLD, 16); - asc_ink_rgb(0, 255, 0); - AscSceneNode* node = asc_text( - .x = 10, .y = 10, - .text = "Score: 0" - ); - asc_add_ui_node(node); -} - -int main(int argc, char** argv) { - asc_context_initialize(); - if (asc_has_error()) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, - "Fatal Error",asc_get_error(),NULL); - return 1; - } - - // create window - AscWindowSettings settings; - asc_window_settings_init_defaults(&settings); - settings.title = "Snake"; - asc_window_initialize(0, &settings); - - // create UI elements - create_fps_counter(); - create_score_counter(); - - // Main Loop - do { - // quit application on any error - if (asc_has_error()) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, - "Fatal Error", asc_get_error(), - asc_active_window->window); - asc_clear_error(); - asc_context_quit(); - } - - // quit application on ESC key press - if (asc_key_pressed(ESCAPE)) { - asc_context_quit(); - } - } while (asc_loop_next()); - - asc_context_destroy(); - return 0; -} -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/snake/Makefile Sat Apr 19 12:54:49 2025 +0200 @@ -0,0 +1,72 @@ +# Copyright 2023 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. +# + +include ../../config.mk + +BUILD_DIR=$(srcdir)/build/test +LIB_ASCENSION=$(srcdir)/build/lib/libascension.a +CFLAGS += -I$(srcdir)/src + +all: $(BUILD_DIR) $(BUILD_DIR)/snake FORCE + @echo "Demo game 'snake' successfully built." + +$(BUILD_DIR): + mkdir -p $@ + +$(BUILD_DIR)/snake: $(BUILD_DIR)/snake.o $(LIB_ASCENSION) + @echo "Linking snake..." + $(CC) $(LDFLAGS) -o $@ $^ + +$(LIB_ASCENSION): + test -f "$@" + +FORCE: + +$(BUILD_DIR)/snake.o: snake.c \ + /home/mike/workspace/c/ascension/src/ascension/core.h \ + /home/mike/workspace/c/ascension/src/ascension/error.h \ + /home/mike/workspace/c/ascension/src/ascension/context.h \ + /home/mike/workspace/c/ascension/src/ascension/datatypes.h \ + /home/mike/workspace/c/ascension/src/ascension/window.h \ + /home/mike/workspace/c/ascension/src/ascension/glcontext.h \ + /home/mike/workspace/c/ascension/src/ascension/primitives.h \ + /home/mike/workspace/c/ascension/src/ascension/mesh.h \ + /home/mike/workspace/c/ascension/src/ascension/shader.h \ + /home/mike/workspace/c/ascension/src/ascension/scene.h \ + /home/mike/workspace/c/ascension/src/ascension/scene_node.h \ + /home/mike/workspace/c/ascension/src/ascension/transform.h \ + /home/mike/workspace/c/ascension/src/ascension/camera.h \ + /home/mike/workspace/c/ascension/src/ascension/input.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/font.h \ + /home/mike/workspace/c/ascension/src/ascension/ui.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/text.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/font.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/../2d/sprite.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/../2d/../scene_node.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/../2d/../texture.h \ + /home/mike/workspace/c/ascension/src/ascension/ui/../utils.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/snake/snake.c Sat Apr 19 12:54:49 2025 +0200 @@ -0,0 +1,116 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2023 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. + */ + +#include "ascension/core.h" +#include <ascension/ui.h> + +#include <cx/printf.h> + +static void update_fps_counter(AscSceneNode *node) { + static uint64_t last_fps = 0; + static uint64_t debounce = ASC_NANOS_SECOND - 1; + debounce += asc_context.frame_nanos; + // only update text every second + if (debounce >= ASC_NANOS_SECOND) { + debounce = 0; + uint64_t fps = ASC_NANOS_SECOND; + fps /= asc_context.frame_nanos; + if (fps != last_fps) { + last_fps = fps; + asc_text_printf(node, "%"PRIu64" FPS", fps); + } + } + // tie to bottom right of the screen + if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) + || asc_active_window->resized) { + asc_vec2i bottom_right = asc_active_window->dimensions; + asc_vec2i scale = asc_get_scale2d(node); + asc_set_position2d( + node, + bottom_right.x - scale.width - 10, + bottom_right.y - scale.height - 10 + ); + } +} + +static void create_fps_counter(void) { + asc_font(ASC_FONT_REGULAR, 12); + asc_ink_rgba(128, 128, 128, 196); + AscSceneNode *node = asc_text(); + asc_scene_add_behavior(node, update_fps_counter); + asc_add_ui_node(node); +} + +static void create_score_counter(void) { + asc_font(ASC_FONT_BOLD, 16); + asc_ink_rgb(0, 255, 0); + AscSceneNode* node = asc_text( + .x = 10, .y = 10, + .text = "Score: 0" + ); + asc_add_ui_node(node); +} + +int main(int argc, char** argv) { + asc_context_initialize(); + if (asc_has_error()) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, + "Fatal Error",asc_get_error(),NULL); + return 1; + } + + // create window + AscWindowSettings settings; + asc_window_settings_init_defaults(&settings); + settings.title = "Snake"; + asc_window_initialize(0, &settings); + + // create UI elements + create_fps_counter(); + create_score_counter(); + + // Main Loop + do { + // quit application on any error + if (asc_has_error()) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, + "Fatal Error", asc_get_error(), + asc_active_window->window); + asc_clear_error(); + asc_context_quit(); + } + + // quit application on ESC key press + if (asc_key_pressed(ESCAPE)) { + asc_context_quit(); + } + } while (asc_loop_next()); + + asc_context_destroy(); + return 0; +} +