usbloadergx/source/libwiigui/gui_bgm.cpp

237 lines
4.7 KiB
C++
Raw Normal View History

/****************************************************************************
* SettingsPrompts
* USB Loader GX 2009
*
* Backgroundmusic
***************************************************************************/
#include <sys/dir.h>
#include "gui_bgm.h"
#include "menu.h"
2010-09-24 02:48:03 +02:00
GuiBGM::GuiBGM(const u8 *s, int l, int v) :
GuiSound(s, l, v)
{
loop = 0;
loopMode = ONCE;
currentPath = NULL;
currentPlaying = 0;
//shouldn't be needed but
//fixes some kind of weird bug in ogg system
2010-09-24 02:48:03 +02:00
GuiSound::Load(s, l, v);
}
GuiBGM::~GuiBGM()
{
2010-09-24 02:48:03 +02:00
if (currentPath) delete[] currentPath;
ClearList();
2010-09-24 02:48:03 +02:00
}
;
2010-09-24 02:48:03 +02:00
void GuiBGM::SetLoop(bool l)
{
}
2010-09-24 02:48:03 +02:00
void GuiBGM::SetLoop(int l)
{
loop = false;
loopMode = ONCE;
2010-09-24 02:48:03 +02:00
if (l == LOOP)
{
loop = true;
}
2010-09-24 02:48:03 +02:00
else loopMode = l;
}
2010-09-24 02:48:03 +02:00
bool GuiBGM::Load(const char *path)
{
2010-09-24 02:48:03 +02:00
if (!path)
{
LoadStandard();
return false;
}
2010-09-24 02:48:03 +02:00
if (strcmp(path, "") == 0)
{
LoadStandard();
return false;
}
2010-09-24 02:48:03 +02:00
if (!GuiSound::Load(path))
{
LoadStandard();
return false;
}
2010-09-24 02:48:03 +02:00
return ParsePath(path);
}
bool GuiBGM::LoadStandard()
{
ClearList();
2010-09-24 02:48:03 +02:00
if (currentPath)
{
2010-09-24 02:48:03 +02:00
delete[] currentPath;
currentPath = NULL;
}
2010-09-24 02:48:03 +02:00
strcpy(Settings.ogg_path, "");
2010-09-24 02:48:03 +02:00
bool ret = GuiSound::Load(bg_music_ogg, bg_music_ogg_size, true);
2010-09-24 02:48:03 +02:00
if (ret) Play();
return ret;
}
2010-09-24 02:48:03 +02:00
bool GuiBGM::ParsePath(const char * folderpath)
{
ClearList();
2010-09-24 02:48:03 +02:00
if (currentPath) delete[] currentPath;
2010-09-24 02:48:03 +02:00
currentPath = new char[strlen(folderpath) + 1];
sprintf(currentPath, "%s", folderpath);
2010-09-24 02:48:03 +02:00
char * isdirpath = strrchr(folderpath, '.');
if (isdirpath)
{
2010-09-24 02:48:03 +02:00
char * pathptr = strrchr(currentPath, '/');
if (pathptr)
{
pathptr++;
pathptr[0] = 0;
}
}
2010-09-24 02:48:03 +02:00
char * LoadedFilename = strrchr(folderpath, '/') + 1;
char filename[1024];
struct stat st;
2010-09-24 02:48:03 +02:00
DIR_ITER * dir = diropen(currentPath);
if (dir == NULL)
{
LoadStandard();
return false;
}
u32 counter = 0;
2010-09-24 02:48:03 +02:00
while (dirnext(dir, filename, &st) == 0)
{
2010-09-24 02:48:03 +02:00
char * fileext = strrchr(filename, '.');
if (fileext)
{
2010-09-24 02:48:03 +02:00
if (strcasecmp(fileext, ".mp3") == 0 || strcasecmp(fileext, ".ogg") == 0 || strcasecmp(fileext, ".wav")
== 0)
{
2010-09-24 02:48:03 +02:00
AddEntrie(filename);
2010-09-24 02:48:03 +02:00
if (strcmp(LoadedFilename, filename) == 0) currentPlaying = counter;
counter++;
}
}
}
2010-09-24 02:48:03 +02:00
dirclose(dir);
2010-09-24 02:48:03 +02:00
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s", folderpath);
return true;
}
2010-09-24 02:48:03 +02:00
void GuiBGM::AddEntrie(const char * filename)
{
2010-09-24 02:48:03 +02:00
if (!filename) return;
2010-09-24 02:48:03 +02:00
char * NewEntrie = new char[strlen(filename) + 1];
sprintf(NewEntrie, "%s", filename);
2010-09-24 02:48:03 +02:00
PlayList.push_back(NewEntrie);
}
void GuiBGM::ClearList()
{
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < PlayList.size(); i++)
{
2010-09-24 02:48:03 +02:00
if (PlayList.at(i) != NULL)
{
2010-09-24 02:48:03 +02:00
delete[] PlayList.at(i);
PlayList.at(i) = NULL;
}
}
PlayList.clear();
}
bool GuiBGM::PlayNext()
{
2010-09-24 02:48:03 +02:00
if (!currentPath) return false;
currentPlaying++;
2010-09-24 02:48:03 +02:00
if (currentPlaying >= (int) PlayList.size()) currentPlaying = 0;
2010-09-24 02:48:03 +02:00
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
2010-09-24 02:48:03 +02:00
if (!GuiSound::Load(Settings.ogg_path)) return false;
Play();
return true;
}
bool GuiBGM::PlayPrevious()
{
2010-09-24 02:48:03 +02:00
if (!currentPath) return false;
currentPlaying--;
2010-09-24 02:48:03 +02:00
if (currentPlaying < 0) currentPlaying = PlayList.size() - 1;
2010-09-24 02:48:03 +02:00
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
2010-09-24 02:48:03 +02:00
if (!GuiSound::Load(Settings.ogg_path)) return false;
Play();
return true;
}
bool GuiBGM::PlayRandom()
{
2010-09-24 02:48:03 +02:00
if (!currentPath) return false;
2010-09-24 02:48:03 +02:00
srand(time(NULL));
currentPlaying = rand() % PlayList.size();
//just in case
2010-09-24 02:48:03 +02:00
if (currentPlaying < 0)
currentPlaying = PlayList.size() - 1;
2010-09-24 02:48:03 +02:00
else if (currentPlaying >= (int) PlayList.size()) currentPlaying = 0;
2010-09-24 02:48:03 +02:00
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
2010-09-24 02:48:03 +02:00
if (!GuiSound::Load(Settings.ogg_path)) return false;
Play();
return true;
}
void GuiBGM::UpdateState()
{
2010-09-24 02:48:03 +02:00
if (!IsPlaying())
{
2010-09-24 02:48:03 +02:00
if (loopMode == DIR_LOOP)
{
PlayNext();
}
2010-09-24 02:48:03 +02:00
else if (loopMode == RANDOM_BGM)
{
PlayRandom();
}
}
}