[gnome-nibbles/wip/vala: 3/16] Load pixmaps and current level file



commit 9b45d027bd630e93ec7b92328efcfa2d90ec3f02
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Fri May 15 01:26:19 2015 +0300

    Load pixmaps and current level file

 data/levels/Makefile.am            |    2 +-
 data/org.gnome.nibbles.gschema.xml |    5 ++
 src/gnome-nibbles.vala             |    2 +
 src/nibbles-game.vala              |    2 +
 src/nibbles-view.vala              |  124 +++++++++++++++++++++++++++++++++++-
 src/properties.vala                |    3 +
 6 files changed, 136 insertions(+), 2 deletions(-)
---
diff --git a/data/levels/Makefile.am b/data/levels/Makefile.am
index 4b9f8b7..559062b 100644
--- a/data/levels/Makefile.am
+++ b/data/levels/Makefile.am
@@ -1,4 +1,4 @@
-configdir = $(datadir)/gnome-nibbles/games
+configdir = $(datadir)/gnome-nibbles/level
 config_DATA = \
        level001.gnl \
        level002.gnl \
diff --git a/data/org.gnome.nibbles.gschema.xml b/data/org.gnome.nibbles.gschema.xml
index ce47207..3a7e893 100644
--- a/data/org.gnome.nibbles.gschema.xml
+++ b/data/org.gnome.nibbles.gschema.xml
@@ -20,5 +20,10 @@
       <summary>Size of game tiles</summary>
       <description>Size of game tiles.</description>
     </key>
+    <key name="start-level" type="i">
+      <default>1</default>
+      <summary>Game level to start on</summary>
+      <summary>Game level to start on.</summary>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index f2b2ded..13259ca 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -177,6 +177,8 @@ public class Nibbles : Gtk.Application
         frame.add (view);
         frame.show_all ();
 
+        game.current_level = game.properties.start_level;
+        view.new_level (game.current_level);
         show_game_view ();
     }
 
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 914aa92..1d13ac2 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -5,6 +5,8 @@ public class NibblesGame : Object
     public int width = 92;
     public int height = 66;
 
+    public int current_level;
+
     public NibblesGame ()
     {
         properties = new Properties ();
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index c66a314..74c1639 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -6,6 +6,17 @@ public class NibblesView : GtkClutter.Embed
     public Clutter.Actor surface;
     public Clutter.Stage stage;
 
+    Gdk.Pixbuf[] wall_pixmaps = { null, null, null, null, null,
+                                  null, null, null, null, null,
+                                  null
+    };
+    Gdk.Pixbuf[] worm_pixmaps = { null, null, null, null, null,
+                                   null, null
+    };
+    Gdk.Pixbuf[] boni_pixmaps = { null, null, null, null, null,
+                                   null, null, null, null
+    };
+
     public NibblesView (NibblesGame game)
     {
         this.game = game;
@@ -20,7 +31,7 @@ public class NibblesView : GtkClutter.Embed
         {
             var pixbuf = new Gdk.Pixbuf.from_file (Path.build_filename (DATADIR, "pixmaps", 
"wall-small-empty.svg"));
             surface = new GtkClutter.Texture ();
-            (surface as GtkClutter.Texture).set_from_pixbuf (pixbuf);
+            ((GtkClutter.Texture) surface).set_from_pixbuf (pixbuf);
 
             var val = Value (typeof (bool));
             val.set_boolean (true);
@@ -41,6 +52,117 @@ public class NibblesView : GtkClutter.Embed
             warning ("Failed to load textures: %s", e.message);
         }
 
+        load_pixmap ();
+
         stage.add_child (surface);
     }
+
+    public void new_level (int level)
+    {
+        string level_name;
+        string filename;
+
+        level_name = "level%03d.gnl".printf (level);
+        filename = Path.build_filename (DATADIR, "levels", level_name, null);
+
+        FileStream file;
+        if ((file = FileStream.open (filename, "r")) == null) {
+            string message =
+                (_("Nibbles couldn't load level file:\n%s\n\n" +
+                   "Please check your Nibbles installation")).printf (filename);
+            var dialog = new Gtk.MessageDialog (null,
+                                                Gtk.DialogFlags.MODAL,
+                                                Gtk.MessageType.ERROR,
+                                                Gtk.ButtonsType.OK,
+                                                message);
+            dialog.run ();
+            dialog.destroy ();
+            Posix.exit (Posix.EXIT_FAILURE);
+        }
+    }
+
+    private Gdk.Pixbuf load_pixmap_file (string pixmap, int xsize, int ysize)
+    {
+        var filename = Path.build_filename (DATADIR, "pixmaps", pixmap, null);
+
+        if (filename == null)
+        {
+            string message =
+                (_("Nibbles couldn't find pixmap file:\n%s\n\n" +
+                   "Please check your Nibbles installation")).printf (pixmap);
+                var dialog = new Gtk.MessageDialog (null,
+                                                    Gtk.DialogFlags.MODAL,
+                                                    Gtk.MessageType.ERROR,
+                                                    Gtk.ButtonsType.OK,
+                                                    message);
+                dialog.run ();
+                dialog.destroy ();
+                Posix.exit (Posix.EXIT_FAILURE);
+        }
+
+        Gdk.Pixbuf image = null;
+        try
+        {
+            image = new Gdk.Pixbuf.from_file_at_scale (filename, xsize, ysize, true);
+        }
+        catch (GLib.Error e)
+        {
+            warning ("Failed to load pixmap file: %s", e.message);
+        }
+        return image;
+    }
+
+    private void load_pixmap ()
+    {
+        string[] bonus_files = {
+            "diamond.svg",
+            "bonus1.svg",
+            "bonus2.svg",
+            "life.svg",
+            "bonus3.svg",
+            "bonus4.svg",
+            "bonus5.svg",
+            "questionmark.svg"
+        };
+
+        string[] small_files = {
+            "wall-straight-up.svg",
+            "wall-straight-side.svg",
+            "wall-corner-bottom-left.svg",
+            "wall-corner-bottom-right.svg",
+            "wall-corner-top-left.svg",
+            "wall-corner-top-right.svg",
+            "wall-tee-up.svg",
+            "wall-tee-right.svg",
+            "wall-tee-left.svg",
+            "wall-tee-down.svg",
+            "wall-cross.svg"
+        };
+
+        string[] worm_files = {
+            "snake-red.svg",
+            "snake-green.svg",
+            "snake-blue.svg",
+            "snake-yellow.svg",
+            "snake-cyan.svg",
+            "snake-magenta.svg",
+            "snake-grey.svg"
+        };
+
+        int tile_size = game.properties.tile_size;
+        for (int i = 0; i < 8; i++) {
+            boni_pixmaps[i] = load_pixmap_file (bonus_files[i],
+                                                2 * tile_size, 2 * tile_size);
+        }
+
+        for (int i = 0; i < 11; i++) {
+            wall_pixmaps[i] = load_pixmap_file (small_files[i],
+                                                2 * tile_size, 2 * tile_size);
+        }
+
+        for (int i = 0; i < 7; i++) {
+            worm_pixmaps[i] = load_pixmap_file (worm_files[i],
+                                                tile_size, tile_size);
+        }
+    }
 }
diff --git a/src/properties.vala b/src/properties.vala
index 301f2c5..8b32249 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -1,6 +1,7 @@
 public class Properties : Object
 {
     public int tile_size;
+    public int start_level;
 
     public Properties ()
     {
@@ -10,10 +11,12 @@ public class Properties : Object
     public void update_settings (GLib.Settings settings)
     {
         settings.set_int ("tile-size", tile_size);
+        settings.set_int ("start-level", start_level);
     }
 
     public void update_properties (GLib.Settings settings)
     {
         tile_size = settings.get_int ("tile-size");
+        start_level = settings.get_int ("start-level");
     }
 }


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