From 57422263b490d2b7e6c6e38ff06ff565b2f2839a Mon Sep 17 00:00:00 2001 From: Architector #4 Date: Wed, 28 Jan 2026 10:49:41 +0300 Subject: [PATCH] [BROKEN] make SoundContainer use shared_ptr everywhere i tried but luabind 0.7.1 will not eat `std::shared_ptr` and i can't use `boost::shared_ptr` because the included boost headers literally do not have it for some reason so i'm just leaving this here as an archive of my work that might one day become workable if luabind is ever updated in this project --- Source/Entities/ACrab.cpp | 10 +- Source/Entities/ACrab.h | 20 ++- Source/Entities/ACraft.cpp | 34 ++--- Source/Entities/ACraft.h | 58 +++++--- Source/Entities/ADoor.cpp | 56 ++++---- Source/Entities/ADoor.h | 77 ++++++---- Source/Entities/AEmitter.cpp | 31 ++--- Source/Entities/AEmitter.h | 58 +++++--- Source/Entities/AHuman.cpp | 11 +- Source/Entities/AHuman.h | 20 ++- Source/Entities/Activity.cpp | 2 +- Source/Entities/Actor.cpp | 52 +++---- Source/Entities/Actor.h | 95 ++++++++----- Source/Entities/DynamicSong.cpp | 40 +++--- Source/Entities/DynamicSong.h | 25 ++-- Source/Entities/HDFirearm.cpp | 81 ++++++----- Source/Entities/HDFirearm.h | 155 +++++++++++++-------- Source/Entities/MOSRotating.cpp | 11 +- Source/Entities/MOSRotating.h | 20 ++- Source/Entities/PieMenu.cpp | 6 +- Source/Entities/SoundContainer.cpp | 21 ++- Source/Entities/SoundContainer.h | 6 + Source/Entities/TDExplosive.cpp | 6 +- Source/GUI/GUISound.cpp | 134 +++++++++--------- Source/GUI/GUISound.h | 109 ++++++++------- Source/Lua/LuaAdapterDefinitions.h | 46 +++--- Source/Lua/LuaAdapters.cpp | 117 ++++++++++++---- Source/Lua/LuaBindingRegisterDefinitions.h | 23 ++- Source/Lua/LuaBindingsEntities.cpp | 54 +++---- Source/Lua/LuaBindingsManagers.cpp | 7 +- Source/Lua/LuabindDefinitions.h | 7 + Source/Managers/AudioMan.cpp | 25 ++-- Source/Managers/AudioMan.h | 11 +- Source/Managers/MusicMan.cpp | 16 ++- Source/Managers/MusicMan.h | 13 +- Source/Managers/SceneMan.cpp | 8 +- Source/Managers/SceneMan.h | 2 +- Source/Menus/InventoryMenuGUI.cpp | 2 +- Source/Menus/MetagameGUI.cpp | 2 +- Source/Menus/TitleScreen.cpp | 12 +- Source/System/Serializable.h | 3 +- 41 files changed, 875 insertions(+), 611 deletions(-) diff --git a/Source/Entities/ACrab.cpp b/Source/Entities/ACrab.cpp index d6c8c559a0..5aae71b501 100644 --- a/Source/Entities/ACrab.cpp +++ b/Source/Entities/ACrab.cpp @@ -202,7 +202,8 @@ int ACrab::Create(const ACrab& reference) { m_BackupRBGFootGroup->SetLimbPos(atomGroupToUseAsFootGroupLFG->GetLimbPos()); if (reference.m_StrideSound) { - m_StrideSound = dynamic_cast(reference.m_StrideSound->Clone()); + m_StrideSound = std::make_shared(); + reference.m_StrideSound->Clone(&*m_StrideSound); } m_MovementState = reference.m_MovementState; @@ -297,8 +298,8 @@ int ACrab::ReadProperty(const std::string_view& propName, Reader& reader) { m_BackupRBGFootGroup->RemoveAllAtoms(); }); MatchProperty("StrideSound", { - m_StrideSound = new SoundContainer; - reader >> m_StrideSound; + m_StrideSound = std::make_shared(); + reader >> *m_StrideSound; }); MatchForwards("LStandLimbPath") MatchProperty("LeftStandLimbPath", { reader >> m_Paths[LEFTSIDE][FGROUND][STAND]; }); MatchForwards("LWalkLimbPath") MatchProperty("LeftWalkLimbPath", { reader >> m_Paths[LEFTSIDE][FGROUND][WALK]; }); @@ -337,7 +338,7 @@ int ACrab::Save(Writer& writer) const { writer.NewProperty("RBGFootGroup"); writer << m_pRBGFootGroup; writer.NewProperty("StrideSound"); - writer << m_StrideSound; + writer << *m_StrideSound; writer.NewProperty("LStandLimbPath"); writer << m_Paths[LEFTSIDE][FGROUND][STAND]; @@ -368,7 +369,6 @@ void ACrab::Destroy(bool notInherited) { delete m_pRFGFootGroup; delete m_pRBGFootGroup; - delete m_StrideSound; // for (deque::iterator itr = m_WalkPaths.begin(); // itr != m_WalkPaths.end(); ++itr) // delete *itr; diff --git a/Source/Entities/ACrab.h b/Source/Entities/ACrab.h index 800e5056cf..1123f2c763 100644 --- a/Source/Entities/ACrab.h +++ b/Source/Entities/ACrab.h @@ -8,6 +8,7 @@ #include "Actor.h" #include "LimbPath.h" #include "Leg.h" +#include struct BITMAP; @@ -243,13 +244,18 @@ namespace RTE { /// @param newForce New push force value in kg * m/s^2. void SetLimbPathPushForce(MovementState movementState, float newForce); - /// Gets this ACrab's stride sound. Ownership is NOT transferred! + /// Gets this ACrab's stride sound. /// @return The SoundContainer for this ACrab's stride sound. - SoundContainer* GetStrideSound() const { return m_StrideSound; } - - /// Sets this ACrab's stride sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ACrab's stride sound. - void SetStrideSound(SoundContainer* newSound) { m_StrideSound = newSound; } + std::shared_ptr GetStrideSound() const { return m_StrideSound; } + + /// Sets this ACrab's stride sound to a copy of the input. + /// @param newSound The new SoundContainer for this ACrab's stride sound to copy. + void SetStrideSound(const SoundContainer* newSound) { + if (!newSound) + m_StrideSound = nullptr; + else + m_StrideSound = std::make_shared(*newSound); + } /// Gets the upper limit of this ACrab's aim range. /// @return The upper limit of this ACrab's aim range. @@ -301,7 +307,7 @@ namespace RTE { AtomGroup* m_pRBGFootGroup; AtomGroup* m_BackupRBGFootGroup; // The sound of the actor taking a step (think robot servo) - SoundContainer* m_StrideSound; + std::shared_ptr m_StrideSound; // Jetpack booster. AEJetpack* m_pJetpack; // Blink timer diff --git a/Source/Entities/ACraft.cpp b/Source/Entities/ACraft.cpp index f43c42db59..8f633b74b3 100644 --- a/Source/Entities/ACraft.cpp +++ b/Source/Entities/ACraft.cpp @@ -230,12 +230,15 @@ int ACraft::Create(const ACraft& reference) { m_HatchState = reference.m_HatchState; m_HatchDelay = reference.m_HatchDelay; if (reference.m_HatchOpenSound) { - m_HatchOpenSound = dynamic_cast(reference.m_HatchOpenSound->Clone()); + m_HatchOpenSound = std::make_shared(); + reference.m_HatchOpenSound->Clone(&*m_HatchOpenSound); } if (reference.m_HatchCloseSound) { - m_HatchCloseSound = dynamic_cast(reference.m_HatchCloseSound->Clone()); + m_HatchCloseSound = std::make_shared(); + reference.m_HatchCloseSound->Clone(&*m_HatchCloseSound); } else if (reference.m_HatchOpenSound) { - m_HatchCloseSound = dynamic_cast(reference.m_HatchOpenSound->Clone()); + m_HatchCloseSound = std::make_shared(); + reference.m_HatchOpenSound->Clone(&*m_HatchCloseSound); } for (std::deque::const_iterator niItr = reference.m_CollectedInventory.begin(); niItr != reference.m_CollectedInventory.end(); ++niItr) m_CollectedInventory.push_back(dynamic_cast((*niItr)->Clone())); @@ -246,7 +249,8 @@ int ACraft::Create(const ACraft& reference) { m_HasDelivered = reference.m_HasDelivered; m_LandingCraft = reference.m_LandingCraft; if (reference.m_CrashSound) { - m_CrashSound = dynamic_cast(reference.m_CrashSound->Clone()); + m_CrashSound = std::make_shared(); + reference.m_CrashSound->Clone(&*m_CrashSound); } m_DeliveryState = reference.m_DeliveryState; @@ -267,16 +271,16 @@ int ACraft::ReadProperty(const std::string_view& propName, Reader& reader) { MatchProperty("HatchDelay", { reader >> m_HatchDelay; }); MatchProperty("HatchOpenSound", { - m_HatchOpenSound = new SoundContainer; - reader >> m_HatchOpenSound; + m_HatchOpenSound = std::make_shared(); + reader >> *m_HatchOpenSound; }); MatchProperty("HatchCloseSound", { - m_HatchCloseSound = new SoundContainer; - reader >> m_HatchCloseSound; + m_HatchCloseSound = std::make_shared(); + reader >> *m_HatchCloseSound; }); MatchProperty("CrashSound", { - m_CrashSound = new SoundContainer; - reader >> m_CrashSound; + m_CrashSound = std::make_shared(); + reader >> *m_CrashSound; }); MatchProperty("AddExit", { @@ -301,9 +305,9 @@ int ACraft::Save(Writer& writer) const { writer.NewProperty("HatchDelay"); writer << m_HatchDelay; writer.NewProperty("HatchOpenSound"); - writer << m_HatchOpenSound; + writer << *m_HatchOpenSound; writer.NewProperty("HatchCloseSound"); - writer << m_HatchCloseSound; + writer << *m_HatchCloseSound; for (std::list::const_iterator itr = m_Exits.begin(); itr != m_Exits.end(); ++itr) { writer.NewProperty("AddExit"); writer << (*itr); @@ -316,7 +320,7 @@ int ACraft::Save(Writer& writer) const { writer << m_LandingCraft; writer.NewProperty("CrashSound"); - writer << m_CrashSound; + writer << *m_CrashSound; writer.NewProperty("CanEnterOrbit"); writer << m_CanEnterOrbit; @@ -332,10 +336,6 @@ int ACraft::Save(Writer& writer) const { } void ACraft::Destroy(bool notInherited) { - delete m_HatchOpenSound; - delete m_HatchCloseSound; - delete m_CrashSound; - if (!notInherited) Actor::Destroy(); Clear(); diff --git a/Source/Entities/ACraft.h b/Source/Entities/ACraft.h index b276a13bee..4c6d276964 100644 --- a/Source/Entities/ACraft.h +++ b/Source/Entities/ACraft.h @@ -7,6 +7,7 @@ /// Inclusions of header files #include "Actor.h" #include "LimbPath.h" +#include struct BITMAP; @@ -305,29 +306,44 @@ namespace RTE { /// @param movableObjectToIgnore A pointer to an MO which the Gibs and Attachables should not be colliding with. void GibThis(const Vector& impactImpulse = Vector(), MovableObject* movableObjectToIgnore = nullptr) override; - /// Gets this ACraft's hatch opening sound. Ownership is NOT transferred! + /// Gets this ACraft's hatch opening sound. /// @return The SoundContainer for this ACraft's hatch opening sound. - SoundContainer* GetHatchOpenSound() const { return m_HatchOpenSound; } - - /// Sets this ACraft's hatch opening sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ACraft's hatch opening sound. - void SetHatchOpenSound(SoundContainer* newSound) { m_HatchOpenSound = newSound; } + std::shared_ptr GetHatchOpenSound() const { return m_HatchOpenSound; } + + /// Sets this ACraft's hatch opening sound to a copy of the input. + /// @param newSound The new SoundContainer for this ACraft's hatch opening sound to copy. + void SetHatchOpenSound(const SoundContainer* newSound) { + if (!newSound) + m_HatchOpenSound = nullptr; + else + m_HatchOpenSound = std::make_shared(*newSound); + } - /// Gets this ACraft's hatch closing sound. Ownership is NOT transferred! + /// Gets this ACraft's hatch closing sound. /// @return The SoundContainer for this ACraft's hatch closing sound. - SoundContainer* GetHatchCloseSound() const { return m_HatchCloseSound; } - - /// Sets this ACraft's hatch closing sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ACraft's hatch closing sound. - void SetHatchCloseSound(SoundContainer* newSound) { m_HatchCloseSound = newSound; } + std::shared_ptr GetHatchCloseSound() const { return m_HatchCloseSound; } + + /// Sets this ACraft's hatch closing sound to a copy of the input. + /// @param newSound The new SoundContainer for this ACraft's hatch closing sound to copy. + void SetHatchCloseSound(const SoundContainer* newSound) { + if (!newSound) + m_HatchCloseSound = nullptr; + else + m_HatchCloseSound = std::make_shared(*newSound); + } - /// Gets this ACraft's crash sound. Ownership is NOT transferred! + /// Gets this ACraft's crash sound. /// @return The SoundContainer for this ACraft's crash sound. - SoundContainer* GetCrashSound() const { return m_CrashSound; } - - /// Sets this ACraft's crash sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ACraft's crash sound. - void SetCrashSound(SoundContainer* newSound) { m_CrashSound = newSound; } + std::shared_ptr GetCrashSound() const { return m_CrashSound; } + + /// Sets this ACraft's crash sound to a copy of the input. + /// @param newSound The new SoundContainer for this ACraft's crash sound to copy. + void SetCrashSound(const SoundContainer* newSound) { + if (!newSound) + m_CrashSound = nullptr; + else + m_CrashSound = std::make_shared(*newSound); + } /// Protected member variable and method declarations protected: @@ -340,9 +356,9 @@ namespace RTE { // The time it takes to open or close the hatch, in ms. int m_HatchDelay; // Sound for opening the hatch - SoundContainer* m_HatchOpenSound; + std::shared_ptr m_HatchOpenSound; // Sound for closing the hatch - SoundContainer* m_HatchCloseSound; + std::shared_ptr m_HatchCloseSound; std::deque m_CollectedInventory; //!< A separate inventory to temporarily store newly collected items, so that they don't get immediately ejected from the main inventory while the hatch is still open. // All the possible exits for when ejecting stuff out of this. std::list m_Exits; @@ -363,7 +379,7 @@ namespace RTE { // Timer to measure how long ago a crash sound was played Timer m_CrashTimer; // Crash sound - SoundContainer* m_CrashSound; + std::shared_ptr m_CrashSound; // Whether this can enter orbit and refund the owning team. If false, will use default out-of-bounds deletion behavior. bool m_CanEnterOrbit; // The maximum number of actors that fit in the inventory diff --git a/Source/Entities/ADoor.cpp b/Source/Entities/ADoor.cpp index 095f601ba5..1b75efe78f 100644 --- a/Source/Entities/ADoor.cpp +++ b/Source/Entities/ADoor.cpp @@ -91,19 +91,23 @@ int ADoor::Create(const ADoor& reference) { m_DoorMaterialID = reference.m_DoorMaterialID; if (reference.m_DoorMoveStartSound) { - m_DoorMoveStartSound.reset(dynamic_cast(reference.m_DoorMoveStartSound->Clone())); + m_DoorMoveStartSound = std::make_shared(); + reference.m_DoorMoveStartSound->Clone(&*m_DoorMoveStartSound); } if (reference.m_DoorMoveSound) { - m_DoorMoveSound.reset(dynamic_cast(reference.m_DoorMoveSound->Clone())); + m_DoorMoveSound = std::make_shared(); + reference.m_DoorMoveSound->Clone(&*m_DoorMoveSound); } if (reference.m_DoorDirectionChangeSound) { - m_DoorDirectionChangeSound.reset(dynamic_cast(reference.m_DoorDirectionChangeSound->Clone())); + m_DoorDirectionChangeSound = std::make_shared(); + reference.m_DoorDirectionChangeSound->Clone(&*m_DoorDirectionChangeSound); } if (reference.m_DoorMoveEndSound) { - m_DoorMoveEndSound.reset(dynamic_cast(reference.m_DoorMoveEndSound->Clone())); + m_DoorMoveEndSound = std::make_shared(); + reference.m_DoorMoveEndSound->Clone(&*m_DoorMoveEndSound); } return 0; @@ -152,10 +156,22 @@ int ADoor::ReadProperty(const std::string_view& propName, Reader& reader) { }); MatchProperty("DrawMaterialLayerWhenOpen", { reader >> m_DrawMaterialLayerWhenOpen; }); MatchProperty("DrawMaterialLayerWhenClosed", { reader >> m_DrawMaterialLayerWhenClosed; }); - MatchProperty("DoorMoveStartSound", { m_DoorMoveStartSound.reset(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); - MatchProperty("DoorMoveSound", { m_DoorMoveSound.reset(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); - MatchProperty("DoorDirectionChangeSound", { m_DoorDirectionChangeSound.reset(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); - MatchProperty("DoorMoveEndSound", { m_DoorMoveEndSound.reset(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); + MatchProperty("DoorMoveStartSound", { + m_DoorMoveStartSound = std::make_shared(); + reader >> *m_DoorMoveStartSound; + }); + MatchProperty("DoorMoveSound", { + m_DoorMoveSound = std::make_shared(); + reader >> *m_DoorMoveSound; + }); + MatchProperty("DoorDirectionChangeSound", { + m_DoorDirectionChangeSound = std::make_shared(); + reader >> *m_DoorDirectionChangeSound; + }); + MatchProperty("DoorMoveEndSound", { + m_DoorMoveEndSound = std::make_shared(); + reader >> *m_DoorMoveEndSound; + }); EndPropertyList; } @@ -190,13 +206,13 @@ int ADoor::Save(Writer& writer) const { writer.NewProperty("DrawMaterialLayerWhenClosed"); writer << m_DrawMaterialLayerWhenClosed; writer.NewProperty("DoorMoveStartSound"); - writer << m_DoorMoveStartSound.get(); + writer << *m_DoorMoveStartSound; writer.NewProperty("DoorMoveSound"); - writer << m_DoorMoveSound.get(); + writer << *m_DoorMoveSound; writer.NewProperty("DoorDirectionChangeSound"); - writer << m_DoorDirectionChangeSound.get(); + writer << *m_DoorDirectionChangeSound; writer.NewProperty("DoorMoveEndSound"); - writer << m_DoorMoveEndSound.get(); + writer << *m_DoorMoveEndSound; return 0; } @@ -298,22 +314,6 @@ bool ADoor::EraseDoorMaterial(bool updateMaterialArea) { return false; } -void ADoor::SetDoorMoveStartSound(SoundContainer* newSound) { - m_DoorMoveStartSound.reset(newSound); -} - -void ADoor::SetDoorMoveSound(SoundContainer* newSound) { - m_DoorMoveSound.reset(newSound); -} - -void ADoor::SetDoorDirectionChangeSound(SoundContainer* newSound) { - m_DoorDirectionChangeSound.reset(newSound); -} - -void ADoor::SetDoorMoveEndSound(SoundContainer* newSound) { - m_DoorMoveEndSound.reset(newSound); -} - void ADoor::TempEraseOrRedrawDoorMaterial(bool erase) { if (!g_SceneMan.GetTerrain() || !g_SceneMan.GetTerrain()->GetMaterialBitmap()) { return; diff --git a/Source/Entities/ADoor.h b/Source/Entities/ADoor.h index b28eceb191..17c1223dcc 100644 --- a/Source/Entities/ADoor.h +++ b/Source/Entities/ADoor.h @@ -2,6 +2,7 @@ #include "Actor.h" #include "ADSensor.h" +#include namespace RTE { @@ -78,37 +79,57 @@ namespace RTE { /// @return Whether or not this ADoor's door material has been drawn. bool GetDoorMaterialDrawn() const { return m_DoorMaterialDrawn; } - /// Gets this ADoor's door move start sound. Ownership is NOT transferred! + /// Gets this ADoor's door move start sound. /// @return The SoundContainer for this ADoor's door move start sound. - SoundContainer* GetDoorMoveStartSound() const { return m_DoorMoveStartSound.get(); } - - /// Sets this ADoor's door move start sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ADoor's door move start sound. - void SetDoorMoveStartSound(SoundContainer* newSound); + std::shared_ptr GetDoorMoveStartSound() const { return m_DoorMoveStartSound; } + + /// Sets this ADoor's door move start sound to a copy of the input. + /// @param newSound The new SoundContainer for this ADoor's door move start sound to copy. + void SetDoorMoveStartSound(const SoundContainer* newSound) { + if (!newSound) + m_DoorMoveStartSound = nullptr; + else + m_DoorMoveStartSound = std::make_shared(*newSound); + } - /// Gets this ADoor's door move sound. Ownership is NOT transferred! + /// Gets this ADoor's door move sound. /// @return The SoundContainer for this ADoor's door move sound. - SoundContainer* GetDoorMoveSound() const { return m_DoorMoveSound.get(); } - - /// Sets this ADoor's door move sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ADoor's door move sound. - void SetDoorMoveSound(SoundContainer* newSound); + std::shared_ptr GetDoorMoveSound() const { return m_DoorMoveSound; } + + /// Sets this ADoor's door move sound to a copy of the input. + /// @param newSound The new SoundContainer for this ADoor's door move sound to copy. + void SetDoorMoveSound(const SoundContainer* newSound) { + if (!newSound) + m_DoorMoveSound = nullptr; + else + m_DoorMoveSound = std::make_shared(*newSound); + } - /// Gets this ADoor's door direction change sound. Ownership is NOT transferred! + /// Gets this ADoor's door direction change sound. /// @return The SoundContainer for this ADoor's door direction change sound. - SoundContainer* GetDoorDirectionChangeSound() const { return m_DoorDirectionChangeSound.get(); } - - /// Sets this ADoor's door direction change sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ADoor's door direction change sound. - void SetDoorDirectionChangeSound(SoundContainer* newSound); + std::shared_ptr GetDoorDirectionChangeSound() const { return m_DoorDirectionChangeSound; } + + /// Sets this ADoor's door direction change sound to a copy of the input. + /// @param newSound The new SoundContainer for this ADoor's door direction change sound to copy. + void SetDoorDirectionChangeSound(const SoundContainer* newSound) { + if (!newSound) + m_DoorDirectionChangeSound = nullptr; + else + m_DoorDirectionChangeSound = std::make_shared(*newSound); + } - /// Gets this ADoor's door move end sound. Ownership is NOT transferred! + /// Gets this ADoor's door move end sound. /// @return The SoundContainer for this ADoor's door move end sound. - SoundContainer* GetDoorMoveEndSound() const { return m_DoorMoveEndSound.get(); } - - /// Sets this ADoor's door move end sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this ADoor's door move end sound. - void SetDoorMoveEndSound(SoundContainer* newSound); + std::shared_ptr GetDoorMoveEndSound() const { return m_DoorMoveEndSound; } + + /// Sets this ADoor's door move end sound to a copy of the input. + /// @param newSound The new SoundContainer for this ADoor's door move end sound to copy. + void SetDoorMoveEndSound(const SoundContainer* newSound) { + if (!newSound) + m_DoorMoveEndSound = nullptr; + else + m_DoorMoveEndSound = std::make_shared(*newSound); + } #pragma endregion #pragma region Concrete Methods @@ -191,10 +212,10 @@ namespace RTE { Timer m_DoorMaterialRedrawTimer; //!< Timer for redrawing the door material layer from time-to-time. Without this, the door's material can be dug through, screwing with its collisions. Vector m_LastDoorMaterialPos; //!< The position the door attachable had when its material was drawn to the material bitmap. This is used to erase the previous material representation. - std::unique_ptr m_DoorMoveStartSound; //!< Sound played when the door starts moving from fully open/closed position towards the opposite end. - std::unique_ptr m_DoorMoveSound; //!< Sound played while the door is moving between open/closed position. - std::unique_ptr m_DoorDirectionChangeSound; //!< Sound played when the door is interrupted while moving and changes directions. - std::unique_ptr m_DoorMoveEndSound; //!< Sound played when the door stops moving and is at fully open/closed position. + std::shared_ptr m_DoorMoveStartSound; //!< Sound played when the door starts moving from fully open/closed position towards the opposite end. + std::shared_ptr m_DoorMoveSound; //!< Sound played while the door is moving between open/closed position. + std::shared_ptr m_DoorDirectionChangeSound; //!< Sound played when the door is interrupted while moving and changes directions. + std::shared_ptr m_DoorMoveEndSound; //!< Sound played when the door stops moving and is at fully open/closed position. private: #pragma region Update Breakdown diff --git a/Source/Entities/AEmitter.cpp b/Source/Entities/AEmitter.cpp index e407c1e403..a1e7a77c1b 100644 --- a/Source/Entities/AEmitter.cpp +++ b/Source/Entities/AEmitter.cpp @@ -68,13 +68,16 @@ int AEmitter::Create(const AEmitter& reference) { m_EmissionList.push_back(static_cast(emission->Clone())); } if (reference.m_EmissionSound) { - m_EmissionSound = dynamic_cast(reference.m_EmissionSound->Clone()); + m_EmissionSound = std::make_shared(); + reference.m_EmissionSound->Clone(&*m_EmissionSound); } if (reference.m_BurstSound) { - m_BurstSound = dynamic_cast(reference.m_BurstSound->Clone()); + m_BurstSound = std::make_shared(); + reference.m_BurstSound->Clone(&*m_BurstSound); } if (reference.m_EndSound) { - m_EndSound = dynamic_cast(reference.m_EndSound->Clone()); + m_EndSound = std::make_shared(); + reference.m_EndSound->Clone(&*m_EndSound); } m_EmitEnabled = reference.m_EmitEnabled; m_EmitCount = reference.m_EmitCount; @@ -110,16 +113,16 @@ int AEmitter::ReadProperty(const std::string_view& propName, Reader& reader) { m_EmissionList.push_back(emission); }); MatchProperty("EmissionSound", { - m_EmissionSound = new SoundContainer; - reader >> m_EmissionSound; + m_EmissionSound = std::make_shared(); + reader >> *m_EmissionSound; }); MatchProperty("BurstSound", { - m_BurstSound = new SoundContainer; - reader >> m_BurstSound; + m_BurstSound = std::make_shared(); + reader >> *m_BurstSound; }); MatchProperty("EndSound", { - m_EndSound = new SoundContainer; - reader >> m_EndSound; + m_EndSound = std::make_shared(); + reader >> *m_EndSound; }); MatchProperty("EmissionEnabled", { reader >> m_EmitEnabled; }); MatchProperty("EmissionCount", { reader >> m_EmitCount; }); @@ -171,11 +174,11 @@ int AEmitter::Save(Writer& writer) const { writer << *emission; } writer.NewProperty("EmissionSound"); - writer << m_EmissionSound; + writer << *m_EmissionSound; writer.NewProperty("BurstSound"); - writer << m_BurstSound; + writer << *m_BurstSound; writer.NewProperty("EndSound"); - writer << m_EndSound; + writer << *m_EndSound; writer.NewProperty("EmissionEnabled"); writer << m_EmitEnabled; writer.NewProperty("EmissionCount"); @@ -237,10 +240,6 @@ void AEmitter::Destroy(bool notInherited) { delete emission; } - delete m_EmissionSound; - delete m_BurstSound; - delete m_EndSound; - // m_BurstSound.Stop(); if (!notInherited) { diff --git a/Source/Entities/AEmitter.h b/Source/Entities/AEmitter.h index 98cf08b57a..2b69aa3272 100644 --- a/Source/Entities/AEmitter.h +++ b/Source/Entities/AEmitter.h @@ -7,6 +7,7 @@ /// Inclusions of header files #include "Attachable.h" #include "Emission.h" +#include namespace RTE { @@ -333,29 +334,44 @@ namespace RTE { /// @param newValue New number of emissions left void SetEmitCountLimit(long newValue) { m_EmitCountLimit = newValue; } - /// Gets this AEmitter's emission sound. Ownership is NOT transferred! + /// Gets this AEmitter's emission sound. /// @return The SoundContainer for this AEmitter's emission sound. - SoundContainer* GetEmissionSound() const { return m_EmissionSound; } - - /// Sets this AEmitter's emission sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this AEmitter's emission sound. - void SetEmissionSound(SoundContainer* newSound) { m_EmissionSound = newSound; } + std::shared_ptr GetEmissionSound() const { return m_EmissionSound; } + + /// Sets this AEmitter's emission sound to a copy of the input. + /// @param newSound The new SoundContainer for this AEmitter's emission sound to copy. + void SetEmissionSound(const SoundContainer* newSound) { + if (!newSound) + m_EmissionSound = nullptr; + else + m_EmissionSound = std::make_shared(*newSound); + } - /// Gets this AEmitter's burst sound. Ownership is NOT transferred! + /// Gets this AEmitter's burst sound. /// @return The SoundContainer for this AEmitter's burst sound. - SoundContainer* GetBurstSound() const { return m_BurstSound; } - - /// Sets this AEmitter's burst sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this AEmitter's burst sound. - void SetBurstSound(SoundContainer* newSound) { m_BurstSound = newSound; } + std::shared_ptr GetBurstSound() const { return m_BurstSound; } + + /// Sets this AEmitter's burst sound to a copy of the input. + /// @param newSound The new SoundContainer for this AEmitter's burst sound to copy. + void SetBurstSound(const SoundContainer* newSound) { + if (!newSound) + m_BurstSound = nullptr; + else + m_BurstSound = std::make_shared(*newSound); + } - /// Gets this AEmitter's end sound. Ownership is NOT transferred! + /// Gets this AEmitter's end sound. /// @return The SoundContainer for this AEmitter's end sound. - SoundContainer* GetEndSound() const { return m_EndSound; } - - /// Sets this AEmitter's end sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this AEmitter's end sound. - void SetEndSound(SoundContainer* newSound) { m_EndSound = newSound; } + std::shared_ptr GetEndSound() const { return m_EndSound; } + + /// Sets this AEmitter's end sound to a copy of the input. + /// @param newSound The new SoundContainer for this AEmitter's end sound to copy. + void SetEndSound(const SoundContainer* newSound) { + if (!newSound) + m_EndSound = nullptr; + else + m_EndSound = std::make_shared(*newSound); + } /// Returns whether this emitter just started emitting this frame. /// @return Whether this emitter just started emitting this frame. @@ -369,9 +385,9 @@ namespace RTE { // The list of MO instances that get emitted std::list m_EmissionList; // Sounds - SoundContainer* m_EmissionSound; - SoundContainer* m_BurstSound; - SoundContainer* m_EndSound; + std::shared_ptr m_EmissionSound; + std::shared_ptr m_BurstSound; + std::shared_ptr m_EndSound; // Whether emitting is currently enabled or not. bool m_EmitEnabled; // Whether or not the it was emitting last frame or not. diff --git a/Source/Entities/AHuman.cpp b/Source/Entities/AHuman.cpp index a21ef1dbe7..78c660368b 100644 --- a/Source/Entities/AHuman.cpp +++ b/Source/Entities/AHuman.cpp @@ -202,7 +202,8 @@ int AHuman::Create(const AHuman& reference) { m_MaxWalkPathCrouchShift = reference.m_MaxWalkPathCrouchShift; if (reference.m_StrideSound) { - m_StrideSound = dynamic_cast(reference.m_StrideSound->Clone()); + m_StrideSound = std::make_shared(); + reference.m_StrideSound->Clone(&*m_StrideSound); } m_ArmsState = reference.m_ArmsState; @@ -272,8 +273,8 @@ int AHuman::ReadProperty(const std::string_view& propName, Reader& reader) { }); MatchProperty("MaxWalkPathCrouchShift", { reader >> m_MaxWalkPathCrouchShift; }); MatchProperty("StrideSound", { - m_StrideSound = new SoundContainer; - reader >> m_StrideSound; + m_StrideSound = std::make_shared(); + reader >> *m_StrideSound; }); MatchProperty("StandLimbPath", { reader >> m_Paths[FGROUND][STAND]; }); MatchProperty("StandLimbPathBG", { reader >> m_Paths[BGROUND][STAND]; }); @@ -331,7 +332,7 @@ int AHuman::Save(Writer& writer) const { writer.NewProperty("MaxWalkPathCrouchShift"); writer << m_MaxWalkPathCrouchShift; writer.NewProperty("StrideSound"); - writer << m_StrideSound; + writer << &*m_StrideSound; writer.NewProperty("StandLimbPath"); writer << m_Paths[FGROUND][STAND]; @@ -369,8 +370,6 @@ void AHuman::Destroy(bool notInherited) { delete m_pFGFootGroup; delete m_pBGFootGroup; - delete m_StrideSound; - if (!notInherited) { Actor::Destroy(); } diff --git a/Source/Entities/AHuman.h b/Source/Entities/AHuman.h index a292b42a09..4e66b50cac 100644 --- a/Source/Entities/AHuman.h +++ b/Source/Entities/AHuman.h @@ -11,6 +11,7 @@ #include "LimbPath.h" #include +#include struct BITMAP; @@ -535,13 +536,18 @@ namespace RTE { /// @param newValue The new value for this AHuman's current crouch amount override. void SetCrouchAmountOverride(float newValue) { m_CrouchAmountOverride = newValue; } - /// Gets this AHuman's stride sound. Ownership is NOT transferred! + /// Gets this AHuman's stride sound. /// @return The SoundContainer for this AHuman's stride sound. - SoundContainer* GetStrideSound() const { return m_StrideSound; } - - /// Sets this AHuman's stride sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this AHuman's stride sound. - void SetStrideSound(SoundContainer* newSound) { m_StrideSound = newSound; } + std::shared_ptr GetStrideSound() const { return m_StrideSound; } + + /// Sets this AHuman's stride sound to a copy of the input. + /// @param newSound The new SoundContainer for this AHuman's stride sound to copy. + void SetStrideSound(const SoundContainer* newSound) { + if (!newSound) + m_StrideSound = nullptr; + else + m_StrideSound = std::make_shared(*newSound); + } /// Protected member variable and method declarations protected: @@ -587,7 +593,7 @@ namespace RTE { AtomGroup* m_pBGFootGroup; AtomGroup* m_BackupBGFootGroup; // The sound of the actor taking a step (think robot servo) - SoundContainer* m_StrideSound; + std::shared_ptr m_StrideSound; // Jetpack booster. AEJetpack* m_pJetpack; bool m_CanActivateBGItem; //!< A flag for whether or not the BG item is waiting to be activated separately. Used for dual-wielding. TODO: Should this be able to be toggled off per actor, device, or controller? diff --git a/Source/Entities/Activity.cpp b/Source/Entities/Activity.cpp index 767a64b8e6..2dd55f1f3c 100644 --- a/Source/Entities/Activity.cpp +++ b/Source/Entities/Activity.cpp @@ -739,7 +739,7 @@ bool Activity::SwitchToActor(Actor* actor, int player, int team) { m_ControlledActor[player]->SetControllerMode(Controller::CIM_PLAYER, player); m_ControlledActor[player]->GetController()->SetDisabled(false); - SoundContainer* actorSwitchSoundToPlay = (m_ControlledActor[player] == m_Brain[player]) ? g_GUISound.BrainSwitchSound() : g_GUISound.ActorSwitchSound(); + auto actorSwitchSoundToPlay = (m_ControlledActor[player] == m_Brain[player]) ? g_GUISound.BrainSwitchSound() : g_GUISound.ActorSwitchSound(); actorSwitchSoundToPlay->Play(player); // If out of frame from the POV of the preswitch actor, play the camera travel noise diff --git a/Source/Entities/Actor.cpp b/Source/Entities/Actor.cpp index 7ea36f424b..2ada24d33a 100644 --- a/Source/Entities/Actor.cpp +++ b/Source/Entities/Actor.cpp @@ -27,6 +27,7 @@ #include "AllegroBitmap.h" #include "tracy/Tracy.hpp" +#include using namespace RTE; @@ -175,19 +176,24 @@ int Actor::Create(const Actor& reference) { m_PlayerControllable = reference.m_PlayerControllable; if (reference.m_BodyHitSound) { - m_BodyHitSound = dynamic_cast(reference.m_BodyHitSound->Clone()); + m_BodyHitSound = std::make_shared(); + reference.m_BodyHitSound->Clone(&*m_BodyHitSound); } if (reference.m_AlarmSound) { - m_AlarmSound = dynamic_cast(reference.m_AlarmSound->Clone()); + m_AlarmSound = std::make_shared(); + reference.m_AlarmSound->Clone(&*m_AlarmSound); } if (reference.m_PainSound) { - m_PainSound = dynamic_cast(reference.m_PainSound->Clone()); + m_PainSound = std::make_shared(); + reference.m_PainSound->Clone(&*m_PainSound); } if (reference.m_DeathSound) { - m_DeathSound = dynamic_cast(reference.m_DeathSound->Clone()); + m_DeathSound = std::make_shared(); + reference.m_DeathSound->Clone(&*m_DeathSound); } if (reference.m_DeviceSwitchSound) { - m_DeviceSwitchSound = dynamic_cast(reference.m_DeviceSwitchSound->Clone()); + m_DeviceSwitchSound = std::make_shared(); + reference.m_DeviceSwitchSound->Clone(&*m_DeviceSwitchSound); } // m_FacingRight = reference.m_FacingRight; m_Status = reference.m_Status; @@ -293,24 +299,24 @@ int Actor::ReadProperty(const std::string_view& propName, Reader& reader) { MatchProperty("PlayerControllable", { reader >> m_PlayerControllable; }); MatchProperty("BodyHitSound", { - m_BodyHitSound = new SoundContainer; - reader >> m_BodyHitSound; + m_BodyHitSound = std::make_shared(); + reader >> *m_BodyHitSound; }); MatchProperty("AlarmSound", { - m_AlarmSound = new SoundContainer; - reader >> m_AlarmSound; + m_AlarmSound = std::make_shared(); + reader >> *m_AlarmSound; }); MatchProperty("PainSound", { - m_PainSound = new SoundContainer; - reader >> m_PainSound; + m_PainSound = std::make_shared(); + reader >> *m_PainSound; }); MatchProperty("DeathSound", { - m_DeathSound = new SoundContainer; - reader >> m_DeathSound; + m_DeathSound = std::make_shared(); + reader >> *m_DeathSound; }); MatchProperty("DeviceSwitchSound", { - m_DeviceSwitchSound = new SoundContainer; - reader >> m_DeviceSwitchSound; + m_DeviceSwitchSound = std::make_shared(); + reader >> *m_DeviceSwitchSound; }); MatchProperty("Status", { reader >> m_Status; }); MatchProperty("DeploymentID", { reader >> m_DeploymentID; }); @@ -385,15 +391,15 @@ int Actor::Save(Writer& writer) const { writer.NewPropertyWithValue("PlayerControllable", m_PlayerControllable); writer.NewProperty("BodyHitSound"); - writer << m_BodyHitSound; + writer << *m_BodyHitSound; writer.NewProperty("AlarmSound"); - writer << m_AlarmSound; + writer << *m_AlarmSound; writer.NewProperty("PainSound"); - writer << m_PainSound; + writer << *m_PainSound; writer.NewProperty("DeathSound"); - writer << m_DeathSound; + writer << *m_DeathSound; writer.NewProperty("DeviceSwitchSound"); - writer << m_DeviceSwitchSound; + writer << *m_DeviceSwitchSound; writer.NewProperty("Status"); writer << m_Status; writer.NewProperty("Health"); @@ -464,12 +470,6 @@ void Actor::DestroyScriptState() { } void Actor::Destroy(bool notInherited) { - delete m_DeviceSwitchSound; - delete m_BodyHitSound; - delete m_PainSound; - delete m_DeathSound; - delete m_AlarmSound; - for (std::deque::const_iterator itr = m_Inventory.begin(); itr != m_Inventory.end(); ++itr) { delete (*itr); } diff --git a/Source/Entities/Actor.h b/Source/Entities/Actor.h index 00be2213e7..3222058372 100644 --- a/Source/Entities/Actor.h +++ b/Source/Entities/Actor.h @@ -740,45 +740,70 @@ namespace RTE { /// @param value Threshold for taking damage from travel impulses, in kg * m/s void SetTravelImpulseDamage(float value) { m_TravelImpulseDamage = value; } - /// Gets this Actor's body hit sound. Ownership is NOT transferred! + /// Gets this Actor's body hit sound. /// @return The SoundContainer for this Actor's body hit sound. - SoundContainer* GetBodyHitSound() const { return m_BodyHitSound; } - - /// Sets this Actor's body hit sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this Actor's body hit sound. - void SetBodyHitSound(SoundContainer* newSound) { m_BodyHitSound = newSound; } + std::shared_ptr GetBodyHitSound() const { return m_BodyHitSound; } + + /// Sets this Actor's body hit sound to a copy of the input. + /// @param newSound The new SoundContainer for this Actor's body hit sound to copy. + void SetBodyHitSound(const SoundContainer* newSound) { + if (!newSound) + m_BodyHitSound = nullptr; + else + m_BodyHitSound = std::make_shared(*newSound); + } - /// Gets this Actor's alarm sound. Ownership is NOT transferred! + /// Gets this Actor's alarm sound. /// @return The SoundContainer for this Actor's alarm sound. - SoundContainer* GetAlarmSound() const { return m_AlarmSound; } - - /// Sets this Actor's alarm sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this Actor's alarm sound. - void SetAlarmSound(SoundContainer* newSound) { m_AlarmSound = newSound; } + std::shared_ptr GetAlarmSound() const { return m_AlarmSound; } + + /// Sets this Actor's alarm sound to a copy of the input. + /// @param newSound The new SoundContainer for this Actor's alarm sound to copy. + void SetAlarmSound(const SoundContainer* newSound) { + if (!newSound) + m_AlarmSound = nullptr; + else + m_AlarmSound = std::make_shared(*newSound); + } - /// Gets this Actor's pain sound. Ownership is NOT transferred! + /// Gets this Actor's pain sound. /// @return The SoundContainer for this Actor's pain sound. - SoundContainer* GetPainSound() const { return m_PainSound; } - - /// Sets this Actor's pain sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this Actor's pain sound. - void SetPainSound(SoundContainer* newSound) { m_PainSound = newSound; } + std::shared_ptr GetPainSound() const { return m_PainSound; } + + /// Sets this Actor's pain sound to a copy of the input. + /// @param newSound The new SoundContainer for this Actor's pain sound to copy. + void SetPainSound(const SoundContainer* newSound) { + if (!newSound) + m_PainSound = nullptr; + else + m_PainSound = std::make_shared(*newSound); + } - /// Gets this Actor's death sound. Ownership is NOT transferred! + /// Gets this Actor's death sound. /// @return The SoundContainer for this Actor's death sound. - SoundContainer* GetDeathSound() const { return m_DeathSound; } - - /// Sets this Actor's death sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this Actor's death sound. - void SetDeathSound(SoundContainer* newSound) { m_DeathSound = newSound; } + std::shared_ptr GetDeathSound() const { return m_DeathSound; } + + /// Sets this Actor's death sound to a copy of the input. + /// @param newSound The new SoundContainer for this Actor's death sound to copy. + void SetDeathSound(const SoundContainer* newSound) { + if (!newSound) + m_DeathSound = nullptr; + else + m_DeathSound = std::make_shared(*newSound); + } - /// Gets this Actor's device switch sound. Ownership is NOT transferred! + /// Gets this Actor's device switch sound. /// @return The SoundContainer for this Actor's device switch sound. - SoundContainer* GetDeviceSwitchSound() const { return m_DeviceSwitchSound; } - - /// Sets this Actor's device switch sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this Actor's device switch sound. - void SetDeviceSwitchSound(SoundContainer* newSound) { m_DeviceSwitchSound = newSound; } + std::shared_ptr GetDeviceSwitchSound() const { return m_DeviceSwitchSound; } + + /// Sets this Actor's device switch sound to a copy of the input. + /// @param newSound The new SoundContainer for this Actor's device switch sound to copy. + void SetDeviceSwitchSound(const SoundContainer* newSound) { + if (!newSound) + m_DeviceSwitchSound = nullptr; + else + m_DeviceSwitchSound = std::make_shared(*newSound); + } /// Gets the X and Y thresholds for how fast the actor can travel before losing stability. /// @return A Vector with the X and Y thresholds for how fast the actor can travel before losing stability. @@ -884,11 +909,11 @@ namespace RTE { bool m_PlayerControllable; //!< Whether or not this Actor can be controlled by human players. // Sounds - SoundContainer* m_BodyHitSound; - SoundContainer* m_AlarmSound; - SoundContainer* m_PainSound; - SoundContainer* m_DeathSound; - SoundContainer* m_DeviceSwitchSound; + std::shared_ptr m_BodyHitSound; + std::shared_ptr m_AlarmSound; + std::shared_ptr m_PainSound; + std::shared_ptr m_DeathSound; + std::shared_ptr m_DeviceSwitchSound; int m_Status; float m_Health; diff --git a/Source/Entities/DynamicSong.cpp b/Source/Entities/DynamicSong.cpp index ef71cb244c..4da3e44341 100644 --- a/Source/Entities/DynamicSong.cpp +++ b/Source/Entities/DynamicSong.cpp @@ -38,17 +38,17 @@ void DynamicSongSection::Clear() { int DynamicSongSection::Create(const DynamicSongSection& reference) { Entity::Create(reference); - for (const SoundContainer& referenceSoundContainer: reference.m_TransitionSoundContainers) { - SoundContainer soundContainer; - soundContainer.Create(referenceSoundContainer); - m_TransitionSoundContainers.push_back(soundContainer); + for (auto reference: reference.m_TransitionSoundContainers) { + auto newSound = std::make_shared(); + reference->Clone(&*newSound); + m_TransitionSoundContainers.push_back(newSound); } m_LastTransitionSoundContainerIndex = reference.m_LastTransitionSoundContainerIndex; - for (const SoundContainer& referenceSoundContainer: reference.m_SoundContainers) { - SoundContainer soundContainer; - soundContainer.Create(referenceSoundContainer); - m_SoundContainers.push_back(soundContainer); + for (auto reference: reference.m_SoundContainers) { + auto newSound = std::make_shared(); + reference->Clone(&*newSound); + m_SoundContainers.push_back(newSound); } m_LastSoundContainerIndex = reference.m_LastSoundContainerIndex; @@ -62,14 +62,14 @@ int DynamicSongSection::ReadProperty(const std::string_view& propName, Reader& r StartPropertyList(return Entity::ReadProperty(propName, reader)); MatchProperty("AddTransitionSoundContainer", { - SoundContainer soundContainerToAdd; - reader >> soundContainerToAdd; - m_TransitionSoundContainers.push_back(soundContainerToAdd); + auto newSound = std::make_shared(); + reader >> *newSound; + m_TransitionSoundContainers.push_back(newSound); }); MatchProperty("AddSoundContainer", { - SoundContainer soundContainerToAdd; - reader >> soundContainerToAdd; - m_SoundContainers.push_back(soundContainerToAdd); + auto newSound = std::make_shared(); + reader >> *newSound; + m_SoundContainers.push_back(newSound); }); MatchProperty("SoundContainerSelectionCycleMode", { std::string soundContainerSelectionCycleModeString = reader.ReadPropValue(); @@ -100,18 +100,18 @@ void DynamicSongSection::SaveSoundContainerSelectionCycleMode(Writer& writer, So int DynamicSongSection::Save(Writer& writer) const { Entity::Save(writer); - for (const SoundContainer& soundContainer: m_TransitionSoundContainers) { + for (auto soundContainer: m_TransitionSoundContainers) { writer.NewProperty("AddTransitionSoundContainer"); writer.ObjectStart("SoundContainer"); - writer << soundContainer; + writer << *soundContainer; writer.ObjectEnd(); } writer.NewProperty("LastTransitionSoundContainerIndex"); writer << m_LastTransitionSoundContainerIndex; - for (const SoundContainer& soundContainer: m_SoundContainers) { + for (auto soundContainer: m_SoundContainers) { writer.NewProperty("AddSoundContainer"); writer.ObjectStart("SoundContainer"); - writer << soundContainer; + writer << *soundContainer; writer.ObjectEnd(); } writer.NewProperty("LastSoundContainerIndex"); @@ -124,7 +124,7 @@ int DynamicSongSection::Save(Writer& writer) const { return 0; } -SoundContainer& DynamicSongSection::SelectTransitionSoundContainer() { +std::shared_ptr DynamicSongSection::SelectTransitionSoundContainer() { if (m_TransitionSoundContainers.empty()) { return SelectSoundContainer(); } @@ -166,7 +166,7 @@ SoundContainer& DynamicSongSection::SelectTransitionSoundContainer() { return m_TransitionSoundContainers[0]; } -SoundContainer& DynamicSongSection::SelectSoundContainer() { +std::shared_ptr DynamicSongSection::SelectSoundContainer() { RTEAssert(!m_SoundContainers.empty(), "Tried to get a SoundContainer from a DynamicSongSection with none to choose from!"); // Shuffle between our options if we have multiple diff --git a/Source/Entities/DynamicSong.h b/Source/Entities/DynamicSong.h index 1c33a638d0..b53513eb97 100644 --- a/Source/Entities/DynamicSong.h +++ b/Source/Entities/DynamicSong.h @@ -2,6 +2,7 @@ #include "Entity.h" #include "SoundContainer.h" +#include namespace RTE { /// A typed SongSection containing one or more SoundContainers to play. @@ -53,13 +54,17 @@ namespace RTE { #pragma endregion #pragma region SoundContainer Addition - /// Adds a new TransitionSoundContainer to this DynamicSongSection. + /// Copies and adds a new TransitionSoundContainer to this DynamicSongSection. /// @param soundContainerToAdd The new SoundContainer to add. - void AddTransitionSoundContainer(const SoundContainer& soundContainerToAdd) { m_TransitionSoundContainers.push_back(soundContainerToAdd); } + void AddTransitionSoundContainer(const SoundContainer& soundContainerToAdd) { + m_TransitionSoundContainers.push_back(std::make_shared(soundContainerToAdd)); + } - /// Adds a new SoundContainer to this DynamicSongSection. + /// Copies and adds a new SoundContainer to this DynamicSongSection. /// @param soundContainerToAdd The new SoundContainer to add. - void AddSoundContainer(const SoundContainer& soundContainerToAdd) { m_SoundContainers.push_back(soundContainerToAdd); } + void AddSoundContainer(const SoundContainer& soundContainerToAdd) { + m_SoundContainers.push_back(std::make_shared(soundContainerToAdd)); + } #pragma endregion #pragma region INI Handling @@ -72,11 +77,11 @@ namespace RTE { #pragma region Getters and Setters /// Gets the vector of TransitionSoundContainers for this DynamicSongSection. /// @return The vector of TransitionSoundContainers for this DynamicSongSection. - std::vector& GetTransitionSoundContainers() { return m_TransitionSoundContainers; } + std::vector>& GetTransitionSoundContainers() { return m_TransitionSoundContainers; } /// Gets the vector of SoundContainers for this DynamicSongSection. /// @return The vector of SoundContainers for this DynamicSongSection. - std::vector& GetSoundContainers() { return m_SoundContainers; } + std::vector>& GetSoundContainers() { return m_SoundContainers; } /// Gets the SoundContainerSelectionCycleMode of this DynamicSongSection. /// @return The SoundContainerSelectionCycleMode of this DynamicSongSection. @@ -108,21 +113,21 @@ namespace RTE { /// Selects a random transitional SoundContainer with no repeats. /// @return The selected transitional SoundContainer. - SoundContainer& SelectTransitionSoundContainer(); + std::shared_ptr SelectTransitionSoundContainer(); /// Selects a random SoundContainer with no repeats. /// @return The selected SoundContainer. - SoundContainer& SelectSoundContainer(); + std::shared_ptr SelectSoundContainer(); #pragma endregion private: static Entity::ClassInfo m_sClass; //!< ClassInfo for this class. static const std::unordered_map c_SoundContainerSelectionCycleModeMap; //!< A map of strings to SoundContainerSelectionCycleModes to support string parsing for the SoundContainerSelectionCycleMode enum. Populated in the implementing cpp file. - std::vector m_TransitionSoundContainers; //!< The SoundContainers that will play when first switching to this DynamicSongSection. + std::vector> m_TransitionSoundContainers; //!< The SoundContainers that will play when first switching to this DynamicSongSection. unsigned int m_LastTransitionSoundContainerIndex; //!< The last index used to select a TransitionSoundContainer. std::vector m_TransitionShuffleUnplayedIndices; //!< Indices left to play if in Shuffle mode. - std::vector m_SoundContainers; //!< The SoundContainers making up this DynamicSongSection. + std::vector> m_SoundContainers; //!< The SoundContainers making up this DynamicSongSection. unsigned int m_LastSoundContainerIndex; //!< The last index used to select a SoundContainer. std::vector m_ShuffleUnplayedIndices; //!< Indices left to play if in Shuffle mode. diff --git a/Source/Entities/HDFirearm.cpp b/Source/Entities/HDFirearm.cpp index dee6346f0f..204dec6fdb 100644 --- a/Source/Entities/HDFirearm.cpp +++ b/Source/Entities/HDFirearm.cpp @@ -108,28 +108,36 @@ int HDFirearm::Create(const HDFirearm& reference) { m_pMagazineReference = reference.m_pMagazineReference; if (reference.m_PreFireSound) { - m_PreFireSound = dynamic_cast(reference.m_PreFireSound->Clone()); + m_PreFireSound = std::make_shared(); + reference.m_PreFireSound->Clone(&*m_PreFireSound); } if (reference.m_FireSound) { - m_FireSound = dynamic_cast(reference.m_FireSound->Clone()); + m_FireSound = std::make_shared(); + reference.m_FireSound->Clone(&*m_FireSound); } if (reference.m_FireEchoSound) { - m_FireEchoSound = dynamic_cast(reference.m_FireEchoSound->Clone()); + m_FireEchoSound = std::make_shared(); + reference.m_FireEchoSound->Clone(&*m_FireEchoSound); } if (reference.m_ActiveSound) { - m_ActiveSound = dynamic_cast(reference.m_ActiveSound->Clone()); + m_ActiveSound = std::make_shared(); + reference.m_ActiveSound->Clone(&*m_ActiveSound); } if (reference.m_DeactivationSound) { - m_DeactivationSound = dynamic_cast(reference.m_DeactivationSound->Clone()); + m_DeactivationSound = std::make_shared(); + reference.m_DeactivationSound->Clone(&*m_DeactivationSound); } if (reference.m_EmptySound) { - m_EmptySound = dynamic_cast(reference.m_EmptySound->Clone()); + m_EmptySound = std::make_shared(); + reference.m_EmptySound->Clone(&*m_EmptySound); } if (reference.m_ReloadStartSound) { - m_ReloadStartSound = dynamic_cast(reference.m_ReloadStartSound->Clone()); + m_ReloadStartSound = std::make_shared(); + reference.m_ReloadStartSound->Clone(&*m_ReloadStartSound); } if (reference.m_ReloadEndSound) { - m_ReloadEndSound = dynamic_cast(reference.m_ReloadEndSound->Clone()); + m_ReloadEndSound = std::make_shared(); + reference.m_ReloadEndSound->Clone(&*m_ReloadEndSound); } m_ReloadEndOffset = reference.m_ReloadEndOffset; m_RateOfFire = reference.m_RateOfFire; @@ -173,37 +181,37 @@ int HDFirearm::ReadProperty(const std::string_view& propName, Reader& reader) { MatchProperty("Magazine", { SetMagazine(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); MatchProperty("Flash", { SetFlash(dynamic_cast(g_PresetMan.ReadReflectedPreset(reader))); }); MatchProperty("PreFireSound", { - m_PreFireSound = new SoundContainer; - reader >> m_PreFireSound; + m_PreFireSound = std::make_shared(); + reader >> *m_PreFireSound; }); MatchProperty("FireSound", { - m_FireSound = new SoundContainer; - reader >> m_FireSound; + m_FireSound = std::make_shared(); + reader >> *m_FireSound; }); MatchProperty("FireEchoSound", { - m_FireEchoSound = new SoundContainer; - reader >> m_FireEchoSound; + m_FireEchoSound = std::make_shared(); + reader >> *m_FireEchoSound; m_FireEchoSound->SetSoundOverlapMode(SoundContainer::SoundOverlapMode::RESTART); }); MatchProperty("ActiveSound", { - m_ActiveSound = new SoundContainer; - reader >> m_ActiveSound; + m_ActiveSound = std::make_shared(); + reader >> *m_ActiveSound; }); MatchProperty("DeactivationSound", { - m_DeactivationSound = new SoundContainer; - reader >> m_DeactivationSound; + m_DeactivationSound = std::make_shared(); + reader >> *m_DeactivationSound; }); MatchProperty("EmptySound", { - m_EmptySound = new SoundContainer; - reader >> m_EmptySound; + m_EmptySound = std::make_shared(); + reader >> *m_EmptySound; }); MatchProperty("ReloadStartSound", { - m_ReloadStartSound = new SoundContainer; - reader >> m_ReloadStartSound; + m_ReloadStartSound = std::make_shared(); + reader >> *m_ReloadStartSound; }); MatchProperty("ReloadEndSound", { - m_ReloadEndSound = new SoundContainer; - reader >> m_ReloadEndSound; + m_ReloadEndSound = std::make_shared(); + reader >> *m_ReloadEndSound; }); MatchProperty("ReloadEndOffset", { reader >> m_ReloadEndOffset; }); MatchProperty("RateOfFire", { reader >> m_RateOfFire; }); @@ -258,21 +266,21 @@ int HDFirearm::Save(Writer& writer) const { writer.NewProperty("Flash"); writer << m_pFlash; writer.NewProperty("PreFireSound"); - writer << m_PreFireSound; + writer << *m_PreFireSound; writer.NewProperty("FireSound"); - writer << m_FireSound; + writer << *m_FireSound; writer.NewProperty("FireEchoSound"); - writer << m_FireEchoSound; + writer << *m_FireEchoSound; writer.NewProperty("ActiveSound"); - writer << m_ActiveSound; + writer << *m_ActiveSound; writer.NewProperty("DeactivationSound"); - writer << m_DeactivationSound; + writer << *m_DeactivationSound; writer.NewProperty("EmptySound"); - writer << m_EmptySound; + writer << *m_EmptySound; writer.NewProperty("ReloadStartSound"); - writer << m_ReloadStartSound; + writer << *m_ReloadStartSound; writer.NewProperty("ReloadEndSound"); - writer << m_ReloadEndSound; + writer << *m_ReloadEndSound; writer.NewPropertyWithValue("ReloadEndOffset", m_ReloadEndOffset); writer.NewProperty("RateOfFire"); writer << m_RateOfFire; @@ -350,15 +358,6 @@ void HDFirearm::Destroy(bool notInherited) { m_ReloadEndSound->Stop(); } - delete m_PreFireSound; - delete m_FireSound; - delete m_FireEchoSound; - delete m_ActiveSound; - delete m_DeactivationSound; - delete m_EmptySound; - delete m_ReloadStartSound; - delete m_ReloadEndSound; - if (!notInherited) HeldDevice::Destroy(); Clear(); diff --git a/Source/Entities/HDFirearm.h b/Source/Entities/HDFirearm.h index 6a4f6e983c..d826c1cca8 100644 --- a/Source/Entities/HDFirearm.h +++ b/Source/Entities/HDFirearm.h @@ -6,6 +6,7 @@ /// http://www.datarealms.com /// Inclusions of header files #include "HeldDevice.h" +#include namespace RTE { @@ -290,71 +291,111 @@ namespace RTE { /// @param newOffset New offset value. void SetEjectionOffset(Vector newOffset) { m_EjectOff = newOffset; } - /// Gets this HDFirearm's pre fire sound. Ownership is NOT transferred! + /// Gets this HDFirearm's pre fire sound. /// @return The SoundContainer for this HDFirearm's pre fire sound. - SoundContainer* GetPreFireSound() const { return m_PreFireSound; } - - /// Sets this HDFirearm's pre fire sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's pre fire sound. - void SetPreFireSound(SoundContainer* newSound) { m_PreFireSound = newSound; } + std::shared_ptr GetPreFireSound() const { return m_PreFireSound; } + + /// Sets this HDFirearm's pre fire sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's pre fire sound to copy. + void SetPreFireSound(const SoundContainer* newSound) { + if (!newSound) + m_PreFireSound = nullptr; + else + m_PreFireSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's fire sound. Ownership is NOT transferred! + /// Gets this HDFirearm's fire sound. /// @return The SoundContainer for this HDFirearm's fire sound. - SoundContainer* GetFireSound() const { return m_FireSound; } - - /// Sets this HDFirearm's fire sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's fire sound. - void SetFireSound(SoundContainer* newSound) { m_FireSound = newSound; } + std::shared_ptr GetFireSound() const { return m_FireSound; } + + /// Sets this HDFirearm's fire sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's fire sound to copy. + void SetFireSound(const SoundContainer* newSound) { + if (!newSound) + m_FireSound = nullptr; + else + m_FireSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's fire echo sound. Ownership is NOT transferred! + /// Gets this HDFirearm's fire echo sound. /// @return The SoundContainer for this HDFirearm's fire echo sound. - SoundContainer* GetFireEchoSound() const { return m_FireEchoSound; } - - /// Sets this HDFirearm's fire echo sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's fire echo sound. - void SetFireEchoSound(SoundContainer* newSound) { m_FireEchoSound = newSound; } + std::shared_ptr GetFireEchoSound() const { return m_FireEchoSound; } + + /// Sets this HDFirearm's fire echo sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's fire echo sound to copy. + void SetFireEchoSound(const SoundContainer* newSound) { + if (!newSound) + m_FireEchoSound = nullptr; + else + m_FireEchoSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's active sound. Ownership is NOT transferred! + /// Gets this HDFirearm's active sound. /// @return The SoundContainer for this HDFirearm's active sound. - SoundContainer* GetActiveSound() const { return m_ActiveSound; } - - /// Sets this HDFirearm's active sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's active sound. - void SetActiveSound(SoundContainer* newSound) { m_ActiveSound = newSound; } + std::shared_ptr GetActiveSound() const { return m_ActiveSound; } + + /// Sets this HDFirearm's active sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's active sound to copy. + void SetActiveSound(const SoundContainer* newSound) { + if (!newSound) + m_ActiveSound = nullptr; + else + m_ActiveSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's deactivation sound. Ownership is NOT transferred! + /// Gets this HDFirearm's deactivation sound. /// @return The SoundContainer for this HDFirearm's deactivation sound. - SoundContainer* GetDeactivationSound() const { return m_DeactivationSound; } - - /// Sets this HDFirearm's deactivation sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's deactivation sound. - void SetDeactivationSound(SoundContainer* newSound) { m_DeactivationSound = newSound; } + std::shared_ptr GetDeactivationSound() const { return m_DeactivationSound; } + + /// Sets this HDFirearm's deactivation sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's deactivation sound to copy. + void SetDeactivationSound(const SoundContainer* newSound) { + if (!newSound) + m_DeactivationSound = nullptr; + else + m_DeactivationSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's empty sound. Ownership is NOT transferred! + /// Gets this HDFirearm's empty sound. /// @return The SoundContainer for this HDFirearm's empty sound. - SoundContainer* GetEmptySound() const { return m_EmptySound; } - - /// Sets this HDFirearm's empty sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's empty sound. - void SetEmptySound(SoundContainer* newSound) { m_EmptySound = newSound; } + std::shared_ptr GetEmptySound() const { return m_EmptySound; } + + /// Sets this HDFirearm's empty sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's empty sound to copy. + void SetEmptySound(const SoundContainer* newSound) { + if (!newSound) + m_EmptySound = nullptr; + else + m_EmptySound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's reload start sound. Ownership is NOT transferred! + /// Gets this HDFirearm's reload start sound. /// @return The SoundContainer for this HDFirearm's reload start sound. - SoundContainer* GetReloadStartSound() const { return m_ReloadStartSound; } - - /// Sets this HDFirearm's reload start sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's reload start sound. - void SetReloadStartSound(SoundContainer* newSound) { m_ReloadStartSound = newSound; } + std::shared_ptr GetReloadStartSound() const { return m_ReloadStartSound; } + + /// Sets this HDFirearm's reload start sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's reload start sound to copy. + void SetReloadStartSound(const SoundContainer* newSound) { + if (!newSound) + m_ReloadStartSound = nullptr; + else + m_ReloadStartSound = std::make_shared(*newSound); + } - /// Gets this HDFirearm's reload end sound. Ownership is NOT transferred! + /// Gets this HDFirearm's reload end sound. /// @return The SoundContainer for this HDFirearm's reload end sound. - SoundContainer* GetReloadEndSound() const { return m_ReloadEndSound; } - - /// Sets this HDFirearm's reload end sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this HDFirearm's reload end sound. - void SetReloadEndSound(SoundContainer* newSound) { m_ReloadEndSound = newSound; } + std::shared_ptr GetReloadEndSound() const { return m_ReloadEndSound; } + + /// Sets this HDFirearm's reload end sound to a copy of the input. + /// @param newSound The new SoundContainer for this HDFirearm's reload end sound to copy. + void SetReloadEndSound(const SoundContainer* newSound) { + if (!newSound) + m_ReloadEndSound = nullptr; + else + m_ReloadEndSound = std::make_shared(*newSound); + } - /// Resest all the timers used by this. Can be emitters, etc. This is to + /// Reset all the timers used by this. Can be emitters, etc. This is to /// prevent backed up emissions to come out all at once while this has been /// held dormant in an inventory. void ResetAllTimers() override { @@ -500,19 +541,19 @@ namespace RTE { // Muzzle Flash Attachable. Owned Attachable* m_pFlash; - SoundContainer* m_PreFireSound; //!< The sound this HDFirearm should play before it starts firing. Distinct from activation sound in that it will play exactly once per trigger pull and not pitch up. + std::shared_ptr m_PreFireSound; //!< The sound this HDFirearm should play before it starts firing. Distinct from activation sound in that it will play exactly once per trigger pull and not pitch up. // The audio of this FireArm being fired. - SoundContainer* m_FireSound; - SoundContainer* m_FireEchoSound; //!< The audio that is played as the echo for the gun. Each shot will restart this sound, so it doesn't ever overlap. + std::shared_ptr m_FireSound; + std::shared_ptr m_FireEchoSound; //!< The audio that is played as the echo for the gun. Each shot will restart this sound, so it doesn't ever overlap. // The audio that is played immediately upon activation, but perhaps before actual first firing, if there's a pre-delay - SoundContainer* m_ActiveSound; + std::shared_ptr m_ActiveSound; // The audio that is played immediately upon cease of activation - SoundContainer* m_DeactivationSound; + std::shared_ptr m_DeactivationSound; // The audio of this FireArm being fired empty. - SoundContainer* m_EmptySound; + std::shared_ptr m_EmptySound; // The audio of this FireArm being reloaded. - SoundContainer* m_ReloadStartSound; - SoundContainer* m_ReloadEndSound; + std::shared_ptr m_ReloadStartSound; + std::shared_ptr m_ReloadEndSound; // The offset of how long before the reload finishes the sound plays float m_ReloadEndOffset; // Whether or not the end-of-relaod sound has already been played or not. diff --git a/Source/Entities/MOSRotating.cpp b/Source/Entities/MOSRotating.cpp index fd036c301f..6b4cb783b7 100644 --- a/Source/Entities/MOSRotating.cpp +++ b/Source/Entities/MOSRotating.cpp @@ -243,7 +243,8 @@ int MOSRotating::Create(const MOSRotating& reference) { m_DetachAttachablesBeforeGibbingFromWounds = reference.m_DetachAttachablesBeforeGibbingFromWounds; m_GibAtEndOfLifetime = reference.m_GibAtEndOfLifetime; if (reference.m_GibSound) { - m_GibSound = dynamic_cast(reference.m_GibSound->Clone()); + m_GibSound = std::make_shared(); + reference.m_GibSound->Clone(&*m_GibSound); } m_EffectOnGib = reference.m_EffectOnGib; m_LoudnessOnGib = reference.m_LoudnessOnGib; @@ -317,10 +318,8 @@ int MOSRotating::ReadProperty(const std::string_view& propName, Reader& reader) MatchProperty("DetachAttachablesBeforeGibbingFromWounds", { reader >> m_DetachAttachablesBeforeGibbingFromWounds; }); MatchProperty("GibAtEndOfLifetime", { reader >> m_GibAtEndOfLifetime; }); MatchProperty("GibSound", { - if (!m_GibSound) { - m_GibSound = new SoundContainer; - } - reader >> m_GibSound; + m_GibSound = std::make_shared(); + reader >> *m_GibSound; }); MatchProperty("EffectOnGib", { reader >> m_EffectOnGib; }); MatchProperty("LoudnessOnGib", { reader >> m_LoudnessOnGib; }); @@ -589,8 +588,6 @@ void MOSRotating::Destroy(bool notInherited) { // Not anymore; point to shared static bitmaps // destroy_bitmap(m_pTempBitmap); - delete m_GibSound; - if (!notInherited) MOSprite::Destroy(); Clear(); diff --git a/Source/Entities/MOSRotating.h b/Source/Entities/MOSRotating.h index 967af352ca..64c9416e82 100644 --- a/Source/Entities/MOSRotating.h +++ b/Source/Entities/MOSRotating.h @@ -8,6 +8,7 @@ #include "MOSprite.h" #include "Gib.h" +#include #include namespace RTE { @@ -478,13 +479,18 @@ namespace RTE { /// @param impulse New impulse value void SetTravelImpulse(Vector impulse) { m_TravelImpulse = impulse; } - /// Gets this MOSRotating's gib sound. Ownership is NOT transferred! + /// Gets this MOSRotating's gib sound. /// @return The SoundContainer for this MOSRotating's gib sound. - SoundContainer* GetGibSound() const { return m_GibSound; } - - /// Sets this MOSRotating's gib sound. Ownership IS transferred! - /// @param newSound The new SoundContainer for this MOSRotating's gib sound. - void SetGibSound(SoundContainer* newSound) { m_GibSound = newSound; } + std::shared_ptr GetGibSound() const { return m_GibSound; } + + /// Sets this MOSRotating's gib sound to a copy of the input. + /// @param newSound The new SoundContainer for this MOSRotating's gib sound to copy. + void SetGibSound(const SoundContainer* newSound) { + if (!newSound) + m_GibSound = nullptr; + else + m_GibSound = std::make_shared(*newSound); + } /// Ensures all attachables and wounds are positioned and rotated correctly. Must be run when this MOSRotating is added to MovableMan to avoid issues with Attachables spawning in at (0, 0). virtual void CorrectAttachableAndWoundPositionsAndRotations() const; @@ -569,7 +575,7 @@ namespace RTE { bool m_DetachAttachablesBeforeGibbingFromWounds; //!< Whether to detach any Attachables of this MOSRotating when it should gib from hitting its wound limit, instead of gibbing the MOSRotating itself. bool m_GibAtEndOfLifetime; //!< Whether or not this MOSRotating should gib when it reaches the end of its lifetime, instead of just deleting. // Gib sound effect - SoundContainer* m_GibSound; + std::shared_ptr m_GibSound; // Whether to flash effect on gib bool m_EffectOnGib; // How far this is audiable (in screens) when gibbing diff --git a/Source/Entities/PieMenu.cpp b/Source/Entities/PieMenu.cpp index 7fa69610dd..b9e3d1ec68 100644 --- a/Source/Entities/PieMenu.cpp +++ b/Source/Entities/PieMenu.cpp @@ -278,7 +278,7 @@ void PieMenu::SetEnabled(bool enable, bool playSounds) { PrepareAnalogCursorForEnableOrDisable(enable); if (playSounds) { - SoundContainer* soundToPlay = enable ? g_GUISound.PieMenuEnterSound() : g_GUISound.PieMenuExitSound(); + auto soundToPlay = enable ? g_GUISound.PieMenuEnterSound() : g_GUISound.PieMenuExitSound(); soundToPlay->Play(); } @@ -843,7 +843,7 @@ void PieMenu::UpdateSliceActivation() { if (m_HoveredPieSlice->GetSubPieMenu() && controller->IsState(ControlState::RELEASE_SECONDARY)) { g_GUISound.UserErrorSound()->Play(); } else { - SoundContainer* soundToPlay = m_HoveredPieSlice->IsEnabled() ? g_GUISound.SlicePickedSound() : g_GUISound.DisabledPickedSound(); + auto soundToPlay = m_HoveredPieSlice->IsEnabled() ? g_GUISound.SlicePickedSound() : g_GUISound.DisabledPickedSound(); soundToPlay->Play(); } } @@ -1229,7 +1229,7 @@ bool PieMenu::SetHoveredPieSlice(const PieSlice* pieSliceToSelect, bool moveCurs m_CursorAngle = GetRotAngle() + m_HoveredPieSlice->GetMidAngle(); } - SoundContainer* soundToPlay = pieSliceToSelect->IsEnabled() ? g_GUISound.HoverChangeSound() : g_GUISound.HoverDisabledSound(); + auto soundToPlay = pieSliceToSelect->IsEnabled() ? g_GUISound.HoverChangeSound() : g_GUISound.HoverDisabledSound(); soundToPlay->Play(); } else { m_CursorInVisiblePosition = false; diff --git a/Source/Entities/SoundContainer.cpp b/Source/Entities/SoundContainer.cpp index c085df5ca1..3329553781 100644 --- a/Source/Entities/SoundContainer.cpp +++ b/Source/Entities/SoundContainer.cpp @@ -318,7 +318,13 @@ bool SoundContainer::Play(int player) { return false; } } - return g_AudioMan.PlaySoundContainer(this, player); + + try { + auto shared = dynamic_pointer_cast(this->shared_from_this()); + return g_AudioMan.PlaySoundContainer(shared, player); + } catch (std::bad_weak_ptr& e) { + RTEAbort("SoundContainer::Play called from a SoundContainer not in a shared_ptr!"); + } } return false; } @@ -328,7 +334,18 @@ bool SoundContainer::Stop(int player) { } bool SoundContainer::Restart(int player) { - return (HasAnySounds() && IsBeingPlayed()) ? g_AudioMan.StopSoundContainerPlayingChannels(this, player) && g_AudioMan.PlaySoundContainer(this, player) : false; + if(HasAnySounds() && IsBeingPlayed() && g_AudioMan.StopSoundContainerPlayingChannels(this, player)) { + try { + auto shared = dynamic_pointer_cast(this->shared_from_this()); + + return g_AudioMan.PlaySoundContainer(shared, player); + + } catch (std::bad_weak_ptr& e) { + RTEAbort("SoundContainer::Restart called from a SoundContainer not in a shared_ptr!"); + } + } + + return false; } void SoundContainer::FadeOut(int fadeOutTime) { diff --git a/Source/Entities/SoundContainer.h b/Source/Entities/SoundContainer.h index 23a0647df4..c2364cf520 100644 --- a/Source/Entities/SoundContainer.h +++ b/Source/Entities/SoundContainer.h @@ -278,20 +278,24 @@ namespace RTE { #pragma region Playback Controls /// Plays the next sound of this SoundContainer at its current position for all players. + /// MUST be called from a shared_ptr! /// @return Whether this SoundContainer successfully started playing on any channels. bool Play() { return Play(-1); } /// Plays the next sound of this container at its current position. + /// MUST be called from a shared_ptr! /// @param player The player to start playback of this SoundContainer's sounds for. /// @return Whether there were sounds to play and they were able to be played. bool Play(int player); /// Plays the next sound of this SoundContainer at the given position for all players. + /// MUST be called from a shared_ptr! /// @param position The position at which to play the SoundContainer's sounds. /// @return Whether this SoundContainer successfully started playing on any channels. bool Play(const Vector& position) { return Play(position, -1); } /// Plays the next sound of this SoundContainer with the given attenuation for a specific player. + /// MUST be called from a shared_ptr! /// @param position The position at which to play the SoundContainer's sounds. /// @param player The player to start playback of this SoundContainer's sounds for. /// @return Whether this SoundContainer successfully started playing on any channels. @@ -310,10 +314,12 @@ namespace RTE { bool Stop(int player); /// Restarts playback of this SoundContainer for all players. + /// MUST be called from a shared_ptr! /// @return Whether this SoundContainer successfully restarted its playback. bool Restart() { return Restart(-1); } /// Restarts playback of this SoundContainer for a specific player. + /// MUST be called from a shared_ptr! /// @param player Player to restart playback of this SoundContainer for. /// @return Whether this SoundContainer successfully restarted its playback. bool Restart(int player); diff --git a/Source/Entities/TDExplosive.cpp b/Source/Entities/TDExplosive.cpp index 26244f7191..7bb0b2587f 100644 --- a/Source/Entities/TDExplosive.cpp +++ b/Source/Entities/TDExplosive.cpp @@ -43,10 +43,8 @@ int TDExplosive::ReadProperty(const std::string_view& propName, Reader& reader) // TODO: Consider removing DetonationSound as GibSound already exists and could be used in its place MatchProperty("DetonationSound", { - if (!m_GibSound) { - m_GibSound = new SoundContainer; - } - reader >> m_GibSound; + m_GibSound = std::make_shared(); + reader >> *m_GibSound; }); MatchProperty("IsAnimatedManually", { reader >> m_IsAnimatedManually; }); diff --git a/Source/GUI/GUISound.cpp b/Source/GUI/GUISound.cpp index 7507000564..30c246e87b 100644 --- a/Source/GUI/GUISound.cpp +++ b/Source/GUI/GUISound.cpp @@ -9,107 +9,107 @@ GUISound::GUISound() { } void GUISound::Clear() { - m_SplashSound.Reset(); - m_EnterMenuSound.Reset(); - m_ExitMenuSound.Reset(); - m_FocusChangeSound.Reset(); - m_SelectionChangeSound.Reset(); - m_ItemChangeSound.Reset(); - m_ButtonPressSound.Reset(); - m_BackButtonPressSound.Reset(); - m_ConfirmSound.Reset(); - m_UserErrorSound.Reset(); - m_TestSound.Reset(); - m_PieMenuEnterSound.Reset(); - m_PieMenuExitSound.Reset(); - m_HoverChangeSound.Reset(); - m_HoverDisabledSound.Reset(); - m_SlicePickedSound.Reset(); - m_DisabledPickedSound.Reset(); - m_FundsChangedSound.Reset(); - m_ActorSwitchSound.Reset(); - m_BrainSwitchSound.Reset(); - m_CameraTravelSound.Reset(); - m_AreaPickedSound.Reset(); - m_ObjectPickedSound.Reset(); - m_PurchaseMadeSound.Reset(); - m_PlacementBlip.Reset(); - m_PlacementThud.Reset(); - m_PlacementGravel.Reset(); + m_SplashSound = std::make_shared(); + m_EnterMenuSound = std::make_shared(); + m_ExitMenuSound = std::make_shared(); + m_FocusChangeSound = std::make_shared(); + m_SelectionChangeSound = std::make_shared(); + m_ItemChangeSound = std::make_shared(); + m_ButtonPressSound = std::make_shared(); + m_BackButtonPressSound = std::make_shared(); + m_ConfirmSound = std::make_shared(); + m_UserErrorSound = std::make_shared(); + m_TestSound = std::make_shared(); + m_PieMenuEnterSound = std::make_shared(); + m_PieMenuExitSound = std::make_shared(); + m_HoverChangeSound = std::make_shared(); + m_HoverDisabledSound = std::make_shared(); + m_SlicePickedSound = std::make_shared(); + m_DisabledPickedSound = std::make_shared(); + m_FundsChangedSound = std::make_shared(); + m_ActorSwitchSound = std::make_shared(); + m_BrainSwitchSound = std::make_shared(); + m_CameraTravelSound = std::make_shared(); + m_AreaPickedSound = std::make_shared(); + m_ObjectPickedSound = std::make_shared(); + m_PurchaseMadeSound = std::make_shared(); + m_PlacementBlip = std::make_shared(); + m_PlacementThud = std::make_shared(); + m_PlacementGravel = std::make_shared(); } void GUISound::Initialize() { // Interface sounds should not be pitched to reinforce the appearance of time decoupling between simulation and UI. - m_SplashSound.Create("Base.rte/Sounds/GUIs/MetaStart.flac", true, false, SoundContainer::BusRouting::UI); + m_SplashSound->Create("Base.rte/Sounds/GUIs/MetaStart.flac", true, false, SoundContainer::BusRouting::UI); - m_EnterMenuSound.Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); + m_EnterMenuSound->Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); - m_ExitMenuSound.Create("Base.rte/Sounds/GUIs/MenuExit1.flac", true, false, SoundContainer::BusRouting::UI); - m_ExitMenuSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/MenuExit2.flac", true); + m_ExitMenuSound->Create("Base.rte/Sounds/GUIs/MenuExit1.flac", true, false, SoundContainer::BusRouting::UI); + m_ExitMenuSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/MenuExit2.flac", true); - m_FocusChangeSound.Create("Base.rte/Sounds/GUIs/FocusChange.flac", true, false, SoundContainer::BusRouting::UI); + m_FocusChangeSound->Create("Base.rte/Sounds/GUIs/FocusChange.flac", true, false, SoundContainer::BusRouting::UI); - m_SelectionChangeSound.Create("Base.rte/Sounds/GUIs/SelectionChange.flac", true, false, SoundContainer::BusRouting::UI); + m_SelectionChangeSound->Create("Base.rte/Sounds/GUIs/SelectionChange.flac", true, false, SoundContainer::BusRouting::UI); - m_ItemChangeSound.Create("Base.rte/Sounds/GUIs/ItemChange.flac", true, false, SoundContainer::BusRouting::UI); + m_ItemChangeSound->Create("Base.rte/Sounds/GUIs/ItemChange.flac", true, false, SoundContainer::BusRouting::UI); - m_ButtonPressSound.Create("Base.rte/Sounds/GUIs/ButtonPress.flac", true, false, SoundContainer::BusRouting::UI); + m_ButtonPressSound->Create("Base.rte/Sounds/GUIs/ButtonPress.flac", true, false, SoundContainer::BusRouting::UI); - m_BackButtonPressSound.Create("Base.rte/Sounds/GUIs/BackButtonPress.flac", true, false, SoundContainer::BusRouting::UI); + m_BackButtonPressSound->Create("Base.rte/Sounds/GUIs/BackButtonPress.flac", true, false, SoundContainer::BusRouting::UI); - m_ConfirmSound.Create("Base.rte/Sounds/GUIs/MenuExit1.flac", true, false, SoundContainer::BusRouting::UI); + m_ConfirmSound->Create("Base.rte/Sounds/GUIs/MenuExit1.flac", true, false, SoundContainer::BusRouting::UI); - m_UserErrorSound.Create("Base.rte/Sounds/GUIs/UserError.flac", true, false, SoundContainer::BusRouting::UI); + m_UserErrorSound->Create("Base.rte/Sounds/GUIs/UserError.flac", true, false, SoundContainer::BusRouting::UI); - m_TestSound.Create("Base.rte/Sounds/GUIs/Test.flac", true, false, SoundContainer::BusRouting::UI); + m_TestSound->Create("Base.rte/Sounds/GUIs/Test.flac", true, false, SoundContainer::BusRouting::UI); - m_PieMenuEnterSound.Create("Base.rte/Sounds/GUIs/PieMenuEnter.flac", true, false, SoundContainer::BusRouting::UI); + m_PieMenuEnterSound->Create("Base.rte/Sounds/GUIs/PieMenuEnter.flac", true, false, SoundContainer::BusRouting::UI); - m_PieMenuExitSound.Create("Base.rte/Sounds/GUIs/PieMenuExit.flac", true, false, SoundContainer::BusRouting::UI); + m_PieMenuExitSound->Create("Base.rte/Sounds/GUIs/PieMenuExit.flac", true, false, SoundContainer::BusRouting::UI); - // m_HoverChangeSound.Create("Base.rte/Sounds/GUIs/SelectionChange.flac", true, false, SoundContainer::BusRouting::UI); + // m_HoverChangeSound->Create("Base.rte/Sounds/GUIs/SelectionChange.flac", true, false, SoundContainer::BusRouting::UI); m_HoverChangeSound = m_SelectionChangeSound; - m_HoverDisabledSound.Create("Base.rte/Sounds/GUIs/PlacementBlip.flac", true, false, SoundContainer::BusRouting::UI); + m_HoverDisabledSound->Create("Base.rte/Sounds/GUIs/PlacementBlip.flac", true, false, SoundContainer::BusRouting::UI); - m_SlicePickedSound.Create("Base.rte/Sounds/GUIs/SlicePicked.flac", true, false, SoundContainer::BusRouting::UI); + m_SlicePickedSound->Create("Base.rte/Sounds/GUIs/SlicePicked.flac", true, false, SoundContainer::BusRouting::UI); - // m_DisabledPickedSound.Create("Base.rte/Sounds/GUIs/PieMenuExit.flac", true, false, SoundContainer::BusRouting::UI); + // m_DisabledPickedSound->Create("Base.rte/Sounds/GUIs/PieMenuExit.flac", true, false, SoundContainer::BusRouting::UI); m_DisabledPickedSound = m_PieMenuExitSound; - m_FundsChangedSound.Create("Base.rte/Sounds/GUIs/FundsChanged1.flac", true, false, SoundContainer::BusRouting::UI); - m_FundsChangedSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged2.flac", true); - m_FundsChangedSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged3.flac", true); - m_FundsChangedSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged4.flac", true); - m_FundsChangedSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged5.flac", true); - m_FundsChangedSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged6.flac", true); - m_FundsChangedSound.SetSoundOverlapMode(SoundContainer::SoundOverlapMode::RESTART); + m_FundsChangedSound->Create("Base.rte/Sounds/GUIs/FundsChanged1.flac", true, false, SoundContainer::BusRouting::UI); + m_FundsChangedSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged2.flac", true); + m_FundsChangedSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged3.flac", true); + m_FundsChangedSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged4.flac", true); + m_FundsChangedSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged5.flac", true); + m_FundsChangedSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/FundsChanged6.flac", true); + m_FundsChangedSound->SetSoundOverlapMode(SoundContainer::SoundOverlapMode::RESTART); - m_ActorSwitchSound.Create("Base.rte/Sounds/GUIs/ActorSwitch.flac", true, false, SoundContainer::BusRouting::UI); + m_ActorSwitchSound->Create("Base.rte/Sounds/GUIs/ActorSwitch.flac", true, false, SoundContainer::BusRouting::UI); - m_BrainSwitchSound.Create("Base.rte/Sounds/GUIs/BrainSwitch.flac", true, false, SoundContainer::BusRouting::UI); + m_BrainSwitchSound->Create("Base.rte/Sounds/GUIs/BrainSwitch.flac", true, false, SoundContainer::BusRouting::UI); - m_CameraTravelSound.Create("Base.rte/Sounds/GUIs/CameraTravel1.flac", true, false, SoundContainer::BusRouting::UI); - m_CameraTravelSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/CameraTravel2.flac", true); - m_CameraTravelSound.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/CameraTravel3.flac", true); + m_CameraTravelSound->Create("Base.rte/Sounds/GUIs/CameraTravel1.flac", true, false, SoundContainer::BusRouting::UI); + m_CameraTravelSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/CameraTravel2.flac", true); + m_CameraTravelSound->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/CameraTravel3.flac", true); - // m_AreaPickedSound.Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); + // m_AreaPickedSound->Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); m_AreaPickedSound = m_ConfirmSound; - // m_ObjectPickedSound.Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); + // m_ObjectPickedSound->Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); m_ObjectPickedSound = m_ConfirmSound; - // m_PurchaseMadeSound.Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); + // m_PurchaseMadeSound->Create("Base.rte/Sounds/GUIs/MenuEnter.flac", true, false, SoundContainer::BusRouting::UI); m_PurchaseMadeSound = m_ConfirmSound; - m_PlacementBlip.Create("Base.rte/Sounds/GUIs/PlacementBlip.flac", true, false, SoundContainer::BusRouting::UI); + m_PlacementBlip->Create("Base.rte/Sounds/GUIs/PlacementBlip.flac", true, false, SoundContainer::BusRouting::UI); - m_PlacementThud.Create("Base.rte/Sounds/GUIs/PlacementThud1.flac", true, false, SoundContainer::BusRouting::UI); - m_PlacementThud.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementThud2.flac", true); + m_PlacementThud->Create("Base.rte/Sounds/GUIs/PlacementThud1.flac", true, false, SoundContainer::BusRouting::UI); + m_PlacementThud->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementThud2.flac", true); - m_PlacementGravel.Create("Base.rte/Sounds/GUIs/PlacementGravel1.flac", true, false, SoundContainer::BusRouting::UI); - m_PlacementGravel.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel2.flac", true); - m_PlacementGravel.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel3.flac", true); - m_PlacementGravel.GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel4.flac", true); + m_PlacementGravel->Create("Base.rte/Sounds/GUIs/PlacementGravel1.flac", true, false, SoundContainer::BusRouting::UI); + m_PlacementGravel->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel2.flac", true); + m_PlacementGravel->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel3.flac", true); + m_PlacementGravel->GetTopLevelSoundSet().AddSound("Base.rte/Sounds/GUIs/PlacementGravel4.flac", true); } diff --git a/Source/GUI/GUISound.h b/Source/GUI/GUISound.h index 2bced228bf..df8a27c3f8 100644 --- a/Source/GUI/GUISound.h +++ b/Source/GUI/GUISound.h @@ -2,6 +2,7 @@ #include "SoundContainer.h" #include "Singleton.h" +#include #define g_GUISound GUISound::Instance() @@ -27,152 +28,152 @@ namespace RTE { #pragma region Getters /// Gets juicy logo signature jingle Sound. /// @return Juicy logo signature jingle Sound. - SoundContainer* SplashSound() { return &m_SplashSound; } + std::shared_ptr SplashSound() { return m_SplashSound; } /// Gets SoundContainer for enabling menu. /// @return SoundContainer for enabling menu. - SoundContainer* EnterMenuSound() { return &m_EnterMenuSound; } + std::shared_ptr EnterMenuSound() { return m_EnterMenuSound; } /// Gets SoundContainer for disabling menu. /// @return SoundContainer for disabling menu. - SoundContainer* ExitMenuSound() { return &m_ExitMenuSound; } + std::shared_ptr ExitMenuSound() { return m_ExitMenuSound; } /// Gets SoundContainer for changing focus. /// @return SoundContainer for changing focus. - SoundContainer* FocusChangeSound() { return &m_FocusChangeSound; } + std::shared_ptr FocusChangeSound() { return m_FocusChangeSound; } /// Gets SoundContainer for selecting items in list, etc. /// @return SoundContainer for selecting items in list, etc. - SoundContainer* SelectionChangeSound() { return &m_SelectionChangeSound; } + std::shared_ptr SelectionChangeSound() { return m_SelectionChangeSound; } /// Gets SoundContainer for adding or deleting items in list. /// @return SoundContainer for adding or deleting items in list. - SoundContainer* ItemChangeSound() { return &m_ItemChangeSound; } + std::shared_ptr ItemChangeSound() { return m_ItemChangeSound; } /// Gets SoundContainer for button press. /// @return SoundContainer for button press. - SoundContainer* ButtonPressSound() { return &m_ButtonPressSound; } + std::shared_ptr ButtonPressSound() { return m_ButtonPressSound; } /// Gets SoundContainer for button press of going back button. /// @return SoundContainer for button press of going back button. - SoundContainer* BackButtonPressSound() { return &m_BackButtonPressSound; } + std::shared_ptr BackButtonPressSound() { return m_BackButtonPressSound; } /// Gets SoundContainer for confirming a selection. /// @return SoundContainer for confirming a selection. - SoundContainer* ConfirmSound() { return &m_ConfirmSound; } + std::shared_ptr ConfirmSound() { return m_ConfirmSound; } /// Gets SoundContainer for erroneous input. /// @return SoundContainer for erroneous input. - SoundContainer* UserErrorSound() { return &m_UserErrorSound; } + std::shared_ptr UserErrorSound() { return m_UserErrorSound; } /// Gets SoundContainer for testing volume when adjusting volume sliders. /// @return SoundContainer for testing volume when adjusting volume sliders. - SoundContainer* TestSound() { return &m_TestSound; } + std::shared_ptr TestSound() { return m_TestSound; } /// Gets SoundContainer for opening pie menu. /// @return SoundContainer for opening pie menu. - SoundContainer* PieMenuEnterSound() { return &m_PieMenuEnterSound; } + std::shared_ptr PieMenuEnterSound() { return m_PieMenuEnterSound; } /// Gets SoundContainer for closing pie menu. /// @return SoundContainer for closing pie menu. - SoundContainer* PieMenuExitSound() { return &m_PieMenuExitSound; } + std::shared_ptr PieMenuExitSound() { return m_PieMenuExitSound; } /// Gets SoundContainer for when PieMenu hover arrow appears or changes slice. /// @return SoundContainer for when PieMenu hover arrow appears or changes slice. - SoundContainer* HoverChangeSound() { return &m_HoverChangeSound; } + std::shared_ptr HoverChangeSound() { return m_HoverChangeSound; } /// Gets SoundContainer for when PieMenu hover arrow appears or changes to a disabled slice. /// @return SoundContainer for when PieMenu hover arrow appears or changes to a disabled slice. - SoundContainer* HoverDisabledSound() { return &m_HoverDisabledSound; } + std::shared_ptr HoverDisabledSound() { return m_HoverDisabledSound; } /// Gets SoundContainer for picking a valid PieMenu slice. /// @return SoundContainer for picking a valid PieMenu slice. - SoundContainer* SlicePickedSound() { return &m_SlicePickedSound; } + std::shared_ptr SlicePickedSound() { return m_SlicePickedSound; } /// Gets SoundContainer for erroneous input in PieMenu. /// @return SoundContainer for erroneous input in PieMenu. - SoundContainer* DisabledPickedSound() { return &m_DisabledPickedSound; } + std::shared_ptr DisabledPickedSound() { return m_DisabledPickedSound; } /// Gets SoundContainer for when the funds of a team changes. /// @return SoundContainer for when the funds of a team changes. - SoundContainer* FundsChangedSound() { return &m_FundsChangedSound; } + std::shared_ptr FundsChangedSound() { return m_FundsChangedSound; } /// Gets SoundContainer for switching between regular (non-brain) actors. /// @return SoundContainer for switching between regular (non-brain) actors. - SoundContainer* ActorSwitchSound() { return &m_ActorSwitchSound; } + std::shared_ptr ActorSwitchSound() { return m_ActorSwitchSound; } /// Gets SoundContainer for switching to the brain shortcut. /// @return SoundContainer for switching to the brain shortcut. - SoundContainer* BrainSwitchSound() { return &m_BrainSwitchSound; } + std::shared_ptr BrainSwitchSound() { return m_BrainSwitchSound; } /// Gets SoundContainer when camera is traveling between actors. /// @return SoundContainer when camera is traveling between actors. - SoundContainer* CameraTravelSound() { return &m_CameraTravelSound; } + std::shared_ptr CameraTravelSound() { return m_CameraTravelSound; } /// Gets SoundContainer for making an area focus. /// @return SoundContainer for making an area focus. - SoundContainer* AreaPickedSound() { return &m_AreaPickedSound; } + std::shared_ptr AreaPickedSound() { return m_AreaPickedSound; } /// Gets SoundContainer for making an object focus. /// @return SoundContainer for making an object focus. - SoundContainer* ObjectPickedSound() { return &m_ObjectPickedSound; } + std::shared_ptr ObjectPickedSound() { return m_ObjectPickedSound; } /// Gets SoundContainer for making a purchase. /// @return SoundContainer for making a purchase. - SoundContainer* PurchaseMadeSound() { return &m_PurchaseMadeSound; } + std::shared_ptr PurchaseMadeSound() { return m_PurchaseMadeSound; } /// Gets SoundContainer for placement of object to scene. /// @return SoundContainer for placement of object to scene. - SoundContainer* PlacementBlip() { return &m_PlacementBlip; } + std::shared_ptr PlacementBlip() { return m_PlacementBlip; } /// Gets SoundContainer for placement of object to scene. /// @return SoundContainer for placement of object to scene. - SoundContainer* PlacementThud() { return &m_PlacementThud; } + std::shared_ptr PlacementThud() { return m_PlacementThud; } /// Gets SoundContainer for gravely placement of object to scene. /// @return SoundContainer for gravely placement of object to scene. - SoundContainer* PlacementGravel() { return &m_PlacementGravel; } + std::shared_ptr PlacementGravel() { return m_PlacementGravel; } #pragma endregion protected: - SoundContainer m_SplashSound; //! Juicy logo signature jingle sound. + std::shared_ptr m_SplashSound; //! Juicy logo signature jingle sound. - SoundContainer m_EnterMenuSound; //! SoundContainer for enabling menu. - SoundContainer m_ExitMenuSound; //! SoundContainer for disabling menu. + std::shared_ptr m_EnterMenuSound; //! SoundContainer for enabling menu. + std::shared_ptr m_ExitMenuSound; //! SoundContainer for disabling menu. - SoundContainer m_FocusChangeSound; //! SoundContainer for changing focus. + std::shared_ptr m_FocusChangeSound; //! SoundContainer for changing focus. - SoundContainer m_SelectionChangeSound; //! SoundContainer for selecting items in list, etc. - SoundContainer m_ItemChangeSound; //! SoundContainer for adding or deleting items in list. + std::shared_ptr m_SelectionChangeSound; //! SoundContainer for selecting items in list, etc. + std::shared_ptr m_ItemChangeSound; //! SoundContainer for adding or deleting items in list. - SoundContainer m_ButtonPressSound; //! SoundContainer for button press. - SoundContainer m_BackButtonPressSound; //! SoundContainer for button press of going back button. - SoundContainer m_ConfirmSound; //! SoundContainer for confirming a selection. - SoundContainer m_UserErrorSound; //! SoundContainer for erroneous input. - SoundContainer m_TestSound; //! SoundContainer for testing volume when adjusting volume sliders. + std::shared_ptr m_ButtonPressSound; //! SoundContainer for button press. + std::shared_ptr m_BackButtonPressSound; //! SoundContainer for button press of going back button. + std::shared_ptr m_ConfirmSound; //! SoundContainer for confirming a selection. + std::shared_ptr m_UserErrorSound; //! SoundContainer for erroneous input. + std::shared_ptr m_TestSound; //! SoundContainer for testing volume when adjusting volume sliders. - SoundContainer m_PieMenuEnterSound; //! SoundContainer for opening pie menu. - SoundContainer m_PieMenuExitSound; //! SoundContainer for closing pie menu. + std::shared_ptr m_PieMenuEnterSound; //! SoundContainer for opening pie menu. + std::shared_ptr m_PieMenuExitSound; //! SoundContainer for closing pie menu. - SoundContainer m_HoverChangeSound; //! SoundContainer for when PieMenu hover arrow appears or changes slice. - SoundContainer m_HoverDisabledSound; //! SoundContainer for when PieMenu hover arrow appears or changes to a disabled slice. - SoundContainer m_SlicePickedSound; //! SoundContainer for picking a valid PieMenu slice. - SoundContainer m_DisabledPickedSound; //! SoundContainer for erroneous input in PieMenu. + std::shared_ptr m_HoverChangeSound; //! SoundContainer for when PieMenu hover arrow appears or changes slice. + std::shared_ptr m_HoverDisabledSound; //! SoundContainer for when PieMenu hover arrow appears or changes to a disabled slice. + std::shared_ptr m_SlicePickedSound; //! SoundContainer for picking a valid PieMenu slice. + std::shared_ptr m_DisabledPickedSound; //! SoundContainer for erroneous input in PieMenu. - SoundContainer m_FundsChangedSound; //! SoundContainer for when the funds of a team changes. + std::shared_ptr m_FundsChangedSound; //! SoundContainer for when the funds of a team changes. - SoundContainer m_ActorSwitchSound; //! SoundContainer for switching between regular (non-brain) actors. - SoundContainer m_BrainSwitchSound; //! SoundContainer for switching to the brain shortcut. + std::shared_ptr m_ActorSwitchSound; //! SoundContainer for switching between regular (non-brain) actors. + std::shared_ptr m_BrainSwitchSound; //! SoundContainer for switching to the brain shortcut. - SoundContainer m_CameraTravelSound; //! SoundContainer when camera is traveling between actors. + std::shared_ptr m_CameraTravelSound; //! SoundContainer when camera is traveling between actors. - SoundContainer m_AreaPickedSound; //! SoundContainer for making an area focus. - SoundContainer m_ObjectPickedSound; //! SoundContainer for making an object focus. - SoundContainer m_PurchaseMadeSound; //! SoundContainer for making a purchase. + std::shared_ptr m_AreaPickedSound; //! SoundContainer for making an area focus. + std::shared_ptr m_ObjectPickedSound; //! SoundContainer for making an object focus. + std::shared_ptr m_PurchaseMadeSound; //! SoundContainer for making a purchase. - SoundContainer m_PlacementBlip; //! SoundContainer for placement of object to scene. - SoundContainer m_PlacementThud; //! SoundContainer for placement of object to scene. - SoundContainer m_PlacementGravel; //! SoundContainer for gravely placement of object to scene. + std::shared_ptr m_PlacementBlip; //! SoundContainer for placement of object to scene. + std::shared_ptr m_PlacementThud; //! SoundContainer for placement of object to scene. + std::shared_ptr m_PlacementGravel; //! SoundContainer for gravely placement of object to scene. /// Clears all the member variables of this GUISound, effectively resetting the members of this abstraction level only. void Clear(); diff --git a/Source/Lua/LuaAdapterDefinitions.h b/Source/Lua/LuaAdapterDefinitions.h index 3e59ae2fe2..5e41ec288b 100644 --- a/Source/Lua/LuaAdapterDefinitions.h +++ b/Source/Lua/LuaAdapterDefinitions.h @@ -102,6 +102,15 @@ namespace RTE { static TYPE* Random##TYPE(std::string groupName, std::string dataModuleName); \ static TYPE* Random##TYPE(std::string groupName) + +// Same as `LuaEntityCreateFunctionsDeclarationsForType` but using shared_ptr return types. +#define LuaEntityCreateFunctionsDeclarationsForTypeUsingSharedPtr(TYPE) \ + static std::shared_ptr Create##TYPE(std::string preseName, std::string moduleName); \ + static std::shared_ptr Create##TYPE(std::string preset); \ + static std::shared_ptr Random##TYPE(std::string groupName, int moduleSpaceID); \ + static std::shared_ptr Random##TYPE(std::string groupName, std::string dataModuleName); \ + static std::shared_ptr Random##TYPE(std::string groupName) + LuaEntityCreateFunctionsDeclarationsForType(SoundContainer); LuaEntityCreateFunctionsDeclarationsForType(Attachable); LuaEntityCreateFunctionsDeclarationsForType(Arm); @@ -137,6 +146,10 @@ namespace RTE { #define LuaEntityCloneFunctionDeclarationForType(TYPE) \ static TYPE* Clone##TYPE(const TYPE* thisEntity) +// Same as `LuaEntityCloneFunctionDeclarationForType` but using shared_ptr return type. +#define LuaEntityCloneFunctionDeclarationForTypeUsingSharedPtr(TYPE) \ + static std::shared_ptr Clone##TYPE(const std::shared_ptr thisEntity) + LuaEntityCloneFunctionDeclarationForType(Entity); LuaEntityCloneFunctionDeclarationForType(SoundContainer); LuaEntityCloneFunctionDeclarationForType(SceneObject); @@ -179,6 +192,13 @@ namespace RTE { static bool Is##TYPE(Entity* entity); \ static LuabindObjectWrapper* ToLuabindObject##TYPE(Entity* entity, lua_State* luaState) +// Same as `LuaEntityCastFunctionsDeclarationsForType(TYPE)` but using shared_ptr types. +#define LuaEntityCastFunctionsDeclarationsForTypeUsingSharedPtr(TYPE) \ + static std::shared_ptr To##TYPE(std::shared_ptr entity); \ + static const std::shared_ptr ToConst##TYPE(const std::shared_ptr entity); \ + static bool Is##TYPE(std::shared_ptr entity); \ + static LuabindObjectWrapper* ToLuabindObject##TYPE(Entity* entity, lua_State* luaState) + static std::unordered_map> s_EntityToLuabindObjectCastFunctions; //!< Map of preset names to casting methods for ensuring objects are downcast properly when passed into Lua. LuaEntityCastFunctionsDeclarationsForType(Entity); @@ -226,26 +246,13 @@ namespace RTE { #define LuaPropertyOwnershipSafetyFakerFunctionDeclaration(OBJECTTYPE, PROPERTYTYPE, SETTERFUNCTION) \ static void OBJECTTYPE##SETTERFUNCTION(OBJECTTYPE* luaSelfObject, PROPERTYTYPE* objectToSet) - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(MOSRotating, SoundContainer, SetGibSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Attachable, AEmitter, SetBreakWound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Attachable, AEmitter, SetParentBreakWound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AEmitter, Attachable, SetFlash); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AEmitter, SoundContainer, SetEmissionSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AEmitter, SoundContainer, SetBurstSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AEmitter, SoundContainer, SetEndSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ADoor, Attachable, SetDoor); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Arm, HeldDevice, SetHeldDevice); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Leg, Attachable, SetFoot); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, PieMenu, SetPieMenu); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, SoundContainer, SetBodyHitSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, SoundContainer, SetAlarmSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, SoundContainer, SetPainSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, SoundContainer, SetDeathSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Actor, SoundContainer, SetDeviceSwitchSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ADoor, SoundContainer, SetDoorMoveStartSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ADoor, SoundContainer, SetDoorMoveSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ADoor, SoundContainer, SetDoorDirectionChangeSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ADoor, SoundContainer, SetDoorMoveEndSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, Attachable, SetHead); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, AEJetpack, SetJetpack); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, Arm, SetFGArm); @@ -254,18 +261,13 @@ namespace RTE { LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, Leg, SetBGLeg); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, Attachable, SetFGFoot); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, Attachable, SetBGFoot); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(AHuman, SoundContainer, SetStrideSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, Turret, SetTurret); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, AEJetpack, SetJetpack); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, Leg, SetLeftFGLeg); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, Leg, SetLeftBGLeg); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, Leg, SetRightFGLeg); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, Leg, SetRightBGLeg); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACrab, SoundContainer, SetStrideSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(Turret, HeldDevice, SetFirstMountedDevice); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACraft, SoundContainer, SetHatchOpenSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACraft, SoundContainer, SetHatchCloseSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACraft, SoundContainer, SetCrashSound); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACDropShip, AEmitter, SetRightThruster); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACDropShip, AEmitter, SetLeftThruster); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACDropShip, AEmitter, SetURightThruster); @@ -281,14 +283,6 @@ namespace RTE { LuaPropertyOwnershipSafetyFakerFunctionDeclaration(ACRocket, AEmitter, SetURightThruster); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, Magazine, SetMagazine); LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, Attachable, SetFlash); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetPreFireSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetFireSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetFireEchoSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetActiveSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetDeactivationSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetEmptySound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetReloadStartSound); - LuaPropertyOwnershipSafetyFakerFunctionDeclaration(HDFirearm, SoundContainer, SetReloadEndSound); }; #pragma endregion diff --git a/Source/Lua/LuaAdapters.cpp b/Source/Lua/LuaAdapters.cpp index a3f1b390df..a84ab3536d 100644 --- a/Source/Lua/LuaAdapters.cpp +++ b/Source/Lua/LuaAdapters.cpp @@ -55,6 +55,59 @@ std::unordered_map LuaAdaptersEntityCreate::Create##TYPE(std::string preseName, std::string moduleName) { \ + const Entity* entityPreset = g_PresetMan.GetEntityPreset(#TYPE, preseName, moduleName); \ + if (!entityPreset) { \ + g_ConsoleMan.PrintString(std::string("ERROR: There is no ") + std::string(#TYPE) + std::string(" of the Preset name \"") + preseName + std::string("\" defined in the \"") + moduleName + std::string("\" Data Module!")); \ + return nullptr; \ + } \ + auto outEnt = std::make_shared(); \ + entityPreset->Clone(&*outEnt); \ + return outEnt; \ + } \ + std::shared_ptr LuaAdaptersEntityCreate::Create##TYPE(std::string preset) { \ + return Create##TYPE(preset, "All"); \ + } \ + std::shared_ptr LuaAdaptersEntityCreate::Random##TYPE(std::string groupName, int moduleSpaceID) { \ + const Entity* entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech(groupName, #TYPE, moduleSpaceID); \ + if (!entityPreset) { \ + entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech(groupName, #TYPE, g_PresetMan.GetModuleID("Base.rte")); \ + } \ + if (!entityPreset) { \ + entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech("Any", #TYPE, moduleSpaceID); \ + } \ + if (!entityPreset) { \ + g_ConsoleMan.PrintString(std::string("WARNING: Could not find any ") + std::string(#TYPE) + std::string(" defined in a Group called \"") + groupName + std::string("\" in module ") + g_PresetMan.GetDataModuleName(moduleSpaceID) + "!"); \ + return nullptr; \ + } \ + auto outEnt = std::make_shared(); \ + entityPreset->Clone(&*outEnt); \ + return outEnt; \ + } \ + std::shared_ptr LuaAdaptersEntityCreate::Random##TYPE(std::string groupName, std::string dataModuleName) { \ + int moduleSpaceID = g_PresetMan.GetModuleID(dataModuleName); \ + const Entity* entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech(groupName, #TYPE, moduleSpaceID); \ + if (!entityPreset) { \ + entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech(groupName, #TYPE, g_PresetMan.GetModuleID("Base.rte")); \ + } \ + if (!entityPreset) { \ + entityPreset = g_PresetMan.GetRandomBuyableOfGroupFromTech("Any", #TYPE, moduleSpaceID); \ + } \ + if (!entityPreset) { \ + g_ConsoleMan.PrintString(std::string("WARNING: Could not find any ") + std::string(#TYPE) + std::string(" defined in a Group called \"") + groupName + std::string("\" in module ") + dataModuleName + "!"); \ + return nullptr; \ + } \ + auto outEnt = std::make_shared(); \ + entityPreset->Clone(&*outEnt); \ + return outEnt; \ + } \ + std::shared_ptr LuaAdaptersEntityCreate::Random##TYPE(std::string groupName) { \ + return Random##TYPE(groupName, "All"); \ + } + LuaEntityCreateFunctionsDefinitionsForType(SoundContainer); LuaEntityCreateFunctionsDefinitionsForType(Attachable); LuaEntityCreateFunctionsDefinitionsForType(Arm); @@ -93,6 +146,18 @@ LuaEntityCreateFunctionsDefinitionsForType(PieMenu); return nullptr; \ } +#define LuaEntityCloneFunctionDefinitionForTypeUsingSharedPtr(TYPE) \ + std::shared_ptr LuaAdaptersEntityClone::Clone##TYPE(const std::shared_ptr thisEntity) { \ + if (thisEntity) { \ + auto newEntity = std::make_shared(); \ + thisEntity->Clone(&*newEntity); \ + return newEntity; \ + } \ + g_ConsoleMan.PrintString(std::string("ERROR: Tried to clone a ") + std::string(#TYPE) + std::string(" reference that is nil!")); \ + return nullptr; \ + } + + LuaEntityCloneFunctionDefinitionForType(Entity); LuaEntityCloneFunctionDefinitionForType(SoundContainer); LuaEntityCloneFunctionDefinitionForType(SceneObject); @@ -153,6 +218,32 @@ LuaEntityCloneFunctionDefinitionForType(PieMenu); return true; \ }() +#define LuaEntityCastFunctionsDefinitionsForTypeUsingSharedPtr(TYPE) \ + std::shared_ptr LuaAdaptersEntityCast::To##TYPE(std::shared_ptr entity) { \ + std::shared_ptr targetType = dynamic_pointer_cast(entity); \ + if (!targetType) { \ + g_ConsoleMan.PrintString(std::string("ERROR: Tried to convert a non-") + std::string(#TYPE) + std::string(" Entity reference to an ") + std::string(#TYPE) + std::string(" reference! Entity was ") + (entity ? entity->GetPresetName() : "nil")); \ + } \ + return targetType; \ + } \ + const std::shared_ptr LuaAdaptersEntityCast::ToConst##TYPE(const std::shared_ptr entity) { \ + const std::shared_ptr targetType = dynamic_pointer_cast(entity); \ + if (!targetType) { \ + g_ConsoleMan.PrintString(std::string("ERROR: Tried to convert a non-") + std::string(#TYPE) + std::string(" Entity reference to an ") + std::string(#TYPE) + std::string(" reference! Entity was ") + (entity ? entity->GetPresetName() : "nil")); \ + } \ + return targetType; \ + } \ + bool LuaAdaptersEntityCast::Is##TYPE(std::shared_ptr entity) { \ + return dynamic_pointer_cast(entity) ? true : false; \ + } \ + LuabindObjectWrapper* LuaAdaptersEntityCast::ToLuabindObject##TYPE(Entity* entity, lua_State* luaState) { \ + return new LuabindObjectWrapper(new luabind::object(luaState, dynamic_cast(entity)), ""); \ + } \ + /* Bullshit semi-hack to automatically populate the Luabind Object cast function map that is used in LuaMan::RunScriptFunctionObject */ \ + static const bool EntityToLuabindObjectCastMapAutoInserterForType##TYPE = []() { \ + LuaAdaptersEntityCast::s_EntityToLuabindObjectCastFunctions.try_emplace(std::string(#TYPE), &LuaAdaptersEntityCast::ToLuabindObject##TYPE); \ + return true; \ + }() LuaEntityCastFunctionsDefinitionsForType(Entity); LuaEntityCastFunctionsDefinitionsForType(SoundContainer); LuaEntityCastFunctionsDefinitionsForType(SceneObject); @@ -196,26 +287,13 @@ LuaEntityCastFunctionsDefinitionsForType(PieMenu); luaSelfObject->SETTERFUNCTION(objectToSet ? dynamic_cast(objectToSet->Clone()) : nullptr); \ } -LuaPropertyOwnershipSafetyFakerFunctionDefinition(MOSRotating, SoundContainer, SetGibSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Attachable, AEmitter, SetBreakWound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Attachable, AEmitter, SetParentBreakWound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AEmitter, Attachable, SetFlash); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(AEmitter, SoundContainer, SetEmissionSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(AEmitter, SoundContainer, SetBurstSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(AEmitter, SoundContainer, SetEndSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ADoor, Attachable, SetDoor); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Arm, HeldDevice, SetHeldDevice); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Leg, Attachable, SetFoot); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, PieMenu, SetPieMenu); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, SoundContainer, SetBodyHitSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, SoundContainer, SetAlarmSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, SoundContainer, SetPainSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, SoundContainer, SetDeathSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(Actor, SoundContainer, SetDeviceSwitchSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ADoor, SoundContainer, SetDoorMoveStartSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ADoor, SoundContainer, SetDoorMoveSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ADoor, SoundContainer, SetDoorDirectionChangeSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ADoor, SoundContainer, SetDoorMoveEndSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Attachable, SetHead); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, AEJetpack, SetJetpack); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Arm, SetFGArm); @@ -224,18 +302,13 @@ LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Leg, SetFGLeg); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Leg, SetBGLeg); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Attachable, SetFGFoot); LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, Attachable, SetBGFoot); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(AHuman, SoundContainer, SetStrideSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, Turret, SetTurret); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, AEJetpack, SetJetpack); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, Leg, SetLeftFGLeg); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, Leg, SetLeftBGLeg); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, Leg, SetRightFGLeg); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, Leg, SetRightBGLeg); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACrab, SoundContainer, SetStrideSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(Turret, HeldDevice, SetFirstMountedDevice); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACraft, SoundContainer, SetHatchOpenSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACraft, SoundContainer, SetHatchCloseSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACraft, SoundContainer, SetCrashSound); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACDropShip, AEmitter, SetRightThruster); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACDropShip, AEmitter, SetLeftThruster); LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACDropShip, AEmitter, SetURightThruster); @@ -251,14 +324,6 @@ LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACRocket, AEmitter, SetULeftTh LuaPropertyOwnershipSafetyFakerFunctionDefinition(ACRocket, AEmitter, SetURightThruster); LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, Magazine, SetMagazine); LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, Attachable, SetFlash); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetPreFireSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetFireSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetFireEchoSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetActiveSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetDeactivationSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetEmptySound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetReloadStartSound); -LuaPropertyOwnershipSafetyFakerFunctionDefinition(HDFirearm, SoundContainer, SetReloadEndSound); void LuaAdaptersEntity::SetPresetName(Entity* luaSelfObject, const std::string& presetName) { luaSelfObject->SetPresetName(presetName, true); diff --git a/Source/Lua/LuaBindingRegisterDefinitions.h b/Source/Lua/LuaBindingRegisterDefinitions.h index 91be1b3c54..1f9d99bd21 100644 --- a/Source/Lua/LuaBindingRegisterDefinitions.h +++ b/Source/Lua/LuaBindingRegisterDefinitions.h @@ -3,6 +3,8 @@ #include "LuabindDefinitions.h" #include "LuaAdapterDefinitions.h" +#include + namespace RTE { // Should be ordered with most-derived classes first @@ -150,6 +152,12 @@ namespace RTE { .def("Clone", &LuaAdaptersEntityClone::Clone##TYPE, luabind::adopt(luabind::result)) \ .property("ClassName", &TYPE::GetClassName) +// Same as `ConcreteTypeLuaClassDefinition` but using std::shared_ptr +#define ConcreteTypeLuaClassDefinitionUsingSharedPtr(TYPE, PARENTTYPE) \ + luabind::class_>(#TYPE) \ + .def("Clone", &LuaAdaptersEntityClone::Clone##TYPE) \ + .property("ClassName", &TYPE::GetClassName) + /// Convenience macro for calling a register function of a type. #define RegisterLuaBindingsOfType(OWNINGSCOPE, TYPE) \ OWNINGSCOPE::Register##TYPE##LuaBindings() @@ -169,9 +177,22 @@ namespace RTE { luabind::def((std::string("Random") + std::string(#TYPE)).c_str(), (TYPE * (*)(std::string, std::string)) & LuaAdaptersEntityCreate::Random##TYPE, luabind::adopt(luabind::result)), \ luabind::def((std::string("Random") + std::string(#TYPE)).c_str(), (TYPE * (*)(std::string)) & LuaAdaptersEntityCreate::Random##TYPE, luabind::adopt(luabind::result)), \ luabind::def((std::string("To") + std::string(#TYPE)).c_str(), (TYPE * (*)(Entity*)) & LuaAdaptersEntityCast::To##TYPE), \ - luabind::def((std::string("To") + std::string(#TYPE)).c_str(), (const TYPE* (*)(const Entity*)) & LuaAdaptersEntityCast::ToConst##TYPE), \ + luabind::def((std::string("To") + std::string(#TYPE)).c_str(), (const TYPE * (*)(const Entity*)) & LuaAdaptersEntityCast::ToConst##TYPE), \ luabind::def((std::string("Is") + std::string(#TYPE)).c_str(), (bool (*)(const Entity*)) & LuaAdaptersEntityCast::Is##TYPE), \ OWNINGSCOPE::Register##TYPE##LuaBindings() + +/// Convenience macro for calling a register function of a concrete type, along with registering global bindings for adapters relevant to the type, using shared_ptr. +#define RegisterLuaBindingsOfConcreteTypeWithSharedPtr(OWNINGSCOPE, TYPE) \ + luabind::def((std::string("Create") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(std::string, std::string)) & LuaAdaptersEntityCreate::Create##TYPE), \ + luabind::def((std::string("Create") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(std::string)) & LuaAdaptersEntityCreate::Create##TYPE), \ + luabind::def((std::string("Random") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(std::string, int)) & LuaAdaptersEntityCreate::Random##TYPE), \ + luabind::def((std::string("Random") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(std::string, std::string)) & LuaAdaptersEntityCreate::Random##TYPE), \ + luabind::def((std::string("Random") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(std::string)) & LuaAdaptersEntityCreate::Random##TYPE), \ + luabind::def((std::string("To") + std::string(#TYPE)).c_str(), (std::shared_ptr (*)(Entity*)) & LuaAdaptersEntityCast::To##TYPE), \ + luabind::def((std::string("To") + std::string(#TYPE)).c_str(), (const std::shared_ptr (*)(const std::shared_ptr)) & LuaAdaptersEntityCast::ToConst##TYPE), \ + luabind::def((std::string("Is") + std::string(#TYPE)).c_str(), (bool (*)(const Entity*)) & LuaAdaptersEntityCast::Is##TYPE), \ + OWNINGSCOPE::Register##TYPE##LuaBindings() + #pragma endregion /// Struct that contains Lua binding registration functions for System classes. diff --git a/Source/Lua/LuaBindingsEntities.cpp b/Source/Lua/LuaBindingsEntities.cpp index 6b138782a3..88a86cfa7e 100644 --- a/Source/Lua/LuaBindingsEntities.cpp +++ b/Source/Lua/LuaBindingsEntities.cpp @@ -58,7 +58,7 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, ACrab) { .property("LeftBGLeg", &ACrab::GetLeftBGLeg, &LuaAdaptersPropertyOwnershipSafetyFaker::ACrabSetLeftBGLeg) .property("RightFGLeg", &ACrab::GetRightFGLeg, &LuaAdaptersPropertyOwnershipSafetyFaker::ACrabSetRightFGLeg) .property("RightBGLeg", &ACrab::GetRightBGLeg, &LuaAdaptersPropertyOwnershipSafetyFaker::ACrabSetRightBGLeg) - .property("StrideSound", &ACrab::GetStrideSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ACrabSetStrideSound) + .property("StrideSound", &ACrab::GetStrideSound, &ACrab::SetStrideSound) .property("StrideFrame", &ACrab::StrideFrame) .property("EquippedItem", &ACrab::GetEquippedItem) .property("FirearmIsReady", &ACrab::FirearmIsReady) @@ -114,9 +114,9 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, ACraft) { return AbstractTypeLuaClassDefinition(ACraft, Actor) .property("HatchState", &ACraft::GetHatchState) - .property("HatchOpenSound", &ACraft::GetHatchOpenSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ACraftSetHatchOpenSound) - .property("HatchCloseSound", &ACraft::GetHatchCloseSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ACraftSetHatchCloseSound) - .property("CrashSound", &ACraft::GetCrashSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ACraftSetCrashSound) + .property("HatchOpenSound", &ACraft::GetHatchOpenSound, &ACraft::SetHatchOpenSound) + .property("HatchCloseSound", &ACraft::GetHatchCloseSound, &ACraft::SetHatchCloseSound) + .property("CrashSound", &ACraft::GetCrashSound, &ACraft::SetCrashSound) .property("CanEnterOrbit", &ACraft::GetCanEnterOrbit, &ACraft::SetCanEnterOrbit) .property("MaxPassengers", &ACraft::GetMaxPassengers) .property("DeliveryDelayMultiplier", &ACraft::GetDeliveryDelayMultiplier) @@ -170,11 +170,11 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, Actor) { .def(luabind::constructor<>()) .property("PlayerControllable", &Actor::IsPlayerControllable, &Actor::SetPlayerControllable) - .property("BodyHitSound", &Actor::GetBodyHitSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ActorSetBodyHitSound) - .property("AlarmSound", &Actor::GetAlarmSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ActorSetAlarmSound) - .property("PainSound", &Actor::GetPainSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ActorSetPainSound) - .property("DeathSound", &Actor::GetDeathSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ActorSetDeathSound) - .property("DeviceSwitchSound", &Actor::GetDeviceSwitchSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ActorSetDeviceSwitchSound) + .property("BodyHitSound", &Actor::GetBodyHitSound, &Actor::SetBodyHitSound) + .property("AlarmSound", &Actor::GetAlarmSound, &Actor::SetAlarmSound) + .property("PainSound", &Actor::GetPainSound, &Actor::SetPainSound) + .property("DeathSound", &Actor::GetDeathSound, &Actor::SetDeathSound) + .property("DeviceSwitchSound", &Actor::GetDeviceSwitchSound, &Actor::SetDeviceSwitchSound) .property("ImpulseDamageThreshold", &Actor::GetTravelImpulseDamage, &Actor::SetTravelImpulseDamage) .property("StableRecoveryDelay", &Actor::GetStableRecoverDelay, &Actor::SetStableRecoverDelay) .property("CanRun", &Actor::GetCanRun, &Actor::SetCanRun) @@ -327,10 +327,10 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, ADoor) { return ConcreteTypeLuaClassDefinition(ADoor, Actor) .property("Door", &ADoor::GetDoor, &LuaAdaptersPropertyOwnershipSafetyFaker::ADoorSetDoor) - .property("DoorMoveStartSound", &ADoor::GetDoorMoveStartSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ADoorSetDoorMoveStartSound) - .property("DoorMoveSound", &ADoor::GetDoorMoveSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ADoorSetDoorMoveSound) - .property("DoorDirectionChangeSound", &ADoor::GetDoorDirectionChangeSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ADoorSetDoorDirectionChangeSound) - .property("DoorMoveEndSound", &ADoor::GetDoorMoveEndSound, &LuaAdaptersPropertyOwnershipSafetyFaker::ADoorSetDoorMoveEndSound) + .property("DoorMoveStartSound", &ADoor::GetDoorMoveStartSound, &ADoor::SetDoorMoveStartSound) + .property("DoorMoveSound", &ADoor::GetDoorMoveSound, &ADoor::SetDoorMoveSound) + .property("DoorDirectionChangeSound", &ADoor::GetDoorDirectionChangeSound, &ADoor::SetDoorDirectionChangeSound) + .property("DoorMoveEndSound", &ADoor::GetDoorMoveEndSound, &ADoor::SetDoorMoveEndSound) .def("GetDoorState", &ADoor::GetDoorState) .def("OpenDoor", &ADoor::OpenDoor) @@ -349,9 +349,9 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, ADoor) { LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, AEmitter) { return ConcreteTypeLuaClassDefinition(AEmitter, Attachable) - .property("EmissionSound", &AEmitter::GetEmissionSound, &LuaAdaptersPropertyOwnershipSafetyFaker::AEmitterSetEmissionSound) - .property("BurstSound", &AEmitter::GetBurstSound, &LuaAdaptersPropertyOwnershipSafetyFaker::AEmitterSetBurstSound) - .property("EndSound", &AEmitter::GetEndSound, &LuaAdaptersPropertyOwnershipSafetyFaker::AEmitterSetEndSound) + .property("EmissionSound", &AEmitter::GetEmissionSound, &AEmitter::SetEmissionSound) + .property("BurstSound", &AEmitter::GetBurstSound, &AEmitter::SetBurstSound) + .property("EndSound", &AEmitter::GetEndSound, &AEmitter::SetEndSound) .property("BurstScale", &AEmitter::GetBurstScale, &AEmitter::SetBurstScale) .property("PlayBurstSound", &AEmitter::GetPlayBurstSound, &AEmitter::SetPlayBurstSound) .property("EmitAngle", &AEmitter::GetEmitAngle, &AEmitter::SetEmitAngle) @@ -419,7 +419,7 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, AHuman) { .property("MaxWalkPathCrouchShift", &AHuman::GetMaxWalkPathCrouchShift, &AHuman::SetMaxWalkPathCrouchShift) .property("CrouchAmount", &AHuman::GetCrouchAmount) .property("CrouchAmountOverride", &AHuman::GetCrouchAmountOverride, &AHuman::SetCrouchAmountOverride) - .property("StrideSound", &AHuman::GetStrideSound, &LuaAdaptersPropertyOwnershipSafetyFaker::AHumanSetStrideSound) + .property("StrideSound", &AHuman::GetStrideSound, &AHuman::SetStrideSound) .property("UpperBodyState", &AHuman::GetUpperBodyState, &AHuman::SetUpperBodyState) .property("ProneState", &AHuman::GetProneState, &AHuman::SetProneState) .property("ThrowPrepTime", &AHuman::GetThrowPrepTime, &AHuman::SetThrowPrepTime) @@ -642,14 +642,14 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, HDFirearm) { .property("RoundInMagCapacity", &HDFirearm::GetRoundInMagCapacity) .property("Magazine", &HDFirearm::GetMagazine, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetMagazine) .property("Flash", &HDFirearm::GetFlash, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetFlash) - .property("PreFireSound", &HDFirearm::GetPreFireSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetPreFireSound) - .property("FireSound", &HDFirearm::GetFireSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetFireSound) - .property("FireEchoSound", &HDFirearm::GetFireEchoSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetFireEchoSound) - .property("ActiveSound", &HDFirearm::GetActiveSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetActiveSound) - .property("DeactivationSound", &HDFirearm::GetDeactivationSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetDeactivationSound) - .property("EmptySound", &HDFirearm::GetEmptySound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetEmptySound) - .property("ReloadStartSound", &HDFirearm::GetReloadStartSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetReloadStartSound) - .property("ReloadEndSound", &HDFirearm::GetReloadEndSound, &LuaAdaptersPropertyOwnershipSafetyFaker::HDFirearmSetReloadEndSound) + .property("PreFireSound", &HDFirearm::GetPreFireSound, &HDFirearm::SetPreFireSound) + .property("FireSound", &HDFirearm::GetFireSound, &HDFirearm::SetFireSound) + .property("FireEchoSound", &HDFirearm::GetFireEchoSound, &HDFirearm::SetFireEchoSound) + .property("ActiveSound", &HDFirearm::GetActiveSound, &HDFirearm::SetActiveSound) + .property("DeactivationSound", &HDFirearm::GetDeactivationSound, &HDFirearm::SetDeactivationSound) + .property("EmptySound", &HDFirearm::GetEmptySound, &HDFirearm::SetEmptySound) + .property("ReloadStartSound", &HDFirearm::GetReloadStartSound, &HDFirearm::SetReloadStartSound) + .property("ReloadEndSound", &HDFirearm::GetReloadEndSound, &HDFirearm::SetReloadEndSound) .property("ActivationDelay", &HDFirearm::GetActivationDelay, &HDFirearm::SetActivationDelay) .property("DeactivationDelay", &HDFirearm::GetDeactivationDelay, &HDFirearm::SetDeactivationDelay) .property("BaseReloadTime", &HDFirearm::GetBaseReloadTime, &HDFirearm::SetBaseReloadTime) @@ -868,7 +868,7 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, MOSRotating) { .property("RecoilOffset", &MOSRotating::GetRecoilOffset) .property("TravelImpulse", &MOSRotating::GetTravelImpulse, &MOSRotating::SetTravelImpulse) .property("GibWoundLimit", (int(MOSRotating::*)() const) & MOSRotating::GetGibWoundLimit, &MOSRotating::SetGibWoundLimit) - .property("GibSound", &MOSRotating::GetGibSound, &LuaAdaptersPropertyOwnershipSafetyFaker::MOSRotatingSetGibSound) + .property("GibSound", &MOSRotating::GetGibSound, &MOSRotating::SetGibSound) .property("GibImpulseLimit", &MOSRotating::GetGibImpulseLimit, &MOSRotating::SetGibImpulseLimit) .property("WoundCountAffectsImpulseLimitRatio", &MOSRotating::GetWoundCountAffectsImpulseLimitRatio) .property("GibAtEndOfLifetime", &MOSRotating::GetGibAtEndOfLifetime, &MOSRotating::SetGibAtEndOfLifetime) @@ -1301,7 +1301,7 @@ LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, SLBackground) { } LuaBindingRegisterFunctionDefinitionForType(EntityLuaBindings, SoundContainer) { - return ConcreteTypeLuaClassDefinition(SoundContainer, Entity) + return ConcreteTypeLuaClassDefinitionUsingSharedPtr(SoundContainer, Entity) .def(luabind::constructor<>()) diff --git a/Source/Lua/LuaBindingsManagers.cpp b/Source/Lua/LuaBindingsManagers.cpp index 55d60d8bfc..ad2c52aa08 100644 --- a/Source/Lua/LuaBindingsManagers.cpp +++ b/Source/Lua/LuaBindingsManagers.cpp @@ -1,6 +1,7 @@ // Make sure that binding definition files are always set to NOT use pre-compiled headers and conformance mode (/permissive) otherwise everything will be on fire! #include "LuaBindingRegisterDefinitions.h" +#include using namespace RTE; @@ -35,9 +36,9 @@ LuaBindingRegisterFunctionDefinitionForType(ManagerLuaBindings, AudioMan) { .def("IsMusicPlaying", &AudioMan::IsMusicPlaying) .def("SetMusicPitch", &AudioMan::SetMusicPitch) .def("SetMusicMuffledState", &AudioMan::SetMusicMuffledState) - .def("PlaySound", (SoundContainer * (AudioMan::*)(const std::string& filePath)) & AudioMan::PlaySound, luabind::adopt(luabind::result)) - .def("PlaySound", (SoundContainer * (AudioMan::*)(const std::string& filePath, const Vector& position)) & AudioMan::PlaySound, luabind::adopt(luabind::result)) - .def("PlaySound", (SoundContainer * (AudioMan::*)(const std::string& filePath, const Vector& position, int player)) & AudioMan::PlaySound, luabind::adopt(luabind::result)); + .def("PlaySound", (std::shared_ptr (AudioMan::*)(const std::string& filePath)) & AudioMan::PlaySound) + .def("PlaySound", (std::shared_ptr (AudioMan::*)(const std::string& filePath, const Vector& position)) & AudioMan::PlaySound) + .def("PlaySound", (std::shared_ptr (AudioMan::*)(const std::string& filePath, const Vector& position, int player)) & AudioMan::PlaySound); } LuaBindingRegisterFunctionDefinitionForType(ManagerLuaBindings, MusicMan) { diff --git a/Source/Lua/LuabindDefinitions.h b/Source/Lua/LuabindDefinitions.h index eb3ef34c10..7d13d79753 100644 --- a/Source/Lua/LuabindDefinitions.h +++ b/Source/Lua/LuabindDefinitions.h @@ -23,6 +23,13 @@ namespace luabind { return ptr.get(); } + /// Function that extracts the raw pointer from the smart pointer. This is needed when Lua calls member functions on held types, the 'this' pointer must be a raw pointer, it is also needed to allow the smart_pointer to raw_pointer conversion from Lua to C++. + /// @param ptr The smart pointer to get raw pointer for. + /// @return Raw pointer of the passed in smart pointer. + template Type* get_pointer(std::shared_ptr& ptr) { + return ptr.get(); + } + /// Can't have global enums in the master state so we use this dummy struct as a class and register the enums under it. struct enum_wrapper {}; } // namespace luabind diff --git a/Source/Managers/AudioMan.cpp b/Source/Managers/AudioMan.cpp index 4839b0af5f..4a9c3632f2 100644 --- a/Source/Managers/AudioMan.cpp +++ b/Source/Managers/AudioMan.cpp @@ -11,6 +11,7 @@ #include #include +#include using namespace RTE; @@ -260,12 +261,12 @@ void AudioMan::FinishIngameLoopingSounds() { } } -SoundContainer* AudioMan::PlaySound(const std::string& filePath, const Vector& position, int player) { +std::shared_ptr AudioMan::PlaySound(const std::string& filePath, const Vector& position, int player) { if (m_IsInMultiplayerMode) { return nullptr; } - SoundContainer* newSoundContainer = new SoundContainer(); + auto newSoundContainer = std::make_shared(); newSoundContainer->SetPosition(position); newSoundContainer->GetTopLevelSoundSet().AddSound(filePath); if (newSoundContainer->HasAnySounds()) { @@ -356,12 +357,19 @@ void AudioMan::ClearSoundEvents(int player) { } } -bool AudioMan::PlaySoundContainer(SoundContainer* soundContainer, int player) { - if (!m_AudioEnabled || !soundContainer || soundContainer->GetPlayingChannels()->size() >= c_MaxPlayingSoundsPerContainer) { +bool AudioMan::PlaySoundContainer(std::shared_ptr soundContainerInput, int player) { + if (!m_AudioEnabled || !soundContainerInput || soundContainerInput->GetPlayingChannels()->size() >= c_MaxPlayingSoundsPerContainer) { return false; } FMOD_RESULT result = FMOD_OK; + // Put the shared_ptr on the heap for FMOD to hold onto it as user data. + std::shared_ptr* soundContainerShared = new std::shared_ptr(); + *soundContainerShared = std::move(soundContainerInput); + + // ...But use a pointer for most of these, because the get() semantics are annoying. + SoundContainer* soundContainer = soundContainerShared->get(); + if (!soundContainer->SoundPropertiesUpToDate()) { result = soundContainer->UpdateSoundProperties(); soundContainer->GetTopLevelSoundSet().SelectNextSounds(); @@ -394,7 +402,7 @@ bool AudioMan::PlaySoundContainer(SoundContainer* soundContainer, int player) { result = (result == FMOD_OK) ? m_AudioSystem->playSound(soundData->SoundObject, channelGroupToPlayIn, true, &channel) : result; result = (result == FMOD_OK) ? channel->getIndex(&channelIndex) : result; - result = (result == FMOD_OK) ? channel->setUserData(soundContainer) : result; + result = (result == FMOD_OK) ? channel->setUserData(soundContainerShared) : result; result = (result == FMOD_OK) ? channel->setCallback(SoundChannelEndedCallback) : result; result = (result == FMOD_OK) ? channel->setPriority(soundContainer->GetPriority()) : result; float pitchVariationMultiplier = pitchVariationFactor == 1.0F ? 1.0F : RandomNum(1.0F / pitchVariationFactor, 1.0F * pitchVariationFactor); @@ -674,7 +682,7 @@ void AudioMan::Update3DEffectsForSFXChannels() { void* userData; result = result == FMOD_OK ? soundChannel->getUserData(&userData) : result; if (result == FMOD_OK) { - const SoundContainer* soundContainer = static_cast(userData); + const SoundContainer* soundContainer = static_cast*>(userData)->get(); if (sqrDistanceToPlayer < (m_MinimumDistanceForPanning * m_MinimumDistanceForPanning) || soundContainer->GetCustomPanValue() != 0.0f) { result = soundChannel->set3DLevel(0); } else if (sqrDistanceToPlayer < (doubleMinimumDistanceForPanning * doubleMinimumDistanceForPanning)) { @@ -701,7 +709,7 @@ FMOD_RESULT AudioMan::UpdatePositionalEffectsForSoundChannel(FMOD::Channel* soun return result; } - const SoundContainer* channelSoundContainer = static_cast(userData); + const SoundContainer* channelSoundContainer = static_cast*>(userData)->get(); bool sceneWraps = g_SceneMan.SceneWrapsX(); @@ -801,7 +809,8 @@ FMOD_RESULT F_CALLBACK AudioMan::SoundChannelEndedCallback(FMOD_CHANNELCONTROL* void* userData; result = (result == FMOD_OK) ? channel->getUserData(&userData) : result; if (result == FMOD_OK) { - SoundContainer* channelSoundContainer = static_cast(userData); + // Make sure to get ahold of the shared_ptr to delete it. + std::shared_ptr channelSoundContainer = *static_cast*>(userData); if (channelSoundContainer->IsBeingPlayed()) { channelSoundContainer->RemovePlayingChannel(channelIndex); } diff --git a/Source/Managers/AudioMan.h b/Source/Managers/AudioMan.h index f95ee170a0..36d046633e 100644 --- a/Source/Managers/AudioMan.h +++ b/Source/Managers/AudioMan.h @@ -8,6 +8,7 @@ #include "fmod/fmod.hpp" #include "fmod/fmod_errors.h" +#include #define g_AudioMan AudioMan::Instance() @@ -276,19 +277,19 @@ namespace RTE { /// Starts playing a certain sound file. /// @param filePath The path to the sound file to play. /// @return The new SoundContainer being played. OWNERSHIP IS TRANSFERRED! - SoundContainer* PlaySound(const std::string& filePath) { return PlaySound(filePath, Vector(), -1); } + std::shared_ptr PlaySound(const std::string& filePath) { return PlaySound(filePath, Vector(), -1); } /// Starts playing a certain sound file at a certain position for all players. /// @param filePath The path to the sound file to play. /// @return The new SoundContainer being played. OWNERSHIP IS TRANSFERRED! - SoundContainer* PlaySound(const std::string& filePath, const Vector& position) { return PlaySound(filePath, position, -1); } + std::shared_ptr PlaySound(const std::string& filePath, const Vector& position) { return PlaySound(filePath, position, -1); } /// Starts playing a certain sound file at a certain position for a certain player. /// @param filePath The path to the sound file to play. /// @param position The position at which to play the SoundContainer's sounds. /// @param player Which player to play the SoundContainer's sounds for, -1 means all players. /// @return The new SoundContainer being played. OWNERSHIP IS TRANSFERRED! - SoundContainer* PlaySound(const std::string& filePath, const Vector& position, int player); + std::shared_ptr PlaySound(const std::string& filePath, const Vector& position, int player); #pragma endregion #pragma region Network Audio Handling @@ -359,10 +360,10 @@ namespace RTE { private: #pragma region Sound Container Actions and Modifications /// Starts playing the next SoundSet of the given SoundContainer for the give player. - /// @param soundContainer Pointer to the SoundContainer to start playing. Ownership is NOT transferred! + /// @param soundContainer Pointer to the SoundContainer to start playing. /// @param player Which player to play the SoundContainer's sounds for, -1 means all players. Defaults to -1. /// @return Whether or not playback of the Sound was successful. - bool PlaySoundContainer(SoundContainer* soundContainer, int player = -1); + bool PlaySoundContainer(std::shared_ptr soundContainer, int player = -1); /// Sets/updates the position of a SoundContainer's playing sounds. /// @param soundContainer A pointer to a SoundContainer object. Ownership IS NOT transferred! diff --git a/Source/Managers/MusicMan.cpp b/Source/Managers/MusicMan.cpp index 6d69ac2f10..1a18acf688 100644 --- a/Source/Managers/MusicMan.cpp +++ b/Source/Managers/MusicMan.cpp @@ -3,6 +3,7 @@ #include "AudioMan.h" #include "ConsoleMan.h" #include "PresetMan.h" +#include using namespace RTE; @@ -171,11 +172,14 @@ bool MusicMan::CyclePlayingSoundContainers(bool smoothFade) { m_PreviousSoundContainer->Stop(); m_PreviousSoundContainer = nullptr; } - m_PreviousSoundContainer = std::unique_ptr(m_CurrentSoundContainer.release()); + m_PreviousSoundContainer = m_CurrentSoundContainer; + m_CurrentSoundContainer = nullptr; } // Clone instead of just point to because we might wanna keep this around even if the DynamicSong is gone - m_CurrentSoundContainer = std::unique_ptr(dynamic_cast(m_NextSoundContainer->Clone())); + m_CurrentSoundContainer = std::make_shared(); + m_NextSoundContainer->Clone(&*m_CurrentSoundContainer); + SelectNextSoundContainer(); m_MusicTimer.Reset(); float exitTime = m_CurrentSoundContainer->GetMusicExitTime(); @@ -215,7 +219,7 @@ bool MusicMan::EndDynamicMusic(bool fadeOutCurrent) { return true; } -void MusicMan::PlayInterruptingMusic(const SoundContainer* soundContainer) { +void MusicMan::PlayInterruptingMusic(const SoundContainer& soundContainer) { if (m_InterruptingMusicSoundContainer != nullptr) { m_InterruptingMusicSoundContainer->Stop(); } @@ -232,7 +236,7 @@ void MusicMan::PlayInterruptingMusic(const SoundContainer* soundContainer) { m_NextSoundContainer->SetPaused(true); } - m_InterruptingMusicSoundContainer = std::unique_ptr(dynamic_cast(soundContainer->Clone())); + m_InterruptingMusicSoundContainer = std::make_shared(soundContainer); m_InterruptingMusicSoundContainer->Play(); if (m_IsPlayingDynamicMusic) { m_ReturnToDynamicMusic = true; @@ -284,8 +288,8 @@ void MusicMan::SelectNextSongSection() { void MusicMan::SelectNextSoundContainer(bool playTransition) { if (playTransition) { - m_NextSoundContainer = &m_NextSongSection->SelectTransitionSoundContainer(); + m_NextSoundContainer = m_NextSongSection->SelectTransitionSoundContainer(); } else { - m_NextSoundContainer = &m_NextSongSection->SelectSoundContainer(); + m_NextSoundContainer = m_NextSongSection->SelectSoundContainer(); } } diff --git a/Source/Managers/MusicMan.h b/Source/Managers/MusicMan.h index e0aa541d20..bd1a0ca202 100644 --- a/Source/Managers/MusicMan.h +++ b/Source/Managers/MusicMan.h @@ -3,6 +3,7 @@ #include "DynamicSong.h" #include "Timer.h" #include "Singleton.h" +#include #define g_MusicMan MusicMan::Instance() @@ -77,7 +78,7 @@ namespace RTE { /// Used for hardcoded music like the intro and main menu. /// This is exposed to Lua, but the above hardcoded calls will happily override the Lua. Something to keep in mind. /// @param soundContainer The SoundContainer to play as interrupting music. - void PlayInterruptingMusic(const SoundContainer* soundContainer); + void PlayInterruptingMusic(const SoundContainer& soundContainer); /// Signals the end of hardcoded music, resuming dynamic music if needed. void EndInterruptingMusic(); @@ -86,16 +87,16 @@ namespace RTE { protected: bool m_IsPlayingDynamicMusic; //!< Whether this is actively playing dynamic music or not. - std::unique_ptr m_InterruptingMusicSoundContainer; //!< Current interrupting music being played. + std::shared_ptr m_InterruptingMusicSoundContainer; //!< Current interrupting music being played. - std::unique_ptr m_CurrentSong; //!< The current DynamicSong being played. + std::shared_ptr m_CurrentSong; //!< The current DynamicSong being played. std::string m_NextSongSectionType; //!< The type of DynamicSongSection we will try to play next. std::string m_CurrentSongSectionType; //!< The current type of DynamicSongSection we are actually playing. DynamicSongSection* m_NextSongSection; //!< The DynamicSongSection we will try to play next. - std::unique_ptr m_PreviousSoundContainer; //!< The previous SoundContainer that was played as music. We keep it to allow it to play out while Current ramps up. - std::unique_ptr m_CurrentSoundContainer; //!< The current selected SoundContainer playing as music. - SoundContainer* m_NextSoundContainer; //!< The next selected SoundContainer to play as music. + std::shared_ptr m_PreviousSoundContainer; //!< The previous SoundContainer that was played as music. We keep it to allow it to play out while Current ramps up. + std::shared_ptr m_CurrentSoundContainer; //!< The current selected SoundContainer playing as music. + std::shared_ptr m_NextSoundContainer; //!< The next selected SoundContainer to play as music. Timer m_MusicFadeTimer; //!< Timer for timing the start of music fading if a piece is cycled prematurely. bool m_PreviousSoundContainerSetToFade; //!< Whether this is waiting to fade out the PreviousSoundContainer in case of premature cycling. diff --git a/Source/Managers/SceneMan.cpp b/Source/Managers/SceneMan.cpp index 2fd0682046..f892b4830f 100644 --- a/Source/Managers/SceneMan.cpp +++ b/Source/Managers/SceneMan.cpp @@ -19,6 +19,7 @@ #include "SoundContainer.h" #include "tracy/Tracy.hpp" +#include using namespace RTE; @@ -139,8 +140,10 @@ int SceneMan::LoadScene(Scene* pNewScene, bool placeObjects, bool placeUnits) { } // Get the unseen reveal sound - if (!m_pUnseenRevealSound) - m_pUnseenRevealSound = dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Unseen Reveal Blip")->Clone()); + if (!m_pUnseenRevealSound) { + m_pUnseenRevealSound = std::make_shared(); + g_PresetMan.GetEntityPreset("SoundContainer", "Unseen Reveal Blip")->Clone(&*m_pUnseenRevealSound); + } // m_pCurrentScene->GetTerrain()->CleanAir(); @@ -284,7 +287,6 @@ void SceneMan::Destroy() { delete m_pCurrentScene; delete m_pDebugLayer; delete m_pMOColorLayer; - delete m_pUnseenRevealSound; destroy_bitmap(m_pOrphanSearchBitmap); m_pOrphanSearchBitmap = 0; diff --git a/Source/Managers/SceneMan.h b/Source/Managers/SceneMan.h index 068bc1e2c1..f6bfcd7bac 100644 --- a/Source/Managers/SceneMan.h +++ b/Source/Managers/SceneMan.h @@ -1004,7 +1004,7 @@ namespace RTE { std::vector m_MaterialCopiesVector; // Sound of an unseen pixel on an unseen layer being revealed. - SoundContainer* m_pUnseenRevealSound; + std::shared_ptr m_pUnseenRevealSound; bool m_DrawRayCastVisualizations; //!< Whether to visibly draw RayCasts to the Scene debug Bitmap. bool m_DrawPixelCheckVisualizations; //!< Whether to visibly draw pixel checks (GetTerrMatter and GetMOIDPixel) to the Scene debug Bitmap. diff --git a/Source/Menus/InventoryMenuGUI.cpp b/Source/Menus/InventoryMenuGUI.cpp index 469ce8131e..259b2e0756 100644 --- a/Source/Menus/InventoryMenuGUI.cpp +++ b/Source/Menus/InventoryMenuGUI.cpp @@ -301,7 +301,7 @@ void InventoryMenuGUI::SetEnabled(bool enable) { for (const auto& [inventoryItem, inventoryItemButton]: m_GUIInventoryItemButtons) { inventoryItemButton->OnMouseLeave(0, 0, 0, 0); } - SoundContainer* soundToPlay = enable ? g_GUISound.EnterMenuSound() : g_GUISound.ExitMenuSound(); + auto soundToPlay = enable ? g_GUISound.EnterMenuSound() : g_GUISound.ExitMenuSound(); soundToPlay->Play(); } } diff --git a/Source/Menus/MetagameGUI.cpp b/Source/Menus/MetagameGUI.cpp index 3884a63a30..44aa367d2c 100644 --- a/Source/Menus/MetagameGUI.cpp +++ b/Source/Menus/MetagameGUI.cpp @@ -2618,7 +2618,7 @@ void MetagameGUI::CompletedActivity() { UpdateScenesBox(true); // Play some nice ambient music - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } } diff --git a/Source/Menus/TitleScreen.cpp b/Source/Menus/TitleScreen.cpp index 906aabd09b..3ca158f845 100644 --- a/Source/Menus/TitleScreen.cpp +++ b/Source/Menus/TitleScreen.cpp @@ -272,7 +272,7 @@ void TitleScreen::UpdateIntroSlideshowSequence(bool skipSlideshow) { m_IntroScrollDuration = 66.6F - m_IntroScrollStartTime; m_ScrollOffset.SetY(m_IntroScrollStartOffsetY); - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Intro Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Intro Music"))); g_AudioMan.SetMusicMuffledState(false); } m_FadeAmount = static_cast(Lerp(0, 1.0F, 255.0F, 0, m_SectionProgress)); @@ -402,7 +402,7 @@ void TitleScreen::UpdateIntroPreMainMenuSequence() { if (m_SectionSwitch) { SetSectionDurationAndResetSwitch(0.5F * g_SettingsMan.GetMenuTransitionDurationMultiplier()); m_FadeAmount = 0; - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } m_ScrollOffset.SetY(EaseOut(m_PreMainMenuScrollOffsetY, 0, m_SectionProgress)); @@ -440,7 +440,7 @@ void TitleScreen::UpdateTitleTransitions() { if (m_SectionSwitch) { SetSectionDurationAndResetSwitch(1.0F * g_SettingsMan.GetMenuTransitionDurationMultiplier()); g_GUISound.SplashSound()->Play(); - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Scenario Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Scenario Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } m_ScrollOffset.SetY(EaseOut(0, m_PlanetViewScrollOffsetY, m_SectionProgress)); @@ -452,7 +452,7 @@ void TitleScreen::UpdateTitleTransitions() { case TitleTransition::PlanetToMainMenu: if (m_SectionSwitch) { SetSectionDurationAndResetSwitch(1.0F * g_SettingsMan.GetMenuTransitionDurationMultiplier()); - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } m_ScrollOffset.SetY(EaseOut(m_PlanetViewScrollOffsetY, 0, m_SectionProgress)); @@ -485,7 +485,7 @@ void TitleScreen::UpdateTitleTransitions() { m_ScrollOffset.SetY(m_PlanetViewScrollOffsetY); m_GameLogo.SetPos(Vector(static_cast(m_TitleScreenMaxWidth / 2), m_GameLogoPlanetViewOffsetY)); m_StationOrbitTimer.SetElapsedRealTimeS(m_StationOrbitTimerElapsedTime); - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Scenario Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Scenario Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } m_FadeAmount = static_cast(Lerp(0, 1.0F, 255.0F, 0, m_SectionProgress)); @@ -506,7 +506,7 @@ void TitleScreen::UpdateTitleTransitions() { if (m_SectionSwitch) { SetSectionDurationAndResetSwitch(0.75F * g_SettingsMan.GetMenuTransitionDurationMultiplier()); m_StationOrbitTimer.SetElapsedRealTimeS(m_StationOrbitTimerElapsedTime); - g_MusicMan.PlayInterruptingMusic(dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); + g_MusicMan.PlayInterruptingMusic(*dynamic_cast(g_PresetMan.GetEntityPreset("SoundContainer", "Main Menu Music"))); g_AudioMan.SetMusicMuffledState(false); } m_ScrollOffset.SetY(EaseOut(250, 0, m_SectionProgress)); diff --git a/Source/System/Serializable.h b/Source/System/Serializable.h index 7af59c409e..b0c4cc85f3 100644 --- a/Source/System/Serializable.h +++ b/Source/System/Serializable.h @@ -3,6 +3,7 @@ #include "Reader.h" #include "Writer.h" +#include #include #include @@ -10,7 +11,7 @@ namespace RTE { /// This base class specifies common creation/destruction patterns associated with reading and writing member data from disk. /// Is only intended to be inherited from in one level. - class Serializable { + class Serializable: public std::enable_shared_from_this { public: #pragma region Global Macro Definitions