Skip to content

Commit

Permalink
Remove CSound::Mapref and use std::shared_ptr instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Apr 20, 2024
1 parent e58bf6e commit dcc91c7
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 107 deletions.
4 changes: 2 additions & 2 deletions src/action/action_research.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/animation/animation_randomsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/animation/animation_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/editor/editloop.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// _________ __ __
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/replay.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// _________ __ __
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 19 additions & 15 deletions src/include/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,33 @@ class GameSound
/**
** Sound definition.
*/
class CSound
class CSound : public std::enable_shared_from_this<CSound>
{
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<CSound> make() { return std::make_shared<CSound>(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
struct {
CSound *First; /// first group: selected sound
CSound *Second; /// second group: annoyed sound
} TwoGroups; /// when it's a double group
} Sound;
} Sound{};
};

/**
Expand Down Expand Up @@ -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<std::string> &files);
extern std::shared_ptr<CSound> RegisterSound(const std::vector<std::string> &files);

/// Create a special sound group with two sounds
extern CSound *RegisterTwoGroups(CSound *first, CSound *second);
extern std::shared_ptr<CSound> RegisterTwoGroups(CSound *first, CSound *second);

/// Initialize client side of the sound layer.
extern void InitSoundClient();
Expand All @@ -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<CSound>);
/// Get the sound id bound to an identifier
extern CSound *SoundForName(const std::string_view &sound_name);
extern std::shared_ptr<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);
extern std::shared_ptr<CSound> MakeSound(const std::string &sound_name, const std::vector<std::string> &files);
/// Make a sound group bound to identifier
extern CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second);
extern std::shared_ptr<CSound> MakeSoundGroup(const std::string &name, CSound *first, CSound *second);

extern void FreeSounds();

Expand Down
4 changes: 3 additions & 1 deletion src/include/unitsound.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "upgrade_structs.h"
#endif

#include <memory>

#define ANIMATIONS_DEATHTYPES 40
/*----------------------------------------------------------------------------
-- Declarations
Expand All @@ -62,7 +64,7 @@ class SoundConfig

public:
std::string Name; /// config sound name
CSound *Sound = nullptr; /// identifier send to sound server
std::shared_ptr<CSound> Sound; /// identifier send to sound server
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/missile/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
25 changes: 13 additions & 12 deletions src/sound/script_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -111,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 = nullptr;
std::shared_ptr<CSound> id;
if (lua_isstring(l, 2)) {
// only one file
files.push_back(std::string{LuaToString(l, 2)});
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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<CSound *>(data->Data)->shared_from_this();
}

/**
Expand All @@ -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<CSound *>(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<CSound *>(data->Data)->shared_from_this();
} else if (value == "placement-error") {
SetSoundConfigRace(l, j, GameSounds.PlacementError);
} else if (value == "placement-success") {
Expand All @@ -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<CSound *>(data->Data)->shared_from_this();
} else if (value == "not-enough-food") {
SetSoundConfigRace(l, j, GameSounds.NotEnoughFood);
} else if (value == "rescue") {
Expand All @@ -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<CSound *>(data->Data)->shared_from_this();
} else {
LuaError(l, "Unsupported tag: %s", value.data());
}
Expand Down
17 changes: 7 additions & 10 deletions src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CSound> ChooseUnitVoiceSound(const CUnit &unit, EUnitVoice voice)
{
switch (voice) {
case EUnitVoice::Acknowledging: return unit.Type->MapSound.Acknowledgement.Sound;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -424,27 +424,24 @@ int PlayFile(const std::string &name, LuaActionListener *listener)
**
** @todo FIXME: Must handle the errors better.
*/
CSound *RegisterSound(const std::vector<std::string> &files)
std::shared_ptr<CSound> RegisterSound(const std::vector<std::string> &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<unsigned char>(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;
Expand All @@ -461,12 +458,12 @@ CSound *RegisterSound(const std::vector<std::string> &files)
**
** @return the special sound unique identifier
*/
CSound *RegisterTwoGroups(CSound *first, CSound *second)
std::shared_ptr<CSound> 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;
Expand Down
Loading

0 comments on commit dcc91c7

Please sign in to comment.