CMusicHandler.h 4.7 KB

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