From b0eb97fa5328d4f8154c931397c66a0a3333ae62 Mon Sep 17 00:00:00 2001 From: Fussmatte Date: Fri, 17 Feb 2023 16:02:30 -0500 Subject: [PATCH] Added custom sound effect functionality to `playef()` --- desktop_version/src/Music.cpp | 40 ++++++++++++++++++++++++++++++++++ desktop_version/src/Music.h | 2 ++ desktop_version/src/Script.cpp | 9 +++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index 9c9ee0aa..b7b676a0 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -121,6 +121,7 @@ public: format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign; format.cbSize = 0; valid = true; + name = std::string(SDL_strrchr(fileName, '/') + 1); end: VVV_free(mem); } @@ -244,6 +245,8 @@ end: static FAudioSourceVoice** voices; static float volume; + + std::string name; }; FAudioSourceVoice** SoundTrack::voices = NULL; float SoundTrack::volume = 0.0f; @@ -657,6 +660,29 @@ void musicclass::init(void) soundTracks.push_back(SoundTrack( "sounds/trophy.wav" )); soundTracks.push_back(SoundTrack( "sounds/rescue.wav" )); + //Here's where we find all the custom sounds in a level's assets folder + EnumHandle handle = {}; + const char* item; + while ((item = FILESYSTEM_enumerateAssets("sounds/", &handle)) != NULL) + { + const std::string str_item = item; + bool match; + for (int j = 0; j < soundTracks.size(); j++) + { + match = (str_item == soundTracks[j].name); + if (match) + { + break; + } + } + if (!match) + { + soundTracks.push_back(SoundTrack( ("sounds/" + str_item).c_str() )); + } + } + FILESYSTEM_freeEnumerate(&handle); + + #ifdef VVV_COMPILEMUSIC binaryBlob musicWriteBlob; #define FOREACH_TRACK(blob, track_name) blob.AddFileToBinaryBlob("data/" track_name); @@ -1144,6 +1170,20 @@ void musicclass::playef(int t) soundTracks[t].Play(); } +void musicclass::playef_name(std::string& t) +{ + for (int i = 0; i < soundTracks.size(); i++) + { + size_t lastindex = soundTracks[i].name.find_last_of('.'); + std::string rawname = soundTracks[i].name.substr(0, lastindex); + if (t == rawname) + { + soundTracks[i].Play(); + return; + } + } +} + void musicclass::pauseef(void) { SoundTrack::Pause(); diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index ce2aed30..c7d4ebad 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -2,6 +2,7 @@ #define MUSIC_H #include "BinaryBlob.h" +#include #define musicroom(rx, ry) ((rx) + ((ry) * 20)) @@ -38,6 +39,7 @@ public: int currentsong; void playef(int t); + void playef_name(std::string& t); void pauseef(void); void resumeef(void); diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index 9e710b43..4bb0d240 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -355,7 +355,14 @@ void scriptclass::run(void) } if (words[0] == "playef") { - music.playef(ss_toi(words[1])); + if (words[1] != "0" && ss_toi(words[1]) == 0) + { + music.playef_name(raw_words[1]); + } + else + { + music.playef(ss_toi(words[1])); + } } if (words[0] == "play") {