OSDN Git Service

448ff7eb345a5628ee6bb85c3eae955693b82230
[hengbandforosx/hengbandosx.git] / src / main-win / main-win-sound.cpp
1 /*!
2  * @file main-win-sound.cpp
3  * @brief Windows版固有実装(効果音)
4  */
5
6 #include "main-win/main-win-sound.h"
7 #include "main-win/main-win-cfg-reader.h"
8 #include "main-win/main-win-define.h"
9 #include "main-win/main-win-file-utils.h"
10 #include "main-win/main-win-mmsystem.h"
11 #include "main-win/main-win-utils.h"
12 #include "util/angband-files.h"
13
14 #include "main/sound-definitions-table.h"
15
16 /*
17  * Directory name
18  */
19 concptr ANGBAND_DIR_XTRA_SOUND;
20
21 /*
22  * "sound.cfg" data
23  */
24 CfgData *sound_cfg_data;
25
26 /*!
27  * @brief action-valに対応する[Sound]セクションのキー名を取得する
28  * @param index "term_xtra()"の第2引数action-valに対応する値
29  * @param buf 使用しない
30  * @return 対応するキー名を返す
31  */
32 static concptr sound_key_at(int index, char *buf)
33 {
34     (void)buf;
35
36     if (index >= SOUND_MAX)
37         return NULL;
38
39     return angband_sound_name[index];
40 }
41
42 /*!
43  * @brief 効果音の設定を読み込む。
44  * @details
45  * "sound_debug.cfg"ファイルを優先して読み込む。無ければ"sound.cfg"ファイルを読み込む。
46  * この処理は複数回実行されることを想定していない。複数回実行した場合には前回読み込まれた設定のメモリは解放されない。
47  */
48 void load_sound_prefs(void)
49 {
50     CfgReader reader(ANGBAND_DIR_XTRA_SOUND, { "sound_debug.cfg", "sound.cfg" });
51     sound_cfg_data = reader.read_sections({ { "Sound", TERM_XTRA_SOUND, sound_key_at } });
52 }
53
54 /*!
55  * @brief 指定の効果音を鳴らす。
56  * @param val see sound_type
57  * @retval 0 正常終了
58  * @retval 1 設定なし
59  * @retval -1 PlaySoundの戻り値が正常終了以外
60  */
61 errr play_sound(int val)
62 {
63     concptr filename = sound_cfg_data->get_rand(TERM_XTRA_SOUND, val);
64     if (!filename) {
65         return 1;
66     }
67
68     char buf[MAIN_WIN_MAX_PATH];
69     path_build(buf, MAIN_WIN_MAX_PATH, ANGBAND_DIR_XTRA_SOUND, filename);
70
71     if (::PlaySoundW(to_wchar(buf).wc_str(), 0, SND_FILENAME | SND_ASYNC)) {
72         return 0;
73     }
74     return -1;
75 }