[gtk/test-coverage: 6/7] gtk-demo: Add a winning sound to the puzzle




commit f7811f5dc3c8909114a21f8af4a0e631290bd6dd
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 16 08:16:37 2022 -0400

    gtk-demo: Add a winning sound to the puzzle
    
    We should celebrate every win.

 demos/gtk-demo/sliding_puzzle.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
---
diff --git a/demos/gtk-demo/sliding_puzzle.c b/demos/gtk-demo/sliding_puzzle.c
index b65bc428fb..592fe0fde2 100644
--- a/demos/gtk-demo/sliding_puzzle.c
+++ b/demos/gtk-demo/sliding_puzzle.c
@@ -5,6 +5,7 @@
  * small sliding puzzle game.
  */
 
+#include "config.h"
 #include <gtk/gtk.h>
 
 /* Include the header for the puzzle piece */
@@ -24,6 +25,30 @@ static guint height = 3;
 static guint pos_x;
 static guint pos_y;
 
+static void
+ended (GObject *object)
+{
+  g_object_unref (object);
+}
+
+static void
+celebrate (gboolean win)
+{
+  char *path;
+  GtkMediaStream *stream;
+
+  if (win)
+    path = g_build_filename (GTK_DATADIR, "sounds", "freedesktop", "stereo", "complete.oga", NULL);
+  else
+    path = g_build_filename (GTK_DATADIR, "sounds", "freedesktop", "stereo", "dialog-error.oga", NULL);
+  stream = gtk_media_file_new_for_filename (path);
+  gtk_media_stream_set_volume (stream, 1.0);
+  gtk_media_stream_play (stream);
+
+  g_signal_connect (stream, "notify::ended", G_CALLBACK (ended), NULL);
+  g_free (path);
+}
+
 static gboolean
 move_puzzle (GtkWidget *grid,
              int        dx,
@@ -157,6 +182,8 @@ check_solved (GtkWidget *grid)
   picture = gtk_grid_get_child_at (GTK_GRID (grid), pos_x, pos_y);
   gtk_picture_set_paintable (GTK_PICTURE (picture), piece);
 
+  celebrate (TRUE);
+
   return TRUE;
 }
 


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