CMusicHandler.h 3.8 KB

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