[gnome-games] First tentative to implement worm with clutter



commit b5c4ecad1f3816fa747820fd9188e70931519be8
Author: Guillaume Beland <guillaubel svn gnome org>
Date:   Sun May 24 21:34:26 2009 -0400

    First tentative to implement worm with clutter
    
    I'm using a special clutter texture filling the actor as its size increase,
    there's rough edge I'll have to polish and some problems that need to be
    adressed but this implementation should work nicely

 gnibbles/board.c        |    2 +-
 gnibbles/main.c         |   10 +++--
 gnibbles/worm-clutter.c |   93 ++++++++++++++++++++++++++++++++---------------
 gnibbles/worm-clutter.h |    2 +-
 4 files changed, 72 insertions(+), 35 deletions(-)
---
diff --git a/gnibbles/board.c b/gnibbles/board.c
index f851760..0372f93 100644
--- a/gnibbles/board.c
+++ b/gnibbles/board.c
@@ -152,7 +152,7 @@ gnibbles_board_new (gint t_w, gint t_h)
                              "    \"y\" : 0,"
                              "    \"width\" : %d,"
                              "    \"height\" : %d,"
-                             "    \"keep-aspect-ratio\" : true"
+                             "    \"keep-aspect-ratio\" : true,"
                              "    \"visible\" : true,"
                              "    \"repeat-x\" : true,"
                              "    \"repeat-y\" : true"
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 6489221..e02eb07 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -225,8 +225,8 @@ load_pixmap ()
       g_object_unref (worm_pixmaps[i]);
 
     worm_pixmaps[i] = load_pixmap_file (worm_files[i],
-                                        4 * properties->tilesize,
-                                        4 * properties->tilesize);
+                                        2 * properties->tilesize,
+                                        2 * properties->tilesize);
   }
 }
 
@@ -1333,8 +1333,10 @@ main (int argc, char **argv)
   
   gnibbles_board_load_level (board, gnibbles_level_new (16));
 
-  //GnibblesCWorm *cworm = gnibbles_cworm_new (1,10,10);
-
+  GnibblesCWorm *cworm = gnibbles_cworm_new (1,15,15);
+  
+  ClutterActor *stage = gnibbles_board_get_stage (board);
+  clutter_container_add_actor (CLUTTER_CONTAINER (stage), cworm->actors);
   //render_logo_clutter (board);
 
   gtk_main ();
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 22283f6..17b4ac1 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -20,17 +20,20 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #include <config.h>
+#include <glib/gprintf.h>
+
 #include <glib/gi18n.h>
 #include <gdk/gdk.h>
 #include <stdlib.h>
 #include <libgames-support/games-runtime.h>
-
+#include <clutter-gtk/clutter-gtk.h>
 #include "main.h"
 #include "gnibbles.h"
 #include "properties.h"
 #include "worm-clutter.h"
 
 extern GnibblesProperties *properties;
+extern GdkPixbuf *worm_pixmaps[];
 
 GnibblesCWorm*
 gnibbles_cworm_new (guint number, gint x_s, gint y_s)
@@ -46,44 +49,76 @@ gnibbles_cworm_new (guint number, gint x_s, gint y_s)
   worm->xstart = x_s;
   worm->ystart = y_s;
 
-  ClutterActor *actor = clutter_rectangle_new ();
-
-  if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT) {
-    clutter_actor_set_size (CLUTTER_ACTOR (actor), 
-                            SLENGTH * properties->tilesize, 
-                            properties->tilesize);
-   
-  } else if (worm->direction == WORMDOWN || worm->direction == WORMUP) {
-    clutter_actor_set_size (CLUTTER_ACTOR (actor),  
-                            properties->tilesize,
-                            SLENGTH * properties->tilesize);
-  }
+  gnibbles_cworm_add_straight_actor (worm, SLENGTH);
 
-  clutter_actor_set_position (CLUTTER_ACTOR (actor), worm->xstart, worm->ystart);
-  clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), actor);  
-  
-  worm->list = g_list_append (worm->list, actor);
   return worm;
 }
 
 void
-gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm)
+gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm, gint size)
 {
-  ClutterActor *straight = clutter_rectangle_new ();
-  
-  if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT)
-    clutter_actor_set_size (straight, properties->tilesize ,0);
-  else if (worm->direction == WORMDOWN || worm->direction == WORMUP)
-    clutter_actor_set_size (straight, 0, properties->tilesize);
+  ClutterScript *script = NULL;
+  ClutterActor *actor = NULL;
+
+  gchar worm_script[300];  
+
+  if (worm->direction == WORMRIGHT || worm->direction == WORMLEFT) {
+    g_sprintf (worm_script,  "["
+                             "  {"
+                             "    \"id\" : \"worm\","
+                             "    \"type\" : \"ClutterTexture\","
+                             "    \"x\" : %d,"
+                             "    \"y\" : %d,"
+                             "    \"width\" : %d,"
+                             "    \"height\" : %d,"
+                             "    \"keep-aspect-ratio\" : true,"
+                             "    \"visible\" : true,"
+                             "    \"repeat-x\" : true,"
+                             "    \"repeat-y\" : false,"
+                             "  }"
+                             "]",
+                             worm->xstart,
+                             worm->ystart,
+                             size * (2 * properties->tilesize),
+                             2 * properties->tilesize);
+
+  } else if (worm->direction == WORMDOWN || worm->direction == WORMUP) {
+    g_sprintf (worm_script,  "["
+                             "  {"
+                             "    \"id\" : \"worm\","
+                             "    \"type\" : \"ClutterTexture\","
+                             "    \"x\" : %d,"
+                             "    \"y\" : %d,"
+                             "    \"width\" : %d,"
+                             "    \"height\" : %d,"
+                             "    \"keep-aspect-ratio\" : true,"
+                             "    \"visible\" : true,"
+                             "    \"repeat-x\" : false,"
+                             "    \"repeat-y\" : true,"
+                             "  }"
+                             "]",
+                             worm->xstart,
+                             worm->ystart,
+                             2 * properties->tilesize,
+                             size * (2 * properties->tilesize));
+
+  }
+
+  script = clutter_script_new ();
 
+  clutter_script_load_from_data (script, worm_script, -1, NULL);
+  clutter_script_get_objects (script, "worm", &actor, NULL);
+
+  gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (actor), worm_pixmaps[0]);
+
+  clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), actor);  
+  
   if (!worm->inverse)
-    worm->list = g_list_append (worm->list, straight);
+    worm->list = g_list_append (worm->list, actor);
   else
-    worm->list = g_list_prepend (worm->list, straight);
+    worm->list = g_list_prepend (worm->list, actor);
   
-  clutter_container_add_actor (CLUTTER_CONTAINER (worm->actors), straight);
-
-  //TODO: start increasing the size of the actor
+  //TODO: connect/timeline: start increasing the size of the actor
 }
 
 void
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index 3d63121..293bcf0 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -62,7 +62,7 @@ typedef struct {
 } WormCorner;
 
 GnibblesCWorm * gnibbles_cworm_new (guint number, gint x_s, gint y_s);
-void gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm);
+void gnibbles_cworm_add_straight_actor (GnibblesCWorm *worm, gint size);
 void gnibbles_cworm_add_corner_actor (GnibblesCWorm *worm);
 void gnibbles_cworm_remove_actor (GnibblesCWorm *worm);
 void gnibbles_cworm_destroy (GnibblesCWorm * worm);



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