1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Corrige SoundEffect

This commit is contained in:
Danilo 2024-05-24 23:12:29 -03:00
parent d17d36cc15
commit 26dd91ffa3
5 changed files with 74 additions and 80 deletions

View File

@ -1,94 +1,105 @@
#include "platform-dx/soundeffect-dx.hpp"
#include "platform-dx/implementations.hpp" #include "platform-dx/implementations.hpp"
using DxSoundEffect = DirectX::SoundEffect; using DxSoundEffect = DirectX::SoundEffect;
namespace xna { namespace xna {
SoundEffectInstance::SoundEffectInstance() {
impl = unew<PlatformImplementation>();
}
SoundEffectInstance::~SoundEffectInstance() {
impl = nullptr;
}
SoundEffect::SoundEffect(AudioEngine& audioEngine, String const& fileName) { SoundEffect::SoundEffect(AudioEngine& audioEngine, String const& fileName) {
if (!audioEngine.impl->_dxAudioEngine) if (!audioEngine.impl->_dxAudioEngine)
return; return;
const auto file = XnaHToWString(fileName); const auto file = XnaHToWString(fileName);
_dxSoundEffect = New<DxSoundEffect>(audioEngine.impl->_dxAudioEngine.get(), file.c_str()); impl->_dxSoundEffect = unew<DxSoundEffect>(audioEngine.impl->_dxAudioEngine.get(), file.c_str());
}
SoundEffect::~SoundEffect() {
impl = nullptr;
} }
void SoundEffect::Play() { void SoundEffect::Play() {
if (!_dxSoundEffect) if (!impl->_dxSoundEffect)
return; return;
_dxSoundEffect->Play(); impl->_dxSoundEffect->Play();
} }
void SoundEffect::Play(float volume, float pitch, float pan) { void SoundEffect::Play(float volume, float pitch, float pan) {
if (!_dxSoundEffect) if (!impl->_dxSoundEffect)
return; return;
_dxSoundEffect->Play(volume, pitch, pan); impl->_dxSoundEffect->Play(volume, pitch, pan);
} }
SoundEffectInstance SoundEffect::CreateInstance() { uptr<SoundEffectInstance> SoundEffect::CreateInstance() {
if (!_dxSoundEffect) if (!impl->_dxSoundEffect)
return SoundEffectInstance(); return unew<SoundEffectInstance>();
SoundEffectInstance i{}; auto instance = unew<SoundEffectInstance>();
i._dxInstance = _dxSoundEffect->CreateInstance(); instance->impl->_dxInstance = impl->_dxSoundEffect->CreateInstance();
return i; return instance;
} }
void SoundEffectInstance::Play(bool loop) { void SoundEffectInstance::Play(bool loop) {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->Play(loop); impl->_dxInstance->Play(loop);
} }
void SoundEffectInstance::Stop(bool immediate) { void SoundEffectInstance::Stop(bool immediate) {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->Stop(immediate); impl->_dxInstance->Stop(immediate);
} }
void SoundEffectInstance::Pause() { void SoundEffectInstance::Pause() {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->Pause(); impl->_dxInstance->Pause();
} }
void SoundEffectInstance::Resume() { void SoundEffectInstance::Resume() {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->Resume(); impl->_dxInstance->Resume();
} }
void SoundEffectInstance::Volume(float volume) { void SoundEffectInstance::Volume(float volume) {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->SetVolume(volume); impl->_dxInstance->SetVolume(volume);
} }
void SoundEffectInstance::Pitch(float pitch) { void SoundEffectInstance::Pitch(float pitch) {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->SetPitch(pitch); impl->_dxInstance->SetPitch(pitch);
} }
void SoundEffectInstance::Pan(float pan) { void SoundEffectInstance::Pan(float pan) {
if (!_dxInstance) if (!impl->_dxInstance)
return; return;
_dxInstance->SetPan(pan); impl->_dxInstance->SetPan(pan);
} }
bool SoundEffectInstance::IsLooped() { bool SoundEffectInstance::IsLooped() {
if (!_dxInstance) if (!impl->_dxInstance)
return false; return false;
return _dxInstance->IsLooped(); return impl->_dxInstance->IsLooped();
} }
} }

View File

@ -4,24 +4,36 @@
#include "../default.hpp" #include "../default.hpp"
namespace xna { namespace xna {
struct ISoundEffectInstance { struct SoundEffectInstance {
public: public:
virtual void Play(bool loop = false) = 0; SoundEffectInstance();
virtual void Stop(bool immediate = true) = 0; ~SoundEffectInstance();
virtual void Pause() = 0;
virtual void Resume() = 0; void Play(bool loop = false);
virtual void Volume(float volume) = 0; void Stop(bool immediate = true);
virtual void Pitch(float pitch) = 0; void Pause();
virtual void Pan(float pan) = 0; void Resume();
virtual bool IsLooped() = 0; void Volume(float volume);
void Pitch(float pitch);
void Pan(float pan);
bool IsLooped();
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
}; };
class ISoundEffect { class SoundEffect {
public: public:
virtual ~ISoundEffect(){} SoundEffect(AudioEngine& audioEngine, String const& fileName);
virtual void Play() = 0; ~SoundEffect();
virtual void Play(float volume, float pitch, float pan) = 0; void Play();
virtual SoundEffectInstance CreateInstance() = 0; void Play(float volume, float pitch, float pan);
uptr<SoundEffectInstance> CreateInstance();
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
}; };
} }

View File

@ -22,6 +22,7 @@
#include "graphics/rendertarget.hpp" #include "graphics/rendertarget.hpp"
#include "game/window.hpp" #include "game/window.hpp"
#include "audio/audioengine.hpp" #include "audio/audioengine.hpp"
#include "audio/soundeffect.hpp"
#include "graphics/viewport.hpp" #include "graphics/viewport.hpp"
#include "common/color.hpp" #include "common/color.hpp"
#include "game/game.hpp" #include "game/game.hpp"
@ -470,6 +471,14 @@ namespace xna {
DX::StepTimer _stepTimer{}; DX::StepTimer _stepTimer{};
}; };
struct SoundEffectInstance::PlatformImplementation {
uptr<DirectX::SoundEffectInstance> _dxInstance = nullptr;
};
struct SoundEffect::PlatformImplementation {
uptr<DirectX::SoundEffect> _dxSoundEffect = nullptr;
};
template <typename T> template <typename T>
inline bool IndexBuffer::Initialize(std::vector<T> const& data, xna_error_ptr_arg) { inline bool IndexBuffer::Initialize(std::vector<T> const& data, xna_error_ptr_arg) {
if (!impl || !m_device || !m_device->impl->_device || data.empty()) { if (!impl || !m_device || !m_device->impl->_device || data.empty()) {

View File

@ -1,37 +0,0 @@
#ifndef XNA_PLATFORM_SOUNDEFFECT_DX_HPP
#define XNA_PLATFORM_SOUNDEFFECT_DX_HPP
#include "../audio/soundeffect.hpp"
#include <Audio.h>
namespace xna {
struct SoundEffectInstance : public ISoundEffectInstance {
public:
SoundEffectInstance() = default;
virtual void Play(bool loop = false) override;
virtual void Stop(bool immediate = true) override;
virtual void Pause() override;
virtual void Resume() override;
virtual void Volume(float volume) override;
virtual void Pitch(float pitch) override;
virtual void Pan(float pan) override;
virtual bool IsLooped() override;
public:
uptr<DirectX::SoundEffectInstance> _dxInstance = nullptr;
};
class SoundEffect : public ISoundEffect {
public:
SoundEffect(AudioEngine& audioEngine, String const& fileName);
virtual void Play() override;
virtual void Play(float volume, float pitch, float pan) override;
virtual SoundEffectInstance CreateInstance() override;
public:
sptr<DirectX::SoundEffect> _dxSoundEffect = nullptr;
};
}
#endif

View File

@ -2,5 +2,4 @@
#include "dx/StepTimer.hpp" #include "dx/StepTimer.hpp"
#include "dxheaders.hpp" #include "dxheaders.hpp"
#include "init-dx.hpp" #include "init-dx.hpp"
#include "soundeffect-dx.hpp"
#include "implementations.hpp" #include "implementations.hpp"