improve macros in datatypes.h - fixes #692 default tip

Wed, 02 Jul 2025 23:55:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 02 Jul 2025 23:55:50 +0200
changeset 175
e5544920377e
parent 174
0009b982bc7d

improve macros in datatypes.h - fixes #692

src/2d.c file | annotate | diff | comparison | revisions
src/ascension/datatypes.h file | annotate | diff | comparison | revisions
src/ascension/scene_node.h file | annotate | diff | comparison | revisions
src/mesh.c file | annotate | diff | comparison | revisions
src/scene_node.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/text.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/2d.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/2d.c	Wed Jul 02 23:55:50 2025 +0200
@@ -179,7 +179,7 @@
     } else {
         rectangle->thickness = args.thickness;
     }
-    if (!args.filled && asc_col4_test_zero(args.border_color)) {
+    if (!args.filled && asc_memcmpz(&args.border_color, sizeof(args.border_color))) {
         // convenience fallback:
         // when we are drawing an outline but have no explicit border color,
         // use the active ink
@@ -188,7 +188,7 @@
 
     AscSceneNode *node = &rectangle->node;
     node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
-    node->scale = asc_vec3f_one;
+    node->scale = ASC_VEC3F_1;
     node->render_group = asc_context.ink.alpha < 255
                              ? ASC_RENDER_GROUP_2D_BLEND
                              : ASC_RENDER_GROUP_2D_OPAQUE;
@@ -333,7 +333,7 @@
     } else {
         ellipsis->thickness = args.thickness;
     }
-    if (!args.filled && asc_col4_test_zero(args.border_color)) {
+    if (!args.filled && asc_memcmpz(&args.border_color, sizeof(args.border_color))) {
         // convenience fallback:
         // when we are drawing an outline but have no explicit border color,
         // use the active ink
@@ -342,7 +342,7 @@
 
     AscSceneNode *node = &ellipsis->node;
     node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
-    node->scale = asc_vec3f_one;
+    node->scale = ASC_VEC3F_1;
     node->render_group = asc_context.ink.alpha < 255
                              ? ASC_RENDER_GROUP_2D_BLEND
                              : ASC_RENDER_GROUP_2D_OPAQUE;
--- a/src/ascension/datatypes.h	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/ascension/datatypes.h	Wed Jul 02 23:55:50 2025 +0200
@@ -75,20 +75,24 @@
     struct { int width, height; };
     int data[2];
 } asc_vec2i;
-#define asc_vec2i_new(x, y) (asc_vec2i){{(int)x, (int)y}}
+#define ASC_VEC2I(x, y) (asc_vec2i){{(int)x, (int)y}}
+#define ASC_VEC2I_0 (asc_vec2i){{0, 0}}
+#define ASC_VEC2I_1 (asc_vec2i){{1, 1}}
 
 typedef union asc_vec2u {
     struct { unsigned x, y; };
     struct { unsigned width, height; };
     unsigned data[2];
 } asc_vec2u;
-#define asc_vec2u_new(x, y) (asc_vec2u){{(unsigned)x, (unsigned)y}}
+#define ASC_VEC2U(x, y) (asc_vec2u){{(unsigned)x, (unsigned)y}}
+#define ASC_VEC2U_0 (asc_vec2u){{0u, 0u}}
+#define ASC_VEC2U_1 (asc_vec2u){{1u, 1u}}
 
 typedef struct asc_recti {
     asc_vec2i pos;
     asc_vec2u size;
 } asc_recti;
-#define asc_recti_new(x, y, w, h) (asc_recti){asc_vec2i_new(x,y), asc_vec2u_new(w,h)}
+#define ASC_RECTI(x, y, w, h) (asc_recti){ASC_VEC2I(x,y), ASC_VEC2U(w,h)}
 
 typedef union asc_vec2f {
     struct { float x, y; };
@@ -96,9 +100,9 @@
     struct { float u, v; };
     float data[2];
 } asc_vec2f;
-#define asc_vec2f_new(x, y) (asc_vec2f){{(float)x, (float)y}}
-#define asc_vec2f_zero (asc_vec2f){{0, 0}}
-#define asc_vec2f_one (asc_vec2f){{1, 1}}
+#define ASC_VEC2F(x, y) (asc_vec2f){{(float)x, (float)y}}
+#define ASC_VEC2F_0 (asc_vec2f){{0.0f, 0.0f}}
+#define ASC_VEC2F_1 (asc_vec2f){{1.0f, 1.0f}}
 
 typedef union asc_vec3f {
     struct { float x, y, z; };
@@ -106,24 +110,22 @@
     struct { float pitch, yaw, roll; };
     float data[3];
 } asc_vec3f;
-#define asc_vec3f_new(x, y, z) (asc_vec3f){{(float)x, (float)y, (float)(z)}}
-#define asc_vec3f_zero (asc_vec3f){{0, 0, 0}}
-#define asc_vec3f_one (asc_vec3f){{1, 1, 1}}
+#define ASC_VEC3F(x, y, z) (asc_vec3f){{(float)x, (float)y, (float)(z)}}
+#define ASC_VEC3F_0 (asc_vec3f){{0.0f, 0.0f, 0.0f}}
+#define ASC_VEC3F_1 (asc_vec3f){{1.0f, 1.0f, 1.0f}}
 
 typedef struct asc_col4i {
     asc_ubyte red, green, blue, alpha;
 } asc_col4i;
-#define asc_col4i_new(r, g, b, a) (asc_col4i){(asc_ubyte)r, (asc_ubyte)g, (asc_ubyte)b, (asc_ubyte)a}
 
 typedef struct asc_col4f {
     float red, green, blue, alpha;
 } asc_col4f;
-#define asc_col4f_new(r, g, b, a) (asc_col4f){(float)r, (float)g, (float)b, (float)a}
 
-#define asc_rgb(r,g,b) asc_col4i_new(r,g,b,255)
-#define asc_rgba(r,g,b,a) asc_col4i_new(r,g,b,a)
-
-#define asc_col4_test_zero(v) (v.red == 0 && v.green == 0 && v.blue == 0 && v.alpha == 0)
+#define ASC_RGB(r,g,b) (asc_col4i){(asc_ubyte)r, (asc_ubyte)g, (asc_ubyte)b, 255u}
+#define ASC_RGBA(r,g,b,a) (asc_col4i){(asc_ubyte)r, (asc_ubyte)g, (asc_ubyte)b, (asc_ubyte)a}
+#define ASC_RGB_F(r,g,b) (asc_col4f){r,g,b,1.f}
+#define ASC_RGBA_F(r,g,b,a) (asc_col4f){r,g,b,a}
 
 typedef float asc_mat4f[16];
 
@@ -131,6 +133,15 @@
 //    General Utility Functions
 // --------------------------------------------------------------------------
 
+static inline bool asc_memcmpz(const void *mem, size_t n) {
+    const unsigned char *p = mem;
+    // TODO: for some reason this is not vectorized - find out why!
+    for (size_t i = 0; i < n ; i++) {
+        if (p[i]>0) return false;
+    }
+    return true;
+}
+
 static inline int asc_clamp_i(int v, int min, int max) {
     if (v < min) return min;
     if (v > max) return max;
@@ -185,11 +196,11 @@
 // --------------------------------------------------------------------------
 
 static inline asc_vec2f asc_vec2f_scale(asc_vec2f v, float s) {
-    return asc_vec2f_new(v.x*s, v.y*s);
+    return ASC_VEC2F(v.x*s, v.y*s);
 }
 
 static inline asc_vec3f asc_vec3f_scale(asc_vec3f v, float s) {
-    return asc_vec3f_new(v.x*s, v.y*s, v.z*s);
+    return ASC_VEC3F(v.x*s, v.y*s, v.z*s);
 }
 
 static inline unsigned asc_vec2u_sqrlen(asc_vec2u v) {
--- a/src/ascension/scene_node.h	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/ascension/scene_node.h	Wed Jul 02 23:55:50 2025 +0200
@@ -201,7 +201,7 @@
 }
 
 ASC_TRANFORM_FUNC asc_vec2i asc_get_position2d(AscSceneNode *node) {
-    return asc_vec2i_new(node->position.x, node->position.y);
+    return ASC_VEC2I(node->position.x, node->position.y);
 }
 
 ASC_TRANFORM_FUNC void asc_set_rotation(AscSceneNode *node, float pitch, float yaw, float roll) {
--- a/src/mesh.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/mesh.c	Wed Jul 02 23:55:50 2025 +0200
@@ -109,17 +109,17 @@
     args.uv_scale.y = ASC_NONZERO_OR(1.f, args.uv_scale.y);
 
     // bottom left
-    data[0].pos = asc_vec2f_new(0.0f, 0.0f);
-    data[0].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y);
+    data[0].pos = ASC_VEC2F(0.0f, 0.0f);
+    data[0].uv = ASC_VEC2F(args.uv_offset.x, args.uv_offset.y);
     // top left
-    data[1].pos = asc_vec2f_new(0.0f, args.size.y);
-    data[1].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y + args.uv_scale.y);
+    data[1].pos = ASC_VEC2F(0.0f, args.size.y);
+    data[1].uv = ASC_VEC2F(args.uv_offset.x, args.uv_offset.y + args.uv_scale.y);
     // bottom right
-    data[2].pos = asc_vec2f_new(args.size.x, 0.0f);
-    data[2].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y);
+    data[2].pos = ASC_VEC2F(args.size.x, 0.0f);
+    data[2].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y);
     // top right
-    data[3].pos = asc_vec2f_new(args.size.x, args.size.y);
-    data[3].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
+    data[3].pos = ASC_VEC2F(args.size.x, args.size.y);
+    data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
     glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
     glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW);
     // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway
--- a/src/scene_node.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/scene_node.c	Wed Jul 02 23:55:50 2025 +0200
@@ -48,7 +48,7 @@
 AscSceneNode *asc_scene_node_empty(void) {
     AscSceneNode *node = cxZallocDefault(sizeof(AscSceneNode));
     node->render_group = ASC_RENDER_GROUP_NONE;
-    node->scale = asc_vec3f_one;
+    node->scale = ASC_VEC3F_1;
     asc_transform_identity(node->transform);
     asc_transform_identity(node->world_transform);
     return node;
--- a/src/sprite.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/sprite.c	Wed Jul 02 23:55:50 2025 +0200
@@ -77,14 +77,14 @@
     asc_vec2f uv_scale;
     if (sprite->texture_scale_mode == ASC_TEXTURE_SCALE_REPEAT) {
         uv_scale = asc_texture_calculate_uv_scale(sprite->texture,
-            asc_vec2u_new(sprite->width, sprite->height), sprite->texture_scale);
+            ASC_VEC2U(sprite->width, sprite->height), sprite->texture_scale);
     } else {
         uv_scale = sprite->texture_scale;
     }
 
     // update mesh
     asc_mesh_plane_2d(&sprite->mesh,
-        .size = asc_vec2f_new(sprite->width, sprite->height),
+        .size = ASC_VEC2F(sprite->width, sprite->height),
         .uv_scale = uv_scale
     );
 }
@@ -133,8 +133,8 @@
     node->destroy_func = asc_sprite_destroy;
     node->draw_func = asc_sprite_draw;
 
-    node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
-    node->scale = asc_vec3f_one;
+    node->position = ASC_VEC3F(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
+    node->scale = ASC_VEC3F_1;
 
     asc_node_update(node);
     return node;
--- a/src/text.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/text.c	Wed Jul 02 23:55:50 2025 +0200
@@ -68,9 +68,9 @@
     if (text->dimension.x != (unsigned)surface->w || text->dimension.y != (unsigned)surface->h) {
         text->dimension.x = surface->w;
         text->dimension.y = surface->h;
-        const asc_vec2f uv_scale = asc_texture_calculate_uv_scale(sprite->texture, text->dimension, asc_vec2f_one);
+        const asc_vec2f uv_scale = asc_texture_calculate_uv_scale(sprite->texture, text->dimension, ASC_VEC2F_1);
         asc_mesh_plane_2d(&text->base.mesh,
-            .size = asc_vec2f_new(surface->w, surface->h),
+            .size = ASC_VEC2F(surface->w, surface->h),
             .uv_scale = uv_scale
         );
     }
@@ -102,8 +102,8 @@
     node->destroy_func = asc_text_destroy;
     node->update_func = asc_text_update;
     node->draw_func = asc_sprite_draw;
-    node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
-    node->scale = asc_vec3f_one;
+    node->position = ASC_VEC3F(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
+    node->scale = ASC_VEC3F_1;
 
     // text properties
     node->flags = args.alignment; // use flags variable to save some space
--- a/src/window.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/src/window.c	Wed Jul 02 23:55:50 2025 +0200
@@ -73,7 +73,7 @@
     {
         Sint32 w, h;
         SDL_GetWindowSize(window->window, &w, &h);
-        window->dimensions = asc_vec2u_new(w, h);
+        window->dimensions = ASC_VEC2U(w, h);
     }
     window->resized = true; // count initial sizing as resize
 
@@ -83,7 +83,7 @@
     if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
         asc_scene_init(&window->ui,
             .type = ASC_CAMERA_ORTHO,
-            .ortho.rect = asc_recti_new(0, 0, window->dimensions.width, window->dimensions.height),
+            .ortho.rect = ASC_RECTI(0, 0, window->dimensions.width, window->dimensions.height),
             .projection_update_func = asc_camera_ortho_update_size
         );
         asc_dprintf("Window %u initialized at index %u", window->id, index);
@@ -206,5 +206,5 @@
         asc_error("Failed to get display mode for window %u on display %d: %s",
             window->id, display, SDL_GetError());
     }
-    return asc_vec2u_new(dm.w, dm.h);
+    return ASC_VEC2U(dm.w, dm.h);
 }
--- a/test/snake/snake.c	Wed Jul 02 23:21:17 2025 +0200
+++ b/test/snake/snake.c	Wed Jul 02 23:55:50 2025 +0200
@@ -144,7 +144,7 @@
     }
 
     // Set the viewport to the scaled and centered region
-    return asc_recti_new(offset_x, offset_y, viewport_size, viewport_size);
+    return ASC_RECTI(offset_x, offset_y, viewport_size, viewport_size);
 }
 
 int main(void) {
@@ -179,7 +179,7 @@
     asc_ink_rgb(0, 128, 90);
     asc_scene_init(MAIN_SCENE,
         .type = ASC_CAMERA_ORTHO,
-        .ortho.rect = asc_recti_new(0, 0, game_field_size, game_field_size),
+        .ortho.rect = ASC_RECTI(0, 0, game_field_size, game_field_size),
         .viewport_clear = true,
         .viewport_update_func = update_viewport_for_window_resize
     );

mercurial