mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 22:31:49 +01:00
Init
This commit is contained in:
77
Engine/SoundEffect.cpp
Normal file
77
Engine/SoundEffect.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "base.h"
|
||||
#include <iostream>
|
||||
#include "SoundEffect.h"
|
||||
|
||||
SoundEffect::SoundEffect( const std::string& path )
|
||||
:m_pMixChunk{ Mix_LoadWAV( path.c_str( ) ) }
|
||||
{
|
||||
if ( m_pMixChunk == nullptr )
|
||||
{
|
||||
const std::string errorMsg = "SoundEffect: Failed to load " + path + ",\nSDL_mixer Error: " + Mix_GetError( );
|
||||
std::cerr << errorMsg;
|
||||
}
|
||||
}
|
||||
SoundEffect::~SoundEffect( )
|
||||
{
|
||||
Mix_FreeChunk( m_pMixChunk );
|
||||
m_pMixChunk = nullptr;
|
||||
}
|
||||
|
||||
bool SoundEffect::IsLoaded( ) const
|
||||
{
|
||||
return m_pMixChunk != nullptr;
|
||||
}
|
||||
|
||||
bool SoundEffect::Play( const 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 )
|
||||
{
|
||||
const int channel{ Mix_PlayChannel( -1, m_pMixChunk, loops ) };
|
||||
return channel == -1 ? false : true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SoundEffect::SetVolume( const int value )
|
||||
{
|
||||
if ( m_pMixChunk != nullptr )
|
||||
{
|
||||
Mix_VolumeChunk( m_pMixChunk, value );
|
||||
}
|
||||
}
|
||||
|
||||
int SoundEffect::GetVolume( ) const
|
||||
{
|
||||
if ( m_pMixChunk != nullptr )
|
||||
{
|
||||
return Mix_VolumeChunk( m_pMixChunk, -1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void SoundEffect::StopAll( )
|
||||
{
|
||||
Mix_HaltChannel(-1 );
|
||||
}
|
||||
|
||||
void SoundEffect::PauseAll( )
|
||||
{
|
||||
Mix_Pause( -1 );
|
||||
}
|
||||
void SoundEffect::ResumeAll( )
|
||||
{
|
||||
Mix_Resume( -1 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user