gnome-games r8130 - trunk/aisleriot



Author: jclinton
Date: Tue Oct 21 19:43:15 2008
New Revision: 8130
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8130&view=rev

Log:
Draw the baize background using a ClutterTexture subclass.

A special subclass of ClutterTexture has been added which draws its
texture repeated to the full size of the stage it is in. This saves
the need to listen for resize events on the stage.

The expose handler for the board has been disabled so that it doesn't
paint over Clutter's drawing.

Added:
   trunk/aisleriot/baize.c
   trunk/aisleriot/baize.h
Modified:
   trunk/aisleriot/Makefile.am
   trunk/aisleriot/board.c

Modified: trunk/aisleriot/Makefile.am
==============================================================================
--- trunk/aisleriot/Makefile.am	(original)
+++ trunk/aisleriot/Makefile.am	Tue Oct 21 19:43:15 2008
@@ -17,6 +17,8 @@
 bin_PROGRAMS = sol
 
 sol_SOURCES = \
+	baize.c		\
+	baize.h		\
 	board.c		\
 	board.h		\
 	conf.c		\

Added: trunk/aisleriot/baize.c
==============================================================================
--- (empty file)
+++ trunk/aisleriot/baize.c	Tue Oct 21 19:43:15 2008
@@ -0,0 +1,86 @@
+/*
+ *  Copyright  2008 Neil Roberts
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <config.h>
+
+#include <clutter/clutter-texture.h>
+#include <cogl/cogl.h>
+
+#include "baize.h"
+
+/* Special version of ClutterTexture that repeats the texture to fill
+   the entire stage. This is used to paint the baize background */
+
+static void aisleriot_baize_paint (ClutterActor *actor);
+
+G_DEFINE_TYPE (AisleriotBaize, aisleriot_baize, CLUTTER_TYPE_TEXTURE);
+
+static void
+aisleriot_baize_class_init (AisleriotBaizeClass *klass)
+{
+  ClutterActorClass *actor_class = (ClutterActorClass *) klass;
+
+  actor_class->paint = aisleriot_baize_paint;
+}
+
+static void
+aisleriot_baize_init (AisleriotBaize *self)
+{
+}
+
+ClutterActor *
+aisleriot_baize_new (void)
+{
+  ClutterActor *self = g_object_new (AISLERIOT_TYPE_BAIZE, NULL);
+
+  return self;
+}
+
+static void
+aisleriot_baize_paint (ClutterActor *actor)
+{
+  ClutterActor *stage;
+  CoglHandle tex;
+  ClutterGeometry stage_geom;
+  guint tex_width, tex_height;
+
+  if ((stage = clutter_actor_get_stage (actor)) == NULL)
+    return;
+
+  if ((tex = clutter_texture_get_cogl_texture (CLUTTER_TEXTURE (actor)))
+      == COGL_INVALID_HANDLE)
+    return;
+
+  tex_width = cogl_texture_get_width (tex);
+  tex_height = cogl_texture_get_height (tex);
+
+  if (tex_width < 1 || tex_height < 1)
+    return;
+
+  clutter_actor_get_allocation_geometry (stage, &stage_geom);
+
+  /* Repeat the texture to fill the size of the stage */
+  cogl_texture_rectangle (tex, 0, 0,
+                          CLUTTER_INT_TO_FIXED (stage_geom.width),
+                          CLUTTER_INT_TO_FIXED (stage_geom.height),
+                          0, 0,
+                          CLUTTER_INT_TO_FIXED (stage_geom.width)
+                          / tex_width,
+                          CLUTTER_INT_TO_FIXED (stage_geom.height)
+                          / tex_height);
+}

Added: trunk/aisleriot/baize.h
==============================================================================
--- (empty file)
+++ trunk/aisleriot/baize.h	Tue Oct 21 19:43:15 2008
@@ -0,0 +1,66 @@
+/*
+ *  Copyright  2008 Neil Roberts
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef AISLERIOT_BAIZE_H
+#define AISLERIOT_BAIZE_H
+
+#include <clutter/clutter-texture.h>
+
+G_BEGIN_DECLS
+
+#define AISLERIOT_TYPE_BAIZE                                            \
+  (aisleriot_baize_get_type())
+#define AISLERIOT_BAIZE(obj)                                            \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj),                                   \
+                               AISLERIOT_TYPE_BAIZE,                    \
+                               AisleriotBaize))
+#define AISLERIOT_BAIZE_CLASS(klass)                                    \
+  (G_TYPE_CHECK_CLASS_CAST ((klass),                                    \
+                            AISLERIOT_TYPE_BAIZE,                       \
+                            AisleriotBaizeClass))
+#define AISLERIOT_IS_BAIZE(obj)                                         \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj),                                   \
+                               AISLERIOT_TYPE_BAIZE))
+#define AISLERIOT_IS_BAIZE_CLASS(klass)                                 \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass),                                    \
+                            AISLERIOT_TYPE_BAIZE))
+#define AISLERIOT_BAIZE_GET_CLASS(obj)                                  \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj),                                    \
+                              AISLERIOT_TYPE_BAIZE,                     \
+                              AisleriotBaizeClass))
+
+typedef struct _AisleriotBaize      AisleriotBaize;
+typedef struct _AisleriotBaizeClass AisleriotBaizeClass;
+
+struct _AisleriotBaizeClass
+{
+  ClutterTextureClass parent_class;
+};
+
+struct _AisleriotBaize
+{
+  ClutterTexture parent;
+};
+
+GType aisleriot_baize_get_type (void) G_GNUC_CONST;
+
+ClutterActor *aisleriot_baize_new (void);
+
+G_END_DECLS
+
+#endif /* AISLERIOT_BAIZE_H */

Modified: trunk/aisleriot/board.c
==============================================================================
--- trunk/aisleriot/board.c	(original)
+++ trunk/aisleriot/board.c	Tue Oct 21 19:43:15 2008
@@ -39,8 +39,8 @@
 #include "conf.h"
 
 #include "game.h"
-
 #include "board.h"
+#include "baize.h"
 
 #define AISLERIOT_BOARD_GET_PRIVATE(board)(G_TYPE_INSTANCE_GET_PRIVATE ((board), AISLERIOT_TYPE_BOARD, AisleriotBoardPrivate))
 
@@ -170,6 +170,9 @@
   /* Highlight */
   Slot *highlight_slot;
 
+  /* Actor used for drawing the baize */
+  ClutterActor *baize_actor;
+
 #ifdef HAVE_MAEMO
   /* Tap-and-Hold */
   Slot *tap_and_hold_slot;
@@ -372,14 +375,12 @@
 /* card drawing functions */
 
 static void
-set_background_from_baize (GtkWidget *widget,
-                           GdkGC *gc)
+set_background_from_baize (AisleriotBoard *board)
 {
+  AisleriotBoardPrivate *priv = board->priv;
   GError *error = NULL;
   GdkPixbuf *pixbuf;
-  GdkPixmap *pixmap;
   char *path;
-  int width, height;
 
   path = games_runtime_get_file (GAMES_RUNTIME_PIXMAP_DIRECTORY, "baize.png");
 
@@ -393,26 +394,20 @@
 
   g_assert (pixbuf != NULL);
 
-  width = gdk_pixbuf_get_width (pixbuf);
-  height = gdk_pixbuf_get_height (pixbuf);
-  pixmap = gdk_pixmap_new (widget->window, width, height, -1);
-  if (!pixmap) {
-    g_object_unref (pixbuf);
-    return;
-  }
+  if (priv->baize_actor == NULL)
+    {
+      ClutterActor *stage
+        = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (board));
 
-  /* FIXMEchpe: if we ever use a baize with an alpha channel,
-   * clear the pixmap first!
-   */
-  gdk_draw_pixbuf (pixmap, NULL, pixbuf,
-                   0, 0, 0, 0, width, height,
-                   GDK_RGB_DITHER_NORMAL, 0, 0);
+      priv->baize_actor = g_object_ref_sink (aisleriot_baize_new ());
+      clutter_container_add (CLUTTER_CONTAINER (stage),
+                             priv->baize_actor, NULL);
+    }
 
-  gdk_gc_set_tile (gc, pixmap);
-  gdk_gc_set_fill (gc, GDK_TILED);
+  gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (priv->baize_actor),
+                                       pixbuf);
 
-  gdk_pixbuf_unref (pixbuf);
-  g_object_unref (pixmap);
+  g_object_unref (pixbuf);
 }
 
 /* Slot helpers */
@@ -2438,7 +2433,7 @@
   priv->draw_gc = gdk_gc_new (widget->window);
 
   priv->bg_gc = gdk_gc_new (widget->window);
-  set_background_from_baize (widget, priv->bg_gc);
+  set_background_from_baize (board);
   
   priv->slot_gc = gdk_gc_new (widget->window);
 
@@ -3398,6 +3393,22 @@
 }
 
 static void
+aisleriot_board_dispose (GObject *object)
+{
+  AisleriotBoard *board = AISLERIOT_BOARD (object);
+  AisleriotBoardPrivate *priv = board->priv;
+
+  if (priv->baize_actor)
+    {
+      clutter_actor_destroy (priv->baize_actor);
+      g_object_unref (priv->baize_actor);
+      priv->baize_actor = NULL;
+    }
+
+  G_OBJECT_CLASS (aisleriot_board_parent_class)->dispose (object);
+}
+
+static void
 aisleriot_board_get_property (GObject *object,
                               guint prop_id,
                               GValue *value,
@@ -3460,6 +3471,7 @@
   g_type_class_add_private (gobject_class, sizeof (AisleriotBoardPrivate));
 
   gobject_class->constructor = aisleriot_board_constructor;
+  gobject_class->dispose = aisleriot_board_dispose;
   gobject_class->finalize = aisleriot_board_finalize;
   gobject_class->set_property = aisleriot_board_set_property;
   gobject_class->get_property = aisleriot_board_get_property;
@@ -3480,7 +3492,6 @@
   widget_class->button_release_event = aisleriot_board_button_release;
   widget_class->motion_notify_event = aisleriot_board_motion_notify;
   widget_class->key_press_event = aisleriot_board_key_press;
-  widget_class->expose_event = aisleriot_board_expose_event;
 #ifdef HAVE_MAEMO
   widget_class->tap_and_hold_query = aisleriot_board_tap_and_hold_query;
   widget_class->tap_and_hold = aisleriot_board_tap_and_hold;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]