2002-12-01 07:56:17 +00:00
|
|
|
/*******************************************************************************
|
2002-12-29 06:50:19 +00:00
|
|
|
This file is Part of the ZEngine Library for 2D game development.
|
|
|
|
Copyright (C) 2002, 2003 James Turk
|
2002-12-01 07:56:17 +00:00
|
|
|
|
2002-12-29 06:50:19 +00:00
|
|
|
Licensed under a BSD-style license.
|
2002-12-01 07:56:17 +00:00
|
|
|
|
2002-12-29 06:50:19 +00:00
|
|
|
The maintainer of this library is James Turk (james@conceptofzero.net)
|
|
|
|
and the home of this Library is http://www.zengine.sourceforge.net
|
2002-12-01 07:56:17 +00:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\par File Header:
|
|
|
|
File: ZE_ZSound.cpp <br>
|
|
|
|
Description: Implementation source file for core ZEngine Sound Object. <br>
|
|
|
|
Author(s): James Turk <br>
|
2003-01-13 06:00:38 +00:00
|
|
|
$Id: ZE_ZSound.cpp,v 1.4 2003/01/13 06:00:38 cozman Exp $<br>
|
2002-12-01 07:56:17 +00:00
|
|
|
|
|
|
|
\file ZE_ZSound.cpp
|
|
|
|
\brief Source file for ZSound.
|
|
|
|
|
|
|
|
Implementation of ZSound, the basic Sound class for ZEngine.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "ZE_ZSound.h"
|
|
|
|
|
|
|
|
#ifdef USE_SDL_MIXER
|
|
|
|
|
|
|
|
namespace ZE
|
|
|
|
{
|
|
|
|
|
|
|
|
const int ZSound::LoopInfinite = -1;
|
|
|
|
|
|
|
|
ZSound::ZSound()
|
|
|
|
{
|
|
|
|
rSound = NULL;
|
|
|
|
rChannelID = -1; //request channel ID
|
|
|
|
}
|
|
|
|
|
|
|
|
ZSound::ZSound(string filename)
|
|
|
|
{
|
|
|
|
rSound = NULL;
|
|
|
|
rChannelID = -1; //request channel ID
|
|
|
|
Open(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZSound::~ZSound()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Open(string filename)
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
rSound = rEngine->LoadSound(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Release()
|
|
|
|
{
|
|
|
|
if(rChannelID >= 0)
|
|
|
|
Mix_HaltChannel(rChannelID);
|
|
|
|
FreeSound(rSound);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Play(int loopNum, int fadeTime)
|
|
|
|
{
|
|
|
|
if(rChannelID >= 0 && Mix_Playing(rChannelID)) //stop currently playing sound
|
|
|
|
Mix_HaltChannel(rChannelID);
|
|
|
|
|
|
|
|
if(rSound)
|
|
|
|
{
|
|
|
|
if(fadeTime)
|
|
|
|
rChannelID = Mix_FadeInChannel(rChannelID, rSound, loopNum, fadeTime);
|
|
|
|
else
|
|
|
|
rChannelID = Mix_PlayChannel(rChannelID, rSound, loopNum);
|
|
|
|
}
|
|
|
|
else if(!rSound)
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "Play");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Pause()
|
|
|
|
{
|
|
|
|
if(rSound && rChannelID >= 0)
|
|
|
|
Mix_Pause(rChannelID);
|
|
|
|
else if(!rSound)
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "Pause");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Unpause()
|
|
|
|
{
|
|
|
|
if(rSound && rChannelID >= 0)
|
|
|
|
Mix_Resume(rChannelID);
|
|
|
|
else if(!rSound)
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "Unpause");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::Stop(int fadeTime)
|
|
|
|
{
|
|
|
|
if(rSound && rChannelID >= 0)
|
|
|
|
{
|
|
|
|
if(fadeTime)
|
|
|
|
Mix_FadeOutChannel(rChannelID,fadeTime);
|
|
|
|
else
|
|
|
|
Mix_HaltChannel(rChannelID);
|
|
|
|
}
|
|
|
|
else if(!rSound)
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "Stop");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZSound::SetVolume(int volume)
|
|
|
|
{
|
|
|
|
if(rSound)
|
|
|
|
Mix_VolumeChunk(rSound,volume);
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "SetVolume");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ZSound::IsLoaded()
|
|
|
|
{
|
|
|
|
return rSound != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZSound::IsPlaying()
|
|
|
|
{
|
|
|
|
if(rSound && rChannelID >= 0)
|
|
|
|
return Mix_Playing(rChannelID) > 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(rChannelID >= 0)
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "IsPlaying");
|
2002-12-01 07:56:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZSound::IsPaused()
|
|
|
|
{
|
|
|
|
if(rSound && rChannelID >= 0)
|
|
|
|
return Mix_Paused(rChannelID) > 0;
|
|
|
|
else
|
|
|
|
{
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "IsPaused");
|
2002-12-01 07:56:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ZSound::Volume()
|
|
|
|
{
|
|
|
|
if(rSound)
|
|
|
|
return Mix_VolumeChunk(rSound,-1);
|
|
|
|
else
|
|
|
|
{
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOSOUND, "GetVolume");
|
2002-12-01 07:56:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|