gtk-demo: Add a winning sound to the puzzle
authorMatthias Clasen <mclasen@redhat.com>
Sat, 16 Jul 2022 12:16:37 +0000 (08:16 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 16 Jul 2022 13:43:46 +0000 (09:43 -0400)
We should celebrate every win.

demos/gtk-demo/sliding_puzzle.c

index b65bc428fbc9da5bc692c1e2bda910ac50be991b..592fe0fde282b7f66d33b68bb998e5849c7a751b 100644 (file)
@@ -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;
 }