2009-01-13 12:39:03 +00:00
|
|
|
/*
|
|
|
|
* DXX Rebirth "jukebox" code
|
|
|
|
* MD 2211 <md2211@users.sourceforge.net>, 2007
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <SDL/SDL_mixer.h>
|
|
|
|
#include "physfsx.h"
|
|
|
|
#include "args.h"
|
|
|
|
#include "dl_list.h"
|
|
|
|
#include "hudmsg.h"
|
|
|
|
#include "digi_mixer_music.h"
|
|
|
|
#include "jukebox.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "console.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#define MUSIC_HUDMSG_MAXLEN 40
|
|
|
|
#define JUKEBOX_HUDMSG_PLAYING "Now playing:"
|
|
|
|
#define JUKEBOX_HUDMSG_STOPPED "Jukebox stopped"
|
|
|
|
|
|
|
|
static char **JukeboxSongs = NULL;
|
|
|
|
char hud_msg_buf[MUSIC_HUDMSG_MAXLEN+4];
|
|
|
|
|
|
|
|
|
|
|
|
void jukebox_unload()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (JukeboxSongs == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; JukeboxSongs[i]!=NULL; i++)
|
|
|
|
{
|
|
|
|
free(JukeboxSongs[i]);
|
|
|
|
}
|
|
|
|
free(JukeboxSongs);
|
|
|
|
JukeboxSongs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loads music file names from a given directory */
|
2010-06-14 15:32:55 +00:00
|
|
|
void jukebox_load()
|
|
|
|
{
|
2009-01-13 12:39:03 +00:00
|
|
|
int count;
|
2010-06-14 08:13:16 +00:00
|
|
|
char *music_exts[] = { ".mp3", ".ogg", ".wav", ".aif", ".mid", NULL };
|
2009-01-13 12:39:03 +00:00
|
|
|
static char curpath[PATH_MAX+1];
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (memcmp(curpath,GameCfg.CMLevelMusicPath,PATH_MAX) || (GameCfg.MusicType != MUSIC_TYPE_CUSTOM))
|
2009-01-13 12:39:03 +00:00
|
|
|
{
|
|
|
|
PHYSFS_removeFromSearchPath(curpath);
|
|
|
|
jukebox_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JukeboxSongs)
|
|
|
|
return;
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (GameCfg.MusicType == MUSIC_TYPE_CUSTOM)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
const char *sep = PHYSFS_getDirSeparator();
|
|
|
|
|
2010-06-27 14:30:12 +00:00
|
|
|
// build path properly.
|
2010-06-14 08:13:16 +00:00
|
|
|
if (strlen(GameCfg.CMLevelMusicPath) >= strlen(sep))
|
|
|
|
{
|
2010-06-27 14:30:12 +00:00
|
|
|
char abspath[PATH_MAX+1];
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
p = GameCfg.CMLevelMusicPath + strlen(GameCfg.CMLevelMusicPath) - strlen(sep);
|
|
|
|
if (strcmp(p, sep))
|
|
|
|
strncat(GameCfg.CMLevelMusicPath, sep, PATH_MAX - 1 - strlen(GameCfg.CMLevelMusicPath));
|
2010-06-27 14:30:12 +00:00
|
|
|
|
|
|
|
if (PHYSFS_isDirectory(GameCfg.CMLevelMusicPath)) // it's a child of Sharepath, build full path
|
|
|
|
{
|
|
|
|
PHYSFSX_getRealPath(GameCfg.CMLevelMusicPath,abspath);
|
|
|
|
snprintf(GameCfg.CMLevelMusicPath,sizeof(char)*PATH_MAX,abspath);
|
|
|
|
}
|
2010-06-14 08:13:16 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 10:33:36 +00:00
|
|
|
PHYSFS_addToSearchPath(GameCfg.CMLevelMusicPath, 0);
|
2010-06-14 08:13:16 +00:00
|
|
|
// as mountpoints are no option (yet), make sure only files originating from GameCfg.CMLevelMusicPath are aded to the list.
|
2010-06-14 10:33:36 +00:00
|
|
|
JukeboxSongs = PHYSFSX_findabsoluteFiles("", GameCfg.CMLevelMusicPath, music_exts);
|
2009-01-13 12:39:03 +00:00
|
|
|
|
2010-06-14 10:33:36 +00:00
|
|
|
if (JukeboxSongs != NULL)
|
|
|
|
{
|
2009-01-13 12:39:03 +00:00
|
|
|
for (count = 0; JukeboxSongs[count]!=NULL; count++) {}
|
|
|
|
if (count)
|
|
|
|
{
|
2010-06-14 08:13:16 +00:00
|
|
|
con_printf(CON_DEBUG,"Jukebox: %d music file(s) found in %s\n", count, GameCfg.CMLevelMusicPath);
|
2010-06-14 15:32:55 +00:00
|
|
|
memcpy(curpath,GameCfg.CMLevelMusicPath,sizeof(char)*PATH_MAX);
|
2010-06-14 08:13:16 +00:00
|
|
|
if (GameCfg.CMLevelMusicTrack[1] != count)
|
|
|
|
{
|
|
|
|
GameCfg.CMLevelMusicTrack[1] = count;
|
|
|
|
GameCfg.CMLevelMusicTrack[0] = 0; // number of songs changed so start from beginning.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GameCfg.CMLevelMusicTrack[0] = -1;
|
|
|
|
GameCfg.CMLevelMusicTrack[1] = -1;
|
2010-06-14 15:32:55 +00:00
|
|
|
PHYSFS_removeFromSearchPath(GameCfg.CMLevelMusicPath);
|
2010-06-14 08:13:16 +00:00
|
|
|
con_printf(CON_DEBUG,"Jukebox music could not be found!\n");
|
2009-01-13 12:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
// To proceed tru our playlist. Usually used for continous play, but can loop as well.
|
|
|
|
void jukebox_hook_next()
|
|
|
|
{
|
|
|
|
if (!JukeboxSongs || GameCfg.CMLevelMusicTrack[0] == -1) return;
|
2009-03-03 12:55:27 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
GameCfg.CMLevelMusicTrack[0]++;
|
|
|
|
if (GameCfg.CMLevelMusicTrack[0] + 1 > GameCfg.CMLevelMusicTrack[1])
|
|
|
|
GameCfg.CMLevelMusicTrack[0] = 0;
|
|
|
|
|
|
|
|
jukebox_play();
|
|
|
|
}
|
2009-01-13 12:39:03 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
// Play tracks from Jukebox directory. Play track specified in GameCfg.CMLevelMusicTrack[0] and loop depending on GameCfg.CMLevelMusicPlayOrder
|
|
|
|
int jukebox_play()
|
|
|
|
{
|
|
|
|
char *music_filename;
|
2009-03-03 12:55:27 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (!JukeboxSongs)
|
|
|
|
return 0;
|
2009-03-03 12:55:27 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (GameCfg.CMLevelMusicTrack[0] < 0 || GameCfg.CMLevelMusicTrack[0] + 1 > GameCfg.CMLevelMusicTrack[1])
|
|
|
|
return 0;
|
2009-01-13 12:39:03 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
music_filename = JukeboxSongs[GameCfg.CMLevelMusicTrack[0]];
|
2010-02-27 13:05:34 +00:00
|
|
|
if (!music_filename)
|
|
|
|
return 0;
|
2009-01-13 12:39:03 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (!mix_play_file(music_filename, ((GameCfg.CMLevelMusicPlayOrder == MUSIC_CM_PLAYORDER_CONT)?0:1), ((GameCfg.CMLevelMusicPlayOrder == MUSIC_CM_PLAYORDER_CONT)?jukebox_hook_next:NULL)))
|
2010-04-05 11:54:23 +00:00
|
|
|
return 0; // whoops, got an error
|
2009-01-13 12:39:03 +00:00
|
|
|
|
|
|
|
// Formatting a pretty message
|
|
|
|
if (strlen(music_filename) >= MUSIC_HUDMSG_MAXLEN) {
|
|
|
|
strncpy(hud_msg_buf, music_filename, MUSIC_HUDMSG_MAXLEN);
|
|
|
|
strcpy(hud_msg_buf+MUSIC_HUDMSG_MAXLEN, "...");
|
|
|
|
hud_msg_buf[MUSIC_HUDMSG_MAXLEN+3] = '\0';
|
|
|
|
} else {
|
|
|
|
strcpy(hud_msg_buf, music_filename);
|
|
|
|
}
|
|
|
|
|
2010-07-13 06:35:25 +00:00
|
|
|
HUD_init_message(HM_DEFAULT, "%s %s", JUKEBOX_HUDMSG_PLAYING, hud_msg_buf);
|
2009-03-03 12:55:27 +00:00
|
|
|
|
|
|
|
return 1;
|
2009-01-13 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *jukebox_current() {
|
2010-06-14 08:13:16 +00:00
|
|
|
return JukeboxSongs[GameCfg.CMLevelMusicTrack[0]];
|
2009-01-13 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int jukebox_is_loaded() { return (JukeboxSongs != NULL); }
|
2010-06-14 08:13:16 +00:00
|
|
|
int jukebox_is_playing() { return GameCfg.CMLevelMusicTrack[0] + 1; }
|
|
|
|
int jukebox_numtracks() { return GameCfg.CMLevelMusicTrack[1]; }
|
2009-01-13 12:39:03 +00:00
|
|
|
|
|
|
|
void jukebox_list() {
|
|
|
|
int i;
|
|
|
|
if (!JukeboxSongs) return;
|
|
|
|
if (!(*JukeboxSongs)) {
|
|
|
|
con_printf(CON_DEBUG,"* No songs have been found\n");
|
|
|
|
}
|
|
|
|
else {
|
2010-06-14 08:13:16 +00:00
|
|
|
for (i = 0; i < GameCfg.CMLevelMusicTrack[1]; i++)
|
2009-01-13 12:39:03 +00:00
|
|
|
con_printf(CON_DEBUG,"* %s\n", JukeboxSongs[i]);
|
|
|
|
}
|
|
|
|
}
|