add asc_create_flags() utility function

Sun, 27 Jul 2025 23:34:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 27 Jul 2025 23:34:19 +0200
changeset 228
ecda73508be6
parent 227
a5775e8dbc13
child 229
5c05b5164a16

add asc_create_flags() utility function

src/2d.c file | annotate | diff | comparison | revisions
src/ascension/datatypes.h file | annotate | diff | comparison | revisions
--- a/src/2d.c	Sat Jul 26 21:13:05 2025 +0200
+++ b/src/2d.c	Sun Jul 27 23:34:19 2025 +0200
@@ -28,7 +28,6 @@
 #include "ascension/2d.h"
 
 #include "ascension/constants.h"
-#include "ascension/error.h"
 #include "ascension/shader.h"
 
 #include <assert.h>
@@ -92,16 +91,12 @@
     const bool border = rectangle->thickness > 0;
 
     // Compute shader flags
-    int shader_flags = 0;
-    // TODO: think about a bool-array to int utility function
-    if (filled) shader_flags |= ASC_RECTANGLE_SHADER_FLAG_FILL;
-    if (border) shader_flags |= ASC_RECTANGLE_SHADER_FLAG_BORDER;
-    if (round) shader_flags |= ASC_RECTANGLE_SHADER_FLAG_ROUND;
+    int flags = asc_create_flags(1, 3, filled, round, border);
 
     // Look up and activate the shader
     const AscShaderProgram *shader = asc_shader_lookup_or_create(
-        ASC_SHADER_RECTANGLE(shader_flags),
-        asc_rectangle_shader_create, shader_flags
+        ASC_SHADER_RECTANGLE(flags),
+        asc_rectangle_shader_create, flags
     );
     if (asc_shader_use(shader, camera)) return;
     asc_cptr_cast(AscRectangleShader, rect_shader, shader);
@@ -227,9 +222,7 @@
     const bool border = ellipsis->thickness > 0;
 
     // Compute shader flags
-    int shader_flags = 0;
-    if (filled) shader_flags |= ASC_ELLIPSIS_SHADER_FLAG_FILL;
-    if (border) shader_flags |= ASC_ELLIPSIS_SHADER_FLAG_BORDER;
+    int shader_flags = asc_create_flags(1, 2, filled, border);
 
     // Look up and activate the shader
     const AscShaderProgram *shader = asc_shader_lookup_or_create(
--- a/src/ascension/datatypes.h	Sat Jul 26 21:13:05 2025 +0200
+++ b/src/ascension/datatypes.h	Sun Jul 27 23:34:19 2025 +0200
@@ -33,6 +33,7 @@
 #endif
 
 #include <stdbool.h>
+#include <stdarg.h>
 #include <string.h>
 #include <SDL2/SDL_pixels.h>
 
@@ -60,6 +61,20 @@
 #define asc_set_flag(reg, flag) (reg |= flag)
 #define asc_set_flag_masked(reg, mask, flag) (reg = (reg & ~(mask)) | flag)
 
+static inline int asc_create_flags(unsigned start, unsigned n, ...) {
+    va_list args;
+    va_start(args, n);
+    int result = 0;
+    for (unsigned i = 0; i < n; i++) {
+        if (va_arg(args, int)) {
+            result |= start;
+        }
+        start <<= 1;
+    }
+    va_end(args);
+    return result;
+}
+
 #define asc_ptr_cast(type, lvalue, rvalue)  type *lvalue = (type *)(rvalue);
 #define asc_cptr_cast(type, lvalue, rvalue)  const type *lvalue = (const type *)(rvalue);
 

mercurial