Skip to content

Commit

Permalink
Clean up:
Browse files Browse the repository at this point in the history
- replace `NO_SOUND` by `nullptr`.
- Remove `SetSoundRange`.
- use some more `std::string_view`.
  • Loading branch information
Jarod42 committed Apr 16, 2024
1 parent aced99f commit 4bcae56
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 59 deletions.
6 changes: 1 addition & 5 deletions src/include/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class LuaActionListener;
----------------------------------------------------------------------------*/

#define MaxSampleVolume 255 /// Maximum sample volume
#define NO_SOUND 0 /// No valid sound ID

/**
** Voice groups for a unit
Expand Down Expand Up @@ -186,9 +185,6 @@ extern void PlayGameSound(CSound *sound, unsigned char volume, bool always = fal
/// Play a sound file
extern int PlayFile(const std::string &name, LuaActionListener *listener = nullptr);

/// Modify the range of a given sound.
extern void SetSoundRange(CSound *sound, unsigned char range);

/// Register a sound (can be a simple sound or a group)
extern CSound *RegisterSound(const std::vector<std::string> &files);

Expand Down Expand Up @@ -218,7 +214,7 @@ extern void CallbackMusicTrigger();
/// Map sound to identifier
extern void MapSound(const std::string &sound_name, CSound *id);
/// Get the sound id bound to an identifier
extern CSound *SoundForName(const std::string &sound_name);
extern CSound *SoundForName(const std::string_view &sound_name);
/// Make a sound bound to identifier
extern CSound *MakeSound(const std::string &sound_name, const std::vector<std::string> &files);
/// Make a sound group bound to identifier
Expand Down
46 changes: 17 additions & 29 deletions src/sound/script_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ static int CclSoundForName(lua_State *l)
*/
static CSound *CclGetSound(lua_State *l)
{
LuaUserData *data;
int pop;

pop = 0;
bool pop = false;
if (lua_isstring(l, -1)) {
CclSoundForName(l);
pop = 1;
pop = true;
}
if (lua_isuserdata(l, -1)) {
data = (LuaUserData *)lua_touserdata(l, -1);
LuaUserData *data = (LuaUserData *)lua_touserdata(l, -1);
if (data->Type == LuaSoundType) {
if (pop) {
lua_pop(l, 1);
Expand Down Expand Up @@ -114,7 +111,7 @@ static int CclMakeSound(lua_State *l)

std::string c_name = std::string{LuaToString(l, 1)};
std::vector<std::string> files;
CSound *id;
CSound *id = nullptr;
if (lua_isstring(l, 2)) {
// only one file
files.push_back(std::string{LuaToString(l, 2)});
Expand Down Expand Up @@ -147,22 +144,16 @@ static int CclMakeSound(lua_State *l)
*/
static int CclMakeSoundGroup(lua_State *l)
{
CSound *id;
std::string c_name;
CSound *first;
CSound *second;
LuaUserData *data;

LuaCheckArgs(l, 3);

c_name = LuaToString(l, 1);
std::string c_name{LuaToString(l, 1)};

lua_pushvalue(l, 2);
first = CclGetSound(l);
CSound *first = CclGetSound(l);
lua_pop(l, 1);
second = CclGetSound(l);
id = MakeSoundGroup(c_name, first, second);
data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
CSound *second = CclGetSound(l);
CSound *id = MakeSoundGroup(c_name, first, second);
LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
data->Type = LuaSoundType;
data->Data = id;
return 1;
Expand Down Expand Up @@ -206,10 +197,7 @@ static int CclPlaySound(lua_State *l)
lua_pushvalue(l, 1);
CSound *id = CclGetSound(l);
lua_pop(l, 1);
bool always = false;
if (args == 2) {
always = LuaToBoolean(l, 2);
}
const bool always = args == 2 && LuaToBoolean(l, 2);
PlayGameSound(id, MaxSampleVolume, always);
return 0;
}
Expand Down Expand Up @@ -339,17 +327,17 @@ static int CclSetGlobalSoundRange(lua_State *l)
*/
static int CclSetSoundRange(lua_State *l)
{

LuaCheckArgs(l, 2);

int tmp = LuaToNumber(l, 2);
clamp(&tmp, 0, 255);
const unsigned char theRange = static_cast<unsigned char>(tmp);
int range = LuaToNumber(l, 2);
clamp(&range, 0, 255);
const unsigned char theRange = static_cast<unsigned char>(range);

lua_pushvalue(l, 1);
CSound *id = CclGetSound(l);
SetSoundRange(id, theRange);
return 1;
if (CSound* id = CclGetSound(l)) {
id->Range = theRange;
}
return 0;
}

/**
Expand Down
21 changes: 4 additions & 17 deletions src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,6 @@ int PlayFile(const std::string &name, LuaActionListener *listener)
return channel;
}

/**
** Ask the sound server to change the range of a sound.
**
** @param sound the id of the sound to modify.
** @param range the new range for this sound.
*/
void SetSoundRange(CSound *sound, unsigned char range)
{
if (sound != NO_SOUND) {
sound->Range = range;
}
}

/**
** Ask the sound server to register a sound (and currently to load it)
** and to return an unique identifier for it. The unique identifier is
Expand All @@ -451,14 +438,14 @@ CSound *RegisterSound(const std::vector<std::string> &files)
if (!id->Sound.OneGroup[i]) {
//delete[] id->Sound.OneGroup;
delete id;
return NO_SOUND;
return nullptr;
}
}
} else { // load a unique sound
id->Sound.OneSound = LoadSample(files[0]);
if (!id->Sound.OneSound) {
delete id;
return NO_SOUND;
return nullptr;
}
id->Number = ONE_SOUND;
}
Expand All @@ -476,8 +463,8 @@ CSound *RegisterSound(const std::vector<std::string> &files)
*/
CSound *RegisterTwoGroups(CSound *first, CSound *second)
{
if (first == NO_SOUND || second == NO_SOUND) {
return NO_SOUND;
if (first == nullptr || second == nullptr) {
return nullptr;
}
CSound *id = new CSound;
id->Number = TWO_GROUPS;
Expand Down
14 changes: 7 additions & 7 deletions src/sound/sound_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
-- Variables
----------------------------------------------------------------------------*/

static std::map<std::string, CSound *> SoundMap;
static std::map<std::string, CSound *, std::less<>> SoundMap;

/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/

static CSound *FindSound(const std::string &name)
static CSound *FindSound(const std::string_view &name)
{
std::map<std::string, CSound *>::iterator ret = SoundMap.find(name);
if (ret != SoundMap.end()) {
Expand All @@ -65,7 +65,7 @@ static CSound *FindSound(const std::string &name)
** Add a new mapping (sound name to sound id) in the hash table
** Create a new mapping between a name and an already valid sound id.
**
** @param name Name of the sound (now freed by caller!).
** @param name Name of the sound.
** @param id Sound identifier.
*/
void MapSound(const std::string &name, CSound *id)
Expand All @@ -85,14 +85,14 @@ void MapSound(const std::string &name, CSound *id)
**
** @return Sound identifier for this name.
*/
CSound *SoundForName(const std::string &name)
CSound *SoundForName(const std::string_view &name)
{
Assert(!name.empty());
CSound *sound = FindSound(name);
if (sound) {
return sound;
}
DebugPrint("Can't find sound '%s' in sound table\n", name.c_str());
DebugPrint("Can't find sound '%s' in sound table\n", name.data());
return nullptr;
}

Expand All @@ -118,7 +118,7 @@ CSound *MakeSound(const std::string &name, const std::vector<std::string> &files
}

sound = RegisterSound(files);
if (sound != NO_SOUND) {
if (sound != nullptr) {
MapSound(name, sound);
}
return sound;
Expand Down Expand Up @@ -147,7 +147,7 @@ CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second)
}

sound = RegisterTwoGroups(first, second);
if (sound != NO_SOUND) {
if (sound != nullptr) {
MapSound(name, sound);
}
return sound;
Expand Down
2 changes: 1 addition & 1 deletion src/sound/unitsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool SoundConfig::MapSound()
void SoundConfig::SetSoundRange(unsigned char range)
{
if (this->Sound) {
::SetSoundRange(this->Sound, range);
this->Sound->Range = range;
}
}

Expand Down

0 comments on commit 4bcae56

Please sign in to comment.