Improvement #1: this will modify the code to support
both /music and /sound/cdtracks. The fallback is fully automatic; if the track isn't found in one location it will just try the other.
Replace your TouchMP3 function with this one:
Code: Select all
char *TouchMP3 (char *MP3Name, int verbose)
{
static char MP3File[1024];
char *musicdirs[] =
{
"music",
"sound/cdtracks",
NULL
};
int i;
// slap on a ".mp3" extension if it doesn't have one
COM_DefaultExtension (MP3Name, ".mp3", sizeof(MP3Name));
for (i = 0; ; i++)
{
// didn't find it at all
if (!musicdirs[i])
{
if (verbose > 0)
{
Con_Printf ("couldn't find %s!!!\n", MP3Name);
}
return NULL;
}
// try the current game directory first
va_snprintf ("TouchMP3", MP3File, sizeof(MP3File), "%s/%s/%s", com_gamedir, musicdirs[i], MP3Name);
FILE *f = fopen (MP3File, "rb");
if (!f)
{
// no music in the current game directory so lets try ID1
va_snprintf ("TouchMP3", MP3File, sizeof(MP3File), "id1/%s/%s", musicdirs[i], MP3Name);
f = fopen (MP3File, "rb");
// no music in ID1 either
if (!f) continue;
}
// found it
fclose (f);
break;
}
if (verbose)
{
Con_Printf ("playing %s\n", MP3Name);
}
return MP3File;
}
All we do here is provide a list of locations we're going to check and loop through that list, checking them all until we either find it or run out of options.
Note that which this provides compatibility with the DarkPlaces
directory convention, it doesn't provide compatibility with the DarkPlaces
name convention. To get that you have a number of options, but probably the easiest is to find the
PlayMP3DShow function and replace this line:
Code: Select all
UserMP3DShow (va ("track%02i", mp3num), -1);
with this one:
Code: Select all
UserMP3DShow (va ("track%03i", mp3num), -1);
Darkplaces provides for up to 3 digits for the track number, which is a bit silly as - as I've previously noted - the Red Book CD audio spec only allows for up to 99 tracks on a CD (i.e. 2 digits).
If you're interested in a more general solution that's capable of checking both directores as well as an optional user-specified directory, that genuinely doesn't
care about what the file is called, and that
doesn't remove CD audio functionality, have a look at the DirectQ source code.
I'll hopefully post a semi-port of the relevant parts of that to the interface used by this tutorial sometime over the next few days, which will also hopefully enable loading from PAK files.