[gnome-robots] Rewrite sound module in Vala



commit 90f782502e18ce04659e37a6eb420e1a9b876387
Author: Andrey Kutejko <andy128k gmail com>
Date:   Mon Aug 24 22:58:56 2020 +0200

    Rewrite sound module in Vala

 src/game.c         |   1 -
 src/gnome-robots.c |   3 --
 src/main.vapi      |   2 +
 src/meson.build    |   9 ++--
 src/sound.c        | 123 -----------------------------------------------------
 src/sound.h        |  25 -----------
 src/sound.vala     |  92 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 98 insertions(+), 157 deletions(-)
---
diff --git a/src/game.c b/src/game.c
index 4c61986..0d967df 100644
--- a/src/game.c
+++ b/src/game.c
@@ -32,7 +32,6 @@
 #include "keyboard.h"
 #include "game.h"
 #include "gnome-robots.h"
-#include "sound.h"
 #include "properties.h"
 #include "graphics.h"
 #include "cursors.h"
diff --git a/src/gnome-robots.c b/src/gnome-robots.c
index b42bccc..493d326 100644
--- a/src/gnome-robots.c
+++ b/src/gnome-robots.c
@@ -36,7 +36,6 @@
 #include "gbdefs.h"
 #include "riiv.h"
 #include "graphics.h"
-#include "sound.h"
 #include "properties.h"
 #include "game.h"
 #include "cursors.h"
@@ -489,8 +488,6 @@ activate (GtkApplication *app, gpointer user_data)
     exit (1);
   }
 
-  init_sound ();
-
   init_game ();
 
   g_settings_sync ();
diff --git a/src/main.vapi b/src/main.vapi
new file mode 100644
index 0000000..cc958b0
--- /dev/null
+++ b/src/main.vapi
@@ -0,0 +1,2 @@
+public bool properties_sound ();
+
diff --git a/src/meson.build b/src/meson.build
index 3342577..4716956 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -6,22 +6,22 @@ config_header = configure_file(
     configuration: config_h
 )
 
-vapi_sources = files(
+vala_sources = files(
     'config.vapi',
-)
+    'main.vapi',
 
-vala_sources = files(
     'image-suffix-list.vala',
     'file-list.vala',
     'find-file.vala',
     'preimage.vala',
     'controls.vala',
     'game-config.vala',
+    'sound.vala',
 )
 
 vala_lib = static_library('riiv',
     config_header,
-    vapi_sources + vala_sources,
+    vala_sources,
     dependencies: [
         gee_dependency,
         gio_dependency,
@@ -45,7 +45,6 @@ sources = files(
     'graphics.c',
     'keyboard.c',
     'properties.c',
-    'sound.c'
 )
 resources = gnome.compile_resources(
     'resources',
diff --git a/src/sound.vala b/src/sound.vala
new file mode 100644
index 0000000..d4586b0
--- /dev/null
+++ b/src/sound.vala
@@ -0,0 +1,92 @@
+/*
+ * Gnome Robots II
+ * written by Mark Rae <m rae inpharmatica co uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+public enum Sound {
+    VICTORY = 0,
+    DIE,
+    TELEPORT,
+    SPLAT,
+    BAD,
+    YAHOO,
+}
+
+GSound.Context ctx = null;
+
+void create_context () {
+    if (ctx != null) {
+        return;
+    }
+
+    try {
+        ctx = new GSound.Context ();
+    } catch (Error error) {
+        warning ("Failed to create gsound context: %s", error.message);
+    }
+}
+
+void play_sound_file (string name) {
+    create_context ();
+    if (ctx == null) {
+        return;
+    }
+
+    var filename = "%s.ogg".printf (name);
+    var path = Path.build_filename (SOUND_DIRECTORY, filename);
+
+    try {
+        ctx.play_simple (null,
+                         GSound.Attribute.MEDIA_NAME, name,
+                         GSound.Attribute.MEDIA_FILENAME, path);
+    } catch (Error error) {
+        warning ("Failed to play sound \"%s\": %s", name, error.message);
+    }
+}
+
+/**
+ * Plays a game sound
+ **/
+public void play_sound (Sound sound) {
+    if (properties_sound ()) {
+        switch (sound) {
+        case Sound.VICTORY:
+            play_sound_file ("victory");
+            break;
+        case Sound.DIE:
+            play_sound_file ("die");
+            break;
+        case Sound.TELEPORT:
+            play_sound_file ("teleport");
+            break;
+        case Sound.SPLAT:
+            play_sound_file ("splat");
+            break;
+        case Sound.BAD:
+            var display = Gdk.Display.get_default ();
+            if (display != null) {
+                display.beep ();
+            }
+            break;
+        case Sound.YAHOO:
+            play_sound_file ("yahoo");
+            break;
+        }
+    }
+}
+


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