On Fri, 2009-05-22 at 16:32 +0200, Jordi Hernández Puigdellívol wrote:

> But.. what about if the texture has transparent parts, the black
> rectangle will darken the images behind them...

You could make a subclass of ClutterTexture and override the paint()
method. Then you can mess with the color in the texture's material. This
would avoid having to submit two rectangles and it will honour the
opacity of the texture. See the attached example.

Hope that helps.

Regards,
- Neil
#include <clutter/clutter.h>

#define TEST_TYPE_BLACK_TEXTURE                                         \
  (test_black_texture_get_type())
#define TEST_BLACK_TEXTURE(obj)                                         \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj),                                   \
                               TEST_TYPE_BLACK_TEXTURE,                 \
                               TestBlackTexture))
#define TEST_BLACK_TEXTURE_CLASS(klass)                                 \
  (G_TYPE_CHECK_CLASS_CAST ((klass),                                    \
                            TEST_TYPE_BLACK_TEXTURE,                    \
                            TestBlackTextureClass))
#define TEST_IS_BLACK_TEXTURE(obj)                                      \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj),                                   \
                               TEST_TYPE_BLACK_TEXTURE))
#define TEST_IS_BLACK_TEXTURE_CLASS(klass)                              \
  (G_TYPE_CHECK_CLASS_TYPE ((klass),                                    \
                            TEST_TYPE_BLACK_TEXTURE))
#define TEST_BLACK_TEXTURE_GET_CLASS(obj)                               \
  (G_TYPE_INSTANCE_GET_CLASS ((obj),                                    \
                              TEST_TYPE_BLACK_TEXTURE,                  \
                              TestBlackTextureClass))

typedef struct _TestBlackTexture      TestBlackTexture;
typedef struct _TestBlackTextureClass TestBlackTextureClass;

struct _TestBlackTextureClass
{
  ClutterTextureClass parent_class;
};

struct _TestBlackTexture
{
  ClutterTexture parent;
};

G_DEFINE_TYPE (TestBlackTexture, test_black_texture, CLUTTER_TYPE_TEXTURE);

static void
test_black_texture_paint (ClutterActor *actor)
{
  CoglHandle material;
  gint x_1, y_1, x_2, y_2;

  clutter_actor_get_allocation_coords (actor, &x_1, &y_1, &x_2, &y_2);

  g_object_get (actor, "cogl-material", &material, NULL);

  cogl_material_set_color4ub (material, 0x80, 0x80, 0x80,
                              clutter_actor_get_paint_opacity (actor));
  cogl_set_source (material);
  cogl_rectangle (0, 0, (x_2 - x_1), (y_2 - y_1));
}

static void
test_black_texture_class_init (TestBlackTextureClass *klass)
{
  ClutterActorClass *actor_class = (ClutterActorClass *) klass;

  actor_class->paint = test_black_texture_paint;
}

static void
test_black_texture_init (TestBlackTexture *self)
{
}

ClutterActor *
test_black_texture_new (void)
{
  ClutterActor *self = g_object_new (TEST_TYPE_BLACK_TEXTURE, NULL);

  return self;
}

int
main (int argc, char **argv)
{
  ClutterActor *stage, *tex;
  const gchar *filename;

  clutter_init (&argc, &argv);

  if (argc < 2)
    {
      g_print ("usage: %s <filename>\n", argv[0]);
      return 1;
    }

  filename = argv[1];

  stage = clutter_stage_get_default ();

  tex = test_black_texture_new ();
  clutter_texture_set_from_file (CLUTTER_TEXTURE (tex), filename, NULL);

  clutter_container_add (CLUTTER_CONTAINER (stage), tex, NULL);

  clutter_actor_show (stage);

  clutter_main ();

  return 0;
}

Reply via email to