2007-01-17 08:03:52 -05:00
|
|
|
/*
|
|
|
|
* Clutter.
|
|
|
|
*
|
|
|
|
* An OpenGL based 'interactive canvas' library.
|
|
|
|
*
|
2007-04-12 08:42:07 -04:00
|
|
|
* Authored By Tomas Frydrych <tf@openedhand.com>
|
2007-01-17 08:03:52 -05:00
|
|
|
*
|
2007-04-12 08:42:07 -04:00
|
|
|
* Copyright (C) 2006, 2007 OpenedHand
|
2007-01-17 08:03:52 -05:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-07-11 06:36:07 -04:00
|
|
|
#define G_IMPLEMENT_INLINES
|
|
|
|
|
2007-10-12 04:17:00 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-06-07 11:28:59 -04:00
|
|
|
#include "config.h"
|
2007-10-12 04:17:00 -04:00
|
|
|
#endif
|
2007-06-07 11:28:59 -04:00
|
|
|
|
2008-04-17 06:08:17 -04:00
|
|
|
#include <glib-object.h>
|
|
|
|
#include <gobject/gvaluecollector.h>
|
|
|
|
|
2007-10-12 05:39:41 -04:00
|
|
|
#include "clutter-fixed.h"
|
|
|
|
#include "clutter-private.h"
|
2007-01-15 13:37:12 -05:00
|
|
|
|
2007-06-07 11:28:59 -04:00
|
|
|
/**
|
|
|
|
* SECTION:clutter-fixed
|
|
|
|
* @short_description: Fixed Point API
|
|
|
|
*
|
2007-08-07 07:35:22 -04:00
|
|
|
* Clutter has a fixed point API targeted at platforms without a
|
|
|
|
* floating point unit, such as embedded devices. On such platforms
|
|
|
|
* this API should be preferred to the floating point one as it does
|
|
|
|
* not trigger the slow path of software emulation, relying on integer
|
|
|
|
* math for fixed-to-floating and floating-to-fixed conversion.
|
|
|
|
*
|
|
|
|
* It is no recommened for use on platforms with a floating point unit
|
2007-10-12 04:17:00 -04:00
|
|
|
* (eg desktop systems) nor for use in bindings.
|
2007-06-07 11:28:59 -04:00
|
|
|
*
|
|
|
|
* Basic rules of Fixed Point arithmethic:
|
|
|
|
*
|
|
|
|
* <itemizedlist>
|
|
|
|
* <listitem>
|
2008-06-25 09:21:25 -04:00
|
|
|
* <para>Two fixed point numbers can be directly added,
|
|
|
|
* subtracted and have their modulus taken.</para>
|
2007-06-07 11:28:59 -04:00
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>To add other numerical type to a fixed point number it has to
|
|
|
|
* be first converted to fixed point.</para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>A fixed point number can be directly multiplied or divided by
|
|
|
|
* an integer.</para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>Two fixed point numbers can only be multiplied and divided by the
|
2008-10-30 12:37:55 -04:00
|
|
|
* provided %CLUTTER_FIXED_MUL and %CLUTTER_FIXED_DIV macros.</para>
|
2007-06-07 11:28:59 -04:00
|
|
|
* </listitem>
|
|
|
|
* </itemizedlist>
|
|
|
|
*/
|
|
|
|
|
2008-04-17 06:08:17 -04:00
|
|
|
static GTypeInfo _info = {
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static GTypeFundamentalInfo _finfo = { 0, };
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_init_fixed (GValue *value)
|
|
|
|
{
|
|
|
|
value->data[0].v_int = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_copy_fixed (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
|
|
|
dest->data[0].v_int = src->data[0].v_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
clutter_value_collect_fixed (GValue *value,
|
|
|
|
guint n_collect_values,
|
|
|
|
GTypeCValue *collect_values,
|
|
|
|
guint collect_flags)
|
|
|
|
{
|
|
|
|
value->data[0].v_int = collect_values[0].v_int;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
clutter_value_lcopy_fixed (const GValue *value,
|
|
|
|
guint n_collect_values,
|
|
|
|
GTypeCValue *collect_values,
|
|
|
|
guint collect_flags)
|
|
|
|
{
|
|
|
|
gint32 *fixed_p = collect_values[0].v_pointer;
|
|
|
|
|
|
|
|
if (!fixed_p)
|
|
|
|
return g_strdup_printf ("value location for `%s' passed as NULL",
|
|
|
|
G_VALUE_TYPE_NAME (value));
|
|
|
|
|
|
|
|
*fixed_p = value->data[0].v_int;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-17 10:08:26 -04:00
|
|
|
static void
|
|
|
|
clutter_value_transform_fixed_int (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_int = COGL_FIXED_TO_INT (src->data[0].v_int);
|
2008-04-17 10:08:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_transform_fixed_double (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_double = COGL_FIXED_TO_DOUBLE (src->data[0].v_int);
|
2008-04-17 10:08:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_transform_fixed_float (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_float = COGL_FIXED_TO_FLOAT (src->data[0].v_int);
|
2008-04-17 10:08:26 -04:00
|
|
|
}
|
|
|
|
|
2008-05-09 06:58:26 -04:00
|
|
|
static void
|
|
|
|
clutter_value_transform_int_fixed (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_int = COGL_FIXED_FROM_INT (src->data[0].v_int);
|
2008-05-09 06:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_transform_double_fixed (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_int = COGL_FIXED_FROM_FLOAT (src->data[0].v_double);
|
2008-05-09 06:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_value_transform_float_fixed (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
dest->data[0].v_int = COGL_FIXED_FROM_FLOAT (src->data[0].v_float);
|
2008-05-09 06:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-17 06:08:17 -04:00
|
|
|
static const GTypeValueTable _clutter_fixed_value_table = {
|
|
|
|
clutter_value_init_fixed,
|
|
|
|
NULL,
|
|
|
|
clutter_value_copy_fixed,
|
|
|
|
NULL,
|
|
|
|
"i",
|
|
|
|
clutter_value_collect_fixed,
|
|
|
|
"p",
|
|
|
|
clutter_value_lcopy_fixed
|
|
|
|
};
|
|
|
|
|
|
|
|
GType
|
|
|
|
clutter_fixed_get_type (void)
|
|
|
|
{
|
|
|
|
static GType _clutter_fixed_type = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (_clutter_fixed_type == 0))
|
|
|
|
{
|
|
|
|
_info.value_table = & _clutter_fixed_value_table;
|
|
|
|
_clutter_fixed_type =
|
|
|
|
g_type_register_fundamental (g_type_fundamental_next (),
|
|
|
|
I_("ClutterFixed"),
|
|
|
|
&_info, &_finfo, 0);
|
2008-04-17 10:08:26 -04:00
|
|
|
|
|
|
|
g_value_register_transform_func (_clutter_fixed_type, G_TYPE_INT,
|
|
|
|
clutter_value_transform_fixed_int);
|
2008-05-09 06:58:26 -04:00
|
|
|
g_value_register_transform_func (G_TYPE_INT, _clutter_fixed_type,
|
|
|
|
clutter_value_transform_int_fixed);
|
2008-04-17 10:08:26 -04:00
|
|
|
g_value_register_transform_func (_clutter_fixed_type, G_TYPE_FLOAT,
|
|
|
|
clutter_value_transform_fixed_float);
|
2008-05-09 06:58:26 -04:00
|
|
|
g_value_register_transform_func (G_TYPE_FLOAT, _clutter_fixed_type,
|
|
|
|
clutter_value_transform_float_fixed);
|
2008-04-17 10:08:26 -04:00
|
|
|
g_value_register_transform_func (_clutter_fixed_type, G_TYPE_DOUBLE,
|
|
|
|
clutter_value_transform_fixed_double);
|
2008-05-09 06:58:26 -04:00
|
|
|
g_value_register_transform_func (G_TYPE_DOUBLE, _clutter_fixed_type,
|
|
|
|
clutter_value_transform_double_fixed);
|
2008-04-17 06:08:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return _clutter_fixed_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_value_set_fixed:
|
|
|
|
* @value: a #GValue initialized to #CLUTTER_TYPE_FIXED
|
|
|
|
* @fixed_: the fixed point value to set
|
|
|
|
*
|
|
|
|
* Sets @value to @fixed_.
|
|
|
|
*
|
|
|
|
* Since: 0.8
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clutter_value_set_fixed (GValue *value,
|
|
|
|
ClutterFixed fixed_)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CLUTTER_VALUE_HOLDS_FIXED (value));
|
|
|
|
|
|
|
|
value->data[0].v_int = fixed_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_value_get_fixed:
|
|
|
|
* @value: a #GValue initialized to #CLUTTER_TYPE_FIXED
|
|
|
|
*
|
|
|
|
* Gets the fixed point value stored inside @value.
|
|
|
|
*
|
|
|
|
* Return value: the value inside the passed #GValue
|
|
|
|
*
|
|
|
|
* Since: 0.8
|
|
|
|
*/
|
|
|
|
ClutterFixed
|
2008-04-17 07:42:08 -04:00
|
|
|
clutter_value_get_fixed (const GValue *value)
|
2008-04-17 06:08:17 -04:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (CLUTTER_VALUE_HOLDS_FIXED (value), 0);
|
|
|
|
|
|
|
|
return value->data[0].v_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
param_fixed_init (GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
ClutterParamSpecFixed *fspec = CLUTTER_PARAM_SPEC_FIXED (pspec);
|
|
|
|
|
2008-10-30 12:37:55 -04:00
|
|
|
fspec->minimum = COGL_FIXED_MIN;
|
|
|
|
fspec->maximum = COGL_FIXED_MAX;
|
2008-04-17 06:08:17 -04:00
|
|
|
fspec->default_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
param_fixed_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
value->data[0].v_int = CLUTTER_PARAM_SPEC_FIXED (pspec)->default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
param_fixed_validate (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
ClutterParamSpecFixed *fspec = CLUTTER_PARAM_SPEC_FIXED (pspec);
|
2008-10-30 12:37:55 -04:00
|
|
|
gint oval = COGL_FIXED_TO_INT (value->data[0].v_int);
|
2008-05-09 06:58:26 -04:00
|
|
|
gint min, max, val;
|
|
|
|
|
|
|
|
g_assert (CLUTTER_IS_PARAM_SPEC_FIXED (pspec));
|
2008-04-17 06:08:17 -04:00
|
|
|
|
2008-05-09 06:58:26 -04:00
|
|
|
/* we compare the integer part of the value because the minimum
|
|
|
|
* and maximum values cover just that part of the representation
|
|
|
|
*/
|
|
|
|
|
|
|
|
min = fspec->minimum;
|
|
|
|
max = fspec->maximum;
|
2008-10-30 12:37:55 -04:00
|
|
|
val = COGL_FIXED_TO_INT (value->data[0].v_int);
|
2008-05-09 06:58:26 -04:00
|
|
|
|
|
|
|
val = CLAMP (val, min, max);
|
|
|
|
if (val != oval)
|
|
|
|
{
|
|
|
|
value->data[0].v_int = val;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-04-17 06:08:17 -04:00
|
|
|
|
2008-05-09 06:58:26 -04:00
|
|
|
return FALSE;
|
2008-04-17 06:08:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
param_fixed_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2)
|
|
|
|
{
|
|
|
|
if (value1->data[0].v_int < value2->data[0].v_int)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return value1->data[0].v_int > value2->data[0].v_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
clutter_param_fixed_get_type (void)
|
|
|
|
{
|
|
|
|
static GType pspec_type = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (pspec_type == 0))
|
|
|
|
{
|
|
|
|
const GParamSpecTypeInfo pspec_info = {
|
|
|
|
sizeof (ClutterParamSpecFixed),
|
|
|
|
16,
|
|
|
|
param_fixed_init,
|
|
|
|
CLUTTER_TYPE_FIXED,
|
|
|
|
NULL,
|
|
|
|
param_fixed_set_default,
|
|
|
|
param_fixed_validate,
|
|
|
|
param_fixed_values_cmp,
|
|
|
|
};
|
|
|
|
|
|
|
|
pspec_type = g_param_type_register_static (I_("ClutterParamSpecFixed"),
|
|
|
|
&pspec_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pspec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_param_spec_fixed:
|
|
|
|
* @name: name of the property
|
|
|
|
* @nick: short name
|
|
|
|
* @blurb: description (can be translatable)
|
|
|
|
* @minimum: lower boundary
|
|
|
|
* @maximum: higher boundary
|
|
|
|
* @default_value: default value
|
|
|
|
* @flags: flags for the param spec
|
|
|
|
*
|
|
|
|
* Creates a #GParamSpec for properties using #ClutterFixed values
|
|
|
|
*
|
|
|
|
* Return value: the newly created #GParamSpec
|
|
|
|
*
|
|
|
|
* Since: 0.8
|
|
|
|
*/
|
|
|
|
GParamSpec *
|
2008-12-15 09:29:59 -05:00
|
|
|
clutter_param_spec_fixed (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
ClutterFixed minimum,
|
|
|
|
ClutterFixed maximum,
|
|
|
|
ClutterFixed default_value,
|
2008-04-17 06:08:17 -04:00
|
|
|
GParamFlags flags)
|
|
|
|
{
|
|
|
|
ClutterParamSpecFixed *fspec;
|
|
|
|
|
|
|
|
g_return_val_if_fail (default_value >= minimum && default_value <= maximum,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
fspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_FIXED,
|
|
|
|
name, nick, blurb,
|
|
|
|
flags);
|
|
|
|
fspec->minimum = minimum;
|
|
|
|
fspec->maximum = maximum;
|
|
|
|
fspec->default_value = default_value;
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (fspec);
|
|
|
|
}
|