This commit is contained in:
Nyako 2025-06-19 21:45:32 +01:00 committed by GitHub
commit 10e5903997
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 114 additions and 7 deletions

View file

@ -372,7 +372,7 @@ float SoundTrack::volume = 0.0f;
class MusicTrack
{
public:
MusicTrack(SDL_RWops *rw)
MusicTrack(SDL_RWops *rw, const char* _id, bool _loose_extra)
{
SDL_zerop(this);
read_buf = (Uint8*) SDL_malloc(rw->size(rw));
@ -380,6 +380,8 @@ public:
int err;
stb_vorbis_info vorbis_info;
stb_vorbis_comment vorbis_comment;
id = SDL_strdup(_id);
loose_extra = _loose_extra;
vorbis = stb_vorbis_open_memory(read_buf, rw->size(rw), &err, NULL);
if (vorbis == NULL)
{
@ -418,6 +420,7 @@ end:
VVV_free(read_buf);
VVV_free(decoded_buf_playing);
VVV_free(decoded_buf_reserve);
VVV_free(id);
if (!IsHalted())
{
VVV_freefunc(FAudioVoice_DestroyVoice, musicVoice);
@ -526,6 +529,8 @@ end:
Uint8* decoded_buf_playing;
Uint8* decoded_buf_reserve;
Uint8* read_buf;
char* id;
bool loose_extra;
bool shouldloop;
bool valid;
@ -809,7 +814,7 @@ void musicclass::init(void)
} \
else \
{ \
musicTracks.push_back(MusicTrack(rw)); \
musicTracks.push_back(MusicTrack(rw, track_name, false)); \
} \
}
@ -829,11 +834,11 @@ void musicclass::init(void)
rw = PHYSFSRWOPS_openRead(track_name); \
if (rw == NULL) \
{ \
vlog_error("Unable to read loose music file: %s", SDL_GetError()); \
vlog_error("Unable to read extra loose music file: %s", SDL_GetError()); \
} \
else \
{ \
musicTracks.push_back(MusicTrack(rw)); \
musicTracks.push_back(MusicTrack(rw, track_name, false)); \
}
TRACK_NAMES(_)
@ -859,7 +864,7 @@ void musicclass::init(void)
while (mmmmmm_blob.nextExtra(&index_))
{
rw = SDL_RWFromConstMem(mmmmmm_blob.getAddress(index_), mmmmmm_blob.getSize(index_));
musicTracks.push_back(MusicTrack( rw ));
musicTracks.push_back(MusicTrack( rw, mmmmmm_blob.m_headers[index_].name, false));
num_mmmmmm_tracks++;
index_++;
@ -881,11 +886,59 @@ void musicclass::init(void)
while (pppppp_blob.nextExtra(&index_))
{
rw = SDL_RWFromConstMem(pppppp_blob.getAddress(index_), pppppp_blob.getSize(index_));
musicTracks.push_back(MusicTrack( rw ));
musicTracks.push_back(MusicTrack( rw, pppppp_blob.m_headers[index_].name, false));
num_pppppp_tracks++;
index_++;
}
EnumHandle handle = {};
const char* item;
while ((item = FILESYSTEM_enumerateAssets("music", &handle)) != NULL)
{
char asset_filename[256];
char id[256];
SDL_snprintf(asset_filename, sizeof(asset_filename), "music/%s", item);
// We need an ID, so chop off the extension
SDL_strlcpy(id, item, sizeof(id));
char* dot = SDL_strrchr(id, '.');
if (dot != NULL)
{
*dot = '\0';
}
if (idexists(id))
{
// Make sure we haven't already loaded this file
continue;
}
vlog_info("Reading loose music file %s as %s", item, id);
unsigned char* mem;
size_t len;
FILESYSTEM_loadAssetToMemory(asset_filename, &mem, &len);
if (mem == NULL)
{
vlog_error("Unable to load loose music file to memory: %s", SDL_GetError());
}
else
{
rw = SDL_RWFromConstMem(mem, len);
if (rw == NULL)
{
vlog_error("Unable to read loose music file from memory: %s", SDL_GetError());
}
else
{
musicTracks.push_back(MusicTrack(rw, id, true));
num_pppppp_tracks++;
}
VVV_free(mem);
}
}
FILESYSTEM_freeEnumerate(&handle);
}
void musicclass::destroy(void)
@ -1002,6 +1055,49 @@ void musicclass::play(int t)
}
}
void musicclass::playid(const char* id)
{
for (size_t i = 0; i < musicTracks.size(); i++)
{
if (SDL_strcmp(musicTracks[i].id, id) == 0)
{
play(i);
return;
}
}
vlog_error("playid() couldn't find music ID: %s", id);
}
bool musicclass::idexists(const char* id)
{
for (size_t i = 0; i < musicTracks.size(); i++)
{
if (SDL_strcmp(musicTracks[i].id, id) == 0)
{
return true;
}
}
return false;
}
bool musicclass::isextra(int t)
{
if (INBOUNDS_VEC(t, musicTracks))
{
return musicTracks[t].loose_extra;
}
return false;
}
const char* musicclass::getid(int t)
{
if (INBOUNDS_VEC(t, musicTracks))
{
return musicTracks[t].id;
}
return NULL;
}
void musicclass::resume(void)
{
if (currentsong == -1)

View file

@ -74,6 +74,10 @@ public:
void set_sound_volume(int volume);
void play(int t);
void playid(const char* id);
bool idexists(const char* id);
bool isextra(int t);
const char* getid(int t);
void resume(void);
void resumefade(const int fadein_ms);
void pause(void);

View file

@ -451,7 +451,14 @@ void scriptclass::run(void)
}
if (words[0] == "play")
{
music.play(ss_toi(words[1]));
if (music.idexists(words[1].c_str()))
{
music.playid(words[1].c_str());
}
else if (!music.isextra(ss_toi(words[1])))
{
music.play(ss_toi(words[1]));
}
}
if (words[0] == "stopmusic")
{