glkterm icon indicating copy to clipboard operation
glkterm copied to clipboard

Add sound support

Open townba opened this issue 4 years ago • 1 comments

Some notes:

  • I've tested this with some games that use sound (like Infocom's Sherlock: The Riddle of the Crown Jewels) and some custom and existing test files; in particular, angstsmurf's soundtest was particularly helpful.

  • Sound support is provided by SDL2 and SDL2_mixer.

  • It reads sound resources from Blorb files or, if not using Blorb, SND files (SND3, SND4, etc.) if it can find them. In particular, it remembers locations of opened file streams so it can try to use those to get a hint about where those files might be.

  • It uses queue.h to help with tail queues.

  • This should be fully thread-safe. SDL2_mixer callbacks often come in on non-main threads, so the code ensures that things like Glk notifications and queued events are handled on the main thread properly.

  • Bocfel requires very light editing of its source to look for GLK_MODULE_SOUND rather than GARGLK in a few places.

townba avatar Apr 19 '20 10:04 townba

Bocfel changes to use this (note that this includes changes for both pull requests erkyrath/glkterm/pull/10 and erkyrath/glkterm/pull/11, but it shouldn't break anything if you're only trying one, or perhaps even neither):

diff --git a/screen.c b/screen.c
index df5fb0f..b6678fe 100644
--- a/screen.c
+++ b/screen.c
@@ -141,7 +141,7 @@ struct input
 
 #ifndef ZTERP_GLK
 static int16_t fg_color = 1, bg_color = 1;
-#elif defined(GARGLK)
+#elif defined(GLK_MODULE_GARGLKTEXT)
 static glui32 zcolor_map[] = {
   zcolor_Default,
 
@@ -167,7 +167,7 @@ void update_color(int which, unsigned long color)
 }
 #endif
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
 /* Idea from Nitfol. */
 static const int style_map[] =
 {
@@ -205,7 +205,7 @@ void set_current_style(void)
 #ifdef ZTERP_GLK
   if(curwin->id == NULL) return;
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   if(use_fixed_font()) style |= STYLE_FIXED;
 
   if(options.disable_fixed) style &= ~STYLE_FIXED;
@@ -1133,7 +1133,7 @@ void zget_cursor(void)
 void zset_colour(void)
 {
   /* Glk (apart from Gargoyle) has no color support. */
-#if !defined(ZTERP_GLK) || defined(GARGLK)
+#if !defined(ZTERP_GLK) || defined(GLK_MODULE_GARGLKTEXT)
   int16_t fg = as_signed(zargs[0]), bg = as_signed(zargs[1]);
 
   /* In V6, each window has its own color settings.  Since multiple
@@ -1145,7 +1145,7 @@ void zset_colour(void)
   if(options.disable_color) return;
 
   /* XXX -1 is a valid color in V6. */
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   if(fg >= 1 && fg <= (zversion >= 5 ? 12 : 9)) fg_color = zcolor_map[fg - 1];
   if(bg >= 1 && bg <= (zversion >= 5 ? 12 : 9)) bg_color = zcolor_map[bg - 1];
 #else
@@ -1157,7 +1157,7 @@ void zset_colour(void)
 #endif
 }
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
 /* Convert a 15-bit color to a 24-bit color. */
 static glui32 convert_color(unsigned long color)
 {
@@ -1177,7 +1177,7 @@ static glui32 convert_color(unsigned long color)
 
 void zset_true_colour(void)
 {
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   long fg = as_signed(zargs[0]), bg = as_signed(zargs[1]);
 
   if     (fg >=  0) fg_color = convert_color(fg);
@@ -1924,7 +1924,7 @@ void zshow_status(void)
 
   glk_window_get_size(statuswin.id, &width, &height);
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   garglk_set_reversevideo(1);
 #else
   glk_set_style(style_Alert);
@@ -2302,7 +2302,7 @@ void zbuffer_screen(void)
   store(0);
 }
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
 /* Glk does not guarantee great control over how various styles are
  * going to look, but Gargoyle does.  Abusing the Glk “style hints”
  * functions allows for quite fine-grained control over style
@@ -2338,7 +2338,7 @@ bool create_mainwin(void)
   have_unicode = glk_gestalt(gestalt_Unicode, 0);
 #endif
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   set_default_styles();
 
   /* Bold */
@@ -2459,7 +2459,7 @@ void init_screen(void)
 
   stop_timer();
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
   fg_color = zcolor_Default;
   bg_color = zcolor_Default;
 #endif
diff --git a/screen.h b/screen.h
index 40f12c1..581c3ce 100644
--- a/screen.h
+++ b/screen.h
@@ -41,7 +41,7 @@ void term_keys_reset(void);
 void term_keys_add(uint8_t);
 #endif
 
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
 void update_color(int, unsigned long);
 #endif
 
diff --git a/sound.c b/sound.c
index 08591a9..b225c6f 100644
--- a/sound.c
+++ b/sound.c
@@ -8,7 +8,7 @@
 
 #ifdef ZTERP_GLK
 #include <glk.h>
-#ifdef GARGLK
+#ifdef GLK_MODULE_SOUND
 #include <glkstart.h>
 #include <gi_blorb.h>
 static schanid_t sound_channel = NULL;
@@ -23,7 +23,7 @@ static schanid_t sound_channel = NULL;
  */
 void init_sound(bool story_is_blorb)
 {
-#ifdef GARGLK
+#ifdef GLK_MODULE_SOUND
   strid_t file = NULL;
 
   if(sound_loaded()) return;
@@ -58,14 +58,14 @@ void init_sound(bool story_is_blorb)
   if(file != NULL)
   {
     giblorb_set_resource_map(file);
-    sound_channel = glk_schannel_create(0);
   }
+  sound_channel = glk_schannel_create(0);
 #endif
 }
 
 bool sound_loaded(void)
 {
-#ifdef GARGLK
+#ifdef GLK_MODULE_SOUND
   return sound_channel != NULL;
 #else
   return false;
@@ -74,7 +74,7 @@ bool sound_loaded(void)
 
 void zsound_effect(void)
 {
-#ifdef GARGLK
+#ifdef GLK_MODULE_SOUND
   uint8_t repeats, volume;
   static uint32_t vols[8] = {
     0x02000, 0x04000, 0x06000, 0x08000,
diff --git a/util.c b/util.c
index 2fcee09..9eaa201 100644
--- a/util.c
+++ b/util.c
@@ -30,6 +30,7 @@
 
 #ifdef ZTERP_GLK
 #include <glk.h>
+#include <glkstart.h>
 #endif
 
 #ifndef ZTERP_NO_SAFETY_CHECKS
@@ -87,7 +88,11 @@ void help(void)
   const struct
   {
     const char *flag;
+#ifdef ZTERP_GLK
+    int arg;
+#else
     enum { glkunix_arg_NoValue, glkunix_arg_NumberValue, glkunix_arg_ValueFollows } arg;
+#endif
     const char *description;
   }
   flags[] = {
@@ -297,4 +302,7 @@ void process_arguments(int argc, char **argv)
 
   /* Just ignore excess stories for now. */
   if(zoptind < argc) game_file = argv[zoptind];
+#ifdef ZTERP_GLK
+  glkunix_set_base_file((char *)game_file);
+#endif
 }
diff --git a/zterp.c b/zterp.c
index d49ee50..12d8d36 100644
--- a/zterp.c
+++ b/zterp.c
@@ -293,7 +293,7 @@ static void read_config(void)
 #define NUMBER(name)	else if(strcmp(key, #name) == 0) options.name = n
 #define STRING(name)	else if(strcmp(key, #name) == 0) do { free(options.name); options.name = xstrdup(val); } while(0)
 #define CHAR(name)	else if(strcmp(key, #name) == 0) options.name = val[0]
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
 #define COLOR(name, num)else if(strcmp(key, "color_" #name) == 0) update_color(num, strtol(val, NULL, 16))
 #else
 #define COLOR(name, num)else if(0)
@@ -399,7 +399,7 @@ void write_header(void)
 
 #ifdef ZTERP_GLK
     if(glk_gestalt(gestalt_Timer, 0)) flags1 |= FLAGS1_TIMED;
-#ifdef GARGLK
+#ifdef GLK_MODULE_GARGLKTEXT
     if(zversion >= 5) flags1 |= FLAGS1_COLORS;
 #endif
 #else
@@ -899,6 +899,7 @@ int main(int argc, char **argv)
     if(ref != NULL)
     {
       game_file = xstrdup(garglk_fileref_get_name(ref));
+      glkunix_set_base_file(game_file);
       glk_fileref_destroy(ref);
     }
   }

townba avatar Apr 19 '20 11:04 townba