From 4bcae564171fee68b7c07dbb0353566765841b02 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Tue, 16 Apr 2024 12:22:13 +0200 Subject: [PATCH] Clean up: - replace `NO_SOUND` by `nullptr`. - Remove `SetSoundRange`. - use some more `std::string_view`. --- src/include/sound.h | 6 +---- src/sound/script_sound.cpp | 46 ++++++++++++++------------------------ src/sound/sound.cpp | 21 ++++------------- src/sound/sound_id.cpp | 14 ++++++------ src/sound/unitsound.cpp | 2 +- 5 files changed, 30 insertions(+), 59 deletions(-) diff --git a/src/include/sound.h b/src/include/sound.h index 16db15424d..940b76e3af 100644 --- a/src/include/sound.h +++ b/src/include/sound.h @@ -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 @@ -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 &files); @@ -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 &files); /// Make a sound group bound to identifier diff --git a/src/sound/script_sound.cpp b/src/sound/script_sound.cpp index 264bf96226..c26a32b992 100644 --- a/src/sound/script_sound.cpp +++ b/src/sound/script_sound.cpp @@ -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); @@ -114,7 +111,7 @@ static int CclMakeSound(lua_State *l) std::string c_name = std::string{LuaToString(l, 1)}; std::vector files; - CSound *id; + CSound *id = nullptr; if (lua_isstring(l, 2)) { // only one file files.push_back(std::string{LuaToString(l, 2)}); @@ -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; @@ -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; } @@ -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(tmp); + int range = LuaToNumber(l, 2); + clamp(&range, 0, 255); + const unsigned char theRange = static_cast(range); lua_pushvalue(l, 1); - CSound *id = CclGetSound(l); - SetSoundRange(id, theRange); - return 1; + if (CSound* id = CclGetSound(l)) { + id->Range = theRange; + } + return 0; } /** diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp index 6b2e18a9b9..98c2463010 100644 --- a/src/sound/sound.cpp +++ b/src/sound/sound.cpp @@ -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 @@ -451,14 +438,14 @@ CSound *RegisterSound(const std::vector &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; } @@ -476,8 +463,8 @@ CSound *RegisterSound(const std::vector &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; diff --git a/src/sound/sound_id.cpp b/src/sound/sound_id.cpp index 8b74c8722e..46a4d2b9e2 100644 --- a/src/sound/sound_id.cpp +++ b/src/sound/sound_id.cpp @@ -46,13 +46,13 @@ -- Variables ----------------------------------------------------------------------------*/ -static std::map SoundMap; +static std::map> SoundMap; /*---------------------------------------------------------------------------- -- Functions ----------------------------------------------------------------------------*/ -static CSound *FindSound(const std::string &name) +static CSound *FindSound(const std::string_view &name) { std::map::iterator ret = SoundMap.find(name); if (ret != SoundMap.end()) { @@ -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) @@ -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; } @@ -118,7 +118,7 @@ CSound *MakeSound(const std::string &name, const std::vector &files } sound = RegisterSound(files); - if (sound != NO_SOUND) { + if (sound != nullptr) { MapSound(name, sound); } return sound; @@ -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; diff --git a/src/sound/unitsound.cpp b/src/sound/unitsound.cpp index 9729a57815..0cac23ed4a 100644 --- a/src/sound/unitsound.cpp +++ b/src/sound/unitsound.cpp @@ -70,7 +70,7 @@ bool SoundConfig::MapSound() void SoundConfig::SetSoundRange(unsigned char range) { if (this->Sound) { - ::SetSoundRange(this->Sound, range); + this->Sound->Range = range; } }