CMusicHandler.h 4.8 KB

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