This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
forked from adamjimenez/ChaosGroove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.cpp
290 lines (226 loc) · 6.27 KB
/
sound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Sound (.Cpp)
// ------------
#include <ptk.h>
#include <vector>
#include <list>
#include <sstream>
#include <string.h>
#include "KPTK.h"
#include "time.h"
using namespace std;
#include "bridge.hpp"
#include "game.hpp"
#include "log.hpp"
#include "timer.hpp"
#include "scene.hpp"
#include "config.hpp"
#include "board.hpp"
#include "piece.hpp"
#include "net.hpp"
#include "chat.hpp"
#include "wizard.hpp"
#include "panel.hpp"
#include "spell.hpp"
#include "ai.hpp"
#include "effect.hpp"
#include "options.hpp"
#include "Ksound.h"
#include "sound.hpp"
struct sound_t sound;
struct music_t music;
bool setup_sound_system(void)
{
log("Setting up sounds..");
load_all_memory_sounds();
music.volume.current = 0.0;
music.volume.target = 1.0;
music.volume.speed = 0.005;
return true;
}
bool load_music(void)
{
char name[MAX_STRING], text[MAX_STRING];
sprintf(name, "\\text_files\\title_music.ini");
sprintf(name, "%s", KMiscTools::makeFilePath(name));
if (!set_config_file_new(CONFIG_TITLE_MUSIC, name, false))
{
log("Can't open title_music config file!");
return false;
}
if (music.mod)
{
music.mod->stopModule();
}
music.mod = NULL;
music.mod = new KSound;
find_random_line_from_text_config(CONFIG_TITLE_MUSIC, text);
log("Loading Music: %s", text);
sprintf(name, "Music\\%s.mod", text);
sprintf(name, "%s", KMiscTools::makeFilePath(name));
if (!music.mod->loadModule( name ) )
{
log("Couldn't load music from : %s", name);
return false;
}
log("");
//myMusic->playModule( true ) ; //play and loop
return true;
}
void play_music(void)
{
if (music.mod)
{
music.mod->stopModule();
music.mod->playModule( true );
//music.volume.current = 0.0;
music.volume.target = 1.0;
wait_time(240);
}
}
void do_music_logic(void)
{
int c;
do_alpha_logic(&music.volume);
if (music.mod)
{
find_option_choice_variables(CONFIG_OPTIONS, "SOUND", "MUSIC VOLUME", &c, NULL, NULL, NULL);
music.mod->setVolume(music.volume.current * c);
c = find_current_option_choice(CONFIG_OPTIONS, "SOUND", "SOUND BALANCE");
if (c == 0) music.mod->setPan(-100);
if (c == 1) music.mod->setPan(0);
if (c == 2) music.mod->setPan(100);
}
// Switched away?
if (!CheckWindowFocus())
{
if (music.mod && music.mod->isPlaying())
{
music.mod->pauseModule();
}
}
else
{
// No, but have we just switched back in?
if (music.mod && !music.mod->isPlaying())
{
music.mod->continueModule();
}
}
}
bool load_all_memory_sounds(void)
{
int s;
char name[MAX_STRING], filepath[MAX_STRING];
// We leave our first entry as free for one off loaded sounds..
sound.available_sounds = 1;
sprintf(name, "Sounds\\Memory\\*.*");
sprintf(filepath, "%s", KMiscTools::makeFilePath(name));
KMiscTools::enumerateFolder( filepath , load_sound ) ;
log("Loaded %d sounds into memory..", sound.available_sounds);
return true;
}
bool load_sound( char *f , bool isFolder, void *userData )
{
char name[MAX_STRING];
if (isFolder) return true; // Skip folders.
sprintf(name, "Sounds\\Memory\\%s", f);
sprintf(name, "%s", KMiscTools::makeFilePath(name));
sound.sample[sound.available_sounds] = NULL;
sound.sample[sound.available_sounds] = new KSound;
if (!sound.sample[sound.available_sounds]->loadSample(name)) return true; // Can't load this sound.
sprintf(sound.name[sound.available_sounds], "%s", f);
sound.available_sounds++;
//log("%s, %s", name, f);
if (sound.available_sounds >= MAX_SOUNDS) return false; // Can't load any more sounds..
return true;
}
int return_sound_number(char *name)
{
int s;
char new_name[MAX_STRING];
for (s = 0 ; s < sound.available_sounds ; s++)
{
sprintf(new_name, "%s.wav", name);
if (stricmp(new_name, sound.name[s]) == 0) return s;
sprintf(new_name, "%s.ogg", name);
if (stricmp(new_name, sound.name[s]) == 0) return s;
}
return -1; // We couldn't find this sound!
}
int play_sound(char *name, bool wait)
{
int s, v;
s = return_sound_number(name);
//log("name: %s, s: %d", name, s);
if (s == -1) return -1; // Couldn't find sound name!
find_option_choice_variables(CONFIG_OPTIONS, "SOUND", "SOUND EFFECTS VOLUME", &v, NULL, NULL, NULL);
if (v <= 0) return s;
sound.sample[s]->playSample();
sound.sample[s]->setVolume(v);
v = find_current_option_choice(CONFIG_OPTIONS, "SOUND", "SOUND BALANCE");
if (v == 0) sound.sample[s]->setPan(-100);
if (v == 1) sound.sample[s]->setPan(0);
if (v == 2) sound.sample[s]->setPan(100);
if (wait && !game.AI_debug)
do
{
wait_time(1);
} while (sound.sample[s]->isPlaying());
return s;
}
int request_sound_effect(char *specific, char *general, char *behaviour, bool wait)
{
bool ret;
int c, choices;
char name[MAX_STRING], var[MAX_STRING], result[MAX_STRING];
sprintf(name, "%s..%s", specific, behaviour);
//log("name: %s", name);
jump:
GetConfigString(CONFIG_SOUNDS, name, "SAMPLE 1", result, MAX_STRING);
if (strlen(result) == 0) GetConfigString(CONFIG_SOUNDS, name, "GOTO 1", result, MAX_STRING);
// Did we find anything with our specific request?
if (strlen(result) == 0)
{
// No. So, let's try our general request..
sprintf(name, "%s..%s", general, behaviour);
GetConfigString(CONFIG_SOUNDS, name, "SAMPLE 1", result, MAX_STRING);
if (strlen(result) == 0) GetConfigString(CONFIG_SOUNDS, name, "GOTO 1", result, MAX_STRING);
}
if (strlen(result) == 0) return -1; // Couldn't find any sample options.
// Ok, we've found at least one option, now let's see how many choices there are..
c = 0;
do
{
c++;
sprintf(var, "SAMPLE %d", c);
GetConfigString(CONFIG_SOUNDS, name, var, result, MAX_STRING);
// Did we find a Sample entry?
if (strlen(result) == 0)
{
// No? Maybe a Goto jump instead?
sprintf(var, "GOTO %d", c);
GetConfigString(CONFIG_SOUNDS, name, var, result, MAX_STRING);
}
} while (strlen(result) > 0);
// Number of choices found.
choices = c - 1;
//log("choices: %d", choices);
c = IRand(1, choices);
sprintf(var, "SAMPLE %d", c);
GetConfigString(CONFIG_SOUNDS, name, var, result, MAX_STRING);
// Not a sample?
if (strlen(result) == 0)
{
sprintf(var, "GOTO %d", c);
GetConfigString(CONFIG_SOUNDS, name, var, result, MAX_STRING);
// It's a goto instead..
if (strlen(result) > 0)
{
sprintf(name, "%s", result);
//log("GOTO : %s", name);
goto jump; // Repick a sample..
}
}
// Play sound
return play_sound(result, wait);
}