[gnome-robots] encapsulate sound routines into SoundPlayer class



commit 90a906c7548d820a4cf2212d42a40becfc0cb3d7
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Sep 27 12:23:24 2020 +0200

    encapsulate sound routines into SoundPlayer class

 src/game-area.vala                    | 13 +++++-
 src/meson.build                       |  2 +-
 src/robots.vala                       |  4 +-
 src/{sound.vala => sound-player.vala} | 78 +++++++++++++++++------------------
 4 files changed, 54 insertions(+), 43 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 5cac1cb..f3bbe96 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -77,13 +77,16 @@ public class GameArea : DrawingArea {
         }
     }
 
+    private SoundPlayer sound_player;
+
     public signal void updated (Game game);
 
     public GameArea (Game game,
                      Theme theme,
                      Bubble aieee_bubble,
                      Bubble yahoo_bubble,
-                     Bubble splat_bubble
+                     Bubble splat_bubble,
+                     SoundPlayer sound_player
     ) {
         this.game = game;
         this.theme = theme;
@@ -91,6 +94,7 @@ public class GameArea : DrawingArea {
         this.aieee_bubble = aieee_bubble;
         this.yahoo_bubble = yahoo_bubble;
         this.splat_bubble = splat_bubble;
+        this.sound_player = sound_player;
 
         add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | 
Gdk.EventMask.POINTER_MOTION_MASK);
         configure_event.connect (event => resize_cb (event));
@@ -333,6 +337,13 @@ public class GameArea : DrawingArea {
         }
     }
 
+
+    private void play_sound (Sound sound) {
+        if (properties_sound ()) {
+            sound_player.play (sound);
+        }
+    }
+
     /**
      * Displays a modal dialog box with a given message
      **/
diff --git a/src/meson.build b/src/meson.build
index 730bfa8..dfa8035 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -18,7 +18,7 @@ sources = files(
     'animation.vala',
     'controls.vala',
     'game-config.vala',
-    'sound.vala',
+    'sound-player.vala',
     'cursors.vala',
     'properties.vala',
     'graphics.vala',
diff --git a/src/robots.vala b/src/robots.vala
index cf6146f..0bbee8f 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -343,11 +343,13 @@ class RobotsApplication : Gtk.Application {
             Bubble yahoo_bubble = new Bubble.from_data_file ("yahoo.png");
             Bubble aieee_bubble = new Bubble.from_data_file ("aieee.png");
             Bubble splat_bubble = new Bubble.from_data_file ("splat.png");
+            SoundPlayer sound_player = new SoundPlayer ();
             return new GameArea (game,
                                  theme,
                                  aieee_bubble,
                                  yahoo_bubble,
-                                 splat_bubble);
+                                 splat_bubble,
+                                 sound_player);
         } catch (Error e) {
             critical ("%s", e.message);
             // TODO message box
diff --git a/src/sound.vala b/src/sound-player.vala
similarity index 50%
rename from src/sound.vala
rename to src/sound-player.vala
index 49b1960..ca82923 100644
--- a/src/sound.vala
+++ b/src/sound-player.vala
@@ -22,68 +22,66 @@ public enum Sound {
     DIE,
     TELEPORT,
     SPLAT,
-    BAD,
     YAHOO,
+    BAD,
 }
 
-GSound.Context ctx = null;
+public class SoundPlayer {
 
-void create_context () {
-    if (ctx != null) {
-        return;
-    }
+    private GSound.Context ctx;
 
-    try {
-        ctx = new GSound.Context ();
-    } catch (Error error) {
-        warning ("Failed to create gsound context: %s", error.message);
+    public SoundPlayer () {
+        try {
+            ctx = new GSound.Context ();
+        } catch (Error error) {
+            warning ("Failed to create gsound context: %s", error.message);
+            ctx = null;
+        }
     }
-}
 
-void play_sound_file (string name) {
-    create_context ();
-    if (ctx == null) {
-        return;
+    public void beep () {
+        var display = Gdk.Display.get_default ();
+        if (display != null) {
+            display.beep ();
+        }
     }
 
-    var filename = "%s.ogg".printf (name);
-    var path = Path.build_filename (SOUND_DIRECTORY, filename);
+    public void play_file (string name) {
+        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);
+        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 ()) {
+    public void play (Sound sound) {
         switch (sound) {
         case Sound.VICTORY:
-            play_sound_file ("victory");
+            play_file ("victory");
             break;
         case Sound.DIE:
-            play_sound_file ("die");
+            play_file ("die");
             break;
         case Sound.TELEPORT:
-            play_sound_file ("teleport");
+            play_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 ();
-            }
+            play_file ("splat");
             break;
         case Sound.YAHOO:
-            play_sound_file ("yahoo");
+            play_file ("yahoo");
+            break;
+        case Sound.BAD:
+            beep ();
             break;
         }
     }


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