CMusicHandler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include "CConfigHandler.h"
  3. #include "CSoundBase.h"
  4. #include "../lib/CCreatureHandler.h"
  5. #include "CSndHandler.h"
  6. /*
  7. * CMusicHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CSpell;
  16. struct _Mix_Music;
  17. typedef struct _Mix_Music Mix_Music;
  18. struct Mix_Chunk;
  19. // Sound infos for creatures in combat
  20. struct CreaturesBattleSounds {
  21. soundBase::soundID attack;
  22. soundBase::soundID defend;
  23. soundBase::soundID killed; // was killed or died
  24. soundBase::soundID move;
  25. soundBase::soundID shoot; // range attack
  26. soundBase::soundID wince; // attacked but did not die
  27. soundBase::soundID ext1; // creature specific extension
  28. soundBase::soundID ext2; // creature specific extension
  29. soundBase::soundID startMoving; // usually same as ext1
  30. soundBase::soundID endMoving; // usually same as ext2
  31. CreaturesBattleSounds(): attack(soundBase::invalid),
  32. defend(soundBase::invalid),
  33. killed(soundBase::invalid),
  34. move(soundBase::invalid),
  35. shoot(soundBase::invalid),
  36. wince(soundBase::invalid),
  37. ext1(soundBase::invalid),
  38. ext2(soundBase::invalid),
  39. startMoving(soundBase::invalid),
  40. endMoving(soundBase::invalid) {};
  41. };
  42. class CAudioBase {
  43. protected:
  44. bool initialized;
  45. int volume; // from 0 (mute) to 100
  46. public:
  47. CAudioBase(): initialized(false), volume(0) {};
  48. virtual void init() = 0;
  49. virtual void release() = 0;
  50. virtual void setVolume(ui32 percent);
  51. ui32 getVolume() { return volume; };
  52. };
  53. class CSoundHandler: public CAudioBase
  54. {
  55. private:
  56. CSndHandler sndh;
  57. soundBase::soundID getSoundID(const std::string &fileName);
  58. //update volume on configuration change
  59. SettingsListener listener;
  60. void onVolumeChange(const JsonNode &volumeNode);
  61. std::map<soundBase::soundID, Mix_Chunk *> soundChunks;
  62. Mix_Chunk *GetSoundChunk(soundBase::soundID soundID);
  63. //have entry for every currently active channel
  64. //boost::function will be NULL if callback was not set
  65. std::map<int, boost::function<void()> > callbacks;
  66. public:
  67. CSoundHandler();
  68. void init();
  69. void release();
  70. void initCreaturesSounds(const std::vector<ConstTransitivePtr<CCreature> > &creatures);
  71. void initSpellsSounds(const std::vector< ConstTransitivePtr<CSpell> > &spells);
  72. void setVolume(ui32 percent);
  73. // Sounds
  74. int playSound(soundBase::soundID soundID, int repeats=0);
  75. int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
  76. void stopSound(int handler);
  77. void setCallback(int channel, boost::function<void()> function);
  78. void soundFinishedCallback(int channel);
  79. std::vector <struct CreaturesBattleSounds> CBattleSounds;
  80. std::map<const CSpell*, soundBase::soundID> spellSounds;
  81. // Sets
  82. std::vector<soundBase::soundID> pickupSounds;
  83. std::vector<soundBase::soundID> horseSounds;
  84. std::vector<soundBase::soundID> battleIntroSounds;
  85. };
  86. // Helper
  87. #define battle_sound(creature,what_sound) CCS->soundh->CBattleSounds[(creature)->idNumber].what_sound
  88. class CMusicHandler;
  89. //Class for handling one music file
  90. class MusicEntry
  91. {
  92. CMusicHandler *owner;
  93. Mix_Music *music;
  94. bool looped;
  95. //if not null - set from which music will be randomly selected
  96. std::string setName;
  97. std::string currentName;
  98. void load(std::string musicURI);
  99. public:
  100. bool isSet(std::string setName);
  101. bool isTrack(std::string trackName);
  102. MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped);
  103. ~MusicEntry();
  104. bool play();
  105. void stop(int fade_ms=0);
  106. };
  107. class CMusicHandler: public CAudioBase
  108. {
  109. private:
  110. // Because we use the SDL music callback, our music variables must
  111. // be protected
  112. boost::mutex musicMutex;
  113. //update volume on configuration change
  114. SettingsListener listener;
  115. void onVolumeChange(const JsonNode &volumeNode);
  116. unique_ptr<MusicEntry> current;
  117. unique_ptr<MusicEntry> next;
  118. void queueNext(MusicEntry *queued);
  119. std::map<std::string, std::map<int, std::string> > musicsSet;
  120. public:
  121. CMusicHandler();
  122. /// add entry with URI musicURI in set. Track will have ID musicID
  123. void addEntryToSet(std::string set, int musicID, std::string musicURI);
  124. void init();
  125. void release();
  126. void setVolume(ui32 percent);
  127. /// play track by URI, if loop = true music will be looped
  128. void playMusic(std::string musicURI, bool loop);
  129. /// play random track from this set
  130. void playMusicFromSet(std::string musicSet, bool loop);
  131. /// play specific track from set
  132. void playMusicFromSet(std::string musicSet, int entryID, bool loop);
  133. void stopMusic(int fade_ms=1000);
  134. void musicFinishedCallback(void);
  135. friend class MusicEntry;
  136. };