Week10 閻覃的上課筆記
試玩遊戲

最高分數是171喔
並且成功在電腦上發出了聲音
播放聲音檔案
由於老師使用的是Windows中的mmsystem.h中的PlaySound函數,我的系統並不支持,所以為了跨平台,我在Google中找到了一個支持跨平台的庫,並且做了一些封裝。這樣就可以播放wav和mp3音樂了。這個庫還可以支持同時播放多個聲音,並不會播放另一個的時候就會將原來的停止,所以彈奏鋼琴時就可以彈出和弦的聲音。也可以為遊戲配背景音樂,甚至可以調整音量大小。
FMOD下載地址:http://www.fmod.org/download/
CMakeLists.txt中配置如下:
141cmake_minimum_required(VERSION 3.7)2project(05052553_week10)34set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -framework GLUT -framework OpenGL -framework OpenAL")56set(SOURCE_FILES main.cpp SoundPlayer.cpp SoundPlayer.h)7link_directories(api/lowlevel/lib/)8include_directories(api/lowlevel/inc/)9include_directories(api/lowlevel/examples/)1011add_executable(05052553_week10 ${SOURCE_FILES})1213target_link_libraries(05052553_week10 fmod)14target_link_libraries(05052553_week10 fmodL)SoundPlayer.h
291//2// Created by Ethan on 2017/5/1.3//4567891011class SoundPlayer {12private:1314 FMOD::System *system;1516public:17 SoundPlayer();1819 virtual ~SoundPlayer();2021 FMOD::Sound *CreateSound(const char *filePath);2223 void playSound(FMOD::Sound *sound, FMOD::Channel **channel = 0);2425 void setSoundVolume(FMOD::Channel *channel, float volume = 0);26};272829//INC_05052553_WEEK10_SOUNDPLAYER_HSoundPlayer.cpp
701//2// Created by Ethan on 2017/5/1.3//45678910FMOD_RESULT result;1112void ERRCHECK_fn(FMOD_RESULT result, const char *file, int line) {13 if (result != FMOD_OK) {14 printf("%s(%d): FMOD error %d - %s\r", file, line, result, FMOD_ErrorString(result));15 exit(1);16 }17}1819void SoundPlayer::playSound(FMOD::Sound *sound, FMOD::Channel **channel) {20 result = system->playSound(sound, 0, false, channel);21 ERRCHECK(result);22}2324FMOD::Sound *SoundPlayer::CreateSound(const char *filePath) {25 FMOD::Sound *sound = NULL;26 result = system->createSound(filePath, FMOD_DEFAULT, 0, &sound);27 ERRCHECK(result);2829 result = sound->setMode(30 FMOD_LOOP_OFF); /* drumloop.wav has embedded loop points which automatically makes looping turn on, */31 ERRCHECK(32 result); /* so turn it off here. We could have also just put FMOD_LOOP_OFF in the above CreateSound call. */3334 return sound;35}3637SoundPlayer::SoundPlayer() {3839 unsigned int version;40 void *extradriverdata = 0;4142 /*43 Create a System object and initialize44 */45 result = FMOD::System_Create(&system);46 ERRCHECK(result);4748 result = system->getVersion(&version);49 ERRCHECK(result);5051 if (version < FMOD_VERSION) {52 printf("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);53 }5455 result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);56 ERRCHECK(result);5758}5960void SoundPlayer::setSoundVolume(FMOD::Channel *channel, float volume) {61 result = channel->setVolume(volume);62 ERRCHECK(result);63}6465SoundPlayer::~SoundPlayer() {66 result = system->close();67 ERRCHECK(result);68 result = system->release();69 ERRCHECK(result);70}通過封裝之後就可以方便的使用這個庫了。由於我的遊戲中聲音比較多,所以看起來程式碼會比較長,實際上只需要很短就可以。
首先定義聲音全局變數:
51FMOD::Sound *soundLol[6];2FMOD::Sound *soundShot;3FMOD::Sound *soundReload;4FMOD::Sound *soundBgm;5SoundPlayer *player;然後初始化剛定義的變數:
161void initSounds() {2 player = new SoundPlayer();345 for (int i = 0; i < 6; i++) {6 char str[50];7 sprintf(str, "sounds/lol/%d.mp3", i + 1);8 cout << str << endl;9 soundLol[i] = player->CreateSound(str);10 }1112 soundShot = player->CreateSound("sounds/shot.mp3");13 soundReload = player->CreateSound("sounds/reload.mp3");1415 soundBgm = player->CreateSound("sounds/bgm.mp3");16}最後就可以播放聲音了。
81int main(int argc, char *argv[]) {23 initSounds();45 FMOD::Channel *channel = NULL;6 player->playSound(soundBgm, &channel);7 player->setSoundVolume(channel, 0.8);8 //...
打茶壺小遊戲哦,想玩的可以找我要程式碼。
沒有留言:
張貼留言