[gnome-games] Reenable iagno's dashed grid, and the grid enabling/disabling option.



commit 8348bce106c9894171ec1a3cfbb04c2db94b0b84
Author: Rufus <rufus myfanwy org uk>
Date:   Sat Jan 22 12:01:22 2011 +0000

    Reenable iagno's dashed grid, and the grid enabling/disabling option.

 iagno/gnothello.c  |  141 +++++++++++++++++++++++++++++++++-------------------
 iagno/gnothello.h  |    4 +-
 iagno/properties.c |   31 +++++------
 iagno/properties.h |    2 +
 4 files changed, 107 insertions(+), 71 deletions(-)
---
diff --git a/iagno/gnothello.c b/iagno/gnothello.c
index 84c1b76..83ce31c 100644
--- a/iagno/gnothello.c
+++ b/iagno/gnothello.c
@@ -68,8 +68,9 @@ GtkAction *leave_network_action;
 GtkAction *player_list_action;
 GtkAction *undo_action;
 
-cairo_surface_t *buffer_surface = NULL;
-cairo_surface_t *tiles_surface = NULL;
+cairo_surface_t *buffer_surface     = NULL;
+cairo_surface_t *tiles_surface      = NULL;
+cairo_surface_t *background_surface = NULL;
 
 static gint flip_pixmaps_id = 0;
 static gint flip_animation_speed = PIXMAP_FLIP_DELAY;
@@ -81,7 +82,6 @@ guint white_computer_id = 0;
 guint computer_speed = COMPUTER_MOVE_DELAY;
 gint animate;
 gint animate_stagger;
-gint grid = 0;
 guint tiles_to_flip = 0;
 
 gint64 milliseconds_total = 0;
@@ -92,6 +92,7 @@ guint game_in_progress;
 guint tile_width = 80, tile_height = 80;
 guint board_width = 648, board_height = 648;
 #define GRIDWIDTH 1
+double dash[1] = {4.0};
 
 gint8 pixmaps[8][8] = { {0, 0, 0, 0, 0, 0, 0, 0}
 ,
@@ -293,7 +294,6 @@ gboolean
 configure_event (GtkWidget * widget, GdkEventConfigure * event)
 {
   static int old_width = 0, old_height = 0;
-  guint i, j;
 
   if (old_width == event->width && old_height == event->height) {
     return FALSE;
@@ -302,11 +302,7 @@ configure_event (GtkWidget * widget, GdkEventConfigure * event)
     old_height = event->height;
   }
 
-  for (i = 0; i < 8; i++)
-    for (j = 0; j < 8; j++)
-      gui_draw_pixmap_buffer (pixmaps[i][j], i, j);
-  gui_draw_grid ();
-
+  gui_draw_board ();
   return FALSE;
 }
 
@@ -343,54 +339,93 @@ button_press_event (GtkWidget * widget, GdkEventButton * event)
   return TRUE;
 }
 
-void
-gui_draw_pixmap (gint which, gint x, gint y)
+static void
+gui_fill_background(cairo_t *cr)
 {
-  GdkRectangle rect;
-
-  rect.x = x * (tile_width + GRIDWIDTH);
-  rect.y = y * (tile_height + GRIDWIDTH);
-  rect.width = tile_width;
-  rect.height = tile_height;
-
-  gui_draw_pixmap_buffer (which, x, y);
-  gdk_window_invalidate_rect (gtk_widget_get_window (drawing_area), &rect, FALSE);
+  cairo_pattern_t *p = cairo_pattern_create_for_surface(background_surface);
+  cairo_pattern_set_extend(p, CAIRO_EXTEND_REPEAT);
+  cairo_set_source(cr, p);
+  cairo_move_to(cr, 0, 0);
+  cairo_line_to (cr, 0, board_height);
+  cairo_line_to (cr, board_width, board_height);
+  cairo_line_to (cr, board_width, 0);
+  cairo_line_to (cr, 0, 0);
+  cairo_fill(cr);
 }
 
-void
-gui_draw_pixmap_buffer (gint which, gint x, gint y)
+static void
+gui_draw_pixmap_buffer (cairo_t *cr, gint which, gint x, gint y)
 {
-  cairo_t *cr;
-
   int tile_surface_x = x * (tile_width + GRIDWIDTH) - (which % 8) * tile_width;
   int tile_surface_y = y * (tile_height + GRIDWIDTH) - (which / 8) * tile_height;
 
-  cr = cairo_create (buffer_surface);
   cairo_set_source_surface (cr, tiles_surface, tile_surface_x, tile_surface_y);
   cairo_rectangle (cr, x * (tile_width + GRIDWIDTH), y * (tile_height + GRIDWIDTH), tile_width, tile_height);
   cairo_fill (cr);
+}
 
-  cairo_destroy (cr);
+static void
+gui_draw_grid (cairo_t *cr)
+{
+  int i;
+  if (!show_grid)
+    return;
+
+  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+  cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
+  cairo_set_dash(cr, dash, 1, 2.5);
+  cairo_set_line_width(cr, GRIDWIDTH);
+  for (i = 1; i < 8; i++) {
+    cairo_move_to (cr, i * board_width / 8 - 0.5, 0);
+    cairo_line_to (cr, i * board_width / 8 - 0.5, board_height);
+
+    cairo_move_to (cr, 0, i * board_height / 8 - 0.5);
+    cairo_line_to (cr, board_width, i * board_height / 8 - 0.5);
+  }
+  cairo_stroke(cr);
 }
 
 void
-gui_draw_grid (void)
+gui_draw_pixmap (gint which, gint x, gint y)
 {
-  int i;
   cairo_t *cr;
+  GdkRectangle rect;
 
   cr = cairo_create (buffer_surface);
-  for (i = 1; i < 8; i++) {
-    cairo_move_to (cr, i * board_width / 8 - 1, 0);
-    cairo_line_to (cr, i * board_width / 8 - 1, board_height);
+  gui_draw_pixmap_buffer (cr, which, x, y);
+  cairo_destroy (cr);
 
-    cairo_move_to (cr, 0, i * board_height / 8 - 1);
-    cairo_line_to (cr, board_width, i * board_height / 8 - 1);
-  }
+  rect.x = x * (tile_width + GRIDWIDTH);
+  rect.y = y * (tile_height + GRIDWIDTH);
+  rect.width = tile_width;
+  rect.height = tile_height;
+  gdk_window_invalidate_rect (gtk_widget_get_window (drawing_area), &rect, FALSE);
+}
+
+void
+gui_draw_board() {
+  cairo_t *cr;
+  guint i, j;
+  GdkRectangle rect;
 
+  cr = cairo_create (buffer_surface);
+  gui_fill_background(cr);
+
+  for (i = 0; i < 8; i++)
+    for (j = 0; j < 8; j++)
+      if (pixmaps[i][j] >= BLACK_TURN && pixmaps[i][j] <= WHITE_TURN)
+        gui_draw_pixmap_buffer (cr, pixmaps[i][j], i, j);
+      else
+        gui_draw_pixmap_buffer (cr, 0, i, j);
+
+  gui_draw_grid(cr);
   cairo_destroy (cr);
 
-  gdk_window_invalidate_rect (gtk_widget_get_window (drawing_area), NULL, FALSE);
+  rect.x = 0;
+  rect.y = 0;
+  rect.width = board_width;
+  rect.height = board_height;
+  gdk_window_invalidate_rect (gtk_widget_get_window (drawing_area), &rect, FALSE);
 }
 
 void
@@ -401,6 +436,7 @@ load_pixmaps (void)
   gchar *fname;
   const char *dname;
   cairo_t *cr;
+  int dash_count;
 
   g_return_if_fail (tile_set != NULL && tile_set[0] != '0');
 
@@ -428,6 +464,14 @@ load_pixmaps (void)
 
   tile_width = gdk_pixbuf_get_width (image) / 8;
   tile_height = gdk_pixbuf_get_height (image) / 4;
+
+  /* Make sure the dash width evenly subdivides the tile height, and is at least 4 pixels long.
+   * This makes the dash crossings always cross in the same place, which looks nicer. */
+  dash_count = (tile_height + GRIDWIDTH)/4;
+  if (dash_count%2 != 0)
+    dash_count--;
+  dash[0] = ((double)(tile_height + GRIDWIDTH))/dash_count;
+
   board_width = (tile_width+GRIDWIDTH) * 8;
   board_height = (tile_height+GRIDWIDTH) * 8;
   if (buffer_surface)
@@ -446,12 +490,21 @@ load_pixmaps (void)
                                                      CAIRO_CONTENT_COLOR_ALPHA,
                                                      gdk_pixbuf_get_width (image),
                                                      gdk_pixbuf_get_height (image));
-
   cr = cairo_create (tiles_surface);
   gdk_cairo_set_source_pixbuf (cr, image, 0, 0);
   cairo_paint (cr);
+  cairo_destroy (cr);
 
+  if (background_surface)
+    cairo_surface_destroy (background_surface);
+  background_surface = gdk_window_create_similar_surface (gtk_widget_get_window (drawing_area),
+                                                     CAIRO_CONTENT_COLOR_ALPHA,
+                                                     1, 1);
+  cr = cairo_create (background_surface);
+  gdk_cairo_set_source_pixbuf (cr, image, 0, 0);
+  cairo_paint (cr);
   cairo_destroy (cr);
+
   g_object_unref (image);
   g_free (fname);
 }
@@ -563,15 +616,8 @@ flip_pixmaps (gpointer data)
 static void
 redraw_board (void)
 {
-  guint i, j;
-
   gui_status ();
-
-  for (i = 0; i < 8; i++)
-    for (j = 0; j < 8; j++)
-      gui_draw_pixmap_buffer (pixmaps[i][j], i, j);
-
-  gui_draw_grid ();
+  gui_draw_board();
 }
 
 void
@@ -753,11 +799,6 @@ add_timeout (guint time, GSourceFunc func, gpointer turn)
   }
 }
 
-void
-set_bg_color (void)
-{
-}
-
 #ifdef WITH_SMCLIENT
 static int
 save_state_cb (EggSMClient *client,
@@ -1030,8 +1071,6 @@ main (int argc, char **argv)
     gdk_window_move (gtk_widget_get_window (window), session_xpos, session_ypos);
   }
 
-  set_bg_color ();
-
   init_new_game ();
 
   gtk_main ();
diff --git a/iagno/gnothello.h b/iagno/gnothello.h
index cf7f93c..2d80ea2 100644
--- a/iagno/gnothello.h
+++ b/iagno/gnothello.h
@@ -65,7 +65,7 @@ gboolean draw_event (GtkWidget * widget, cairo_t * cr);
 gint configure_event (GtkWidget * widget, GdkEventConfigure * event);
 gint button_press_event (GtkWidget * widget, GdkEventButton * event);
 void gui_draw_pixmap (gint which, gint x, gint y);
-void gui_draw_pixmap_buffer (gint which, gint x, gint y);
+void gui_draw_board (void);
 void set_animation_speed (gint speed);
 void start_animation (void);
 void stop_animation (void);
@@ -79,7 +79,5 @@ guint check_computer_players (void);
 guint add_timeout (guint time, GSourceFunc func, gpointer turn);
 void load_pixmaps (void);
 void properties_cb (GtkWidget * widget, gpointer data);
-void set_bg_color (void);
-void gui_draw_grid (void);
 
 #endif
diff --git a/iagno/properties.c b/iagno/properties.c
index b0e8670..0469104 100644
--- a/iagno/properties.c
+++ b/iagno/properties.c
@@ -62,7 +62,7 @@ extern gint8 pixmaps[8][8];
 extern gint animate;
 extern gint animate_stagger;
 extern gint flip_final;
-extern gint grid;
+gint show_grid;
 gint sound;
 
 guint t_black_computer_level;
@@ -120,7 +120,7 @@ load_properties (void)
   sound = games_conf_get_boolean (NULL, KEY_SOUND, NULL);
   games_sound_enable (sound);
 
-  grid = games_conf_get_boolean (NULL, KEY_SHOW_GRID, NULL);
+  show_grid = games_conf_get_boolean (NULL, KEY_SHOW_GRID, NULL);
 
   flip_final = games_conf_get_boolean (NULL, KEY_FLIP_FINAL_RESULTS, NULL);
 
@@ -159,7 +159,7 @@ reset_properties (void)
 
   t_animate = animate;
   t_animate_stagger = animate_stagger;
-  t_grid = grid;
+  t_grid = show_grid;
   t_flip_final = flip_final;
 }
 
@@ -252,7 +252,7 @@ save_properties (void)
   games_conf_set_integer (NULL, KEY_ANIMATE, animate);
 
   games_conf_set_boolean (NULL, KEY_ANIMATE_STAGGER, animate_stagger);
-  games_conf_set_boolean (NULL, KEY_SHOW_GRID, grid);
+  games_conf_set_boolean (NULL, KEY_SHOW_GRID, show_grid);
   games_conf_set_boolean (NULL, KEY_FLIP_FINAL_RESULTS, flip_final);
   games_conf_set_boolean (NULL, KEY_SOUND, sound);
 }
@@ -260,7 +260,7 @@ save_properties (void)
 static void
 apply_changes (void)
 {
-  guint i, j;
+  guint redraw = 0;
 
   black_computer_level = t_black_computer_level;
   white_computer_level = t_white_computer_level;
@@ -283,15 +283,7 @@ apply_changes (void)
   if (strcmp (tile_set, tile_set_tmp)) {
     g_free (tile_set);
     tile_set = g_strdup (tile_set_tmp);
-    load_pixmaps ();
-    set_bg_color ();
-    for (i = 0; i < 8; i++)
-      for (j = 0; j < 8; j++)
-	if (pixmaps[i][j] >= BLACK_TURN && pixmaps[i][j] <= WHITE_TURN)
-	  gui_draw_pixmap_buffer (pixmaps[i][j], i, j);
-	else
-	  gui_draw_pixmap_buffer (0, i, j);
-    gui_draw_grid ();
+    redraw = 1;
   }
 
   animate = t_animate;
@@ -313,9 +305,14 @@ apply_changes (void)
 
   flip_final = t_flip_final;
 
-  if (grid != t_grid) {
-    grid = t_grid;
-    gui_draw_grid ();
+  if (show_grid != t_grid) {
+    show_grid = t_grid;
+    redraw = 1;
+  }
+
+  if (redraw) {
+    load_pixmaps ();
+    gui_draw_board ();
   }
 
   games_sound_enable (sound);
diff --git a/iagno/properties.h b/iagno/properties.h
index 3b72d79..cbbe8d8 100644
--- a/iagno/properties.h
+++ b/iagno/properties.h
@@ -1,6 +1,8 @@
 #ifndef _PROPERTIES_H_
 #define _PROPERTIES_H_
 
+extern int show_grid;
+
 void load_properties (void);
 void show_properties_dialog (void);
 



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