Skip to content

Commit 3fbf7f4

Browse files
committed
Refactored ref_sound class
1 parent 83b277d commit 3fbf7f4

12 files changed

Lines changed: 193 additions & 192 deletions

src/xrEngine/IGame_Level.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ void IGame_Level::net_Stop()
7171

7272
//-------------------------------------------------------------------------------------------
7373
// extern CStatTimer tscreate;
74-
void _sound_event(const ref_sound_data_ptr& S, float range)
75-
{
76-
if (g_pGameLevel && S && S->feedback)
77-
g_pGameLevel->SoundEvent_Register(S, range);
78-
}
7974

8075
static void build_callback(Fvector* V, int Vcnt, CDB::TRI* T, int Tcnt, void* params)
8176
{
@@ -121,7 +116,11 @@ bool IGame_Level::Load(u32 dwNum)
121116
g_pGamePersistent->SpatialSpacePhysic.initialize(ObjectSpace.GetBoundingVolume());
122117

123118
GEnv.Sound->set_geometry_occ(ObjectSpace.GetStaticModel());
124-
GEnv.Sound->set_handler(_sound_event);
119+
GEnv.Sound->set_handler([](const ref_sound& S, float range)
120+
{
121+
if (g_pGameLevel && S && S->feedback)
122+
g_pGameLevel->SoundEvent_Register(S, range);
123+
});
125124

126125
pApp->LoadSwitch();
127126

@@ -258,7 +257,7 @@ void IGame_Level::SetViewEntity(IGameObject* O)
258257
pCurrentViewEntity = O;
259258
}
260259

261-
void IGame_Level::SoundEvent_Register(ref_sound_data_ptr S, float range)
260+
void IGame_Level::SoundEvent_Register(const ref_sound& S, float range)
262261
{
263262
if (!g_bLoaded)
264263
return;

src/xrEngine/IGame_Level.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ENGINE_API IGame_Level : public FactoryObjectBase,
8787
struct _esound_delegate
8888
{
8989
Feel::Sound* dest;
90-
ref_sound_data_ptr source;
90+
ref_sound source;
9191
float power;
9292
};
9393
xr_vector<_esound_delegate> snd_Events;
@@ -129,7 +129,7 @@ class ENGINE_API IGame_Level : public FactoryObjectBase,
129129
void SetEntity(IGameObject* O); // { pCurrentEntity=pCurrentViewEntity=O; }
130130
void SetViewEntity(IGameObject* O); // { pCurrentViewEntity=O; }
131131

132-
void SoundEvent_Register(ref_sound_data_ptr S, float range);
132+
void SoundEvent_Register(const ref_sound& S, float range);
133133
void SoundEvent_Dispatch();
134134
void SoundEvent_OnDestDestroy(Feel::Sound*);
135135

src/xrGame/script_entity.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class CScriptEntityAction;
1717
class CEntity;
1818
class CScriptGameObject;
1919
class CCustomMonster;
20-
class ref_sound;
2120

2221
using namespace ScriptEntity;
2322

src/xrGame/script_sound.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ CScriptSound::CScriptSound(LPCSTR caSoundName, ESoundTypes sound_type)
1919
m_bIsNoSound = !Engine.Sound.IsSoundEnabled();
2020
m_caSoundToPlay = caSoundName;
2121
string_path l_caFileName;
22-
VERIFY(GEnv.Sound);
2322
if (FS.exist(l_caFileName, "$game_sounds$", caSoundName, ".ogg"))
2423
m_sound.create(caSoundName, st_Effect, sound_type);
2524
else

src/xrGame/sound_player.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ void CSoundPlayer::play(
199199
**/
200200
sound_single.m_sound->clone((*I).second.second->random(id), st_Effect, sg_SourceType);
201201

202-
sound_single.m_sound->_p->g_object = m_object;
203-
sound_single.m_sound->_p->g_userdata = (*I).second.first.m_data;
202+
sound_single.m_sound->_get()->g_object = m_object;
203+
sound_single.m_sound->_get()->g_userdata = (*I).second.first.m_data;
204204
VERIFY(sound_single.m_sound->_handle());
205205

206206
VERIFY(max_start_time >= min_start_time);

src/xrGame/sound_player_inline.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ IC ref_sound* CSoundPlayer::CSoundCollection::add(ESoundTypes type, LPCSTR name)
4646
{
4747
ref_sound* temp = xr_new<ref_sound>();
4848
temp->create(name, st_Effect, type);
49-
if (!temp->_p)
49+
if (!temp)
5050
{
5151
xr_delete(temp);
52-
return (0);
52+
return nullptr;
5353
}
54-
return (temp);
54+
return temp;
5555
}
5656

5757
IC const CSoundPlayer::SOUND_COLLECTIONS& CSoundPlayer::objects() const { return (m_sounds); }

src/xrSound/Sound.h

Lines changed: 106 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ constexpr pcstr SNDENV_FILENAME = "sEnvironment.xr";
2222

2323
// refs
2424
class IGameObject;
25-
class ref_sound;
26-
class ref_sound_data;
25+
class CSound;
26+
struct resptrcode_sound;
2727
class XRSOUND_API CSound_params;
2828
class XRSOUND_API CSound_source;
2929
class XRSOUND_API CSound_emitter;
@@ -190,10 +190,10 @@ class XRSOUND_API CSound_stats
190190
u32 _events;
191191
};
192192

193-
typedef resptr_core<ref_sound_data, resptr_base<ref_sound_data>> ref_sound_data_ptr;
193+
using ref_sound = resptr_core<CSound, resptrcode_sound>;
194194

195195
/// definition (Sound Callback)
196-
typedef void sound_event(const ref_sound_data_ptr& S, float range);
196+
typedef void sound_event(const ref_sound& S, float range);
197197

198198
namespace CDB
199199
{
@@ -204,31 +204,30 @@ namespace CDB
204204
class XRSOUND_API XR_NOVTABLE ISoundManager
205205
{
206206
protected:
207-
friend class ref_sound_data;
208-
virtual bool _create_data(ref_sound_data& S, pcstr fName, esound_type sound_type, int game_type, bool replaceWithNoSound = true) = 0;
209-
virtual void _destroy_data(ref_sound_data& S) = 0;
207+
friend class CSound;
208+
friend struct resptrcode_sound;
209+
210+
virtual CSound* create(pcstr fName, esound_type sound_type, int game_type, bool replaceWithNoSound = true) = 0;
211+
virtual void destroy(CSound& S) = 0;
212+
213+
virtual void attach_tail(CSound& S, pcstr fName) = 0;
214+
215+
virtual void play(ref_sound& S, IGameObject* O, u32 flags = 0, float delay = 0.f) = 0;
216+
virtual void play_at_pos(ref_sound& S, IGameObject* O, const Fvector& pos, u32 flags = 0, float delay = 0.f) = 0;
217+
virtual void play_no_feedback(ref_sound& S, IGameObject* O, u32 flags = 0, float delay = 0.f, Fvector* pos = nullptr,
218+
float* vol = nullptr, float* freq = nullptr, Fvector2* range = nullptr) = 0;
210219

211220
public:
212221
virtual ~ISoundManager() = default;
213222

214223
virtual void _restart() = 0;
215224
virtual bool i_locked() = 0;
216225

217-
virtual bool create(ref_sound& S, pcstr fName, esound_type sound_type, int game_type, bool replaceWithNoSound = true) = 0;
218-
virtual void attach_tail(ref_sound& S, pcstr fName) = 0;
219-
virtual void clone(ref_sound& S, const ref_sound& from, esound_type sound_type, int game_type) = 0;
220-
virtual void destroy(ref_sound& S) = 0;
221-
222226
virtual void prefetch() = 0;
223227

224228
virtual void stop_emitters() = 0;
225229
virtual int pause_emitters(bool val) = 0;
226230

227-
virtual void play(ref_sound& S, IGameObject* O, u32 flags = 0, float delay = 0.f) = 0;
228-
virtual void play_at_pos(ref_sound& S, IGameObject* O, const Fvector& pos, u32 flags = 0, float delay = 0.f) = 0;
229-
virtual void play_no_feedback(ref_sound& S, IGameObject* O, u32 flags = 0, float delay = 0.f, Fvector* pos = nullptr,
230-
float* vol = nullptr, float* freq = nullptr, Fvector2* range = nullptr) = 0;
231-
232231
virtual void set_master_volume(float f = 1.f) = 0;
233232
virtual void set_geometry_env(IReader* I) = 0;
234233
virtual void set_geometry_som(IReader* I) = 0;
@@ -284,100 +283,129 @@ class CSound_UserData : public xr_resource
284283

285284
using CSound_UserDataPtr = resptr_core<CSound_UserData, resptr_base<CSound_UserData>>;
286285

287-
class ref_sound_data : public xr_resource
286+
class CSound : public xr_resource
288287
{
289288
public:
290289
//shared_str nm;
291-
CSound_source* handle; //!< Pointer to wave-source interface
292-
CSound_emitter* feedback; //!< Pointer to emitter, automatically clears on emitter-stop
293-
esound_type s_type;
294-
int g_type; //!< Sound type, usually for AI
295-
IGameObject* g_object; //!< Game object that emits ref_sound
296-
CSound_UserDataPtr g_userdata;
297-
shared_str fn_attached[2];
290+
CSound_source* handle{}; //!< Pointer to wave-source interface
291+
CSound_emitter* feedback{}; //!< Pointer to emitter, automatically clears on emitter-stop
298292

299-
u32 dwBytesTotal;
300-
float fTimeTotal;
293+
esound_type s_type{ st_Effect };
294+
int g_type{}; //!< Sound type, usually for AI
301295

302-
ref_sound_data() noexcept
303-
: handle(0), feedback(0), s_type(st_Effect), g_type(0), g_object(0), dwBytesTotal(0), fTimeTotal(0)
304-
{
305-
}
296+
IGameObject* g_object{}; //!< Game object that emits ref_sound
297+
CSound_UserDataPtr g_userdata{};
298+
shared_str fn_attached[2];
306299

307-
ref_sound_data(pcstr fName, esound_type sound_type, int game_type, bool replaceWithNoSound = true)
308-
{
309-
GEnv.Sound->_create_data(*this, fName, sound_type, game_type, replaceWithNoSound);
310-
}
300+
u32 dwBytesTotal{};
301+
float fTimeTotal{};
311302

312-
virtual ~ref_sound_data() { GEnv.Sound->_destroy_data(*this); }
303+
~CSound() override { GEnv.Sound->destroy(*this); }
313304
float get_length_sec() const { return fTimeTotal; }
314305
};
315306

316-
inline void VerSndUnlocked() { VERIFY(!GEnv.Sound->i_locked()); }
317307
/*! \class ref_sound
318308
\brief Sound source + control
319309
320310
The main class representing source/emitter interface
321311
This class in fact just hides internals and redirect calls to
322312
specific sub-systems
323313
*/
324-
class ref_sound
314+
struct resptrcode_sound : public resptr_base<CSound>
325315
{
326-
public:
327-
ref_sound_data_ptr _p;
316+
[[nodiscard]]
317+
ICF CSound_source* _handle() const { return p_ ? p_->handle : nullptr; }
318+
319+
[[nodiscard]]
320+
ICF CSound_emitter* _feedback() const { return p_ ? p_->feedback : nullptr; }
321+
322+
[[nodiscard]]
323+
ICF IGameObject* _g_object() const { VERIFY(p_); return p_ ? p_->g_object : nullptr; }
328324

329-
ref_sound() = default;
330-
~ref_sound() = default;
325+
[[nodiscard]]
326+
ICF int _g_type() const { VERIFY(p_); return p_ ? p_->g_type : 0; }
327+
328+
[[nodiscard]]
329+
ICF esound_type _sound_type() const { VERIFY(p_); return p_ ? p_->s_type : st_Effect; }
330+
331+
[[nodiscard]]
332+
ICF CSound_UserDataPtr _g_userdata() const { VERIFY(p_); return p_ ? p_->g_userdata : nullptr; }
331333

332-
CSound_source* _handle() const { return _p ? _p->handle : nullptr; }
333-
CSound_emitter* _feedback() const { return _p ? _p->feedback : nullptr; }
334-
IGameObject* _g_object() { VERIFY(_p); return _p->g_object; }
335-
int _g_type() { VERIFY(_p); return _p->g_type; }
336-
esound_type _sound_type() { VERIFY(_p); return _p->s_type; }
337-
CSound_UserDataPtr _g_userdata() { VERIFY(_p); return _p->g_userdata; }
338334

339335
bool create(pcstr name, esound_type sound_type, int game_type, bool replaceWithNoSound = true)
340-
{ VerSndUnlocked(); return GEnv.Sound->create(*this, name, sound_type, game_type, replaceWithNoSound); }
336+
{
337+
VerSndUnlocked();
338+
_set(GEnv.Sound->create(name, sound_type, game_type, replaceWithNoSound));
339+
return _get();
340+
}
341341

342-
void attach_tail(pcstr name)
343-
{ VerSndUnlocked(); GEnv.Sound->attach_tail(*this, name); }
342+
ICF void destroy()
343+
{
344+
VerSndUnlocked();
345+
_set(nullptr);
346+
}
344347

345-
void clone(const ref_sound& from, esound_type sound_type, int game_type)
346-
{ VerSndUnlocked(); GEnv.Sound->clone(*this, from, sound_type, game_type); }
348+
void attach_tail(pcstr name) const
349+
{
350+
VerSndUnlocked();
351+
if (!p_)
352+
return;
353+
GEnv.Sound->attach_tail(*p_, name);
354+
}
347355

348-
void destroy()
349-
{ VerSndUnlocked(); GEnv.Sound->destroy(*this); }
356+
void clone(const ref_sound& from, esound_type sound_type, int game_type)
357+
{
358+
if (!from._get())
359+
return;
360+
_set(xr_new<CSound>());
361+
p_->handle = from->handle;
362+
p_->dwBytesTotal = from->dwBytesTotal;
363+
p_->fTimeTotal = from->fTimeTotal;
364+
p_->fn_attached[0] = from->fn_attached[0];
365+
p_->fn_attached[1] = from->fn_attached[1];
366+
p_->g_type = (game_type == sg_SourceType) ? p_->handle->game_type() : game_type;
367+
p_->s_type = sound_type;
368+
}
350369

351370
void play(IGameObject* O, u32 flags = 0, float delay = 0.f)
352-
{ VerSndUnlocked(); GEnv.Sound->play(*this, O, flags, delay); }
371+
{
372+
VerSndUnlocked();
373+
GEnv.Sound->play(static_cast<ref_sound&>(*this), O, flags, delay);
374+
}
353375

354376
void play_at_pos(IGameObject* O, const Fvector& pos, u32 flags = 0, float delay = 0.f)
355-
{ VerSndUnlocked(); GEnv.Sound->play_at_pos(*this, O, pos, flags, delay); }
377+
{
378+
VerSndUnlocked();
379+
GEnv.Sound->play_at_pos(static_cast<ref_sound&>(*this), O, pos, flags, delay);
380+
}
356381

357382
void play_no_feedback(IGameObject* O, u32 flags = 0, float delay = 0.f, Fvector* pos = nullptr, float* vol = nullptr, float* freq = nullptr, Fvector2* range = nullptr)
358-
{ VerSndUnlocked(); GEnv.Sound->play_no_feedback(*this, O, flags, delay, pos, vol, freq, range); }
383+
{
384+
VerSndUnlocked();
385+
GEnv.Sound->play_no_feedback(static_cast<ref_sound&>(*this), O, flags, delay, pos, vol, freq, range);
386+
}
359387

360-
void stop() { VerSndUnlocked(); if (_feedback()) _feedback()->stop(false); }
361-
void stop_deferred() { VerSndUnlocked(); if (_feedback()) _feedback()->stop(true ); }
388+
ICF void stop() const { VerSndUnlocked(); if (_feedback()) _feedback()->stop(false); }
389+
ICF void stop_deferred() const { VerSndUnlocked(); if (_feedback()) _feedback()->stop(true ); }
362390

363-
void set_position(const Fvector& pos) { VerSndUnlocked(); if (_feedback()) _feedback()->set_position(pos); }
364-
void set_frequency(float freq) { VerSndUnlocked(); if (_feedback()) _feedback()->set_frequency(freq); }
365-
void set_range(float min, float max) { VerSndUnlocked(); if (_feedback()) _feedback()->set_range(min, max); }
366-
void set_volume(float vol) { VerSndUnlocked(); if (_feedback()) _feedback()->set_volume(vol); }
367-
void set_priority(float p) { VerSndUnlocked(); if (_feedback()) _feedback()->set_priority(p); }
368-
void set_time(float t) { VerSndUnlocked(); if (_feedback()) _feedback()->set_time(t); }; //--#SM+#--
391+
ICF void set_position(const Fvector& pos) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_position(pos); }
392+
ICF void set_frequency(float freq) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_frequency(freq); }
393+
ICF void set_range(float min, float max) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_range(min, max); }
394+
ICF void set_volume(float vol) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_volume(vol); }
395+
ICF void set_priority(float p) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_priority(p); }
396+
ICF void set_time(float t) const { VerSndUnlocked(); if (_feedback()) _feedback()->set_time(t); }; //--#SM+#--
369397

370-
const CSound_params* get_params()
398+
[[nodiscard]]
399+
ICF const CSound_params* get_params() const
371400
{
372401
VerSndUnlocked();
373-
return _feedback() ? _feedback()->get_params() : 0;
402+
return _feedback() ? _feedback()->get_params() : nullptr;
374403
}
375404

376-
void set_params(CSound_params* p)
405+
void set_params(CSound_params* p) const
377406
{
378407
VerSndUnlocked();
379-
CSound_emitter* const feedback = _feedback();
380-
if (feedback)
408+
if (CSound_emitter* const feedback = _feedback())
381409
{
382410
feedback->set_position(p->position);
383411
feedback->set_frequency(p->freq);
@@ -386,7 +414,13 @@ class ref_sound
386414
}
387415
}
388416

389-
float get_length_sec() const { return _p ? _p->get_length_sec() : 0.0f; }
417+
[[nodiscard]]
418+
ICF float get_length_sec() const { return p_ ? p_->get_length_sec() : 0.0f; }
419+
420+
IC static void VerSndUnlocked()
421+
{
422+
VERIFY(!GEnv.Sound->i_locked());
423+
}
390424
};
391425

392426
class XRSOUND_API CSound_stats_ext

0 commit comments

Comments
 (0)