//UVi Soft ( 2008 )
//Long Fei ( lf426 ), E-mail: zbln426@163.com
//Laboratory of ZaiBieLiunNian
//http://www.cppblog.com/lf426/
//FileName: MixSoundClass.hpp
#ifndef MIX_SOUND_CLASS_HPP
#define MIX_SOUND_CLASS_HPP
#include <iostream>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
class BaseMixSound
{
private:
static int MixNUM;
protected:
BaseMixSound();
public:
virtual ~BaseMixSound();
};
class EffectSound: public BaseMixSound
{
private:
Mix_Chunk* sound;
public:
EffectSound(const std::string& sound_fileName);
~EffectSound();
void play() const;
};
class MusicSound: public BaseMixSound
{
private:
Mix_Music* music;
public:
MusicSound(const std::string& music_fileName);
~MusicSound();
void play() const;
void stop() const;
};
#endif
//UVi Soft ( 2008 )
//Long Fei ( lf426 ), E-mail: zbln426@163.com
//Laboratory of ZaiBieLiunNian
//http://www.cppblog.com/lf426/
#include "MixSoundClass.hpp"
//*******************************
//class BaseMixSound
int BaseMixSound::MixNUM = 0;
BaseMixSound::BaseMixSound()
{
if ( MixNUM == 0 ){
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ){
std::cerr << "Mix_Open ERROR" << std::endl;
exit(-1);
}
}
MixNUM++;
}
BaseMixSound::~BaseMixSound()
{
MixNUM--;
if ( MixNUM == 0 ){
Mix_CloseAudio();
}
}
//*******************************
//*******************************
//class EffectSound
EffectSound::EffectSound(const std::string& sound_fileName)
{
sound = Mix_LoadWAV(sound_fileName.c_str());
if ( sound == 0 ){
std::cerr << sound_fileName << " : load failed!" << std::endl;
}
}
EffectSound::~EffectSound()
{
Mix_FreeChunk(sound);
}
void EffectSound::play() const
{
if( Mix_PlayChannel(-1, sound, 0) == -1 ){
std::cerr << "Mix_PlayChannel() ERROR" << std::endl;
}
}
//*******************************
//*******************************
//class MusicSound
MusicSound::MusicSound(const std::string& music_fileName)
{
music = Mix_LoadMUS(music_fileName.c_str());
if ( music == 0 ){
std::cerr << music_fileName << " : load failed!" << std::endl;
}
}
MusicSound::~MusicSound()
{
Mix_FreeMusic(music);
}
void MusicSound::play() const
{
if( Mix_PlayingMusic() == false ){
if( Mix_PlayMusic( music, -1 ) == -1 ){
std::cerr << "Mix_PlayMusic() ERROR" << std::endl;
}
} else {
if( Mix_PausedMusic() == 1) {
Mix_ResumeMusic();
}
else {
Mix_PauseMusic();
}
}
}
void MusicSound::stop() const
{
Mix_HaltMusic();
}
//***************************************
lib: SDL.lib, SDLmain.lib
SDL_mixer.lib
dll: SDL.dll
libogg-0.dll, libvorbis-0.dll, libvorbisfile-3.dll, SDL_mixer.dll, smpeg.dll
last update: 2008-04-20
posted on 2008-04-20 01:49
lf426 阅读(3333)
评论(0) 编辑 收藏 引用 所属分类:
mySDL_GameEngine