test/snake/snake.c

changeset 89
e1f682a8a145
parent 88
6234b7ea48f3
--- a/test/snake/snake.c	Sun Apr 20 15:41:16 2025 +0200
+++ b/test/snake/snake.c	Mon Apr 21 17:27:33 2025 +0200
@@ -25,11 +25,27 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "ascension/core.h"
+#include <ascension/core.h>
 #include <ascension/ui.h>
 
 #include <cx/printf.h>
 
+enum Textures2d {
+    TEX_SHIP = 0,
+    TEX2D_COUNT
+};
+static AscTexture tex2d[TEX2D_COUNT];
+#define TEXTURE_SHIP &tex2d[TEX_SHIP]
+
+static void init_textures(void) {
+    asc_texture_init_2d(tex2d, TEX2D_COUNT);
+    asc_texture_from_file(TEXTURE_SHIP, "ship.png");
+}
+
+static void destroy_textures(void) {
+    asc_texture_destroy(tex2d, TEX2D_COUNT);
+}
+
 static void update_fps_counter(AscSceneNode *node) {
     static uint64_t last_fps = 0;
     static uint64_t debounce = ASC_NANOS_SECOND - 1;
@@ -75,18 +91,9 @@
     asc_add_ui_node(node);
 }
 
-static void free_spaceship(AscSceneNode *node) {
-    asc_texture_destroy(&((AscSprite*)node)->tex);
-    free(node);
-}
-
 static void create_spaceship(void) {
-    // TODO: textures should be passed by pointers (and probably ref-counted)
-    AscTexture texture;
-    asc_texture_init_2d(&texture);
-    asc_texture_from_file(&texture, "ship.png");
     AscSceneNode *sprite = asc_sprite(
-        .texture = texture,
+        .texture = TEXTURE_SHIP,
         .x = 250,
         .y = 300,
         .width = 64,
@@ -96,9 +103,6 @@
     // TODO: add to 2D scene instead of UI scene, once we have one
     asc_add_ui_node(sprite);
 
-    // TODO: remove this hack by refactoring how textures work
-    sprite->free_func = free_spaceship;
-
     // TODO: return something
 }
 
@@ -106,7 +110,7 @@
     asc_context_initialize();
     if (asc_has_error()) {
         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
-                "Fatal Error",asc_get_error(),NULL);
+                "Fatal Error",asc_get_error(), NULL);
         return 1;
     }
 #ifdef TEST_BUILD
@@ -123,6 +127,9 @@
     settings.title = "Snake";
     asc_window_initialize(0, &settings);
 
+    // load textures
+    init_textures();
+
     // create UI elements
     create_fps_counter();
     create_score_counter();
@@ -147,6 +154,8 @@
         }
     } while (asc_loop_next());
 
+    // cleanup
+    destroy_textures();
     asc_context_destroy();
     return 0;
 }

mercurial