Re: Investigating gnomine display corruption.



Hi All

Here's a patch that fixes the background grid for iagno and reenables the "show grid" option.  It uses a somewhat dodgy mechanism for getting the background colour, but it works.  What we really need is a "pick color" method in gdk-pixbuf....

Let me know if it isn't any good and/or if you want me to raise a bug in bugzilla to contain this patch. 

rufus

On Fri, Jan 21, 2011 at 6:51 AM, Thomas H.P. Andersen <phomes gmail com> wrote:
On Thu, Jan 20, 2011 at 17:58, rufus hamade <rufus myfanwy org uk> wrote:
> Hi All,
>
> I've recently been playing around with tweaking gnomine to make it more
> interesting (for me at least.)
>
> Of course, gnomine is currently broken, so I've been doing some
> investigation into why that is the case.  My limited GTK/Cairo knowledge
> suggests the problem lies somewhere quite deep.
>
> Is anyone else looking at this issue?  I don't want to be stepping on any
> toes and/or duplicating work.  But I'd be happy to investigate myself, as
> this is a good way of improving my GTK knowledge.

This would be very cool. Thanks.

> Any hints before I start?

The commit to port to gtk3/cairo is this one:
http://git.gnome.org/browse/gnome-games/commit/?id=47f69c40286d7f7df86d0c61a585e0011a879c0f

Also if you wish to there is another known bug graphics in iagno (my
fault). The grid is not drawn yet. The relevant commit for that is:
http://git.gnome.org/browse/gnome-games/commit/?id=764c51939d8eb7eb647b76bbd9d28d2adc3a0726

> Cheers,
>
> rufus
>
> _______________________________________________
> games-list mailing list
> games-list gnome org
> http://mail.gnome.org/mailman/listinfo/games-list
>
>

From cb19633e7694c99e9290e3e9f0a0be395658c9be Mon Sep 17 00:00:00 2001
From: Rufus <rufus myfanwy org uk>
Date: Sat, 22 Jan 2011 12:01:22 +0000
Subject: [PATCH] Reenable iagno's dashed grid, and the grid enabling/disabling option.

---
 iagno/gnothello.c  |  128 ++++++++++++++++++++++++++++++++-------------------
 iagno/gnothello.h  |    4 +-
 iagno/properties.c |   31 ++++++-------
 iagno/properties.h |    2 +
 4 files changed, 97 insertions(+), 68 deletions(-)

diff --git a/iagno/gnothello.c b/iagno/gnothello.c
index 84c1b76..794b39c 100644
--- a/iagno/gnothello.c
+++ b/iagno/gnothello.c
@@ -81,7 +81,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 +91,8 @@ 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};
+double background_color[3];
 
 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,89 @@ 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_set_source_rgb(cr, background_color[0], background_color[1], background_color[2]);
+  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);
 }
 
-void
-gui_draw_grid (void)
+static void
+gui_draw_grid (cairo_t *cr)
 {
-  int i;
-  cairo_t *cr;
+  int i;	
+  if (!show_grid)
+    return;
 
-  cr = cairo_create (buffer_surface);
+  cairo_set_source_rgb(cr, 1.0 - background_color[0], 1.0 - background_color[1], 1.0 - background_color[2]);
+  cairo_set_dash(cr, dash, 1, 3);
   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);
+    cairo_line_to (cr, i * board_width / 8 - 1, board_height - 1);
 
     cairo_move_to (cr, 0, i * board_height / 8 - 1);
-    cairo_line_to (cr, board_width, i * board_height / 8 - 1);
+    cairo_line_to (cr, board_width - 1, i * board_height / 8 - 1);
   }
+  cairo_stroke(cr);
+}
 
+void
+gui_draw_pixmap (gint which, gint x, gint y)
+{
+  cairo_t *cr;
+  GdkRectangle rect;
+
+  cr = cairo_create (buffer_surface);
+  gui_draw_pixmap_buffer (cr, which, x, y);
+  cairo_destroy (cr);
+
+  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 +432,8 @@ load_pixmaps (void)
   gchar *fname;
   const char *dname;
   cairo_t *cr;
+  int dash_count;
+  guchar *pixels;
 
   g_return_if_fail (tile_set != NULL && tile_set[0] != '0');
 
@@ -428,6 +461,19 @@ 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;
+
+  pixels = gdk_pixbuf_get_pixels (image);
+  background_color[0] = ((double)pixels[0])/256;
+  background_color[1] = ((double)pixels[1])/256;
+  background_color[2] = ((double)pixels[2])/256;
+
   board_width = (tile_width+GRIDWIDTH) * 8;
   board_height = (tile_height+GRIDWIDTH) * 8;
   if (buffer_surface)
@@ -563,15 +609,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 +792,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 +1064,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);
 
-- 
1.7.1



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