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
|
|
|
|
|
|
|
The maintainer of this library is James Turk (james@conceptofzero.net)
|
2002-12-29 06:50:19 +00:00
|
|
|
and the home of this Library is http://www.zengine.sourceforge.net
|
2002-12-01 07:56:17 +00:00
|
|
|
*******************************************************************************/
|
|
|
|
|
2003-05-07 20:34:50 +00:00
|
|
|
/**
|
2002-12-01 07:56:17 +00:00
|
|
|
\file ZE_ZMusic.cpp
|
|
|
|
\brief Source file for ZMusic.
|
|
|
|
|
|
|
|
Implementation of ZMusic, the basic Music class for ZEngine.
|
2003-06-11 00:15:07 +00:00
|
|
|
<br>$Id: ZE_ZMusic.cpp,v 1.9 2003/06/11 00:15:10 cozman Exp $<br>
|
2003-05-07 20:34:50 +00:00
|
|
|
\author James Turk
|
2002-12-01 07:56:17 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "ZE_ZMusic.h"
|
|
|
|
|
|
|
|
#ifdef USE_SDL_MIXER
|
|
|
|
|
|
|
|
namespace ZE
|
|
|
|
{
|
|
|
|
|
|
|
|
const int ZMusic::LoopInfinite = -1;
|
|
|
|
|
|
|
|
ZMusic::ZMusic()
|
|
|
|
{
|
2003-02-10 04:02:38 +00:00
|
|
|
rEngine = ZEngine::GetInstance();
|
2002-12-01 07:56:17 +00:00
|
|
|
rMusic = NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-11 00:15:07 +00:00
|
|
|
ZMusic::ZMusic(std::string filename)
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
2003-02-10 04:02:38 +00:00
|
|
|
rEngine = ZEngine::GetInstance();
|
2002-12-01 07:56:17 +00:00
|
|
|
rMusic = NULL;
|
|
|
|
Open(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZMusic::~ZMusic()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2003-06-11 00:15:07 +00:00
|
|
|
void ZMusic::Open(std::string filename)
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
Release();
|
|
|
|
rMusic = rEngine->LoadMusic(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZMusic::Release()
|
|
|
|
{
|
|
|
|
Mix_HaltMusic();
|
|
|
|
FreeMusic(rMusic);
|
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
void ZMusic::Play(int loopNum, int fadeTime) const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(Mix_PlayingMusic()) //stop currently playing music
|
|
|
|
Mix_HaltMusic();
|
|
|
|
|
|
|
|
if(rMusic)
|
|
|
|
{
|
|
|
|
if(fadeTime)
|
|
|
|
Mix_FadeInMusic(rMusic, loopNum, fadeTime);
|
|
|
|
else
|
|
|
|
Mix_PlayMusic(rMusic, loopNum);
|
|
|
|
}
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "Play");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
void ZMusic::Pause() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
Mix_PauseMusic();
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "Pause");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
void ZMusic::Unpause() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
Mix_ResumeMusic();
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "Unpause");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
void ZMusic::Rewind() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
Mix_RewindMusic();
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "Rewind");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
void ZMusic::Stop(int fadeTime) const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
{
|
|
|
|
if(fadeTime)
|
|
|
|
Mix_FadeOutMusic(fadeTime);
|
|
|
|
else
|
|
|
|
Mix_HaltMusic();
|
|
|
|
}
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "Stop");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZMusic::SetVolume(int volume)
|
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
Mix_VolumeMusic(volume);
|
|
|
|
else
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "SetVolume");
|
2002-12-01 07:56:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
bool ZMusic::IsLoaded() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
return rMusic != NULL;
|
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
bool ZMusic::IsPlaying() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
return Mix_PlayingMusic() > 0;
|
|
|
|
else
|
|
|
|
{
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "IsPlaying");
|
2002-12-01 07:56:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
bool ZMusic::IsPaused() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
return Mix_PausedMusic() > 0;
|
|
|
|
else
|
|
|
|
{
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "IsPaused");
|
2002-12-01 07:56:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-16 05:45:58 +00:00
|
|
|
int ZMusic::Volume() const
|
2002-12-01 07:56:17 +00:00
|
|
|
{
|
|
|
|
if(rMusic)
|
|
|
|
return Mix_VolumeMusic(-1);
|
|
|
|
else
|
|
|
|
{
|
2003-01-13 06:00:38 +00:00
|
|
|
rEngine->ReportError(ZERR_NOMUSIC, "GetVolume");
|
2002-12-01 07:56:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|