[gnome-mines] Performance improvements



commit 3c3b330bcbf4d0b198e87e6500ed4a2fc9169715
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Jun 26 01:52:01 2015 +0300

    Performance improvements

 src/minefield-view.vala        |   67 ++-------------------------------------
 src/minefield.vala             |   68 +++++++++++++++++++++++++++++++++------
 src/theme-selector-dialog.vala |    2 +-
 3 files changed, 61 insertions(+), 76 deletions(-)
---
diff --git a/src/minefield-view.vala b/src/minefield-view.vala
index 0532c1a..39ff817 100644
--- a/src/minefield-view.vala
+++ b/src/minefield-view.vala
@@ -202,7 +202,6 @@ public class MinefieldView : Gtk.Grid
             }
             selected.is_set = false;
 
-            selected.redraw.connect ((x, y) => { if (_minefield.is_cleared (x, y)) redraw_adjacent (x, y); 
});
             selected.validate.connect (_minefield.is_location);
 
             keyboard_cursor.is_set = false;
@@ -214,6 +213,7 @@ public class MinefieldView : Gtk.Grid
             _minefield.explode.connect (explode_cb);
             _minefield.paused_changed.connect (() => { queue_draw (); });
             _minefield.cleared.connect (complete_cb);
+            _minefield.use_autoflag = use_autoflag;
             queue_resize ();
         }
     }
@@ -291,7 +291,7 @@ public class MinefieldView : Gtk.Grid
         unlook ();
 
         if (minefield.is_cleared (selected.x, selected.y))
-            multi_release (selected.x, selected.y);
+            minefield.multi_release (selected.x, selected.y);
         else if (minefield.get_flag (selected.x, selected.y) != FlagType.FLAG)
             minefield.clear_mine (selected.x, selected.y);
 
@@ -448,67 +448,6 @@ public class MinefieldView : Gtk.Grid
         }
     }
 
-    private void redraw_adjacent (uint x, uint y)
-    {
-        foreach (var neighbour in neighbour_map)
-        {
-            var nx = (int) x + neighbour.x;
-            var ny = (int) y + neighbour.y;
-            if (minefield.is_location (nx, ny))
-                redraw_sector_cb (nx, ny);
-        }
-    }
-
-    public void multi_release (uint x, uint y)
-    {
-        if (!minefield.is_cleared (x, y) || minefield.get_flag (x, y) == FlagType.FLAG)
-            return;
-
-        /* Work out how many flags / unknown squares surround this one */
-        var n_mines = minefield.get_n_adjacent_mines (x, y);
-        uint n_flags = 0;
-        uint n_unknown = 0;
-        foreach (var neighbour in neighbour_map)
-        {
-            var nx = (int) x + neighbour.x;
-            var ny = (int) y + neighbour.y;
-            if (!minefield.is_location (nx, ny))
-                continue;
-            if (minefield.get_flag (nx, ny) == FlagType.FLAG)
-                n_flags++;
-            if (!minefield.is_cleared (nx, ny))
-                n_unknown++;
-        }
-
-        /* If have correct number of flags to mines then clear the other
-         * locations, otherwise if the number of unknown squares is the
-         * same as the number of mines flag them all */
-        var do_clear = false;
-        if (n_mines == n_flags)
-            do_clear = true;
-        else if (use_autoflag && n_unknown == n_mines)
-            do_clear = false;
-        else
-            return;
-
-        /* Use the same minefield for the whole time (it may complete as we do it) */
-        var m = minefield;
-
-        foreach (var neighbour in neighbour_map)
-        {
-            var nx = (int) x + neighbour.x;
-            var ny = (int) y + neighbour.y;
-            if (!m.is_location (nx, ny))
-                continue;
-
-            if (do_clear && m.get_flag (nx, ny) != FlagType.FLAG)
-                m.clear_mine (nx, ny);
-            else
-                m.set_flag (nx, ny, FlagType.FLAG);
-        }
-        redraw_adjacent (x, y);
-    }
-
     public override bool key_press_event (Gdk.EventKey event)
     {
         /* Check for end cases and paused game */
@@ -614,7 +553,7 @@ public class MinefieldView : Gtk.Grid
         unlook ();
 
         if (minefield.is_cleared (selected.x, selected.y))
-            multi_release (selected.x, selected.y);
+            minefield.multi_release (selected.x, selected.y);
         else if (minefield.get_flag (selected.x, selected.y) != FlagType.FLAG)
             minefield.clear_mine (selected.x, selected.y);
 
diff --git a/src/minefield.vala b/src/minefield.vala
index 413353d..0d61367 100644
--- a/src/minefield.vala
+++ b/src/minefield.vala
@@ -67,6 +67,8 @@ public class Minefield : Object
     private uint _n_cleared = 0;
     private uint _n_flags = 0;
 
+    protected bool _use_autoflag;
+
     public uint n_cleared
     {
         get { return _n_cleared; }
@@ -84,6 +86,12 @@ public class Minefield : Object
         set { _n_flags = value; }
     }
 
+    public bool use_autoflag
+    {
+        get { return _use_autoflag; }
+        set { _use_autoflag = value; }
+    }
+
     /* Game timer */
     private double clock_elapsed;
     private Timer? clock;
@@ -162,6 +170,54 @@ public class Minefield : Object
         return x >= 0 && y >= 0 && x < width && y < height;
     }
 
+    public void multi_release (uint x, uint y)
+    {
+        if (!is_cleared (x, y) || get_flag (x, y) == FlagType.FLAG)
+            return;
+
+        /* Work out how many flags / unknown squares surround this one */
+        var n_mines = get_n_adjacent_mines (x, y);
+        uint n_flags = 0;
+        uint n_unknown = 0;
+        foreach (var neighbour in neighbour_map)
+        {
+            var nx = (int) x + neighbour.x;
+            var ny = (int) y + neighbour.y;
+            if (!is_location (nx, ny))
+                continue;
+            if (get_flag (nx, ny) == FlagType.FLAG)
+                n_flags++;
+            if (!is_cleared (nx, ny))
+                n_unknown++;
+        }
+
+        /* If have correct number of flags to mines then clear the other
+         * locations, otherwise if the number of unknown squares is the
+         * same as the number of mines flag them all */
+        var do_clear = false;
+        if (n_mines == n_flags)
+            do_clear = true;
+        else if (use_autoflag && n_unknown == n_mines)
+            do_clear = false;
+        else
+            return;
+
+        /* Use the same minefield for the whole time (it may complete as we do it) */
+
+        foreach (var neighbour in neighbour_map)
+        {
+            var nx = (int) x + neighbour.x;
+            var ny = (int) y + neighbour.y;
+            if (!is_location (nx, ny))
+                continue;
+
+            if (do_clear && get_flag (nx, ny) != FlagType.FLAG)
+                clear_mine (nx, ny);
+            else
+                set_flag (nx, ny, FlagType.FLAG);
+        }
+    }
+
     public void clear_mine (uint x, uint y)
     {
         if (!exploded)
@@ -242,18 +298,8 @@ public class Minefield : Object
 
         locations[x, y].flag = flag;
         redraw_sector (x, y);
-
-        /* Update warnings */
-        /* FIXME: Doesn't check if have changed, just if might have changed */
-        foreach (var neighbour in neighbour_map)
-        {
-            var nx = (int) x + neighbour.x;
-            var ny = (int) y + neighbour.y;
-            if (is_location (nx, ny) && is_cleared (nx, ny))
-                redraw_sector (nx, ny);
-        }
-
         marks_changed ();
+
     }
 
     public FlagType get_flag (uint x, uint y)
diff --git a/src/theme-selector-dialog.vala b/src/theme-selector-dialog.vala
index 31f710a..e7f9e37 100644
--- a/src/theme-selector-dialog.vala
+++ b/src/theme-selector-dialog.vala
@@ -174,7 +174,7 @@ public class ThemeSelectorDialog : Gtk.Dialog
             {
                 if (!view.minefield.has_mine (i, j)) {
                     view.minefield.clear_mine (i, j);
-                    view.multi_release (i, j);
+                    view.minefield.multi_release (i, j);
                 } else {
                     view.toggle_mark (i, j);
                 }


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