CMusicHandler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "../lib/CConfigHandler.h"
  3. #include "CSoundBase.h"
  4. #include "../lib/CCreatureHandler.h"
  5. /*
  6. * CMusicHandler.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CSpell;
  15. struct _Mix_Music;
  16. struct SDL_RWops;
  17. typedef struct _Mix_Music Mix_Music;
  18. struct Mix_Chunk;
  19. class CAudioBase {
  20. protected:
  21. bool initialized;
  22. int volume; // from 0 (mute) to 100
  23. public:
  24. CAudioBase(): initialized(false), volume(0) {};
  25. virtual void init() = 0;
  26. virtual void release() = 0;
  27. virtual void setVolume(ui32 percent);
  28. ui32 getVolume() { return volume; };
  29. };
  30. class CSoundHandler: public CAudioBase
  31. {
  32. private:
  33. //soundBase::soundID getSoundID(const std::string &fileName);
  34. //update volume on configuration change
  35. SettingsListener listener;
  36. void onVolumeChange(const JsonNode &volumeNode);
  37. std::map<soundBase::soundID, Mix_Chunk *> soundChunks;
  38. Mix_Chunk *GetSoundChunk(soundBase::soundID soundID);
  39. Mix_Chunk *GetSoundChunk(std::string &sound);
  40. //have entry for every currently active channel
  41. //std::function will be nullptr if callback was not set
  42. std::map<int, std::function<void()> > callbacks;
  43. public:
  44. CSoundHandler();
  45. void init();
  46. void release();
  47. void setVolume(ui32 percent);
  48. // Sounds
  49. int playSound(soundBase::soundID soundID, int repeats=0);
  50. int playSound(std::string sound, int repeats=0);
  51. int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
  52. void stopSound(int handler);
  53. void setCallback(int channel, std::function<void()> function);
  54. void soundFinishedCallback(int channel);
  55. // Sets
  56. std::vector<soundBase::soundID> pickupSounds;
  57. std::vector<soundBase::soundID> horseSounds;
  58. std::vector<soundBase::soundID> battleIntroSounds;
  59. };
  60. // Helper //now it looks somewhat useless
  61. #define battle_sound(creature,what_sound) creature->sounds.what_sound
  62. class CMusicHandler;
  63. //Class for handling one music file
  64. class MusicEntry
  65. {
  66. std::pair<std::unique_ptr<ui8[]>, size_t> data;
  67. CMusicHandler *owner;
  68. Mix_Music *music;
  69. SDL_RWops *musicFile;
  70. int loop; // -1 = indefinite
  71. //if not null - set from which music will be randomly selected
  72. std::string setName;
  73. std::string currentName;
  74. void load(std::string musicURI);
  75. public:
  76. bool isSet(std::string setName);
  77. bool isTrack(std::string trackName);
  78. MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped);
  79. ~MusicEntry();
  80. bool play();
  81. bool stop(int fade_ms=0);
  82. };
  83. class CMusicHandler: public CAudioBase
  84. {
  85. private:
  86. // Because we use the SDL music callback, our music variables must
  87. // be protected
  88. boost::mutex musicMutex;
  89. //update volume on configuration change
  90. SettingsListener listener;
  91. void onVolumeChange(const JsonNode &volumeNode);
  92. unique_ptr<MusicEntry> current;
  93. unique_ptr<MusicEntry> next;
  94. void queueNext(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped);
  95. void queueNext(unique_ptr<MusicEntry> queued);
  96. std::map<std::string, std::map<int, std::string> > musicsSet;
  97. public:
  98. CMusicHandler();
  99. /// add entry with URI musicURI in set. Track will have ID musicID
  100. void addEntryToSet(std::string set, int musicID, std::string musicURI);
  101. void init();
  102. void release();
  103. void setVolume(ui32 percent);
  104. /// play track by URI, if loop = true music will be looped
  105. void playMusic(std::string musicURI, bool loop);
  106. /// play random track from this set
  107. void playMusicFromSet(std::string musicSet, bool loop);
  108. /// play specific track from set
  109. void playMusicFromSet(std::string musicSet, int entryID, bool loop);
  110. void stopMusic(int fade_ms=1000);
  111. void musicFinishedCallback(void);
  112. friend class MusicEntry;
  113. };