zengine/src/ZE_ZMusic.cpp

162 lines
3.3 KiB
C++
Raw Normal View History

/*******************************************************************************
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-29 06:50:19 +00:00
Licensed under a BSD-style license.
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
*******************************************************************************/
2003-05-07 20:34:50 +00:00
/**
\file ZE_ZMusic.cpp
\brief Source file for ZMusic.
Implementation of ZMusic, the basic Music class for ZEngine.
2003-06-11 05:51:15 +00:00
<br>$Id: ZE_ZMusic.cpp,v 1.10 2003/06/11 05:51:16 cozman Exp $<br>
2003-05-07 20:34:50 +00:00
\author James Turk
**/
#include "ZE_ZMusic.h"
#ifdef USE_SDL_MIXER
namespace ZE
{
2003-06-11 05:51:15 +00:00
//ZMusic is a very simple class, each call basically wraps a self-explanatory function of SDL_Mixer
const int ZMusic::LoopInfinite = -1; //constant for infinite, as used by SDL_Mixer
2003-06-11 05:51:15 +00:00
ZMusic::ZMusic() :
rEngine(ZEngine::GetInstance()),
rMusic(NULL)
{
}
2003-06-11 05:51:15 +00:00
ZMusic::ZMusic(std::string filename) :
rEngine(ZEngine::GetInstance()),
rMusic(NULL)
{
Open(filename);
}
ZMusic::~ZMusic()
{
Release();
}
2003-06-11 00:15:07 +00:00
void ZMusic::Open(std::string filename)
{
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
{
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");
}
2003-01-16 05:45:58 +00:00
void ZMusic::Pause() const
{
if(rMusic)
Mix_PauseMusic();
else
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "Pause");
}
2003-01-16 05:45:58 +00:00
void ZMusic::Unpause() const
{
if(rMusic)
Mix_ResumeMusic();
else
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "Unpause");
}
2003-01-16 05:45:58 +00:00
void ZMusic::Rewind() const
{
if(rMusic)
Mix_RewindMusic();
else
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "Rewind");
}
2003-01-16 05:45:58 +00:00
void ZMusic::Stop(int fadeTime) const
{
if(rMusic)
{
if(fadeTime)
Mix_FadeOutMusic(fadeTime);
else
Mix_HaltMusic();
}
else
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "Stop");
}
void ZMusic::SetVolume(int volume)
{
if(rMusic)
Mix_VolumeMusic(volume);
else
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "SetVolume");
}
2003-01-16 05:45:58 +00:00
bool ZMusic::IsLoaded() const
{
return rMusic != NULL;
}
2003-01-16 05:45:58 +00:00
bool ZMusic::IsPlaying() const
{
if(rMusic)
return Mix_PlayingMusic() > 0;
else
{
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "IsPlaying");
return false;
}
}
2003-01-16 05:45:58 +00:00
bool ZMusic::IsPaused() const
{
if(rMusic)
return Mix_PausedMusic() > 0;
else
{
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "IsPaused");
return false;
}
}
2003-01-16 05:45:58 +00:00
int ZMusic::Volume() const
{
if(rMusic)
return Mix_VolumeMusic(-1);
else
{
2003-01-13 06:00:38 +00:00
rEngine->ReportError(ZERR_NOMUSIC, "GetVolume");
return false;
}
}
}
#endif