2012-10-13 18:29:53 +00:00
|
|
|
#include "Sound.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include "AudioManager.h"
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2012-10-13 18:29:53 +00:00
|
|
|
|
|
|
|
Sound::Sound(std::string path)
|
|
|
|
{
|
|
|
|
mSound = NULL;
|
2012-10-25 23:23:26 +00:00
|
|
|
mChannel = -1;
|
2012-10-13 18:29:53 +00:00
|
|
|
|
|
|
|
AudioManager::registerSound(this);
|
|
|
|
|
|
|
|
loadFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound()
|
|
|
|
{
|
|
|
|
deinit();
|
|
|
|
|
|
|
|
AudioManager::unregisterSound(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::loadFile(std::string path)
|
|
|
|
{
|
|
|
|
mPath = path;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::init()
|
|
|
|
{
|
|
|
|
if(!AudioManager::isInitialized())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(mSound != NULL)
|
|
|
|
deinit();
|
|
|
|
|
|
|
|
if(mPath.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
mSound = Mix_LoadWAV(mPath.c_str());
|
|
|
|
|
|
|
|
if(mSound == NULL)
|
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogError) << "Error loading sound \"" << mPath << "\"!\n" << " " << Mix_GetError();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::deinit()
|
|
|
|
{
|
|
|
|
if(mSound != NULL)
|
|
|
|
{
|
|
|
|
Mix_FreeChunk(mSound);
|
|
|
|
mSound = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::play()
|
|
|
|
{
|
|
|
|
if(mSound == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mChannel = -1;
|
|
|
|
|
|
|
|
mChannel = Mix_PlayChannel(-1, mSound, 0);
|
|
|
|
if(mChannel == -1)
|
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogError) << "Error playing sound!\n " << Mix_GetError();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sound::isPlaying()
|
|
|
|
{
|
|
|
|
if(mChannel != -1 && Mix_Playing(mChannel))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|