foxygit / fake-08-xbox360 Log in
commit aebd6b9648d1c450910ad79c78f9280dfbd20b25
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Jul 23 19:52:04 2023 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun Jul 23 19:52:04 2023 -0600

    Better fix for audio channel -2 (stop sfx)
---
 source/Audio.cpp    | 12 +++++++++++-
 test/audiotests.cpp | 13 +++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index 314cc26..c82133e 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -46,7 +46,17 @@ audioState_t* Audio::getAudioState() {

 void Audio::api_sfx(int sfx, int channel, int offset){

-    if (sfx < -2 || sfx > 63 || channel < -1 || channel > 3 || offset > 31) {
+    if (sfx < -2 || sfx > 63 || channel < -2 || channel > 3 || offset > 31) {
+        return;
+    }
+
+    //CHANNEL -2: to stop the given sound from playing on any channel
+    if (channel == -2) {
+        for(int i = 0; i < 4; i++) {
+            if (_audioState._sfxChannels[i].sfxId == sfx) {
+                _audioState._sfxChannels[i].sfxId = -1;
+            }
+        }
         return;
     }

diff --git a/test/audiotests.cpp b/test/audiotests.cpp
index 4e01946..1decba9 100644
--- a/test/audiotests.cpp
+++ b/test/audiotests.cpp
@@ -188,6 +188,19 @@ TEST_CASE("audio class behaves as expected") {

         CHECK_EQ(audioState->_sfxChannels[2].sfxId, 5);
     }
+    SUBCASE("api_sfx() with -2 channel stops the sfx on any channel") {
+        audio->api_sfx(1, 0, 0);
+        audio->api_sfx(10, 1, 0);
+        audio->api_sfx(20, 2, 0);
+        audio->api_sfx(1, 3, 0);
+        audio->api_sfx(1, -2, 0);
+
+
+        CHECK_EQ(audioState->_sfxChannels[0].sfxId, -1);
+        CHECK_EQ(audioState->_sfxChannels[1].sfxId, 10);
+        CHECK_EQ(audioState->_sfxChannels[2].sfxId, 20);
+        CHECK_EQ(audioState->_sfxChannels[3].sfxId, -1);
+    }
     SUBCASE("api_sfx() -2 sfx id stops looping") {
         audioState->_sfxChannels[3].can_loop = true;