1
0
Fork 0

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: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3965>
This commit is contained in:
Joan Torres 2024-09-05 16:04:14 +02:00 committed by Marge Bot
parent 1eba07f64f
commit 9c0200570a
2 changed files with 122 additions and 0 deletions

View file

@ -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;

View file

@ -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);