-
Notifications
You must be signed in to change notification settings - Fork 15
/
GSM.h
550 lines (513 loc) · 17 KB
/
GSM.h
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
is::Engine (Infinity Solutions Engine)
Copyright (C) 2018-2024 Is Daouda <isdaouda.n@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef GSM_H_INCLUDED
#define GSM_H_INCLUDED
#include "../sound/GameSound.h"
#include "../sound/GameMusic.h"
namespace is
{
////////////////////////////////////////////////////////////
/// Class for manage SFML Sound and Music without using an
/// SFML object
////////////////////////////////////////////////////////////
class GSM
{
public:
/// Game Sound container
std::vector<std::shared_ptr<GameSound>> m_GSMsound;
//#if !defined(__ANDROID__)
/// Game Music container
std::vector<std::shared_ptr<GameMusic>> m_GSMmusic;
//#endif
//////////////////////////////////////////////////////
/// \brief Allows to add SFML Sound in container
///
/// \param name of sound which will be used to identify
/// it in the container in order to be able to access it
/// \param filePath path of the sound file to add
//////////////////////////////////////////////////////
virtual void GSMaddSound(const std::string& name, const std::string& filePath)
{
if (!soundFileExists(filePath))
{
auto obj = std::make_shared<GameSound>(name, filePath);
m_GSMsound.push_back(obj);
}
else is::showLog("WARNING: <" + name + "> sound has already been added!");
}
//////////////////////////////////////////////////////
/// \brief Allows to add existing sound in container
///
/// \param sound object
//////////////////////////////////////////////////////
void GSMaddSoundObject(std::shared_ptr<GameSound> sound, bool showError = true)
{
if (!soundFileExists(sound->getFilePath())) m_GSMsound.push_back(sound);
else
{
if (showError) is::showLog("WARNING: <" + sound->getName() + "> sound has already been added!");
}
}
//////////////////////////////////////////////////////
/// \brief Allows to add SFML Music in container
///
/// \param name of music which will be used to identify
/// it in the container in order to be able to access it
/// \param filePath path of the music file to add
//////////////////////////////////////////////////////
virtual void GSMaddMusic(const std::string& name, const std::string& filePath)
{
//#if defined(__ANDROID__)
// GSMaddSound(name, filePath);
//#else
if (!musicFileExists(filePath))
{
auto obj = std::make_shared<GameMusic>(name, filePath);
m_GSMmusic.push_back(obj);
}
else is::showLog("WARNING: <" + name + "> music has already been added!");
//#endif
}
//////////////////////////////////////////////////////
/// \brief Allows to add existing music in container
///
/// \param music object
//////////////////////////////////////////////////////
void GSMaddMusicObject(std::shared_ptr<
//#if defined(__ANDROID__)
// GameSound
//#else
GameMusic
//#endif
>music, bool showError = true)
{
//#if defined(__ANDROID__)
// GSMaddSoundObject(music);
//#else
if (!musicFileExists(music->getFilePath())) m_GSMmusic.push_back(music);
else
{
if (showError) is::showLog("WARNING: <" + music->getName() + "> music has already been added!");
}
//#endif
}
/// Allows to set sound loop
virtual void GSMsetSoundLoop(const std::string& name, bool loop)
{
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getName() == name && m_GSMsound[_I]->getFileIsLoaded())
{
m_GSMsound[_I]->getSound().setLoop(loop);
return;
}
}
}
is::showLog("ERROR: Can't loop <" + name + "> sound because sound does not exist!");
}
/// Allows to set music loop
virtual void GSMsetMusicLoop(const std::string& name, bool loop)
{
//#if defined(__ANDROID__)
// GSMsetSoundLoop(name, loop);
//#else
WITH(m_GSMmusic.size())
{
if (m_GSMmusic[_I].get() != nullptr)
{
if (m_GSMmusic[_I]->getName() == name && m_GSMmusic[_I]->getFileIsLoaded())
{
m_GSMmusic[_I]->getMusic().setLoop(loop);
return;
}
}
}
is::showLog("ERROR: Can't loop <" + name + "> music because music does not exist!");
//#endif
}
/// Allows to get sound in container by his name
virtual sf::Sound* GSMgetSound(const std::string& name, bool showError = true)
{
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getName() == name && m_GSMsound[_I]->getFileIsLoaded())
{
return &m_GSMsound[_I]->getSound();
}
}
}
if (showError) is::showLog("ERROR: <" + name + "> sound does not exist!");
return nullptr;
}
/// Allows to get music in container by his name
virtual
//#if defined(__ANDROID__)
// sf::Sound*
//#else
sf::Music*
//#endif
GSMgetMusic(const std::string& name, bool showError = true)
{
WITH(
//#if defined(__ANDROID__)
// m_GSMsound.size()
//#else
m_GSMmusic.size()
//#endif
)
{
if (
//#if defined(__ANDROID__)
// m_GSMsound[_I].get() != nullptr
//#else
m_GSMmusic[_I].get() != nullptr
//#endif
)
{
if (
//#if defined(__ANDROID__)
// m_GSMsound[_I]->getName() == name && m_GSMsound[_I]->getFileIsLoaded()
//#else
m_GSMmusic[_I]->getName() == name && m_GSMmusic[_I]->getFileIsLoaded()
//#endif
)
{
return
//#if defined(__ANDROID__)
// &m_GSMsound[_I]->getSound();
//#else
&m_GSMmusic[_I]->getMusic();
//#endif
}
}
}
if (showError) is::showLog("ERROR: <" + name + "> music does not exist!");
return nullptr;
}
/// Allows to pause sound in container by his name
virtual void GSMpauseSound(const std::string& name)
{
bool soundExist(false);
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getName() == name)
{
soundExist = true;
if (m_GSMsound[_I]->getFileIsLoaded())
{
if (is::checkSFMLSndState(m_GSMsound[_I]->getSound(), is::SFMLSndStatus::Playing)) m_GSMsound[_I]->getSound().pause();
}
else is::showLog("ERROR: Can't pause <" + name + "> sound!");
break;
}
}
}
if (!soundExist) is::showLog("ERROR: Can't pause <" + name + "> sound because sound does not exist!");
}
/// Allows to stop sound in container by his name
virtual void GSMstopSound(const std::string& name)
{
bool soundExist(false);
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getName() == name)
{
soundExist = true;
if (m_GSMsound[_I]->getFileIsLoaded())
{
if (is::checkSFMLSndState(m_GSMsound[_I]->getSound(), is::SFMLSndStatus::Playing)) m_GSMsound[_I]->getSound().stop();
}
else is::showLog("ERROR: Can't stop <" + name + "> sound!");
break;
}
}
}
if (!soundExist) is::showLog("ERROR: Can't stop <" + name + "> sound because sound does not exist!");
}
/// Allows to pause music in container by his name
virtual void GSMpauseMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMpauseSound(name);
// #else
bool musicExist(false);
WITH(m_GSMmusic.size())
{
if (m_GSMmusic[_I].get() != nullptr)
{
if (m_GSMmusic[_I]->getName() == name)
{
musicExist = true;
if (m_GSMmusic[_I]->getFileIsLoaded())
{
if (is::checkSFMLSndState(m_GSMmusic[_I]->getMusic(), is::SFMLSndStatus::Playing)) m_GSMmusic[_I]->getMusic().pause();
}
else is::showLog("ERROR: Can't pause <" + name + "> music!");
break;
}
}
}
if (!musicExist) is::showLog("ERROR: Can't pause <" + name + "> music because music does not exist!");
// #endif
}
/// Allows to stop music in container by his name
virtual void GSMstopMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMstopSound(name);
// #else
bool musicExist(false);
WITH(m_GSMmusic.size())
{
if (m_GSMmusic[_I].get() != nullptr)
{
if (m_GSMmusic[_I]->getName() == name)
{
musicExist = true;
if (m_GSMmusic[_I]->getFileIsLoaded())
{
if (is::checkSFMLSndState(m_GSMmusic[_I]->getMusic(), is::SFMLSndStatus::Playing)) m_GSMmusic[_I]->getMusic().stop();
}
else is::showLog("ERROR: Can't stop <" + name + "> music!");
break;
}
}
}
if (!musicExist) is::showLog("ERROR: Can't stop <" + name + "> music because music does not exist!");
// #endif
}
/// Allows to delete sound in container by his name
virtual void GSMdeleteSound(const std::string& name)
{
int soundId(-1);
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getName() == name)
{
soundId = _I;
break;
}
}
}
if (soundId == -1) is::showLog("ERROR: Can't delete <" + name + "> sound because sound does not exist!");
else
{
m_GSMsound[soundId].reset();
m_GSMsound[soundId] = nullptr;
}
}
/// Allows to delete music in container by his name
virtual void GSMdeleteMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMdeleteSound(name);
// #else
int musicId(-1);
WITH(m_GSMmusic.size())
{
if (m_GSMmusic[_I].get() != nullptr)
{
if (m_GSMmusic[_I]->getName() == name)
{
musicId = _I;
break;
}
}
}
if (musicId == -1) is::showLog("ERROR: Can't delete <" + name + "> music because music does not exist!");
else
{
m_GSMmusic[musicId].reset();
m_GSMmusic[musicId] = nullptr;
}
// #endif
}
////////////////////////////////////////////////////////////
/// These methods below have the same role as those above.
/// The difference here is that their name starting with GSM
/// is replaced by GRM (Game Resource Manager).
////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/// \brief Allows to add SFML Sound in container
///
/// \param name of sound which will be used to identify
/// it in the container in order to be able to access it
/// \param filePath path of the sound file to add
//////////////////////////////////////////////////////
virtual void GRMaddSound(const std::string& name, const std::string& filePath)
{
GSMaddSound(name, filePath);
}
//////////////////////////////////////////////////////
/// \brief Allows to add existing sound in container
///
/// \param sound object
//////////////////////////////////////////////////////
void GRMaddSoundObject(std::shared_ptr<GameSound> sound, bool showError = true)
{
GSMaddSoundObject(sound, showError);
}
//////////////////////////////////////////////////////
/// \brief Allows to add SFML Music in container
///
/// \param name of music which will be used to identify
/// it in the container in order to be able to access it
/// \param filePath path of the music file to add
//////////////////////////////////////////////////////
virtual void GRMaddMusic(const std::string& name, const std::string& filePath)
{
//#if defined(__ANDROID__)
// GSMaddSound(name, filePath);
//#else
GSMaddMusic(name, filePath);
//#endif
}
//////////////////////////////////////////////////////
/// \brief Allows to add existing music in container
///
/// \param music object
//////////////////////////////////////////////////////
void GRMaddMusicObject(std::shared_ptr<
//#if defined(__ANDROID__)
// GameSound
//#else
GameMusic
//#endif
>music, bool showError = true)
{
//#if defined(__ANDROID__)
// GSMaddSoundObject(music, showError);
//#else
GSMaddMusicObject(music, showError);
//#endif
}
/// Allows to set sound loop
virtual void GRMsetSoundLoop(const std::string& name, bool loop)
{
GSMsetSoundLoop(name, loop);
}
/// Allows to set music loop
virtual void GRMsetMusicLoop(const std::string& name, bool loop)
{
//#if defined(__ANDROID__)
// GSMsetSoundLoop(name, loop);
//#else
GSMsetMusicLoop(name, loop);
//#endif
}
/// Allows to get sound in container by his name
virtual sf::Sound* GRMgetSound(const std::string& name, bool showError = true)
{
return GSMgetSound(name, showError);
}
/// Allows to get music in container by his name
virtual
//#if defined(__ANDROID__)
// sf::Sound*
//#else
sf::Music*
//#endif
GRMgetMusic(const std::string& name, bool showError = true)
{
return
//#if defined(__ANDROID__)
// GSMgetSound(name, showError);
//#else
GSMgetMusic(name, showError);
//#endif
}
/// Allows to pause sound in container by his name
virtual void GRMpauseSound(const std::string& name)
{
GSMpauseSound(name);
}
/// Allows to stop sound in container by his name
virtual void GRMstopSound(const std::string& name)
{
GSMstopSound(name);
}
/// Allows to pause music in container by his name
virtual void GRMpauseMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMpauseSound(name);
// #else
GSMpauseMusic(name);
// #endif
}
/// Allows to stop music in container by his name
virtual void GRMstopMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMstopSound(name);
// #else
GSMstopMusic(name);
// #endif
}
/// Allows to delete sound in container by his name
virtual void GRMdeleteSound(const std::string& name)
{
GSMdeleteSound(name);
}
/// Allows to delete music in container by his name
virtual void GRMdeleteMusic(const std::string& name)
{
// #if defined(__ANDROID__)
// GSMdeleteSound(name);
// #else
GSMdeleteMusic(name);
// #endif
}
private:
bool soundFileExists(const std::string& filePath) const
{
WITH(m_GSMsound.size())
{
if (m_GSMsound[_I].get() != nullptr)
{
if (m_GSMsound[_I]->getFilePath() == filePath) return true;
}
}
return false;
}
//#if !defined(__ANDROID__)
bool musicFileExists(const std::string& filePath) const
{
WITH(m_GSMmusic.size())
{
if (m_GSMmusic[_I].get() != nullptr)
{
if (m_GSMmusic[_I]->getFilePath() == filePath) return true;
}
}
return false;
}
//#endif
};
}
#endif // GSM_H_INCLUDED