[atomix/wip/gtk3-port] Board: converted level drawing to plain gtk



commit 48b9785593cb1a2e3ab4846725c1689f67f91f81
Author: Robert Roth <robert roth off gmail com>
Date:   Mon Jan 12 03:41:40 2015 +0200

    Board: converted level drawing to plain gtk

 configure.ac    |    6 +-
 src/board_gtk.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/board_gtk.h |    2 +
 src/clock.c     |    8 +-
 src/main.c      |    4 +-
 5 files changed, 255 insertions(+), 22 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f449566..4af17a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,17 +18,17 @@ GNOME_COMPILE_WARNINGS([maximum])
 GNOME_MAINTAINER_MODE_DEFINES
 
 dnl ================= Requirements =======================
-LIBGTK_REQUIRED=2.24.0
+LIBGTK_REQUIRED=3.0.0
 LIBXML_REQUIRED=2.4.23
 GLIB_REQUIRED=2.36.0
 GDK_PIXBUF_REQUIRED=2.0.5
-LIBGNOMECANVAS_REQUIRED=2.0.1
+dnl.LIBGNOMECANVAS_REQUIRED=2.0.1
 
 dnl ******************************
 dnl pkg-config checks
 dnl ******************************
 
-ATOMIX_MODULES="gtk+-2.0 >= $LIBGTK_REQUIRED libxml-2.0 >= $LIBXML_REQUIRED gdk-pixbuf-2.0 >= 
$GDK_PIXBUF_REQUIRED libgnomecanvas-2.0 >= $LIBGNOMECANVAS_REQUIRED glib-2.0 >= $GLIB_REQUIRED"
+ATOMIX_MODULES="gtk+-3.0 >= $LIBGTK_REQUIRED libxml-2.0 >= $LIBXML_REQUIRED gdk-pixbuf-2.0 >= 
$GDK_PIXBUF_REQUIRED glib-2.0 >= $GLIB_REQUIRED"
 PKG_CHECK_MODULES(ATOMIX, $ATOMIX_MODULES)
 AC_SUBST(ATOMIX_CFLAGS)
 AC_SUBST(ATOMIX_LIBS)
diff --git a/src/board_gtk.c b/src/board_gtk.c
index e08c4c7..51a3235 100644
--- a/src/board_gtk.c
+++ b/src/board_gtk.c
@@ -19,12 +19,56 @@
 
 #include "board_gtk.h"
 
+#define BGR_FLOOR_ROWS 15
+#define BGR_FLOOR_COLS 15
+
+#define ANIM_TIMEOUT     10    /* time in milliseconds between 
+                               two atom movements */
+
+typedef struct
+{
+  gint timeout_id;
+  gint counter;
+  gint dest_row;
+  gint dest_col;
+  double x_step;
+  double y_step;
+} AnimData;
+
+typedef enum
+{
+  UP,
+  DOWN,
+  LEFT,
+  RIGHT
+} ItemDirection;
+
+typedef struct
+{
+  GSList *level;
+  GSList *obstacles;
+  GSList *moveables;
+  GSList *shadows;
+} LevelItems;
+
+/* Static declarations, to be removed */
 static GtkFixed *board_canvas = NULL;
 static Theme *board_theme = NULL;
+static PlayField *board_env = NULL;    /* the actual playfield */
+static PlayField *board_sce = NULL;    /* the actual playfield */
+static PlayField *board_shadow = NULL; /* the shadow positions */
+static AnimData *anim_data;    /* holds the date for the atom 
+                               animation */
+static GSList *board_canvas_items = NULL;      /* a list of all used  */
+static Goal *board_goal = NULL;        /* the goal of this level */
+static LevelItems *level_items;        /* references to the level groups */
 
-#define BGR_FLOOR_ROWS 15
-#define BGR_FLOOR_COLS 15
+/* Forward declarations of internal functions */
+void board_gtk_render (void);
+static void render_tile (Tile *tile, gint row, gint col);
+GtkImage* create_tile (double x, double y, Tile *tile);
 
+/* Function implementations */
 static void create_background_floor (void)
 {
   int row, col;
@@ -54,15 +98,6 @@ static void create_background_floor (void)
 
     item = gtk_image_new_from_pixbuf (pixbuf);
     gtk_fixed_put (GTK_FIXED (board_canvas), item, x, y);
-       //item = gnome_canvas_item_new (level_items->floor,
-       //                            gnome_canvas_pixbuf_get_type (),
-       //                            "pixbuf", pixbuf, "x", x, "x_in_pixels",
-       //                            TRUE, "y", y, "y_in_pixels", TRUE,
-       //                            "width",
-       //                            (double) gdk_pixbuf_get_width (pixbuf),
-       //                            "height",
-       //                            (double) gdk_pixbuf_get_height (pixbuf),
-       //                            "anchor", GTK_ANCHOR_NW, NULL);
       }
 
   g_object_unref (pixbuf);
@@ -96,13 +131,211 @@ void board_gtk_init (Theme * theme, gpointer canvas)
 {
   board_theme = theme;
   board_canvas = canvas;
+  g_object_ref (theme);
+  board_theme = theme;
+  board_env = NULL;
+  board_sce = NULL;
+  board_goal = NULL;
+
+  /* Animation Data Setup */
+  anim_data = g_new0 (AnimData, 1);
+  anim_data->timeout_id = -1;
+  anim_data->counter = 0;
+  anim_data->dest_row = 0;
+  anim_data->dest_col = 0;
+  anim_data->x_step = 0.0;
+  anim_data->y_step = 0.0;
+
+  undo_clear ();
+
+  /* Canvas setup */
+  level_items = g_new0 (LevelItems, 1);
+
+  g_signal_connect (G_OBJECT (canvas), "key_press_event",
+                   G_CALLBACK (board_gtk_handle_key_event), NULL);
 
   create_background_floor ();
   gtk_widget_show_all (GTK_WIDGET(board_canvas));
 }
 
-void board_gtk_init_level (PlayField * env, PlayField * sce, Goal * goal)
+void board_gtk_render ()
+{
+  gint row, col;
+  Tile *tile;
+
+  g_return_if_fail (board_theme != NULL);
+
+  /* render one row more than the actual environment because
+     of the shadow, which is one row/col larger */
+
+  for (row = 0; row <= playfield_get_n_rows (board_env); row++)
+    {
+      for (col = 0; col <= playfield_get_n_cols (board_env); col++)
+       {
+         if (row < playfield_get_n_rows (board_env) &&
+             col < playfield_get_n_cols (board_env))
+           {
+             tile = playfield_get_tile (board_sce, row, col);
+             if (tile != NULL)
+               {
+                 render_tile (tile, row, col);
+                 g_object_unref (tile);
+               }
+
+             tile = playfield_get_tile (board_env, row, col);
+
+             if (tile != NULL)
+               {
+                 render_tile (tile, row, col);
+                 if (tile_get_tile_type (tile) == TILE_TYPE_WALL)
+                   playfield_set_tile (board_sce, row, col, tile);
+
+                 g_object_unref (tile);
+               }
+           }
+
+         tile = playfield_get_tile (board_shadow, row, col);
+         if (tile != NULL)
+           {
+             render_tile (tile, row, col);
+             g_object_unref (tile);
+           }
+       }
+    }
+
+}
+
+static void render_tile (Tile *tile, gint row, gint col)
+{
+  gboolean create = FALSE;
+  TileType type;
+  gint row_offset, col_offset;
+  gdouble x, y;
+
+  type = tile_get_tile_type (tile);
+  switch (type)
+    {
+    case TILE_TYPE_ATOM:
+      create = TRUE;
+      break;
+
+    case TILE_TYPE_WALL:
+      create = TRUE;
+      break;
+
+    case TILE_TYPE_SHADOW:
+      create = TRUE;
+      break;
+
+    case TILE_TYPE_UNKNOWN:
+    case TILE_TYPE_FLOOR:
+    default:
+      break;
+    }
+
+
+  if (create)
+    {
+      row_offset = BGR_FLOOR_ROWS / 2 - playfield_get_n_rows (board_env) / 2;
+      col_offset = BGR_FLOOR_COLS / 2 - playfield_get_n_cols (board_env) / 2;
+      convert_to_canvas (board_theme, row + row_offset, col + col_offset, &x, &y);
+      create_tile (x, y, tile);
+    }
+}
+
+GtkImage* create_tile (double x, double y,
+                             Tile *tile)
+{
+  GdkPixbuf *pixbuf = NULL;
+  GtkWidget *item = NULL;
+  TileType type;
+
+  type = tile_get_tile_type (tile);
+  pixbuf = theme_get_tile_image (board_theme, tile);
+
+  item = gtk_image_new_from_pixbuf (pixbuf);
+  gtk_widget_show (item);
+  gtk_fixed_put (GTK_FIXED (board_canvas), item, x, y);
+
+  g_object_set_data (G_OBJECT (item), "tile", tile);
+
+// TODO handle button click
+//  if (tile_get_tile_type (tile) == TILE_TYPE_ATOM)
+//    {
+//      g_signal_connect (G_OBJECT (item), "event",
+//                     G_CALLBACK (board_handle_item_event), NULL);
+//    }
+
+  board_canvas_items = g_slist_prepend (board_canvas_items, item);
+  switch (type)
+    {
+    case TILE_TYPE_ATOM:
+      level_items->moveables =  g_slist_prepend (level_items->moveables, item);
+      break;
+
+    case TILE_TYPE_WALL:
+      level_items->obstacles = g_slist_prepend (level_items->obstacles, item);
+      break;
+
+    case TILE_TYPE_SHADOW:
+      level_items->shadows = g_slist_prepend (level_items->shadows, item);
+      break;
+
+    case TILE_TYPE_UNKNOWN:
+    case TILE_TYPE_FLOOR:
+    default:
+      break;
+    }
+
+  return GTK_IMAGE (item);
+}
+
+static void remove_items (GSList **list)
+{
+  GSList *to_free = NULL;
+
+  while (*list != NULL) {
+    to_free = *list;
+    gtk_container_remove (GTK_CONTAINER (board_canvas), GTK_WIDGET ((*list)->data));
+    *list = g_slist_next (*list);
+    g_slist_free_1 (to_free);
+  }
+  
+}
+
+static void level_clear (void)
 {
+  remove_items (&(level_items->moveables));
+  remove_items (&(level_items->obstacles));
+  remove_items (&(level_items->shadows));
+}
+
+void board_gtk_init_level (PlayField * base_env, PlayField * sce, Goal * goal)
+{
+  /* init item anim structure */
+  anim_data->timeout_id = -1;
+  anim_data->counter = 0;
+  anim_data->dest_row = 0;
+  anim_data->dest_col = 0;
+  anim_data->x_step = 0.0;
+  anim_data->y_step = 0.0;
+
+  /* reset undo of moves */
+  undo_clear ();
+
+  level_clear ();
+
+  /* init board */
+  board_env = playfield_generate_environment (base_env, board_theme);
+  board_sce = playfield_copy (sce);
+  board_shadow = playfield_generate_shadow (base_env);
+
+  /* init goal */
+  board_goal = g_object_ref (goal);
+
+  /* render level */
+  board_gtk_render ();
+  board_gtk_show ();
 }
 
 void board_gtk_destroy (void)
diff --git a/src/board_gtk.h b/src/board_gtk.h
index 64630d6..7d83669 100644
--- a/src/board_gtk.h
+++ b/src/board_gtk.h
@@ -24,6 +24,8 @@
 #include "theme.h"
 #include "playfield.h"
 #include "goal.h"
+#include "undo.h"
+#include "canvas_helper.h"
 
 void board_gtk_init (Theme * theme, gpointer canvas);
 
diff --git a/src/clock.c b/src/clock.c
index 35c68f6..e56d777 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -52,17 +52,17 @@ GType clock_get_type (void)
   return object_type;
 }
 
-static void clock_destroy (GtkObject *object)
+static void clock_destroy (GtkWidget *object)
 {
   g_return_if_fail (object != NULL);
 
   clock_stop (CLOCK (object));
-  GTK_OBJECT_CLASS (parent_class)->destroy (object);
+  GTK_WIDGET_CLASS (parent_class)->destroy (object);
 }
 
 static void clock_class_init (ClockClass *klass)
 {
-  GtkObjectClass *object_class = (GtkObjectClass *) klass;
+  GtkWidgetClass *object_class = (GtkWidgetClass *) klass;
 
   object_class->destroy = clock_destroy;
   parent_class = g_type_class_peek (gtk_label_get_type ());
@@ -96,9 +96,7 @@ static gint clock_timer_callback (gpointer data)
 {
   Clock *clock = (Clock *) data;
 
-  GDK_THREADS_ENTER ();
   clock_gen_str (clock);
-  GDK_THREADS_LEAVE ();
 
   return TRUE;
 }
diff --git a/src/main.c b/src/main.c
index 4ec7371..ab91600 100644
--- a/src/main.c
+++ b/src/main.c
@@ -669,7 +669,7 @@ static GtkWidget *create_mainwin_content (AtomixApp *app)
                    G_CALLBACK (on_key_press_event), app);
 
   /* create right window side */
-  vbox = gtk_vbox_new (FALSE, 6);
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
   gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (vbox), FALSE, TRUE, 0);
 
   /* create statistics frame */
@@ -770,7 +770,7 @@ static AtomixApp *create_gui (void)
                                                           NULL),
                                                           NULL);
 
-  vbox = gtk_vbox_new (FALSE, 0);
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   gtk_container_add (GTK_CONTAINER (app->mainwin), vbox);
   gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
   gtk_box_pack_start (GTK_BOX (vbox), content, TRUE, TRUE, 0);


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