[gnome-games/gnibbles-clutter] Big commit, began to remove old code, improvement on worm...



commit f329e02ce4594276f8988bd133992d8d3d3dfe1d
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Wed Jul 1 16:39:40 2009 -0400

    Big commit, began to remove old code, improvement on worm...
    
    - Added the code to control a worm with the keyboard
    - Removed/commented code from previous version
    - Refactored a lot of code from main.c and gnibbles.c to make it closer to a
    real game, now the game load the logo on start and the game start when asked to
    - Some more tweak on worm

 gnibbles/board.c        |    1 -
 gnibbles/boni.c         |   64 ++++++++------
 gnibbles/bonus.c        |   13 +++-
 gnibbles/bonus.h        |    2 +
 gnibbles/gnibbles.c     |   23 +++--
 gnibbles/gnibbles.h     |    5 +-
 gnibbles/main.c         |  229 ++++++++++++++++++++++++++---------------------
 gnibbles/main.h         |    1 +
 gnibbles/warp.c         |    8 ++-
 gnibbles/warp.h         |    1 +
 gnibbles/warpmanager.c  |   10 ++-
 gnibbles/worm-clutter.c |  167 ++++++++++++++++++++++++++++++++++
 gnibbles/worm-clutter.h |    5 +-
 13 files changed, 377 insertions(+), 152 deletions(-)
---
diff --git a/gnibbles/board.c b/gnibbles/board.c
index 8447f76..fa02573 100644
--- a/gnibbles/board.c
+++ b/gnibbles/board.c
@@ -169,7 +169,6 @@ gnibbles_board_load_level (GnibblesBoard *board, GnibblesLevel *level)
 
   ClutterActor *stage = gnibbles_board_get_stage (board);
   clutter_container_add_actor (CLUTTER_CONTAINER (stage), board->level);
-  //raise the level above the surface
   clutter_actor_raise (board->level,board->surface);
 }
 
diff --git a/gnibbles/boni.c b/gnibbles/boni.c
index 380361e..6d46ca9 100644
--- a/gnibbles/boni.c
+++ b/gnibbles/boni.c
@@ -32,7 +32,7 @@
 #include "ggz-network.h"
 #include "level.h"
 
-extern gchar board[BOARDWIDTH][BOARDHEIGHT];
+//extern gchar board[BOARDWIDTH][BOARDHEIGHT];
 extern GnibblesLevel *level;
 
 GnibblesBoni *
@@ -71,18 +71,21 @@ gnibbles_boni_add_bonus (GnibblesBoni * boni, gint t_x, gint t_y,
   boni->bonuses[boni->numbonuses] = gnibbles_bonus_new (t_x, t_y,
 							                                          t_type, t_fake,
 							                                          t_countdown);
+/*
   board[t_x][t_y] = t_type + 'A';
   board[t_x + 1][t_y] = t_type + 'A';
   board[t_x][t_y + 1] = t_type + 'A';
   board[t_x + 1][t_y + 1] = t_type + 'A';
-
+*/
   //gnibbles-clutter-level
   level->walls[t_x][t_y] = t_type = 'A';
   level->walls[t_x + 1][t_y] = t_type + 'A';
   level->walls[t_x][t_y + 1] = t_type + 'A';
   level->walls[t_x + 1][t_y + 1] = t_type + 'A';
 
-  gnibbles_bonus_draw (boni->bonuses[boni->numbonuses]);
+  //gnibbles_bonus_draw (boni->bonuses[boni->numbonuses]);
+  gnibbles_bonus_draw_clutter (boni->bonuses[boni->numbonuses]);
+  
   boni->numbonuses++;
   if (t_type != BONUSREGULAR)
     games_sound_play ("appear");
@@ -91,7 +94,6 @@ gnibbles_boni_add_bonus (GnibblesBoni * boni, gint t_x, gint t_y,
     network_add_bonus (t_x, t_y, t_type, t_fake, t_countdown);
   }
 #endif
-
 }
 
 void
@@ -103,18 +105,20 @@ gnibbles_boni_add_bonus_final (GnibblesBoni * boni, gint t_x, gint t_y,
   boni->bonuses[boni->numbonuses] = gnibbles_bonus_new (t_x, t_y,
 							t_type, t_fake,
 							t_countdown);
+/*
   board[t_x][t_y] = t_type + 'A';
   board[t_x + 1][t_y] = t_type + 'A';
   board[t_x][t_y + 1] = t_type + 'A';
   board[t_x + 1][t_y + 1] = t_type + 'A';
-
+*/
   //gnibbles-clutter-level
   level->walls[t_x][t_y] = t_type + 'A';
   level->walls[t_x + 1][t_y] = t_type + 'A';
   level->walls[t_x][t_y + 1] = t_type + 'A';
   level->walls[t_x + 1][t_y + 1] = t_type + 'A';
 
-  gnibbles_bonus_draw (boni->bonuses[boni->numbonuses]);
+  //gnibbles_bonus_draw (boni->bonuses[boni->numbonuses]);
+  gnibbles_bonus_draw_clutter (boni->bonuses[boni->numbonuses]);
   boni->numbonuses++;
   if (t_type != BONUSREGULAR)
     games_sound_play ("appear");
@@ -127,12 +131,12 @@ gnibbles_boni_fake (GnibblesBoni * boni, gint x, gint y)
 
   for (i = 0; i < boni->numbonuses; i++) {
     if ((x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x + 1 &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y + 1) ||
-	(x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
+	      y == boni->bonuses[i]->y) ||
+    	  (x == boni->bonuses[i]->x + 1 &&
+	      y == boni->bonuses[i]->y) ||
+	      (x == boni->bonuses[i]->x &&
+	      y == boni->bonuses[i]->y + 1) ||
+	      (x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
       return (boni->bonuses[i]->fake);
     }
   }
@@ -152,24 +156,26 @@ gnibbles_boni_remove_bonus (GnibblesBoni * boni, gint x, gint y)
 #endif
   for (i = 0; i < boni->numbonuses; i++) {
     if ((x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x + 1 &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y + 1) ||
-	(x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
+	      y == boni->bonuses[i]->y) ||
+	      (x == boni->bonuses[i]->x + 1 &&
+	      y == boni->bonuses[i]->y) ||
+	      (x == boni->bonuses[i]->x &&
+	      y == boni->bonuses[i]->y + 1) ||
+	      (x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
+/*      
       board[boni->bonuses[i]->x][boni->bonuses[i]->y] = EMPTYCHAR;
       board[boni->bonuses[i]->x + 1][boni->bonuses[i]->y] = EMPTYCHAR;
       board[boni->bonuses[i]->x][boni->bonuses[i]->y + 1] = EMPTYCHAR;
       board[boni->bonuses[i]->x + 1][boni->bonuses[i]->y + 1]	= EMPTYCHAR;
-
+*/
       //gnibbles-clutter-level
       level->walls[boni->bonuses[i]->x][boni->bonuses[i]->y] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x + 1][boni->bonuses[i]->y] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x][boni->bonuses[i]->y + 1] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x + 1][boni->bonuses[i]->y + 1]	= EMPTYCHAR;
 
-      gnibbles_bonus_erase (boni->bonuses[i]);
+      //gnibbles_bonus_erase (boni->bonuses[i]);
+      gnibbles_bonus_erase_clutter (boni->bonuses[i]);
       boni->bonuses[i] = boni->bonuses[--boni->numbonuses];
       return;
     }
@@ -183,24 +189,26 @@ gnibbles_boni_remove_bonus_final (GnibblesBoni * boni, gint x, gint y)
 
   for (i = 0; i < boni->numbonuses; i++) {
     if ((x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x + 1 &&
-	 y == boni->bonuses[i]->y) ||
-	(x == boni->bonuses[i]->x &&
-	 y == boni->bonuses[i]->y + 1) ||
-	(x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
+	      y == boni->bonuses[i]->y) ||
+	      (x == boni->bonuses[i]->x + 1 &&
+	      y == boni->bonuses[i]->y) ||
+	      (x == boni->bonuses[i]->x &&
+	      y == boni->bonuses[i]->y + 1) ||
+	      (x == boni->bonuses[i]->x + 1 && y == boni->bonuses[i]->y + 1)) {
+/*      
       board[boni->bonuses[i]->x][boni->bonuses[i]->y] = EMPTYCHAR;
       board[boni->bonuses[i]->x + 1][boni->bonuses[i]->y] = EMPTYCHAR;
       board[boni->bonuses[i]->x][boni->bonuses[i]->y + 1] = EMPTYCHAR;
       board[boni->bonuses[i]->x + 1][boni->bonuses[i]->y + 1] = EMPTYCHAR;
-
+*/
       //gnibbles-clutter-level
       level->walls[boni->bonuses[i]->x][boni->bonuses[i]->y] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x + 1][boni->bonuses[i]->y] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x][boni->bonuses[i]->y + 1] = EMPTYCHAR;
       level->walls[boni->bonuses[i]->x + 1][boni->bonuses[i]->y + 1] = EMPTYCHAR;
 
-      gnibbles_bonus_erase (boni->bonuses[i]);
+      //gnibbles_bonus_erase (boni->bonuses[i]);
+      gnibbles_bonus_erase_clutter (boni->bonuses[i]);
       boni->bonuses[i] = boni->bonuses[--boni->numbonuses];
       return;
     }
diff --git a/gnibbles/bonus.c b/gnibbles/bonus.c
index fa4a651..7de904a 100644
--- a/gnibbles/bonus.c
+++ b/gnibbles/bonus.c
@@ -55,6 +55,12 @@ gnibbles_bonus_new (gint t_x, gint t_y, gint t_type,
 void
 gnibbles_bonus_draw (GnibblesBonus * bonus)
 {
+  gnibbles_draw_big_pixmap (bonus->type, bonus->x, bonus->y);
+}
+
+void
+gnibbles_bonus_draw_clutter (GnibblesBonus *bonus)
+{
   gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (bonus->actor),
                                        boni_pixmaps[bonus->type]);
   clutter_actor_set_position (CLUTTER_ACTOR (bonus->actor),
@@ -62,7 +68,6 @@ gnibbles_bonus_draw (GnibblesBonus * bonus)
                               bonus->y * properties->tilesize);
   ClutterActor *stage = gnibbles_board_get_stage (clutter_board);
   clutter_container_add_actor (CLUTTER_CONTAINER (stage), bonus->actor);
-  gnibbles_draw_big_pixmap (bonus->type, bonus->x, bonus->y);
 }
 
 void
@@ -74,3 +79,9 @@ gnibbles_bonus_erase (GnibblesBonus * bonus)
 
   free (bonus);
 }
+
+void
+gnibbles_bonus_erase_clutter (GnibblesBonus *bonus)
+{
+  clutter_actor_hide (bonus->actor);
+}
diff --git a/gnibbles/bonus.h b/gnibbles/bonus.h
index ab79d86..a127c1c 100644
--- a/gnibbles/bonus.h
+++ b/gnibbles/bonus.h
@@ -46,7 +46,9 @@ GnibblesBonus *gnibbles_bonus_new (gint t_x, gint t_y, gint t_type,
 				   gint t_fake, gint t_countdown);
 
 void gnibbles_bonus_draw (GnibblesBonus * bonus);
+void gnibbles_bonus_draw_clutter (GnibblesBonus *bonus);
 
 void gnibbles_bonus_erase (GnibblesBonus * bonus);
+void gnibbles_bonus_erase_clutter (GnibblesBonus *bonus);
 
 #endif
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index 6fa9d12..02d76eb 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -285,17 +285,19 @@ gnibbles_draw_big_pixmap_buffer (gint which, gint x, gint y)
 void
 gnibbles_load_logo (GtkWidget * window)
 {
-  gint width = drawing_area->allocation.width;
-  gint height = drawing_area->allocation.height;
+  gfloat width; 
+  gfloat height;
 
-  if (GTK_WIDGET_REALIZED (drawing_area) == FALSE)
+  ClutterActor *stage = gnibbles_board_get_stage (clutter_board);
+  clutter_actor_get_size (CLUTTER_ACTOR (stage), &width, &height);
+  if (GTK_WIDGET_REALIZED (clutter_board->clutter_widget) == FALSE)
     return;
 
   if (logo_pixmap)
     g_object_unref (logo_pixmap);
-  logo_pixmap =
-    gnibbles_load_pixmap_file (window, "gnibbles-logo.svg",
-			       width, height);
+    
+  logo_pixmap = gnibbles_clutter_load_pixmap_file ("gnibbles-logo.svg",
+                               			       (gint)width, (gint)height);
 }
 
 void
@@ -312,6 +314,7 @@ gnibbles_load_pixmap (GtkWidget * window)
     "bonus5.svg",
     "questionmark.svg"
   };
+
   gchar *small_files[] = {
     "wall-empty.svg",
     "wall-straight-up.svg",
@@ -555,13 +558,13 @@ gnibbles_clutter_add_bonus (gint regular)
       x = rand () % (BOARDWIDTH - 1);
       y = rand () % (BOARDHEIGHT - 1);
       if (level->walls[x][y] != EMPTYCHAR)
-	good = 0;
+	      good = 0;
       if (level->walls[x + 1][y] != EMPTYCHAR)
-	good = 0;
+	      good = 0;
       if (level->walls[x][y + 1] != EMPTYCHAR)
-	good = 0;
+	      good = 0;
       if (level->walls[x + 1][y + 1] != EMPTYCHAR)
-	good = 0;
+	      good = 0;
     }
     gnibbles_boni_add_bonus (boni, x, y, BONUSREGULAR, 0, 300);
   } else if (boni->missed <= MAXMISSED) {
diff --git a/gnibbles/gnibbles.h b/gnibbles/gnibbles.h
index a2e6acc..75dd1b0 100644
--- a/gnibbles/gnibbles.h
+++ b/gnibbles/gnibbles.h
@@ -56,16 +56,15 @@
 #define MAXLEVEL 26
 
 extern GnibblesWarpManager *warpmanager;
-
+void gnibbles_clutter_init ();
 void gnibbles_draw_pixmap (gint which, gint x, gint y);
 void gnibbles_draw_big_pixmap (gint which, gint x, gint y);
 void gnibbles_draw_pixmap_buffer (gint which, gint x, gint y);
 void gnibbles_draw_big_pixmap_buffer (gint which, gint x, gint y);
 void gnibbles_clutter_load_pixmap (gint tilesize);
 void gnibbles_load_pixmap (GtkWidget * window);
-void gnibbles_load_logo (GtkWidget * window);
+void gnibbles_load_logo ();
 void gnibbles_load_level (GtkWidget * window, gint level);
-void gnibbles_clutter_init ();
 void gnibbles_init (void);
 void gnibbles_clutter_add_bonus (gint regular);
 void gnibbles_add_bonus (gint regular);
diff --git a/gnibbles/main.c b/gnibbles/main.c
index e419ccf..b754f49 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -115,7 +115,7 @@ NULL};
 
 static gint add_bonus_cb (gpointer data);
 static void render_logo (void);
-static void render_logo_clutter (GnibblesBoard *board);
+static void render_logo_clutter ();
 static gint end_game_cb (GtkAction * action, gpointer data);
 
 static GtkAction *new_game_action;
@@ -329,6 +329,51 @@ draw_board (void)
 }
 
 static gboolean
+configure_clutter_event_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
+{
+  int tilesize, ts_x, ts_y;
+
+  /* Compute the new tile size based on the size of the
+   * drawing area, rounded down. */
+  ts_x = event->width / BOARDWIDTH;
+  ts_y = event->height / BOARDHEIGHT;
+  if (ts_x * BOARDWIDTH > event->width)
+    ts_x--;
+  if (ts_y * BOARDHEIGHT > event->height)
+    ts_y--;
+  tilesize = MIN (ts_x, ts_y);
+
+  int i;
+
+  if (game_running ()) {
+    if (data) {
+      GnibblesBoard *clutter_board = (GnibblesBoard *)data;
+      gnibbles_board_resize (clutter_board, tilesize);
+      for (i=0; i<properties->numworms; i++)
+        gnibbles_cworm_resize (cworms[i], tilesize);
+    }
+  } else {
+    render_logo_clutter ();
+  }
+
+  /* But, has the tile size changed? */
+  if (properties->tilesize == tilesize) {
+    /* We must always re-load the logo. */
+    gnibbles_load_logo (window);
+    return FALSE;
+  }
+
+  gnibbles_clutter_load_pixmap (tilesize);
+
+  properties->tilesize = tilesize;
+  gnibbles_properties_set_tile_size (tilesize);
+
+  gnibbles_load_logo (window);
+  
+  return FALSE;
+}
+
+static gboolean
 configure_event_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
 {
   int tilesize, ts_x, ts_y;
@@ -384,8 +429,6 @@ configure_event_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data
     draw_board ();
   else {
     render_logo ();
-    /*if (data && !((GnibblesBoard*)data)->level)
-      render_logo_clutter ((GnibblesBoard*)data);*/
   }
   
   return FALSE;
@@ -470,6 +513,60 @@ new_game_2_cb (GtkWidget * widget, gpointer data)
   return (FALSE);
 }
 
+static void
+move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
+{
+  const int elapsed_time = clutter_timeline_get_elapsed_time (timeline);
+  const int duration = clutter_timeline_get_duration (timeline);
+
+  if (!(elapsed_time == duration))
+    return;
+
+  gint i, olddir, length, nbr_actor;
+
+  for (i = 0; i < properties->numworms; i++) {
+    // get the current direction of the worm
+    olddir = cworms[i]->direction;
+    // determine the new direction the worm will take
+    gnibbles_cworm_ai_move (cworms[i]);
+    // Add an actor when we change direction
+    if (olddir != cworms[i]->direction)
+      gnibbles_cworm_add_actor (cworms[i]);
+
+    nbr_actor = g_list_length (cworms[i]->list);
+    length = gnibbles_cworm_get_length (cworms[i]);
+    printf ("\nWorm ID: %d, Actors: %d, Length: %d,  xhead: %d, yhead:%d",
+            i, nbr_actor, length, cworms[i]->xhead, cworms[i]->yhead);
+
+    if (cworms[i]->xhead >= BOARDWIDTH) {
+      cworms[i]->xhead = 0;
+      gnibbles_cworm_add_actor(cworms[i]);
+    } else if (cworms[i]->xhead < 0) {
+      cworms[i]->xhead = BOARDWIDTH;
+      gnibbles_cworm_add_actor (cworms[i]);
+    } else if (cworms[i]->yhead >= BOARDHEIGHT) {
+      cworms[i]->yhead = 0;
+      gnibbles_cworm_add_actor (cworms[i]);
+    } else if (cworms[i]->xhead < 0) {
+      cworms[i]->yhead = BOARDHEIGHT;
+      gnibbles_cworm_add_actor (cworms[i]);
+    }
+    //if there's only one actor in the list, just move the actor
+    if (nbr_actor == 1) {
+      gnibbles_cworm_move_straight_worm (cworms[i]);
+    } else if (nbr_actor >= 2) {
+      gnibbles_cworm_move_tail (cworms[i]);
+      if (g_list_length (cworms[i]->list) == 1)
+        gnibbles_cworm_move_straight_worm (cworms[i]);
+      else 
+        gnibbles_cworm_move_head (cworms[i]);
+    } else if (nbr_actor < 1) {
+      //worm's dead
+      return;
+    }
+  }
+}
+
 gint
 new_game_clutter (void)
 {
@@ -496,7 +593,6 @@ new_game_clutter (void)
     current_level = rand () % MAXLEVEL + 1;
   }
 
-  //zero_board ();
   level = gnibbles_level_new (current_level);
   gnibbles_board_load_level (clutter_board, level);
   gnibbles_clutter_add_bonus (1);
@@ -526,6 +622,14 @@ new_game_clutter (void)
 
   dummy_id = g_timeout_add_seconds (1, (GSourceFunc) new_game_clutter_2_cb, NULL);
 
+  
+  ClutterTimeline *timeline = clutter_timeline_new (115);
+  clutter_timeline_set_loop (timeline, TRUE);
+ 
+  g_signal_connect (timeline, "new-frame", G_CALLBACK (move_worm_cb), NULL);
+
+  clutter_timeline_start (timeline);
+
   network_gui_update ();
 
   return TRUE;
@@ -595,7 +699,7 @@ new_game (void)
 static void
 new_game_cb (GtkAction * action, gpointer data)
 {
-  new_game ();
+  new_game_clutter ();
 }
 
 gint
@@ -716,6 +820,7 @@ restart_game_clutter (gpointer data)
   
   return FALSE;
 }
+
 static gint
 restart_game (gpointer data)
 {
@@ -791,8 +896,6 @@ main_loop (gpointer data)
     gnibbles_log_score (window);
 
     return FALSE;
-
-
   }
 
   if (status == GAMEOVER) {
@@ -863,7 +966,7 @@ main_loop (gpointer data)
     else if (properties->random && !ggz_network_mode) {
       tmp = rand () % MAXLEVEL + 1;
       while (tmp == current_level)
-	tmp = rand () % MAXLEVEL + 1;
+	      tmp = rand () % MAXLEVEL + 1;
       current_level = tmp;
     }
     restart_id = g_timeout_add_seconds (1, (GSourceFunc) restart_game, NULL);
@@ -980,7 +1083,6 @@ create_menus (GtkUIManager * ui_manager)
   player_list_action =
     gtk_action_group_get_action (action_group, "PlayerList");
 
-
 }
 
 static void
@@ -996,10 +1098,12 @@ setup_window_clutter ()
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window), _("Nibbles"));
 
-  gtk_window_set_default_size (GTK_WINDOW (window), DEFAULT_WIDTH, DEFAULT_HEIGHT);
+  gtk_window_set_default_size (GTK_WINDOW (window), 
+                               DEFAULT_WIDTH, DEFAULT_HEIGHT);
   games_conf_add_window (GTK_WINDOW (window), KEY_PREFERENCES_GROUP);
 
-  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
+  g_signal_connect (G_OBJECT (window), "destroy", 
+        G_CALLBACK (gtk_main_quit), NULL);
   g_signal_connect (G_OBJECT (window), "delete_event",
 		    G_CALLBACK (delete_cb), NULL);
   g_signal_connect (G_OBJECT (window), "window_state_event",
@@ -1034,7 +1138,7 @@ setup_window_clutter ()
 #endif
 
   g_signal_connect (G_OBJECT (clutter_board->clutter_widget), "configure_event",
-		    G_CALLBACK (configure_event_cb), clutter_board);
+		    G_CALLBACK (configure_clutter_event_cb), clutter_board);
 
   g_signal_connect (G_OBJECT (window), "focus_out_event",
 		    G_CALLBACK (show_cursor_cb), NULL);
@@ -1162,7 +1266,7 @@ setup_window (void)
 }
 
 static void 
-render_logo_clutter (GnibblesBoard *clutter_board)
+render_logo_clutter ()
 {
   
   gfloat width, height;
@@ -1254,61 +1358,6 @@ render_logo (void)
 
 
 }
-
-static void
-move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
-{
-  const int elapsed_time = clutter_timeline_get_elapsed_time (timeline);
-  const int duration = clutter_timeline_get_duration (timeline);
-
-  if (!(elapsed_time == duration))
-    return;
-
-  gint i, olddir, length, nbr_actor;
-
-  for (i = 0; i < properties->numworms; i++) {
-    // get the current direction of the worm
-    olddir = cworms[i]->direction;
-    // determine the new direction the worm will take
-    gnibbles_cworm_ai_move (cworms[i]);
-    // Add an actor when we change direction
-    if (olddir != cworms[i]->direction)
-      gnibbles_cworm_add_actor (cworms[i]);
-
-    nbr_actor = g_list_length (cworms[i]->list);
-    length = gnibbles_cworm_get_length (cworms[i]);
-    printf ("\nWorm ID: %d, Actors: %d, Length: %d,  xhead: %d, yhead:%d",
-            i, nbr_actor, length, cworms[i]->xhead, cworms[i]->yhead);
-
-    if (cworms[i]->xhead >= BOARDWIDTH) {
-      cworms[i]->xhead = 0;
-      gnibbles_cworm_add_actor(cworms[i]);
-    } else if (cworms[i]->xhead < 0) {
-      cworms[i]->xhead = BOARDWIDTH;
-      gnibbles_cworm_add_actor (cworms[i]);
-    } else if (cworms[i]->yhead >= BOARDHEIGHT) {
-      cworms[i]->yhead = 0;
-      gnibbles_cworm_add_actor (cworms[i]);
-    } else if (cworms[i]->xhead < 0) {
-      cworms[i]->yhead = BOARDHEIGHT;
-      gnibbles_cworm_add_actor (cworms[i]);
-    }
-    //if there's only one actor in the list, just move the actor
-    if (nbr_actor == 1) {
-      gnibbles_cworm_move_straight_worm (cworms[i]);
-    } else if (nbr_actor >= 2) {
-      gnibbles_cworm_move_tail (cworms[i]);
-      if (g_list_length (cworms[i]->list) == 1)
-        gnibbles_cworm_move_straight_worm (cworms[i]);
-      else 
-        gnibbles_cworm_move_head (cworms[i]);
-    } else if (nbr_actor < 1) {
-      //worm's dead
-      return;
-    }
-  }
-}
-
 int
 main (int argc, char **argv)
 {
@@ -1319,6 +1368,8 @@ main (int argc, char **argv)
   if (!games_runtime_init ("gnibbles"))
     return 1;
 
+  gtk_clutter_init (&argc, &argv);
+
   setgid_io_init ();
 
   context = g_option_context_new (NULL);
@@ -1352,22 +1403,20 @@ main (int argc, char **argv)
 
   properties = gnibbles_properties_new ();
 
-  setup_window ();
-
-  gnibbles_load_logo (window);
-  gnibbles_load_pixmap (window);
+  gnibbles_clutter_load_pixmap (properties->tilesize);
+  clutter_board = gnibbles_board_new (BOARDWIDTH, BOARDHEIGHT);
+  setup_window_clutter ();
+  
+  gnibbles_load_logo ();
 
   gtk_widget_show (window);
 
-  buffer_pixmap = gdk_pixmap_new (drawing_area->window,
-				  BOARDWIDTH * properties->tilesize,
-				  BOARDHEIGHT * properties->tilesize, -1);
 #ifdef GGZ_CLIENT
   network_init ();
   network_gui_update ();
 #endif
 
-  render_logo ();
+  render_logo_clutter ();
 
   gtk_action_set_sensitive (pause_action, FALSE);
   gtk_action_set_sensitive (resume_action, FALSE);
@@ -1376,36 +1425,10 @@ main (int argc, char **argv)
   gtk_action_set_visible (new_game_action, !ggz_network_mode);
   gtk_action_set_visible (player_list_action, ggz_network_mode);
 
-  // clutter fun
-  gtk_clutter_init (&argc, &argv);
-
-  gnibbles_clutter_load_pixmap (properties->tilesize);
-  clutter_board = gnibbles_board_new (BOARDWIDTH, BOARDHEIGHT);
-  setup_window_clutter ();
-  
-  ClutterActor *stage = gnibbles_board_get_stage (clutter_board);
-
-  int i;
-
-  level = gnibbles_level_new (5);
-
-  gnibbles_board_load_level (clutter_board, level);
- 
-  for (i = 0; i < properties->numworms; i++) {
-    clutter_container_add_actor (CLUTTER_CONTAINER (stage), cworms[i]->actors);
-    clutter_actor_raise_top (cworms[i]->actors);
-  }
-
-  ClutterTimeline *timeline = clutter_timeline_new (115);
-  clutter_timeline_set_loop (timeline, TRUE);
- 
-  g_signal_connect (timeline, "new-frame", G_CALLBACK (move_worm_cb), NULL);
-
-  clutter_timeline_start (timeline);
-  //render_logo_clutter (clutter_board);
 
   gtk_main ();
 
+ // g_object_unref (timeline);
   gnibbles_properties_destroy (properties);
   games_conf_shutdown ();
 
diff --git a/gnibbles/main.h b/gnibbles/main.h
index ae9ce60..6a31227 100644
--- a/gnibbles/main.h
+++ b/gnibbles/main.h
@@ -38,6 +38,7 @@ char names[NUMWORMS][17];
 gint game_running (void);
 gint pause_game_cb (GtkAction * action, gpointer data);
 void end_game (gboolean);
+
 gint new_game_clutter ();
 gint new_game (void);
 gint main_loop (gpointer data);
diff --git a/gnibbles/warp.c b/gnibbles/warp.c
index 58084c6..53380d3 100644
--- a/gnibbles/warp.c
+++ b/gnibbles/warp.c
@@ -51,6 +51,12 @@ gnibbles_warp_new (gint t_x, gint t_y, gint t_wx, gint t_wy)
 void
 gnibbles_warp_draw_buffer (GnibblesWarp * warp)
 {
+  gnibbles_draw_big_pixmap_buffer (WARP, warp->x, warp->y);
+}
+
+void
+gnibbles_warp_draw (GnibblesWarp *warp)
+{
   gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (warp->actor),
                                       boni_pixmaps[WARP]) ;
   clutter_actor_set_position (CLUTTER_ACTOR (warp->actor),
@@ -58,5 +64,5 @@ gnibbles_warp_draw_buffer (GnibblesWarp * warp)
                               properties->tilesize * warp->y);
   ClutterActor *stage = gnibbles_board_get_stage (clutter_board);
   clutter_container_add_actor (CLUTTER_CONTAINER (stage), warp->actor);
-  gnibbles_draw_big_pixmap_buffer (WARP, warp->x, warp->y);
+
 }
diff --git a/gnibbles/warp.h b/gnibbles/warp.h
index 6ee0806..804bc34 100644
--- a/gnibbles/warp.h
+++ b/gnibbles/warp.h
@@ -36,5 +36,6 @@ typedef struct {
 GnibblesWarp *gnibbles_warp_new (gint t_x, gint t_y, gint t_wx, gint t_wy);
 
 void gnibbles_warp_draw_buffer (GnibblesWarp * warp);
+void gnibbles_warp_draw (GnibblesWarp *warp);
 
 #endif
diff --git a/gnibbles/warpmanager.c b/gnibbles/warpmanager.c
index b4334d2..12f0581 100644
--- a/gnibbles/warpmanager.c
+++ b/gnibbles/warpmanager.c
@@ -97,24 +97,26 @@ gnibbles_warpmanager_add_warp (GnibblesWarpManager * warpmanager, gint t_x,
       draw = warpmanager->numwarps;
       warpmanager->numwarps++;
     }
+/*
     board[t_x][t_y] = WARPLETTER;
     board[t_x + 1][t_y] = WARPLETTER;
     board[t_x][t_y + 1] = WARPLETTER;
     board[t_x + 1][t_y + 1] = WARPLETTER;
-
+*/
     //gnibbles-clutter-level
     level->walls[t_x][t_y] = WARPLETTER;
     level->walls[t_x + 1][t_y] = WARPLETTER;
     level->walls[t_x][t_y + 1] = WARPLETTER;
     level->walls[t_x + 1][t_y + 1] = WARPLETTER;
 
-    gnibbles_warp_draw_buffer (warpmanager->warps[draw]);
+    //gnibbles_warp_draw_buffer (warpmanager->warps[draw]);
+    gnibbles_warp_draw (warpmanager->warps[draw]);
   }
 }
-
+// useless ?
 void
 gnibbles_warpmanager_worm_change_pos (GnibblesWarpManager * warpmanager,
-				      GnibblesWorm * worm)
+				                              GnibblesWorm * worm)
 {
   int i, x, y, good;
 
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 4fd0db1..c8c2baa 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -35,6 +35,9 @@
 #include "bonus.h"
 #include "warpmanager.h"
 #include "properties.h"
+#ifdef GGZ_CLIENT
+#include "ggz-network.h"
+#endif
 
 #include "worm-clutter.h"
 
@@ -45,6 +48,110 @@ extern GnibblesBoni *boni;
 extern GnibblesWarpManager *warpmanager;
 extern GnibblesCWorm *cworms[NUMWORMS];
 
+typedef struct _key_queue_entry {
+  GnibblesCWorm *worm;
+  guint dir;
+} key_queue_entry;
+
+static void cworm_handle_direction (int worm, int dir);
+static void cworm_set_direction (int worm, int dir);
+static void gnibbles_worm_dequeue_keypress (GnibblesCWorm *worm);
+static void gnibbles_worm_queue_keypress (GnibblesCWorm *worm, guint dir);
+static void gnibbles_worm_queue_empty (GnibblesCWorm *worm);
+
+static GQueue *key_queue[NUMWORMS] = { NULL, NULL, NULL, NULL };
+
+static void
+gnibbles_worm_queue_keypress (GnibblesCWorm * worm, guint dir)
+{
+  key_queue_entry *entry;
+  int n = worm->number;
+
+  if (key_queue[n] == NULL)
+    key_queue[n] = g_queue_new ();
+
+  /* Ignore duplicates in normal movement mode. This resolves the
+   * key repeat issue. We ignore this in relative mode because then
+   * you do want two keys that are the same in quick succession. */
+  if ((!properties->wormprops[worm->number]->relmove) &&
+      (!g_queue_is_empty (key_queue[n])) &&
+      (dir == ((key_queue_entry *) g_queue_peek_tail (key_queue[n]))->dir))
+    return;
+
+  entry = g_malloc (sizeof (key_queue_entry));
+  entry->worm = worm;
+  entry->dir = dir;
+  g_queue_push_tail (key_queue[n], (gpointer) entry);
+}
+
+static void
+cworm_handle_direction (int worm, int dir)
+{
+  if (ggz_network_mode) {
+#ifdef GGZ_CLIENT
+    network_game_move (dir);
+
+    cworms[0]->direction = dir;
+    cworms[0]->keypress = 1;
+#endif
+  } else {
+    cworm_set_direction (worm, dir);
+  }
+}
+
+static void
+cworm_set_direction (int worm, int dir)
+{
+
+  if (worm >= properties->human) {
+    return;
+  }
+
+  if (cworms[worm]) {
+
+    if (dir > 4)
+      dir = 1;
+    if (dir < 1)
+      dir = 4;
+
+    if (cworms[worm]->keypress) {
+      gnibbles_worm_queue_keypress (cworms[worm], dir);
+      return;
+    }
+
+    cworms[worm]->direction = dir;
+    cworms[worm]->keypress = 1;
+  }
+}
+
+static void
+gnibbles_worm_queue_empty (GnibblesCWorm * worm)
+{
+  key_queue_entry *entry;
+  int n = worm->number;
+
+  if (!key_queue[n])
+    return;
+
+  while (!g_queue_is_empty (key_queue[n])) {
+    entry = g_queue_pop_head (key_queue[n]);
+    g_free (entry);
+  }
+}
+
+static void
+gnibbles_worm_dequeue_keypress (GnibblesCWorm * worm)
+{
+  key_queue_entry *entry;
+  int n = worm->number;
+
+  entry = (key_queue_entry *) g_queue_pop_head (key_queue[n]);
+
+  cworm_set_direction (entry->worm->number, entry->dir);
+
+  g_free (entry);
+}
+
 static ClutterActor*
 gnibbles_cworm_get_head_actor (GnibblesCWorm *worm)
 {
@@ -57,6 +164,56 @@ gnibbles_cworm_get_tail_actor (GnibblesCWorm *worm)
   return CLUTTER_ACTOR (g_list_last (worm->list)->data);
 }
 
+gint
+gnibbles_cworm_handle_keypress (GnibblesCWorm * worm, guint keyval)
+{
+  GnibblesWormProps *props;
+  guint propsUp, propsLeft, propsDown, propsRight, keyvalUpper;
+/*	if (worm->keypress) {
+                gnibbles_worm_queue_keypress (worm, keyval);
+		return FALSE;
+	} */
+
+  props = properties->wormprops[ggz_network_mode ? 0 : worm->number];
+  propsUp = toupper(props->up);
+  propsLeft = toupper(props->left);
+  propsDown = toupper(props->down);
+  propsRight = toupper(props->right);
+  keyvalUpper = toupper(keyval);
+
+  if (properties->wormprops[worm->number]->relmove) {
+    if (keyvalUpper == propsLeft)
+      cworm_handle_direction (worm->number, worm->direction - 1);
+    else if (keyvalUpper == propsRight)
+      cworm_handle_direction (worm->number, worm->direction + 1);
+    else
+      return FALSE;
+    return TRUE;
+  } else {
+    if ((keyvalUpper == propsUp) && (worm->direction != WORMDOWN)) {
+      cworm_handle_direction (worm->number, WORMUP);
+      /*worm->keypress = 1; */
+      return TRUE;
+    }
+    if ((keyvalUpper == propsRight) && (worm->direction != WORMLEFT)) {
+      cworm_handle_direction (worm->number, WORMRIGHT);
+      /*worm->keypress = 1; */
+      return TRUE;
+    }
+    if ((keyvalUpper == propsDown) && (worm->direction != WORMUP)) {
+      cworm_handle_direction (worm->number, WORMDOWN);
+      /*worm->keypress = 1; */
+      return TRUE;
+    }
+    if ((keyvalUpper == propsLeft) && (worm->direction != WORMRIGHT)) {
+      cworm_handle_direction (worm->number, WORMLEFT);
+      /*worm->keypress = 1; */
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
 static gint
 gnibbles_cworm_get_tail_direction (GnibblesCWorm *worm)
 {
@@ -210,6 +367,16 @@ void
 gnibbles_cworm_inverse (GnibblesCWorm *worm)
 {
   worm->list = g_list_reverse (worm->list);
+  
+  gint tmp;
+
+  tmp = worm->xhead;
+  worm->xhead = worm->xtail;
+  worm->xtail = tmp;
+  tmp = worm->yhead;
+  worm->yhead = worm->ytail;
+  worm->ytail = tmp;
+  tmp = worm->yhead;
 }
 
 void 
diff --git a/gnibbles/worm-clutter.h b/gnibbles/worm-clutter.h
index 2133783..21aa0e6 100644
--- a/gnibbles/worm-clutter.h
+++ b/gnibbles/worm-clutter.h
@@ -53,6 +53,7 @@ typedef struct {
   gint start;
   gint stop;
   gint change;
+  gint keypress;
 } GnibblesCWorm;
 
 typedef struct {
@@ -66,7 +67,9 @@ typedef struct {
 } WormCorner;
 
 GnibblesCWorm * gnibbles_cworm_new (guint number, guint t_xhead,
-			                    guint t_yhead, gint t_direction);       
+			                    guint t_yhead, gint t_direction); 
+gint gnibbles_cworm_handle_keypress (GnibblesCWorm * worm, guint keyval);
+
 void gnibbles_cworm_add_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]