From 9c0200570a073b980e0f6fd84f453f4861b9da1e Mon Sep 17 00:00:00 2001 From: Joan Torres Date: Thu, 5 Sep 2024 16:04:14 +0200 Subject: [PATCH] cogl/boxed-value: Add function _cogl_boxed_value_to_string This function returns a string of the boxed value considering type, size and count; and adds a name. e.g: vec2 scale = vec2(1.0000, 1.0000) Also handle NULL cases on _cogl_boxed_value_equal. This will be used by the next commit. Part-of: --- cogl/cogl/cogl-boxed-value.c | 118 +++++++++++++++++++++++++++++++++++ cogl/cogl/cogl-boxed-value.h | 4 ++ 2 files changed, 122 insertions(+) diff --git a/cogl/cogl/cogl-boxed-value.c b/cogl/cogl/cogl-boxed-value.c index 1325fcb7d..70a31da73 100644 --- a/cogl/cogl/cogl-boxed-value.c +++ b/cogl/cogl/cogl-boxed-value.c @@ -35,12 +35,130 @@ #include "cogl/cogl-boxed-value.h" #include "cogl/cogl-context-private.h" +static void +_cogl_boxed_value_append_type_to_string (GString *buf, + const CoglBoxedValue *bv) +{ + switch (bv->type) + { + case COGL_BOXED_INT: + if (bv->size == 1) + g_string_append (buf, "int"); + else + g_string_append_printf (buf, "ivec%i", bv->size); + return; + case COGL_BOXED_FLOAT: + if (bv->size == 1) + g_string_append (buf, "float"); + else + g_string_append_printf (buf, "vec%i", bv->size); + return; + case COGL_BOXED_MATRIX: + g_string_append_printf (buf, "mat%i", bv->size); + return; + case COGL_BOXED_NONE: + return; + } +} + +static void +_cogl_boxed_value_append_value_to_string (GString *buf, + const CoglBoxedValue *bv, + int count) +{ + int i, j; + int offset; + + if (bv->size > 1) + { + _cogl_boxed_value_append_type_to_string (buf, bv); + g_string_append (buf, "("); + } + + switch (bv->type) + { + case COGL_BOXED_INT: + for (i = 0; i < bv->size; i++) + { + if (bv->count > 1) + g_string_append_printf (buf, "%i, ", bv->v.int_array[(count * bv->size) + i]); + else + g_string_append_printf (buf, "%i, ", bv->v.int_value[i]); + } + break; + case COGL_BOXED_FLOAT: + for (i = 0; i < bv->size; i++) + { + if (bv->count > 1) + g_string_append_printf (buf, "%f, ", bv->v.float_array[(count * bv->size) + i]); + else + g_string_append_printf (buf, "%f, ", bv->v.float_value[i]); + } + break; + case COGL_BOXED_MATRIX: + offset = count * bv->size * bv->size; + + for (i = 0; i < bv->size; i++) + { + g_string_append (buf, "("); + + for (j = 0; j < bv->size; j++) + { + if (bv->count > 1) + g_string_append_printf (buf, "%f, ", bv->v.float_array[ + offset + (i * bv->size) + j]); + else + g_string_append_printf (buf, "%f, ", bv->v.matrix[(i * bv->size) + j]); + } + + g_string_erase (buf, buf->len - 2, 2); + g_string_append (buf, "), "); + } + break; + case COGL_BOXED_NONE: + return; + } + + g_string_erase (buf, buf->len - 2, 2); + + if (bv->size > 1) + g_string_append (buf, ")"); +} + +char * +_cogl_boxed_value_to_string (const CoglBoxedValue *bv, + const char *name) +{ + GString *buf; + int i; + + buf = g_string_new (NULL); + for (i = 0; i < bv->count; i++) + { + _cogl_boxed_value_append_type_to_string (buf, bv); + + g_string_append_printf (buf, " %s", name); + + if (bv->count > 1) + g_string_append_printf (buf, "[%i] = ", i); + else + g_string_append (buf, " = "); + + _cogl_boxed_value_append_value_to_string (buf, bv, i); + } + + return g_string_free_and_steal (buf); +} + gboolean _cogl_boxed_value_equal (const CoglBoxedValue *bva, const CoglBoxedValue *bvb) { const void *pa, *pb; + if (bva == NULL || bvb == NULL) + return bva == bvb; + if (bva->type != bvb->type) return FALSE; diff --git a/cogl/cogl/cogl-boxed-value.h b/cogl/cogl/cogl-boxed-value.h index 1fbfed831..0a03bd0e4 100644 --- a/cogl/cogl/cogl-boxed-value.h +++ b/cogl/cogl/cogl-boxed-value.h @@ -63,6 +63,10 @@ typedef struct _CoglBoxedValue _bv->count = 1; \ } G_STMT_END +char * +_cogl_boxed_value_to_string (const CoglBoxedValue *bv, + const char *name); + gboolean _cogl_boxed_value_equal (const CoglBoxedValue *bva, const CoglBoxedValue *bvb);