game-music-emu: WIP: Add a function to avoid automatic track ending.

From 0cf7cbbd5e7342ae44f7a2d550c3ca0e90ee96eb Mon Sep 17 00:00:00 2001
From: Michael Pyne <[EMAIL REDACTED]>
Date: Thu, 13 Jun 2019 23:04:35 -0400
Subject: [PATCH] WIP: Add a function to avoid automatic track ending.

This maintains automatic track ending as the default to address issue #25
while making it easier to restore the previous default behavior of
having music loop infinitely.

To maintain binary compatibility on Music_Emu and its subclasses, I make
this a global library setting that can be queried and set (or reset),
instead of specific to a Music_Emu class.

This should resolve issue #29.
---
 gme/Spc_Emu.cpp |  2 +-
 gme/gme.cpp     | 12 ++++++++++++
 gme/gme.h       | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/gme/Spc_Emu.cpp b/gme/Spc_Emu.cpp
index 35086ce..a1fdfc1 100644
--- a/gme/Spc_Emu.cpp
+++ b/gme/Spc_Emu.cpp
@@ -302,7 +302,7 @@ blargg_err_t Spc_Emu::start_track_( int track )
 	track_info_t spc_info;
 	RETURN_ERR( track_info_( &spc_info, track ) );
 	// Set a default track length, need a non-zero fadeout
-	if ( spc_info.length > 0 )
+	if ( gme_autoload_playback_limit() && ( spc_info.length > 0 ) )
 		set_fade ( spc_info.length, 50 );
 	return 0;
 }
diff --git a/gme/gme.cpp b/gme/gme.cpp
index 292cbff..a9611ed 100644
--- a/gme/gme.cpp
+++ b/gme/gme.cpp
@@ -197,6 +197,18 @@ BLARGG_EXPORT gme_err_t gme_open_file( const char* path, Music_Emu** out, int sa
 	return err;
 }
 
+static int gme_global_autoload_playback_limit = 1;
+
+void gme_set_autoload_playback_limit( int do_autoload_limit )
+{
+	gme_global_autoload_playback_limit = (do_autoload_limit != 0);
+};
+
+int gme_autoload_playback_limit( void )
+{
+	return gme_global_autoload_playback_limit;
+}
+
 // Used to implement gme_new_emu and gme_new_emu_multi_channel
 Music_Emu* gme_internal_new_emu_( gme_type_t type, int rate, bool multi_channel )
 {
diff --git a/gme/gme.h b/gme/gme.h
index 80c6ce8..aa623d8 100644
--- a/gme/gme.h
+++ b/gme/gme.h
@@ -41,6 +41,22 @@ void gme_delete( Music_Emu* );
 Fade time can be changed while track is playing. */
 void gme_set_fade( Music_Emu*, int start_msec );
 
+/**
+ * On supported emulators, automatically load track length metadata (if present)
+ * and terminate playback once the track length has been reached. This uses
+ * gme_set_fade behind the scenes.
+ *
+ * By default, playback limits are loaded and applied.
+ *
+ * @since 0.6.2
+ */
+void gme_set_autoload_playback_limit( int do_autoload_limit );
+
+/** See gme_set_autoload_playback_limit.
+ * @since 0.6.2
+ */
+int gme_autoload_playback_limit( void );
+
 /* True if a track has reached its end */
 int gme_track_ended( Music_Emu const* );