Skip to content

Commit

Permalink
Some cleanup in the music list code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 31, 2024
1 parent 4be7a82 commit e54ee72
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 207 deletions.
243 changes: 100 additions & 143 deletions src/common/FileList.cpp

Large diffs are not rendered by default.

82 changes: 39 additions & 43 deletions src/common/FileList.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,76 +104,72 @@ class SkinList {
};


class MusicOverride
{
class MusicOverride {
public:
std::vector<int> songs;
std::vector<size_t> songs;
};

class MusicEntry
{

class MusicEntry {
public:
MusicEntry(const std::string & musicdirectory);
~MusicEntry() {}
explicit MusicEntry(std::string name);

static std::unique_ptr<MusicEntry> load(const std::string& musicDirectory);

void UpdateWithOverrides();
void updateWithOverrides();

const std::string& name() const { return m_name; }
const std::string& music(size_t musicID) const;
const std::string& randomMusic(size_t categoryID, const std::string& mapName, const std::string& background);
const std::string& nextMusic(size_t categoryID, const std::string& mapName, const std::string& background);

std::string GetMusic(unsigned int musicID);
std::string GetRandomMusic(int iCategoryID, const char * szMapName, const char * szBackground);
std::string GetNextMusic(int iCategoryID, const char * szMapName, const char * szBackground);
private:
std::string m_name;
size_t m_currentMusic = 0;

int numsongsforcategory[MAXMUSICCATEGORY];
int songsforcategory[MAXMUSICCATEGORY][MAXCATEGORYTRACKS];
size_t numsongsforcategory[MAXMUSICCATEGORY];
size_t songsforcategory[MAXMUSICCATEGORY][MAXCATEGORYTRACKS];
std::vector<std::string> songFileNames;

std::map<std::string, MusicOverride*> mapoverride;
std::map<std::string, MusicOverride*> backgroundoverride;

std::string name;
unsigned short iCurrentMusic;

bool fError;

bool fUsesMapOverrides;
bool fUsesBackgroundOverrides;
};


class MusicList
{
public:
MusicList();
~MusicList();

std::string GetMusic(int musicID);
void SetRandomMusic(int iCategoryID, const char * szMapName, const char * szBackground);
void SetNextMusic(int iCategoryID, const char * szMapName, const char * szBackground);
std::string GetCurrentMusic();
const std::string& music(size_t musicID) const {
return m_entries[m_currentIndex]->music(musicID);
}
const std::string& currentMusic() const {
return m_currentMusic;
}
void setRandomMusic(size_t iCategoryID, const std::string& szMapName, const std::string& szBackground);
void setNextMusic(size_t iCategoryID, const std::string& szMapName, const std::string& szBackground);

int GetCurrentIndex() {
return currentIndex;
size_t currentIndex() const {
return m_currentIndex;
};
void SetCurrent(unsigned int index) {
if (index < entries.size())
currentIndex = index;
else
currentIndex = 0;
void setCurrent(size_t index) {
m_currentIndex = index < m_entries.size() ? index : 0;
};

const char * current_name() {
return entries[currentIndex]->name.c_str();
const std::string& currentName() const {
return m_entries[m_currentIndex]->name();
};

void next();
void prev();
void random() {
currentIndex = RANDOM_INT(entries.size());
};
void random();

void UpdateEntriesWithOverrides();
void updateEntriesWithOverrides();

private:
std::string CurrentMusic;
std::vector<MusicEntry*> entries;
int currentIndex;
std::string m_currentMusic;
std::vector<std::unique_ptr<MusicEntry>> m_entries;
size_t m_currentIndex = 0;
};


Expand Down
4 changes: 2 additions & 2 deletions src/common/GameValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ void CGameConfig::ReadBinaryConfig() {
}

announcerlist->setCurrentIndex(options.read_u8());
musiclist->SetCurrent(options.read_u8());
musiclist->setCurrent(options.read_u8());
worldmusiclist->setCurrent(options.read_u8());
soundpacklist->setCurrentIndex(options.read_u8());
menugraphicspacklist->setCurrentIndex(options.read_u8());
Expand Down Expand Up @@ -533,7 +533,7 @@ void CGameConfig::WriteConfig() const
options.write_raw(&playercontrol, sizeof(short) * 4);

options.write_u8(announcerlist->currentIndex());
options.write_u8(musiclist->GetCurrentIndex());
options.write_u8(musiclist->currentIndex());
options.write_u8(worldmusiclist->currentIndex());
options.write_u8(soundpacklist->currentIndex());
options.write_u8(menugraphicspacklist->currentIndex());
Expand Down
12 changes: 6 additions & 6 deletions src/smw/GSGameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,8 +1866,8 @@ void playMusic()
//If no background music is playing, then play some
if (!rm->backgroundmusic[0].isPlaying() && !rm->sfx_invinciblemusic.isPlaying() && !rm->sfx_timewarning.isPlaying() && !game_values.gamemode->gameover) {
if (game_values.playnextmusic) {
musiclist->SetNextMusic(g_map->musicCategoryID, maplist->currentShortmapname(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->GetCurrentMusic());
musiclist->setNextMusic(g_map->musicCategoryID, maplist->currentShortmapname(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->currentMusic());
}

rm->backgroundmusic[0].play(game_values.playnextmusic, false);
Expand All @@ -1880,8 +1880,8 @@ void PlayNextMusicTrack()
return;

rm->backgroundmusic[0].stop();
musiclist->SetNextMusic(g_map->musicCategoryID, maplist->currentShortmapname(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->GetCurrentMusic());
musiclist->setNextMusic(g_map->musicCategoryID, maplist->currentShortmapname(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->currentMusic());
rm->backgroundmusic[0].play(game_values.playnextmusic, false);
}

Expand Down Expand Up @@ -2384,8 +2384,8 @@ void start_gameplay()
//secretBoss();

if (game_values.music) {
musiclist->SetRandomMusic(g_map->musicCategoryID, "", "");
rm->backgroundmusic[0].load(musiclist->GetCurrentMusic());
musiclist->setRandomMusic(g_map->musicCategoryID, "", "");
rm->backgroundmusic[0].load(musiclist->currentMusic());
rm->backgroundmusic[0].play(game_values.playnextmusic, false);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/smw/GSMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,8 @@ void MenuState::update()
game_values.singleplayermode = -1;

if (game_values.music) {
musiclist->SetRandomMusic(g_map->musicCategoryID, sShortMapName.c_str(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->GetCurrentMusic());
musiclist->setRandomMusic(g_map->musicCategoryID, sShortMapName.c_str(), g_map->szBackgroundFile);
rm->backgroundmusic[0].load(musiclist->currentMusic());
rm->backgroundmusic[0].play(game_values.playnextmusic, false);
}
}
Expand Down Expand Up @@ -1605,11 +1605,11 @@ void MenuState::StartGame()
}

//Load soundtrack music if changed
if (game_values.loadedmusic != musiclist->GetCurrentIndex()) {
game_values.loadedmusic = (short)musiclist->GetCurrentIndex();
rm->backgroundmusic[1].load(musiclist->GetMusic(0)); //Stage Clear
rm->backgroundmusic[3].load(musiclist->GetMusic(2)); //Tournament Menu
rm->backgroundmusic[4].load(musiclist->GetMusic(3)); //Tournament Over
if (game_values.loadedmusic != musiclist->currentIndex()) {
game_values.loadedmusic = (short)musiclist->currentIndex();
rm->backgroundmusic[1].load(musiclist->music(0)); //Stage Clear
rm->backgroundmusic[3].load(musiclist->music(2)); //Tournament Menu
rm->backgroundmusic[4].load(musiclist->music(3)); //Tournament Over
}

rm->backgroundmusic[2].stop();
Expand Down
2 changes: 1 addition & 1 deletion src/smw/GSSplashScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void SplashScreenState::update()

if (state == 7) {
// load initial coin sound
rm->backgroundmusic[2].load(musiclist->GetMusic(1));
rm->backgroundmusic[2].load(musiclist->music(1));

rm->LoadAllGraphics();
rm->LoadGameSounds();
Expand Down
2 changes: 1 addition & 1 deletion src/smw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void create_globals()
gamegraphicspacklist = new GraphicsList();

announcerlist->setCurrentIndex(0);
musiclist->SetCurrent(0);
musiclist->setCurrent(0);
worldmusiclist->setCurrent(0);
menugraphicspacklist->setCurrentIndex(0);
worldgraphicspacklist->setCurrentIndex(0);
Expand Down
8 changes: 4 additions & 4 deletions src/smw/ui/MI_PlaylistField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MenuCodeEnum MI_PlaylistField::SendInput(CPlayerInput * playerInput)
if (playerInput->outputControls[iPlayer].menu_right.fPressed || playerInput->outputControls[iPlayer].menu_down.fPressed) {
musiclist->next();
rm->backgroundmusic[2].stop();
rm->backgroundmusic[2].load(musiclist->GetMusic(1));
rm->backgroundmusic[2].load(musiclist->music(1));

if (game_values.music)
rm->backgroundmusic[2].play(false, false);
Expand All @@ -52,7 +52,7 @@ MenuCodeEnum MI_PlaylistField::SendInput(CPlayerInput * playerInput)
if (playerInput->outputControls[iPlayer].menu_left.fPressed || playerInput->outputControls[iPlayer].menu_up.fPressed) {
musiclist->prev();
rm->backgroundmusic[2].stop();
rm->backgroundmusic[2].load(musiclist->GetMusic(1));
rm->backgroundmusic[2].load(musiclist->music(1));

if (game_values.music)
rm->backgroundmusic[2].play(false, false);
Expand All @@ -63,7 +63,7 @@ MenuCodeEnum MI_PlaylistField::SendInput(CPlayerInput * playerInput)
if (playerInput->outputControls[iPlayer].menu_random.fPressed) {
musiclist->random();
rm->backgroundmusic[2].stop();
rm->backgroundmusic[2].load(musiclist->GetMusic(1));
rm->backgroundmusic[2].load(musiclist->music(1));

if (game_values.music)
rm->backgroundmusic[2].play(false, false);
Expand Down Expand Up @@ -101,7 +101,7 @@ void MI_PlaylistField::Draw()
spr->draw(ix + iIndent + 16, iy, 528 - iWidth + iIndent, (fSelected ? 32 : 0), iWidth - iIndent - 16, 32);

rm->menu_font_large.drawChopRight(ix + 16, iy + 5, iIndent - 8, szName.c_str());
rm->menu_font_large.drawChopRight(ix + iIndent + 8, iy + 5, iWidth - iIndent - 24, musiclist->current_name());
rm->menu_font_large.drawChopRight(ix + iIndent + 8, iy + 5, iWidth - iIndent - 24, musiclist->currentName().c_str());

miModifyImageLeft->Draw();
miModifyImageRight->Draw();
Expand Down

0 comments on commit e54ee72

Please sign in to comment.