随笔 - 96  文章 - 255  trackbacks - 0
<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

E-mail:zbln426@163.com QQ:85132383 长期寻找对战略游戏感兴趣的合作伙伴。

常用链接

留言簿(21)

随笔分类

随笔档案

SDL相关网站

我的个人网页

我的小游戏

资源下载

搜索

  •  

积分与排名

  • 积分 - 484855
  • 排名 - 37

最新评论

阅读排行榜

评论排行榜


//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, 24096 ) == -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 阅读(3300) 评论(0)  编辑 收藏 引用 所属分类: mySDL_GameEngine

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理