#include "pch.h" #include "SoundEffect.h" #include float SoundEffect::m_GlobalVolume{ 1.f }; SoundEffect::SoundEffect(const std::string& path ) :m_pMixChunk{ Mix_LoadWAV( path.c_str( ) ) } { if ( m_pMixChunk == nullptr ) { throw std::runtime_error("SoundEffect: Failed to load " + path + ",\nSDL_mixer Error: " + Mix_GetError()); } else { Mix_VolumeChunk(m_pMixChunk, int(m_GlobalVolume * 128)); } } SoundEffect::~SoundEffect( ) { Mix_FreeChunk( m_pMixChunk ); m_pMixChunk = nullptr; } bool SoundEffect::IsLoaded( ) const { return m_pMixChunk != nullptr; } bool SoundEffect::Play( int loops ) const { // Don't save the channel as a data member, // because when it stops playing the channel becomes free // and available for usage by other effects if ( m_pMixChunk != nullptr ) { int channel{ Mix_PlayChannel( -1, m_pMixChunk, loops ) }; return channel == -1 ? false : true; } else { return false; } } void SoundEffect::SetVolume( float value ) { if ( m_pMixChunk != nullptr ) { Mix_VolumeChunk( m_pMixChunk, int(value * m_GlobalVolume * 128) ); } } float SoundEffect::GetVolume( ) const { if ( m_pMixChunk != nullptr ) { return Mix_VolumeChunk( m_pMixChunk, -1 ) / 128.f; } else { return -1; } } void SoundEffect::StopAll( ) { Mix_HaltChannel(-1 ); } void SoundEffect::PauseAll( ) { Mix_Pause( -1 ); } void SoundEffect::ResumeAll( ) { Mix_Resume( -1 ); }