[gnome-mines] Speed Improvements, minor bug fix and removal of some deprecated calls.



commit 4498bea81c7e24809ac83028a825ca82012bef9a
Author: Isaac Lenton <isaac isuniversal com>
Date:   Sat Feb 9 23:50:45 2013 +1000

    Speed Improvements, minor bug fix and removal of some deprecated calls.
    
    Speed Improvements:  In the class Minefield, two of the frequently
        used getters used nested for-loops to revaluate a value which
        could be stored/manually incremented.
    
    Minor Bug Fix: Fixed an issue where the hint function assumed that
        the user is always correct with their flag/question placement.
        Now the function reveals all other squares before revealing
        flagged squares that do not contain mines.
    
    Depreceated Calls: These have been replaced with newer alternatives.
    
    Also Note: There are a couple of other changes for how the board is
        displayed, particularly the reavealed squares.  An active button
        is no longer displayed on a revealed square.  (Could be undone).

 src/gnome-mines.vala    |  118 +++++++++++++++++++++++++++--------------------
 src/minefield-view.vala |   85 ++++++++++++++++++++-------------
 src/minefield.vala      |   41 ++++++++--------
 3 files changed, 140 insertions(+), 104 deletions(-)
---
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 6dddcf7..3bee564 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -220,114 +220,132 @@ public class Mines : Gtk.Application
         minefield_view.unlook.connect (unlook_cb);
         view_box.pack_start (minefield_view, true, true, 0);
 
-        /* New game screen */
+        /* Initialize New Game Screen */
+        startup_new_game_screen ();
+        view_box.pack_start (new_game_screen, true, true, 0);
+
+        /* Initialize Custom Game Screen */
+        startup_custom_game_screen ();
+        view_box.pack_start (custom_game_screen, true, false);
+
+        tick_cb ();
+
+        history = new History (Path.build_filename (Environment.get_user_data_dir (), "gnome-mines", 
"history"));
+        history.load ();
+    }
+
+    private void startup_new_game_screen ()
+    {
         new_game_screen = new Gtk.AspectFrame (_("Field Size"), 0.5f, 0.5f, 1.0f, false);
         new_game_screen.set_shadow_type (Gtk.ShadowType.NONE);
-        new_game_screen.set_size_request(200, 200);
+        new_game_screen.set_size_request (200, 200);
 
-        var new_game_table = new Gtk.Table (2, 2, true);
-        new_game_screen.add (new_game_table);
+        var new_game_grid = new Gtk.Grid ();
+        new_game_grid.column_homogeneous = true;
+        new_game_grid.column_spacing = 0;
+        new_game_grid.row_homogeneous = true;
+        new_game_grid.row_spacing = 0;
+        new_game_screen.add (new_game_grid);
 
-        var button_small = new Gtk.Button ();
-        new_game_table.attach_defaults (button_small, 0, 1, 0, 1);
-        button_small.clicked.connect (small_size_clicked_cb);
+        var button = new Gtk.Button ();
+        button.clicked.connect (small_size_clicked_cb);
+        new_game_grid.attach (button, 0, 0, 1, 1);
 
         var label = new Gtk.Label (null);
         label.set_markup (make_minefield_description ("#0000ff", 8, 8, 10));
         label.set_justify (Gtk.Justification.CENTER);
-        button_small.add (label);
+        button.add (label);
 
-        var button_medium = new Gtk.Button ();
-        new_game_table.attach_defaults (button_medium, 1, 2, 0, 1);
-        button_medium.clicked.connect (medium_size_clicked_cb);
+        button = new Gtk.Button ();
+        button.clicked.connect (medium_size_clicked_cb);
+        new_game_grid.attach (button, 1, 0, 1, 1);
 
         label = new Gtk.Label (null);
         label.set_markup (make_minefield_description ("#00a000", 16, 16, 40));
         label.set_justify (Gtk.Justification.CENTER);
-        button_medium.add (label);
+        button.add (label);
 
-        var button_large = new Gtk.Button ();
-        new_game_table.attach_defaults (button_large, 0, 1, 1, 2);
-        button_large.clicked.connect (large_size_clicked_cb);
+        button = new Gtk.Button ();
+        button.clicked.connect (large_size_clicked_cb);
+        new_game_grid.attach (button, 0, 1, 1, 1);
 
         label = new Gtk.Label (null);
-        label.set_markup (make_minefield_description ("#ff0000", 30, 16, 99));
+        label.set_markup (make_minefield_description ("#ff0000", 16, 16, 40));
         label.set_justify (Gtk.Justification.CENTER);
-        button_large.add (label);
+        button.add (label);
 
-        var button_custom = new Gtk.Button ();
-        new_game_table.attach_defaults (button_custom, 1, 2, 1, 2);
-        button_custom.clicked.connect (show_custom_game_screen);
+        button = new Gtk.Button ();
+        button.clicked.connect (show_custom_game_screen);
+        new_game_grid.attach (button, 1, 1, 1, 1);
 
         label = new Gtk.Label (null);
         label.set_markup_with_mnemonic ("<span fgcolor='#00007f'><span size='xx-large' 
weight='heavy'>?</span>\n" + dpgettext2 (null, "board size", "Custom") + "</span>");
         label.set_justify (Gtk.Justification.CENTER);
-        button_custom.add (label);
+        button.add (label);
 
         new_game_screen.show_all ();
-        view_box.pack_start (new_game_screen, true, true, 0);
+    }
 
-        /* Custom game screen */
+    private void startup_custom_game_screen ()
+    {
         custom_game_screen = new Gtk.AspectFrame ("", 0.5f, 0.5f, 0.0f, true);
         custom_game_screen.set_shadow_type (Gtk.ShadowType.NONE);
 
-        var custom_field_grid = new Gtk.Grid ();
-        custom_field_grid.set_row_spacing (6);
-        custom_field_grid.set_column_spacing (12);
-        custom_game_screen.add (custom_field_grid);
+        var custom_game_grid = new Gtk.Grid ();
+        custom_game_grid.column_homogeneous = false;
+        custom_game_grid.column_spacing = 12;
+        custom_game_grid.row_spacing = 6;
+        custom_game_screen.add (custom_game_grid);
 
-        label = new Gtk.Label.with_mnemonic (_("H_orizontal:"));
+        var label = new Gtk.Label.with_mnemonic (_("H_orizontal:"));
         label.set_alignment (0, 0.5f);
-        custom_field_grid.attach (label, 0, 0, 1, 1);
+        custom_game_grid.attach (label, 0, 0, 1, 1);
 
         var field_width_entry = new Gtk.SpinButton.with_range (XSIZE_MIN, XSIZE_MAX, 1);
         field_width_entry.value_changed.connect (xsize_spin_cb);
         field_width_entry.set_value (settings.get_int (KEY_XSIZE));
-        custom_field_grid.attach (field_width_entry, 1, 0, 1, 1);
+        custom_game_grid.attach (field_width_entry, 1, 0, 1, 1);
         label.set_mnemonic_widget (field_width_entry);
 
         label = new Gtk.Label.with_mnemonic (_("_Vertical:"));
         label.set_alignment (0, 0.5f);
-        custom_field_grid.attach (label, 0, 1, 1, 1);
+        custom_game_grid.attach (label, 0, 1, 1, 1);
 
         var field_height_entry = new Gtk.SpinButton.with_range (YSIZE_MIN, YSIZE_MAX, 1);
         field_height_entry.value_changed.connect (ysize_spin_cb);
         field_height_entry.set_value (settings.get_int (KEY_YSIZE));
-        custom_field_grid.attach (field_height_entry, 1, 1, 1, 1);
+        custom_game_grid.attach (field_height_entry, 1, 1, 1, 1);
         label.set_mnemonic_widget (field_height_entry);
 
         label = new Gtk.Label.with_mnemonic (_("_Number of mines:"));
         label.set_alignment (0, 0.5f);
-        custom_field_grid.attach (label, 0, 2, 1, 1);
+        custom_game_grid.attach (label, 0, 2, 1, 1);
 
         n_mines_spin = new Gtk.SpinButton.with_range (1, XSIZE_MAX * YSIZE_MAX, 1);
         n_mines_spin.value_changed.connect (n_mines_spin_cb);
         n_mines_spin.set_value (settings.get_int (KEY_NMINES));
-        custom_field_grid.attach (n_mines_spin, 1, 2, 1, 1);
-
+        custom_game_grid.attach (n_mines_spin, 1, 2, 1, 1);
         set_n_mines_limit ();
         label.set_mnemonic_widget (n_mines_spin);
 
-        var hbox = new Gtk.HBox (false, 5);
-        custom_field_grid.attach (hbox, 0, 3, 2, 1);
+        var button_grid = new Gtk.Grid ();
+        button_grid.column_homogeneous = false;
+        button_grid.column_spacing = 5;
+        custom_game_grid.attach (button_grid, 0, 3, 2, 1);
 
-        var button_back = new Gtk.Button.from_stock (Gtk.Stock.CANCEL);
-        button_back.clicked.connect (show_new_game_screen);
-        hbox.pack_start (button_back, true, true);
+        var button = new Gtk.Button.from_stock (Gtk.Stock.CANCEL);
+        button.expand = true;
+        button.clicked.connect (show_new_game_screen);
+        button_grid.attach (button, 0, 0, 1, 1);
 
-        button_custom = new Gtk.Button.with_mnemonic (_("_Play Game"));
-        button_custom.set_image (new Gtk.Image.from_stock (Gtk.Stock.GO_FORWARD, Gtk.IconSize.BUTTON));
-        button_custom.clicked.connect (custom_size_clicked_cb);
-        hbox.pack_start (button_custom, true, true);
+        button = new Gtk.Button.with_mnemonic (_("_Play Game"));
+        button.expand = true;
+        button.set_image (new Gtk.Image.from_stock (Gtk.Stock.GO_FORWARD, Gtk.IconSize.BUTTON));
+        button.clicked.connect (custom_size_clicked_cb);
+        button_grid.attach (button, 1, 0, 1, 1);
 
         custom_game_screen.show_all ();
         custom_game_screen.hide ();
-        view_box.pack_start (custom_game_screen, true, false);
-
-        tick_cb ();
-
-        history = new History (Path.build_filename (Environment.get_user_data_dir (), "gnome-mines", 
"history"));
-        history.load ();
     }
 
     private bool window_configure_event_cb (Gdk.EventConfigure event)
diff --git a/src/minefield-view.vala b/src/minefield-view.vala
index 1d16aea..a0dd9a5 100644
--- a/src/minefield-view.vala
+++ b/src/minefield-view.vala
@@ -339,32 +339,6 @@ public class MinefieldView : Gtk.DrawingArea
         /* Draw grid on ocean floor */
         if (minefield.is_cleared (x, y))
         {
-            Gtk.paint_box (get_style (), cr, is_down ? Gtk.StateType.ACTIVE : Gtk.StateType.NORMAL, 
Gtk.ShadowType.IN, this,
-                           "button", 0, 0, (int) mine_size, (int) mine_size);
-
-            /* Draw dotted border */
-            if (y == 0)
-            {
-                cr.move_to (0, 0);
-                cr.line_to (mine_size - 1, 0);
-            }
-            if (x == 0)
-            {
-                cr.move_to (0, 0);
-                cr.line_to (0, mine_size - 1);
-            }
-            cr.move_to (mine_size - 1 + 0.5, 0.5);
-            cr.line_to (mine_size - 1 + 0.5, mine_size - 1 + 0.5);
-            cr.move_to (0.5, mine_size - 1 + 0.5);
-            cr.line_to (mine_size - 1 + 0.5, mine_size - 1 + 0.5);
-            cr.save ();
-            Gdk.cairo_set_source_color (cr, get_style ().dark[get_state ()]);
-            cr.set_line_width (1);
-            double[] dots = {2, 2};
-            cr.set_dash (dots, 0);
-            cr.stroke ();
-            cr.restore ();
-
             if (minefield.paused)
                 return;
 
@@ -403,13 +377,14 @@ public class MinefieldView : Gtk.DrawingArea
         }
         else
         {
-            /* Draw shadow around possible mine location */
-            Gtk.paint_box (get_style (), cr,
-                           is_down ? Gtk.StateType.ACTIVE : Gtk.StateType.SELECTED,
-                           is_down ? Gtk.ShadowType.IN : Gtk.ShadowType.OUT,
-                           this,
-                           "button", 0, 0, (int) mine_size, (int) mine_size);
-                           
+            var style_context = get_style_context ();
+            style_context.save ();
+            style_context.add_class (Gtk.STYLE_CLASS_BUTTON);
+            style_context.set_state (is_down ? Gtk.StateFlags.ACTIVE : Gtk.StateFlags.NORMAL);
+            style_context.render_frame (cr, 0, 0, (int) mine_size, (int) mine_size);
+            style_context.render_background (cr, 0, 0, (int) mine_size, (int) mine_size);
+            style_context.restore ();
+
             if (minefield.paused)
                 return;
 
@@ -436,7 +411,7 @@ public class MinefieldView : Gtk.DrawingArea
                     cr.line_to (x2, y1);
 
                     cr.save ();
-                    Gdk.cairo_set_source_color (cr, get_style ().black);
+                    cr.set_source_rgba (0.0, 0.0, 0.0, 1.0);
                     cr.set_line_width (double.max (1, 0.1 * mine_size));
                     cr.set_line_join (Cairo.LineJoin.ROUND);
                     cr.set_line_cap (Cairo.LineCap.ROUND);
@@ -478,6 +453,48 @@ public class MinefieldView : Gtk.DrawingArea
                 number_patterns[i] = null;
         }
 
+        double dimensions[2] = {minefield.width * mine_size, minefield.height * mine_size};
+        double centre[2] = { x_offset + 0.5 * dimensions[0], y_offset + 0.5 * dimensions[1] };
+        double radius = Math.fmax (dimensions[0], dimensions[1]);
+
+        /* Draw Background */
+        var pattern = new Cairo.Pattern.radial (centre[0], centre[1], 0.0, centre[0], centre[1], radius);
+        pattern.add_color_stop_rgba (0.0, 0.0, 0.0, 0.0, 0.1);
+        pattern.add_color_stop_rgba (1.0, 0.0, 0.0, 0.0, 0.4);
+
+        cr.rectangle (x_offset - 0.5, y_offset - 0.5, dimensions[0] + 0.5, dimensions[1] + 0.5);
+        cr.save ();
+        cr.set_source (pattern);
+        cr.fill_preserve ();
+        cr.set_line_width (0.5);
+        cr.set_source_rgba (0.0, 0.0, 0.0, 1.0);
+        cr.stroke ();
+        cr.restore ();
+
+        /* Draw Grid */
+        cr.save ();
+        cr.set_line_width (0.5);
+        cr.set_source_rgba (0.0, 0.0, 0.0, 1.0);
+        double[] dots = {2, 2};
+        cr.set_dash (dots, 0);
+
+        for (var x = 1; x < minefield.width; x++)
+        {
+            cr.move_to (x_offset + x * mine_size, y_offset);
+            cr.line_to (x_offset + x * mine_size, y_offset + dimensions[1]);
+            cr.stroke ();
+        }
+
+        for (var y = 1; y < minefield.height; y++)
+        {
+            cr.move_to (x_offset, y_offset + y * mine_size);
+            cr.line_to (x_offset + dimensions[0], y_offset + y * mine_size);
+            cr.stroke ();
+        }
+
+        cr.restore ();
+
+        /* Draw Minefield */
         for (var x = 0; x < minefield.width; x++)
         {
             for (var y = 0; y < minefield.height; y++)
diff --git a/src/minefield.vala b/src/minefield.vala
index d547641..4521d4f 100644
--- a/src/minefield.vala
+++ b/src/minefield.vala
@@ -53,17 +53,14 @@ public class Minefield
     /* true if have placed the mines onto the map */
     private bool placed_mines = false;
 
+    /* keep track of flags and cleared squares */
+    private uint _n_cleared = 0;
+    private uint _n_flags = 0;
+
     public uint n_cleared
     {
-        get
-        {
-            var n = 0;
-            for (var x = 0; x < width; x++)
-                for (var y = 0; y < height; y++)
-                    if (locations[x, y].cleared)
-                        n++;
-            return n;
-        }
+        get { return _n_cleared; }
+        set { _n_cleared = value; }
     }
 
     public bool is_complete
@@ -73,15 +70,8 @@ public class Minefield
 
     public uint n_flags
     {
-        get
-        {
-            var n = 0;
-            for (var x = 0; x < width; x++)
-                for (var y = 0; y < height; y++)
-                    if (locations[x, y].flag == FlagType.FLAG)
-                        n++;
-            return n;
-        }
+        get { return _n_flags; }
+        set { _n_flags = value; }
     }
 
     /* Game timer */
@@ -199,6 +189,9 @@ public class Minefield
             return;
 
         locations[x, y].cleared = true;
+        n_cleared++;
+        if (locations[x, y].flag == FlagType.FLAG)
+            n_flags--;
         locations[x, y].flag = FlagType.NONE;
         redraw_sector (x, y);
         marks_changed ();
@@ -221,6 +214,11 @@ public class Minefield
         if (locations[x, y].cleared || locations[x, y].flag == flag)
             return;
 
+        if (flag == FlagType.FLAG)
+            n_flags++;
+        else if (locations[x, y].flag == FlagType.FLAG)
+            n_flags--;
+
         locations[x, y].flag = flag;
         redraw_sector (x, y);
 
@@ -269,10 +267,10 @@ public class Minefield
             for (var my = 0; my < height; my++)
             {
                 var m = locations[mx, my];
-                if (!m.has_mine && !m.cleared && m.flag == FlagType.NONE)
+                if (!m.has_mine && !m.cleared)
                 {
                     case3list.append (mx * width + my);
-                    if (get_n_adjacent_mines (mx, my) > 0)
+                    if (m.flag != FlagType.FLAG && get_n_adjacent_mines (mx, my) > 0)
                     {
                         case2list.append (mx * width + my);
                         foreach (var neighbour in neighbour_map)
@@ -303,6 +301,9 @@ public class Minefield
         var x = hint_location / width;
         var y = hint_location % width;
 
+        if (locations[x, y].flag == FlagType.FLAG)
+            locations[x, y].flag = FlagType.NONE;
+
         /* There is a ten second penalty for accepting a hint_action. */
         clear_mine (x, y);
         clock_elapsed += 10.0;


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