diff --git a/src/action/action_research.cpp b/src/action/action_research.cpp index 45d476c3ea..842a7bd68b 100644 --- a/src/action/action_research.cpp +++ b/src/action/action_research.cpp @@ -153,10 +153,10 @@ void COrder_Research::Execute(CUnit &unit) /* override */ ColorGreen, unit.tilePos, _("%s: research complete"), upgrade.Name.c_str()); } if (&player == ThisPlayer) { - CSound *sound = GameSounds.ResearchComplete[player.Race].Sound; + auto sound = GameSounds.ResearchComplete[player.Race].Sound; if (sound) { - PlayGameSound(sound, MaxSampleVolume); + PlayGameSound(sound.get(), MaxSampleVolume); } } if (player.AiEnabled) { diff --git a/src/animation/animation_randomsound.cpp b/src/animation/animation_randomsound.cpp index 4fae3af1cb..6a0fcf67f2 100644 --- a/src/animation/animation_randomsound.cpp +++ b/src/animation/animation_randomsound.cpp @@ -51,7 +51,7 @@ void CAnimation_RandomSound::Action(CUnit &unit, int & /*move*/, int /*scale*/) if (unit.IsVisible(*ThisPlayer) || ReplayRevealMap) { const size_t index = MyRand() % this->sounds.size(); - PlayUnitSound(unit, this->sounds[index].Sound); + PlayUnitSound(unit, this->sounds[index].Sound.get()); } } diff --git a/src/animation/animation_sound.cpp b/src/animation/animation_sound.cpp index 2c5b3c7be0..750c4f3804 100644 --- a/src/animation/animation_sound.cpp +++ b/src/animation/animation_sound.cpp @@ -46,7 +46,7 @@ void CAnimation_Sound::Action(CUnit &unit, int & /*move*/, int /*scale*/) const Assert(unit.Anim.CurrAnim); Assert((*unit.Anim.CurrAnim)[unit.Anim.Anim].get() == this); if (unit.IsVisible(*ThisPlayer) || ReplayRevealMap) { - PlayUnitSound(unit, this->sound.Sound); + PlayUnitSound(unit, this->sound.Sound.get()); } } diff --git a/src/editor/editloop.cpp b/src/editor/editloop.cpp index 3fb6ed6c4a..5d6f55e967 100644 --- a/src/editor/editloop.cpp +++ b/src/editor/editloop.cpp @@ -1,4 +1,4 @@ -// _________ __ __ +// _________ __ __ // / _____// |_____________ _/ |______ ____ __ __ ______ // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | @@ -1199,7 +1199,7 @@ static void EditorCallbackButtonDown(unsigned button) // Click on menu button if (CursorOn == ECursorOn::Button && ButtonAreaUnderCursor == ButtonArea::Menu && (MouseButtons & LeftButton) && !GameMenuButtonClicked) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); GameMenuButtonClicked = true; return; } @@ -1344,14 +1344,14 @@ static void EditorCallbackButtonDown(unsigned button) } else if (Editor.State == EditorStateType::EditUnit) { if (!UnitPlacedThisPress && CursorBuilding) { if (CanBuildUnitType(nullptr, *CursorBuilding, tilePos, 1)) { - PlayGameSound(GameSounds.PlacementSuccess[ThisPlayer->Race].Sound, + PlayGameSound(GameSounds.PlacementSuccess[ThisPlayer->Race].Sound.get(), MaxSampleVolume); EditorPlaceUnit(tilePos, *CursorBuilding, Players + Editor.SelectedPlayer); UnitPlacedThisPress = true; UI.StatusLine.Clear(); } else { UI.StatusLine.Set(_("Unit cannot be placed here.")); - PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound, + PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound.get(), MaxSampleVolume); } } diff --git a/src/game/replay.cpp b/src/game/replay.cpp index 8ad93426d3..0a9921a2aa 100644 --- a/src/game/replay.cpp +++ b/src/game/replay.cpp @@ -1,4 +1,4 @@ -// _________ __ __ +// _________ __ __ // / _____// |_____________ _/ |______ ____ __ __ ______ // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | @@ -753,7 +753,7 @@ static void DoNextReplay() } } else if (action == "chat") { SetMessage("%s", val.data()); - PlayGameSound(GameSounds.ChatMessage.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.ChatMessage.Sound.get(), MaxSampleVolume); } else if (action == "quit") { CommandQuit(arg1); } else { diff --git a/src/include/sound.h b/src/include/sound.h index 940b76e3af..8549535b82 100644 --- a/src/include/sound.h +++ b/src/include/sound.h @@ -96,21 +96,25 @@ class GameSound /** ** Sound definition. */ -class CSound +class CSound : public std::enable_shared_from_this { -public: - CSound() : Mapref(0), Range(0), Number(0) + class Key { - memset(&Sound, 0, sizeof(Sound)); - } + friend CSound; + Key(){}; + }; +public: + explicit CSound(Key){} ~CSound(); - unsigned int Mapref; + + static std::shared_ptr make() { return std::make_shared(Key{}); } + /** ** Range is a multiplier for ::DistanceSilent. ** 255 means infinite range of the sound. */ - unsigned char Range; /// Range is a multiplier for DistanceSilent - unsigned char Number; /// single, group, or table of sounds. + unsigned char Range = 0; /// Range is a multiplier for DistanceSilent + unsigned char Number = 0; /// single, group, or table of sounds. union { Mix_Chunk *OneSound; /// if it's only a simple sound Mix_Chunk **OneGroup; /// when it's a simple group @@ -118,7 +122,7 @@ class CSound CSound *First; /// first group: selected sound CSound *Second; /// second group: annoyed sound } TwoGroups; /// when it's a double group - } Sound; + } Sound{}; }; /** @@ -186,10 +190,10 @@ extern void PlayGameSound(CSound *sound, unsigned char volume, bool always = fal extern int PlayFile(const std::string &name, LuaActionListener *listener = nullptr); /// Register a sound (can be a simple sound or a group) -extern CSound *RegisterSound(const std::vector &files); +extern std::shared_ptr RegisterSound(const std::vector &files); /// Create a special sound group with two sounds -extern CSound *RegisterTwoGroups(CSound *first, CSound *second); +extern std::shared_ptr RegisterTwoGroups(CSound *first, CSound *second); /// Initialize client side of the sound layer. extern void InitSoundClient(); @@ -212,13 +216,13 @@ extern void CallbackMusicTrigger(); // sound_id.cpp /// Map sound to identifier -extern void MapSound(const std::string &sound_name, CSound *id); +extern void MapSound(const std::string &sound_name, std::shared_ptr); /// Get the sound id bound to an identifier -extern CSound *SoundForName(const std::string_view &sound_name); +extern std::shared_ptr 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); +extern std::shared_ptr MakeSound(const std::string &sound_name, const std::vector &files); /// Make a sound group bound to identifier -extern CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second); +extern std::shared_ptr MakeSoundGroup(const std::string &name, CSound *first, CSound *second); extern void FreeSounds(); diff --git a/src/include/unitsound.h b/src/include/unitsound.h index f6be4a97fe..66dcabcaaf 100644 --- a/src/include/unitsound.h +++ b/src/include/unitsound.h @@ -41,6 +41,8 @@ #include "upgrade_structs.h" #endif +#include + #define ANIMATIONS_DEATHTYPES 40 /*---------------------------------------------------------------------------- -- Declarations @@ -62,7 +64,7 @@ class SoundConfig public: std::string Name; /// config sound name - CSound *Sound = nullptr; /// identifier send to sound server + std::shared_ptr Sound; /// identifier send to sound server }; /** diff --git a/src/missile/missile.cpp b/src/missile/missile.cpp index 3b643c2db7..4432a2509a 100644 --- a/src/missile/missile.cpp +++ b/src/missile/missile.cpp @@ -187,7 +187,7 @@ Missile::Init(const MissileType &mtype, const PixelPos &startPos, const PixelPos missile->Delay = mtype.StartDelay; missile->TTL = mtype.TTL; if (mtype.FiredSound.Sound) { - PlayMissileSound(*missile, mtype.FiredSound.Sound); + PlayMissileSound(*missile, mtype.FiredSound.Sound.get()); } return missile; @@ -866,7 +866,7 @@ void Missile::MissileHit(CUnit *unit) const MissileType &mtype = *this->Type; if (mtype.ImpactSound.Sound) { - PlayMissileSound(*this, mtype.ImpactSound.Sound); + PlayMissileSound(*this, mtype.ImpactSound.Sound.get()); } const PixelPos pixelPos = this->position + this->Type->size / 2; diff --git a/src/network/network.cpp b/src/network/network.cpp index 62eb43c5f2..0cbcd0a41f 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -992,7 +992,7 @@ static void NetworkExecCommand_Chat(const CNetworkCommandQueue &ncq) nc.Deserialize(&ncq.Data[0]); SetMessage("%s", nc.Text.c_str()); - PlayGameSound(GameSounds.ChatMessage.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.ChatMessage.Sound.get(), MaxSampleVolume); CommandLog("chat", nullptr, FlushCommands, -1, -1, nullptr, nc.Text.c_str(), -1); } diff --git a/src/sound/script_sound.cpp b/src/sound/script_sound.cpp index c26a32b992..ad7967eef0 100644 --- a/src/sound/script_sound.cpp +++ b/src/sound/script_sound.cpp @@ -59,11 +59,11 @@ static int CclSoundForName(lua_State *l) { const std::string sound_name = std::string{LuaToString(l, -1)}; - CSound *id = SoundForName(sound_name); + auto id = SoundForName(sound_name); LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData)); data->Type = LuaSoundType; - data->Data = id; + data->Data = id.get(); return 1; } @@ -111,7 +111,7 @@ static int CclMakeSound(lua_State *l) std::string c_name = std::string{LuaToString(l, 1)}; std::vector files; - CSound *id = nullptr; + std::shared_ptr id; if (lua_isstring(l, 2)) { // only one file files.push_back(std::string{LuaToString(l, 2)}); @@ -130,7 +130,7 @@ static int CclMakeSound(lua_State *l) } LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData)); data->Type = LuaSoundType; - data->Data = id; + data->Data = id.get(); return 1; } @@ -152,10 +152,10 @@ static int CclMakeSoundGroup(lua_State *l) CSound *first = CclGetSound(l); lua_pop(l, 1); CSound *second = CclGetSound(l); - CSound *id = MakeSoundGroup(c_name, first, second); + auto id = MakeSoundGroup(c_name, first, second); LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData)); data->Type = LuaSoundType; - data->Data = id; + data->Data = id.get(); return 1; } @@ -171,7 +171,8 @@ static int CclMapSound(lua_State *l) { LuaCheckArgs(l, 2); std::string sound_name = std::string{LuaToString(l, 1)}; - MapSound(sound_name, CclGetSound(l)); + auto sound = CclGetSound(l); + MapSound(sound_name, sound ? sound->shared_from_this() : nullptr); lua_pushvalue(l, 2); return 1; } @@ -219,7 +220,7 @@ static void SetSoundConfigRace(lua_State *l, int j, SoundConfig soundConfigs[]) LuaError(l, "Sound id expected"); } lua_pop(l, 1); - soundConfigs[raceIndex].Sound = (CSound *)data->Data; + soundConfigs[raceIndex].Sound = reinterpret_cast(data->Data)->shared_from_this(); } /** @@ -245,13 +246,13 @@ static int CclDefineGameSounds(lua_State *l) || (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) { LuaError(l, "Sound id expected"); } - GameSounds.Click.Sound = (CSound *)data->Data; + GameSounds.Click.Sound = reinterpret_cast(data->Data)->shared_from_this(); } else if (value == "transport-docking") { if (!lua_isuserdata(l, j + 1) || (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) { LuaError(l, "Sound id expected"); } - GameSounds.Docking.Sound = (CSound *)data->Data; + GameSounds.Docking.Sound = reinterpret_cast(data->Data)->shared_from_this(); } else if (value == "placement-error") { SetSoundConfigRace(l, j, GameSounds.PlacementError); } else if (value == "placement-success") { @@ -277,7 +278,7 @@ static int CclDefineGameSounds(lua_State *l) LuaError(l, "Sound id expected"); } lua_pop(l, 1); - GameSounds.NotEnoughRes[raceIndex][resId].Sound = (CSound *)data->Data; + GameSounds.NotEnoughRes[raceIndex][resId].Sound = reinterpret_cast(data->Data)->shared_from_this(); } else if (value == "not-enough-food") { SetSoundConfigRace(l, j, GameSounds.NotEnoughFood); } else if (value == "rescue") { @@ -289,7 +290,7 @@ static int CclDefineGameSounds(lua_State *l) || (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) { LuaError(l, "Sound id expected"); } - GameSounds.ChatMessage.Sound = (CSound *)data->Data; + GameSounds.ChatMessage.Sound = reinterpret_cast(data->Data)->shared_from_this(); } else { LuaError(l, "Unsupported tag: %s", value.data()); } diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp index 98c2463010..91c12f713b 100644 --- a/src/sound/sound.cpp +++ b/src/sound/sound.cpp @@ -155,7 +155,7 @@ static Mix_Chunk *ChooseSample(CSound &sound, bool selection, Origin &source) ** ** @return Sound identifier */ -static CSound *ChooseUnitVoiceSound(const CUnit &unit, EUnitVoice voice) +static std::shared_ptr ChooseUnitVoiceSound(const CUnit &unit, EUnitVoice voice) { switch (voice) { case EUnitVoice::Acknowledging: return unit.Type->MapSound.Acknowledgement.Sound; @@ -247,7 +247,7 @@ static char CalculateStereo(const CUnit &unit) */ void PlayUnitSound(const CUnit &unit, EUnitVoice voice, bool sampleUnique) { - CSound *sound = ChooseUnitVoiceSound(unit, voice); + auto sound = ChooseUnitVoiceSound(unit, voice); if (!sound) { return; } @@ -424,27 +424,24 @@ int PlayFile(const std::string &name, LuaActionListener *listener) ** ** @todo FIXME: Must handle the errors better. */ -CSound *RegisterSound(const std::vector &files) +std::shared_ptr RegisterSound(const std::vector &files) { - CSound *id = new CSound; + auto id = CSound::make(); size_t number = files.size(); if (number > 1) { // load a sound group - id->Sound.OneGroup = new Mix_Chunk *[number]; - memset(id->Sound.OneGroup, 0, sizeof(Mix_Chunk *) * number); + id->Sound.OneGroup = new Mix_Chunk *[number] {}; id->Number = static_cast(number); for (unsigned int i = 0; i < number; ++i) { id->Sound.OneGroup[i] = LoadSample(files[i]); if (!id->Sound.OneGroup[i]) { //delete[] id->Sound.OneGroup; - delete id; return nullptr; } } } else { // load a unique sound id->Sound.OneSound = LoadSample(files[0]); if (!id->Sound.OneSound) { - delete id; return nullptr; } id->Number = ONE_SOUND; @@ -461,12 +458,12 @@ CSound *RegisterSound(const std::vector &files) ** ** @return the special sound unique identifier */ -CSound *RegisterTwoGroups(CSound *first, CSound *second) +std::shared_ptr RegisterTwoGroups(CSound *first, CSound *second) { if (first == nullptr || second == nullptr) { return nullptr; } - CSound *id = new CSound; + auto id = CSound::make(); id->Number = TWO_GROUPS; id->Sound.TwoGroups.First = first; id->Sound.TwoGroups.Second = second; diff --git a/src/sound/sound_id.cpp b/src/sound/sound_id.cpp index 46a4d2b9e2..54e4409c9e 100644 --- a/src/sound/sound_id.cpp +++ b/src/sound/sound_id.cpp @@ -46,17 +46,16 @@ -- Variables ----------------------------------------------------------------------------*/ -static std::map> SoundMap; +static std::map, std::less<>> SoundMap; /*---------------------------------------------------------------------------- -- Functions ----------------------------------------------------------------------------*/ -static CSound *FindSound(const std::string_view &name) +static std::shared_ptr FindSound(const std::string_view &name) { - std::map::iterator ret = SoundMap.find(name); - if (ret != SoundMap.end()) { - return (*ret).second; + if (auto it = SoundMap.find(name); it != SoundMap.end()) { + return (*it).second; } return nullptr; } @@ -68,14 +67,13 @@ static CSound *FindSound(const std::string_view &name) ** @param name Name of the sound. ** @param id Sound identifier. */ -void MapSound(const std::string &name, CSound *id) +void MapSound(const std::string &name, std::shared_ptr id) { if (!id) { DebugPrint("Null Sound for %s is not acceptable by sound table\n", name.c_str()); return; } - id->Mapref++; - SoundMap[name] = id; + SoundMap[name] = std::move(id); } /** @@ -85,11 +83,10 @@ void MapSound(const std::string &name, CSound *id) ** ** @return Sound identifier for this name. */ -CSound *SoundForName(const std::string_view &name) +std::shared_ptr SoundForName(const std::string_view &name) { Assert(!name.empty()); - CSound *sound = FindSound(name); - if (sound) { + if (std::shared_ptr sound = FindSound(name)) { return sound; } DebugPrint("Can't find sound '%s' in sound table\n", name.data()); @@ -108,20 +105,18 @@ CSound *SoundForName(const std::string_view &name) ** ** @return the sound id of the created group */ -CSound *MakeSound(const std::string &name, const std::vector &files) +std::shared_ptr MakeSound(const std::string &name, const std::vector &files) { - CSound *sound = FindSound(name); - - if (sound) { + if (auto sound = FindSound(name)) { DebugPrint("re-register sound '%s'\n", name.c_str()); return sound; } - sound = RegisterSound(files); - if (sound != nullptr) { + if (auto sound = RegisterSound(files)) { MapSound(name, sound); + return sound; } - return sound; + return nullptr; } /** @@ -137,30 +132,22 @@ CSound *MakeSound(const std::string &name, const std::vector &files ** ** @return Registered sound identifier. */ -CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second) +std::shared_ptr MakeSoundGroup(const std::string &name, CSound *first, CSound *second) { - CSound *sound = FindSound(name); - - if (sound) { + if (auto sound = FindSound(name)) { DebugPrint("re-register sound '%s'\n", name.c_str()); return sound; } - - sound = RegisterTwoGroups(first, second); - if (sound != nullptr) { + if (auto sound = RegisterTwoGroups(first, second)) { MapSound(name, sound); + return sound; } - return sound; + return nullptr; } void FreeSounds() { - for (auto& [_, sound] : SoundMap) { - Assert(sound && sound->Mapref != 0); - if (sound && !--sound->Mapref) { - delete sound; - } - } + SoundMap.clear(); } //@} diff --git a/src/spell/spells.cpp b/src/spell/spells.cpp index fa02f07632..4a76da1126 100644 --- a/src/spell/spells.cpp +++ b/src/spell/spells.cpp @@ -506,10 +506,10 @@ int SpellCast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i // if (spell.SoundWhenCast.Sound) { if (spell.Target == ETarget::Self) { - PlayUnitSound(caster, spell.SoundWhenCast.Sound); + PlayUnitSound(caster, spell.SoundWhenCast.Sound.get()); } else { PlayGameSound( - spell.SoundWhenCast.Sound, + spell.SoundWhenCast.Sound.get(), VolumeForDistance(ViewPointDistance(target ? target->tilePos : goalPos), spell.SoundWhenCast.Sound->Range)); } diff --git a/src/stratagus/player.cpp b/src/stratagus/player.cpp index d9a8f774ce..25b51439d1 100644 --- a/src/stratagus/player.cpp +++ b/src/stratagus/player.cpp @@ -1064,7 +1064,7 @@ int CPlayer::CheckCosts(const int (&costs)[MaxCosts], bool notify) const Notify(_("Not enough %s...%s more %s."), _(name), _(actionName), _(name)); if (this == ThisPlayer && GameSounds.NotEnoughRes[this->Race][i].Sound) { - PlayGameSound(GameSounds.NotEnoughRes[this->Race][i].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.NotEnoughRes[this->Race][i].Sound.get(), MaxSampleVolume); } } err |= 1 << i; diff --git a/src/ui/botpanel.cpp b/src/ui/botpanel.cpp index 13bfa871f7..08e9e49fc9 100644 --- a/src/ui/botpanel.cpp +++ b/src/ui/botpanel.cpp @@ -1109,7 +1109,7 @@ void CButtonPanel::DoClicked_SpellCast(int button) const int spellId = CurrentButtons[button].Value; if (KeyModifiers & ModifierControl) { if (!SpellTypeTable[spellId]->AutoCast) { - PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound.get(), MaxSampleVolume); return; } @@ -1273,7 +1273,7 @@ void CButtonPanel::DoClicked_Train(int button) } else if (ThisPlayer->CheckLimits(type) == -3) { if (GameSounds.NotEnoughFood[ThisPlayer->Race].Sound) { - PlayGameSound(GameSounds.NotEnoughFood[ThisPlayer->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.NotEnoughFood[ThisPlayer->Race].Sound.get(), MaxSampleVolume); } } } @@ -1299,7 +1299,7 @@ void CButtonPanel::DoClicked_Train(int button) UI.StatusLine.ClearCosts(); } else if (Selected[0]->Player->CheckLimits(type) == -3) { if (GameSounds.NotEnoughFood[Selected[0]->Player->Race].Sound) { - PlayGameSound(GameSounds.NotEnoughFood[Selected[0]->Player->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.NotEnoughFood[Selected[0]->Player->Race].Sound.get(), MaxSampleVolume); } } } @@ -1362,9 +1362,9 @@ void CButtonPanel::DoClicked(int button) if (CurrentButtons[button].Pos == -1 || !(ThisPlayer->IsTeamed(*Selected[0]) || Selected[0]->Player->Index == PlayerMax - 1)) { return; } - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); if (CurrentButtons[button].CommentSound.Sound) { - PlayGameSound(CurrentButtons[button].CommentSound.Sound, MaxSampleVolume); + PlayGameSound(CurrentButtons[button].CommentSound.Sound.get(), MaxSampleVolume); } // Handle action on button. diff --git a/src/ui/mouse.cpp b/src/ui/mouse.cpp index f40b017452..b47cbe5937 100644 --- a/src/ui/mouse.cpp +++ b/src/ui/mouse.cpp @@ -1692,7 +1692,7 @@ static void UIHandleButtonDown_OnMap(unsigned button) // 0 Test build, don't really build if (CanBuildUnitType(Selected[0], *CursorBuilding, tilePos, 0) && (explored || ReplayRevealMap)) { const int flush = !(KeyModifiers & ModifierShift); - PlayGameSound(GameSounds.PlacementSuccess[ThisPlayer->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.PlacementSuccess[ThisPlayer->Race].Sound.get(), MaxSampleVolume); PlayUnitSound(*Selected[0], EUnitVoice::Build); for (CUnit *unit : Selected) { SendCommandBuildBuilding(*unit, tilePos, *CursorBuilding, flush); @@ -1701,7 +1701,8 @@ static void UIHandleButtonDown_OnMap(unsigned button) CancelBuildingMode(); } } else { - PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.PlacementError[ThisPlayer->Race].Sound.get(), + MaxSampleVolume); } } else { CancelBuildingMode(); @@ -1768,17 +1769,17 @@ static void UIHandleButtonDown_OnButton(unsigned button) { // clicked on info panel - selection shown if (Selected.size() > 1 && ButtonAreaUnderCursor == ButtonArea::Selected) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); DoSelectionButtons(ButtonUnderCursor, button); } else if ((MouseButtons & LeftButton)) { // clicked on menu button if (ButtonAreaUnderCursor == ButtonArea::Menu) { if ((ButtonUnderCursor == ButtonUnderMenu || ButtonUnderCursor == ButtonUnderNetworkMenu) && !GameMenuButtonClicked) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); GameMenuButtonClicked = true; } else if (ButtonUnderCursor == ButtonUnderNetworkDiplomacy && !GameDiplomacyButtonClicked) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); GameDiplomacyButtonClicked = true; } else if (ButtonUnderCursor == ButtonUnderFreeWorkers) { UiFindIdleWorker(); @@ -1789,7 +1790,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) CUIUserButton &button = UI.UserButtons[i]; if (i == size_t(ButtonUnderCursor) && !button.Clicked) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); button.Clicked = true; } } @@ -1797,7 +1798,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) } else if (ButtonAreaUnderCursor == ButtonArea::Selected) { // clicked on single unit shown if (ButtonUnderCursor == 0 && Selected.size() == 1) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); UI.SelectedViewport->Center(Selected[0]->GetMapPixelPosCenter()); } // clicked on training button @@ -1810,7 +1811,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) DebugPrint("Cancel slot %d %s\n", ButtonUnderCursor, order.GetUnitType().Ident.c_str()); - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); SendCommandCancelTraining(*Selected[0], ButtonUnderCursor, &order.GetUnitType()); } } @@ -1819,7 +1820,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) if (!GameObserve && !GamePaused && !GameEstablishing && ThisPlayer->IsTeamed(*Selected[0])) { if (ButtonUnderCursor == 0 && Selected.size() == 1) { DebugPrint("Cancel upgrade %s\n", Selected[0]->Type->Ident.c_str()); - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); SendCommandCancelUpgradeTo(*Selected[0]); } } @@ -1828,7 +1829,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) if (!GameObserve && !GamePaused && !GameEstablishing && ThisPlayer->IsTeamed(*Selected[0])) { if (ButtonUnderCursor == 0 && Selected.size() == 1) { DebugPrint("Cancel research %s\n", Selected[0]->Type->Ident.c_str()); - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); SendCommandCancelResearch(*Selected[0]); } } @@ -1851,7 +1852,7 @@ static void UIHandleButtonDown_OnButton(unsigned button) Assert(uins->Boarded); const int flush = !(KeyModifiers & ModifierShift); if (ThisPlayer->IsTeamed(*Selected[0]) || uins->Player == ThisPlayer) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); SendCommandUnload(*Selected[0], Selected[0]->tilePos, uins, flush); } } @@ -1863,14 +1864,14 @@ static void UIHandleButtonDown_OnButton(unsigned button) } } else if (ButtonAreaUnderCursor == ButtonArea::Button) { if (!GameObserve && !GamePaused && !GameEstablishing && (ThisPlayer->IsTeamed(*Selected[0]) || Selected[0]->Player->Index == PlayerMax - 1)) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); OldButtonUnderCursor = ButtonUnderCursor; } } } else if ((MouseButtons & MiddleButton)) { // clicked on info panel - single unit shown if (ButtonAreaUnderCursor == ButtonArea::Selected && ButtonUnderCursor == 0 && Selected.size() == 1) { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); if (UI.SelectedViewport->Unit == Selected[0]) { UI.SelectedViewport->Unit = nullptr; } else { @@ -2164,12 +2165,12 @@ void UIHandleButtonUp(unsigned button) PlayUnitSound(*Selected[0], EUnitVoice::Building); } else if (Selected[0]->Burning) { // FIXME: use GameSounds.Burning - PlayGameSound(SoundForName("burning"), MaxSampleVolume); + PlayGameSound(SoundForName("burning").get(), MaxSampleVolume); } else if (Selected[0]->Player == ThisPlayer || ThisPlayer->IsTeamed(*Selected[0]) || Selected[0]->Player->Type == PlayerTypes::PlayerNeutral) { PlayUnitSound(*Selected[0], EUnitVoice::Selected); } else { - PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Click.Sound.get(), MaxSampleVolume); } if (Selected[0]->Player == ThisPlayer) { char buf[64]; diff --git a/src/unit/unit.cpp b/src/unit/unit.cpp index 47f2167ee2..950b9da2b8 100644 --- a/src/unit/unit.cpp +++ b/src/unit/unit.cpp @@ -2014,7 +2014,7 @@ void RescueUnits() unit->RescuedFrom = unit->Player; unit->ChangeOwner(*around[i]->Player); unit->Blink = 5; - PlayGameSound(GameSounds.Rescue[unit->Player->Race].Sound, MaxSampleVolume); + PlayGameSound(GameSounds.Rescue[unit->Player->Race].Sound.get(), MaxSampleVolume); break; } }