Commit c38987bb authored by Jordon Moss's avatar Jordon Moss Committed by Evan Ramos

Move S_DefineSound, S_DefineMusic, and S_DefineAudioIfSupported out of...

Move S_DefineSound, S_DefineMusic, and S_DefineAudioIfSupported out of game.cpp and into sounds.cpp, and made CON_DEFINESOUND use S_DefineSound, reducing code duplication.
parent 2d6473e4
......@@ -5153,78 +5153,6 @@ FAKE_F3:
}
}
static int32_t S_DefineAudioIfSupported(char **fn, const char *name)
{
#if !defined HAVE_FLAC || !defined HAVE_VORBIS
const char *extension = Bstrrchr(name, '.');
# if !defined HAVE_FLAC
if (extension && !Bstrcasecmp(extension, ".flac"))
return -2;
# endif
# if !defined HAVE_VORBIS
if (extension && !Bstrcasecmp(extension, ".ogg"))
return -2;
# endif
#endif
realloc_copy(fn, name);
return 0;
}
static int32_t S_DefineSound(int sndidx, const char *name, int minpitch, int maxpitch, int priority, int type, int distance, float volume)
{
if ((unsigned)sndidx >= MAXSOUNDS || S_DefineAudioIfSupported(&g_sounds[sndidx].filename, name))
return -1;
auto &snd = g_sounds[sndidx];
snd.ps = clamp(minpitch, INT16_MIN, INT16_MAX);
snd.pe = clamp(maxpitch, INT16_MIN, INT16_MAX);
snd.pr = priority & 255;
snd.m = type & ~SF_ONEINST_INTERNAL;
snd.vo = clamp(distance, INT16_MIN, INT16_MAX);
snd.volume = volume * fix16_one;
if (snd.m & SF_LOOP)
snd.m |= SF_ONEINST_INTERNAL;
return 0;
}
// Returns:
// 0: all OK
// -1: ID declaration was invalid:
static int32_t S_DefineMusic(const char *ID, const char *name)
{
int32_t sel = MUS_FIRST_SPECIAL;
Bassert(ID != NULL);
if (!Bstrcmp(ID,"intro"))
{
// nothing
}
else if (!Bstrcmp(ID,"briefing"))
{
sel++;
}
else if (!Bstrcmp(ID,"loading"))
{
sel += 2;
}
else if (!Bstrcmp(ID,"usermap"))
{
sel += 3;
}
else
{
sel = G_GetMusicIdx(ID);
if (sel < 0)
return -1;
}
return S_DefineAudioIfSupported(&g_mapInfo[sel].musicfn, name);
}
static int parsedefinitions_game(scriptfile *, int);
static void parsedefinitions_game_include(const char *fileName, scriptfile *pScript, const char *cmdtokptr, int const firstPass)
......
......@@ -5801,23 +5801,24 @@ repeatcase:
continue;
case CON_DEFINESOUND:
{
g_scriptPtr--;
C_GetNextValue(LABEL_DEFINE);
// Ideally we could keep the value of i from C_GetNextValue() instead of having to hash_find() again.
// This depends on tempbuf remaining in place after C_GetNextValue():
j = hash_find(&h_labels,tempbuf);
j = hash_find(&h_labels, tempbuf);
k = g_scriptPtr[-1];
if (EDUKE32_PREDICT_FALSE((unsigned)k >= MAXSOUNDS-1))
if (EDUKE32_PREDICT_FALSE((unsigned)k >= MAXSOUNDS - 1))
{
initprintf("%s:%d: error: sound index exceeds limit of %d.\n",g_scriptFileName,g_lineNumber, MAXSOUNDS-1);
initprintf("%s:%d: error: sound index exceeds limit of %d.\n", g_scriptFileName, g_lineNumber, MAXSOUNDS - 1);
g_errorCnt++;
k = MAXSOUNDS-1;
k = MAXSOUNDS - 1;
}
else if (EDUKE32_PREDICT_FALSE(g_sounds[k].filename != NULL))
{
initprintf("%s:%d: warning: sound %d already defined (%s)\n",g_scriptFileName,g_lineNumber,k,g_sounds[k].filename);
initprintf("%s:%d: warning: sound %d already defined (%s)\n", g_scriptFileName, g_lineNumber, k, g_sounds[k].filename);
g_warningCnt++;
}
......@@ -5825,18 +5826,17 @@ repeatcase:
i = 0;
C_SkipComments();
if (g_sounds[k].filename == NULL)
g_sounds[k].filename = (char *)Xcalloc(BMAX_PATH,sizeof(uint8_t));
char filename[BMAX_PATH];
if (*textptr == '\"')
{
textptr++;
while (*textptr && *textptr != '\"')
{
g_sounds[k].filename[i++] = *textptr++;
if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH-1))
filename[i++] = *textptr++;
if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH - 1))
{
initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n",g_scriptFileName,g_lineNumber,BMAX_PATH-1);
initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n", g_scriptFileName, g_lineNumber, BMAX_PATH - 1);
g_errorCnt++;
C_SkipComments();
break;
......@@ -5846,43 +5846,44 @@ repeatcase:
}
else while (*textptr != ' ' && *textptr != '\t' && *textptr != '\r' && *textptr != '\n')
{
g_sounds[k].filename[i++] = *textptr++;
if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH-1))
filename[i++] = *textptr++;
if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH - 1))
{
initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n",g_scriptFileName,g_lineNumber,BMAX_PATH-1);
initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n", g_scriptFileName, g_lineNumber, BMAX_PATH - 1);
g_errorCnt++;
C_SkipComments();
break;
}
}
g_sounds[k].filename[i] = '\0';
filename[i] = '\0';
check_filename_case(g_sounds[k].filename);
check_filename_case(filename);
C_GetNextValue(LABEL_DEFINE);
g_sounds[k].ps = g_scriptPtr[-1];
int minpitch = g_scriptPtr[-1];
C_GetNextValue(LABEL_DEFINE);
g_sounds[k].pe = g_scriptPtr[-1];
int maxpitch = g_scriptPtr[-1];
C_GetNextValue(LABEL_DEFINE);
g_sounds[k].pr = g_scriptPtr[-1];
int priority = g_scriptPtr[-1];
C_GetNextValue(LABEL_DEFINE);
g_sounds[k].m = g_scriptPtr[-1] & ~SF_ONEINST_INTERNAL;
if (g_scriptPtr[-1] & SF_LOOP)
g_sounds[k].m |= SF_ONEINST_INTERNAL;
int type = g_scriptPtr[-1];
C_GetNextValue(LABEL_DEFINE);
g_sounds[k].vo = g_scriptPtr[-1];
int distance = g_scriptPtr[-1];
g_scriptPtr -= 5;
g_sounds[k].volume = fix16_one;
float volume = 1.0;
S_DefineSound(k, filename, minpitch, maxpitch, priority, type, distance, volume);
if (k > g_highestSoundIdx)
g_highestSoundIdx = k;
if (g_dynamicSoundMapping && j >= 0 && (labeltype[j] & LABEL_DEFINE))
G_ProcessDynamicSoundMapping(label+(j<<6), k);
G_ProcessDynamicSoundMapping(label + (j << 6), k);
continue;
}
case CON_ENDEVENT:
......
......@@ -504,6 +504,78 @@ void cacheAllSounds(void)
}
}
static int32_t S_DefineAudioIfSupported(char** fn, const char* name)
{
#if !defined HAVE_FLAC || !defined HAVE_VORBIS
const char* extension = Bstrrchr(name, '.');
# if !defined HAVE_FLAC
if (extension && !Bstrcasecmp(extension, ".flac"))
return -2;
# endif
# if !defined HAVE_VORBIS
if (extension && !Bstrcasecmp(extension, ".ogg"))
return -2;
# endif
#endif
realloc_copy(fn, name);
return 0;
}
int32_t S_DefineSound(int sndidx, const char* name, int minpitch, int maxpitch, int priority, int type, int distance, float volume)
{
if ((unsigned)sndidx >= MAXSOUNDS || S_DefineAudioIfSupported(&g_sounds[sndidx].filename, name))
return -1;
auto& snd = g_sounds[sndidx];
snd.ps = clamp(minpitch, INT16_MIN, INT16_MAX);
snd.pe = clamp(maxpitch, INT16_MIN, INT16_MAX);
snd.pr = priority & 255;
snd.m = type & ~SF_ONEINST_INTERNAL;
snd.vo = clamp(distance, INT16_MIN, INT16_MAX);
snd.volume = volume * fix16_one;
if (snd.m & SF_LOOP)
snd.m |= SF_ONEINST_INTERNAL;
return 0;
}
// Returns:
// 0: all OK
// -1: ID declaration was invalid:
int32_t S_DefineMusic(const char* ID, const char* name)
{
int32_t sel = MUS_FIRST_SPECIAL;
Bassert(ID != NULL);
if (!Bstrcmp(ID, "intro"))
{
// nothing
}
else if (!Bstrcmp(ID, "briefing"))
{
sel++;
}
else if (!Bstrcmp(ID, "loading"))
{
sel += 2;
}
else if (!Bstrcmp(ID, "usermap"))
{
sel += 3;
}
else
{
sel = G_GetMusicIdx(ID);
if (sel < 0)
return -1;
}
return S_DefineAudioIfSupported(&g_mapInfo[sel].musicfn, name);
}
static inline int S_GetPitch(int num)
{
auto const &snd = g_sounds[num];
......
......@@ -74,6 +74,8 @@ void S_Cleanup(void);
void S_ClearSoundLocks(void);
int32_t S_LoadSound(uint32_t num);
void cacheAllSounds(void);
int32_t S_DefineSound(int sndidx, const char* name, int minpitch, int maxpitch, int priority, int type, int distance, float volume);
int32_t S_DefineMusic(const char* ID, const char* name);
void S_MenuSound(void);
void S_MusicShutdown(void);
void S_MusicStartup(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment