2017年5月1日 星期一

Week10 閻覃的上課筆記

Week10 閻覃的上課筆記.md

Week10 閻覃的上課筆記

試玩遊戲

1

最高分數是171喔

並且成功在電腦上發出了聲音

播放聲音檔案

由於老師使用的是Windows中的mmsystem.h中的PlaySound函數,我的系統並不支持,所以為了跨平台,我在Google中找到了一個支持跨平台的庫,並且做了一些封裝。這樣就可以播放wav和mp3音樂了。這個庫還可以支持同時播放多個聲音,並不會播放另一個的時候就會將原來的停止,所以彈奏鋼琴時就可以彈出和弦的聲音。也可以為遊戲配背景音樂,甚至可以調整音量大小。

FMOD下載地址:http://www.fmod.org/download/

CMakeLists.txt中配置如下:

 
14
1
cmake_minimum_required(VERSION 3.7)
2
project(05052553_week10)
3
4
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -framework GLUT -framework OpenGL -framework OpenAL")
5
6
set(SOURCE_FILES main.cpp SoundPlayer.cpp SoundPlayer.h)
7
link_directories(api/lowlevel/lib/)
8
include_directories(api/lowlevel/inc/)
9
include_directories(api/lowlevel/examples/)
10
11
add_executable(05052553_week10 ${SOURCE_FILES})
12
13
target_link_libraries(05052553_week10 fmod)
14
target_link_libraries(05052553_week10 fmodL)

SoundPlayer.h

 
29
1
//
2
// Created by Ethan on 2017/5/1.
3
//
4
5
#ifndef INC_05052553_WEEK10_SOUNDPLAYER_H
6
#define INC_05052553_WEEK10_SOUNDPLAYER_H
7
8
#include "fmod.hpp"
9
#include "fmod_errors.h"
10
11
class SoundPlayer {
12
private:
13
14
    FMOD::System *system;
15
16
public:
17
    SoundPlayer();
18
19
    virtual ~SoundPlayer();
20
21
    FMOD::Sound *CreateSound(const char *filePath);
22
23
    void playSound(FMOD::Sound *sound, FMOD::Channel **channel = 0);
24
25
    void setSoundVolume(FMOD::Channel *channel, float volume = 0);
26
};
27
28
29
#endif //INC_05052553_WEEK10_SOUNDPLAYER_H

SoundPlayer.cpp

 
70
1
//
2
// Created by Ethan on 2017/5/1.
3
//
4
5
#include "SoundPlayer.h"
6
#include <common.h>
7
8
#define ERRCHECK(_result) ERRCHECK_fn(_result, __FILE__, __LINE__)
9
10
FMOD_RESULT result;
11
12
void 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
}
18
19
void SoundPlayer::playSound(FMOD::Sound *sound, FMOD::Channel **channel) {
20
    result = system->playSound(sound, 0, false, channel);
21
    ERRCHECK(result);
22
}
23
24
FMOD::Sound *SoundPlayer::CreateSound(const char *filePath) {
25
    FMOD::Sound *sound = NULL;
26
    result = system->createSound(filePath, FMOD_DEFAULT, 0, &sound);
27
    ERRCHECK(result);
28
29
    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. */
33
34
    return sound;
35
}
36
37
SoundPlayer::SoundPlayer() {
38
39
    unsigned int version;
40
    void *extradriverdata = 0;
41
42
    /*
43
        Create a System object and initialize
44
    */
45
    result = FMOD::System_Create(&system);
46
    ERRCHECK(result);
47
48
    result = system->getVersion(&version);
49
    ERRCHECK(result);
50
51
    if (version < FMOD_VERSION) {
52
        printf("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);
53
    }
54
55
    result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
56
    ERRCHECK(result);
57
58
}
59
60
void SoundPlayer::setSoundVolume(FMOD::Channel *channel, float volume) {
61
    result = channel->setVolume(volume);
62
    ERRCHECK(result);
63
}
64
65
SoundPlayer::~SoundPlayer() {
66
    result = system->close();
67
    ERRCHECK(result);
68
    result = system->release();
69
    ERRCHECK(result);
70
}

通過封裝之後就可以方便的使用這個庫了。由於我的遊戲中聲音比較多,所以看起來程式碼會比較長,實際上只需要很短就可以。

首先定義聲音全局變數:

 
5
1
FMOD::Sound *soundLol[6];
2
FMOD::Sound *soundShot;
3
FMOD::Sound *soundReload;
4
FMOD::Sound *soundBgm;
5
SoundPlayer *player;

然後初始化剛定義的變數:

 
16
1
void initSounds() {
2
    player = new SoundPlayer();
3
4
5
    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
    }
11
12
    soundShot = player->CreateSound("sounds/shot.mp3");
13
    soundReload = player->CreateSound("sounds/reload.mp3");
14
15
    soundBgm = player->CreateSound("sounds/bgm.mp3");
16
}

最後就可以播放聲音了。

 
8
1
int main(int argc, char *argv[]) {
2
3
    initSounds();
4
5
    FMOD::Channel *channel = NULL;
6
    player->playSound(soundBgm, &channel);
7
    player->setSoundVolume(channel, 0.8);
8
  //...

F5DC6455-1641-4091-9E2F-1F378F10F58D

打茶壺小遊戲哦,想玩的可以找我要程式碼。

沒有留言:

張貼留言