CMusicHandler.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef __CMUSICHANDLER_H__
  2. #define __CMUSICHANDLER_H__
  3. #include <boost/thread/mutex.hpp>
  4. #include "CSoundBase.h"
  5. #include "CMusicBase.h"
  6. #include "../lib/CCreatureHandler.h"
  7. /*
  8. * CMusicHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CSndHandler;
  17. class CSpell;
  18. struct _Mix_Music;
  19. typedef struct _Mix_Music Mix_Music;
  20. struct Mix_Chunk;
  21. // Sound infos for creatures in combat
  22. struct CreaturesBattleSounds {
  23. soundBase::soundID attack;
  24. soundBase::soundID defend;
  25. soundBase::soundID killed; // was killed or died
  26. soundBase::soundID move;
  27. soundBase::soundID shoot; // range attack
  28. soundBase::soundID wince; // attacked but did not die
  29. soundBase::soundID ext1; // creature specific extension
  30. soundBase::soundID ext2; // creature specific extension
  31. soundBase::soundID startMoving; // usually same as ext1
  32. soundBase::soundID endMoving; // usually same as ext2
  33. CreaturesBattleSounds(): attack(soundBase::invalid),
  34. defend(soundBase::invalid),
  35. killed(soundBase::invalid),
  36. move(soundBase::invalid),
  37. shoot(soundBase::invalid),
  38. wince(soundBase::invalid),
  39. ext1(soundBase::invalid),
  40. ext2(soundBase::invalid),
  41. startMoving(soundBase::invalid),
  42. endMoving(soundBase::invalid) {};
  43. };
  44. class CAudioBase {
  45. protected:
  46. bool initialized;
  47. int volume; // from 0 (mute) to 100
  48. public:
  49. CAudioBase(): initialized(false), volume(0) {};
  50. virtual void init() = 0;
  51. virtual void release() = 0;
  52. virtual void setVolume(unsigned int percent);
  53. unsigned int getVolume() { return volume; };
  54. };
  55. class CSoundHandler: public CAudioBase
  56. {
  57. private:
  58. CSndHandler *sndh;
  59. soundBase::soundID getSoundID(std::string &fileName);
  60. std::map<soundBase::soundID, Mix_Chunk *> soundChunks;
  61. Mix_Chunk *GetSoundChunk(soundBase::soundID soundID);
  62. public:
  63. CSoundHandler();
  64. void init();
  65. void release();
  66. void initCreaturesSounds(const std::vector<ConstTransitivePtr<CCreature> > &creatures);
  67. void initSpellsSounds(const std::vector< ConstTransitivePtr<CSpell> > &spells);
  68. void setVolume(unsigned int percent);
  69. // Sounds
  70. int playSound(soundBase::soundID soundID, int repeats=0);
  71. int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
  72. void stopSound(int handler);
  73. std::vector <struct CreaturesBattleSounds> CBattleSounds;
  74. std::map<const CSpell*, soundBase::soundID> spellSounds;
  75. // Sets
  76. std::vector<soundBase::soundID> pickupSounds;
  77. std::vector<soundBase::soundID> horseSounds;
  78. };
  79. // Helper
  80. #define battle_sound(creature,what_sound) CCS->soundh->CBattleSounds[(creature)->idNumber].what_sound
  81. class CMusicHandler: public CAudioBase
  82. {
  83. private:
  84. // Because we use the SDL music callback, our music variables must
  85. // be protected
  86. boost::mutex musicMutex;
  87. Mix_Music *currentMusic;
  88. Mix_Music *nextMusic;
  89. int nextMusicLoop;
  90. Mix_Music * LoadMUS(const char *file); //calls Mix_LoadMUS and checks for errors
  91. int PlayMusic(Mix_Music *music, int loops); //calls Mix_PlayMusic and checks for errors
  92. public:
  93. CMusicHandler();
  94. void init();
  95. void release();
  96. void setVolume(unsigned int percent);
  97. // Musics
  98. std::map<musicBase::musicID, std::string> musics;
  99. std::vector<musicBase::musicID> battleMusics;
  100. std::vector<musicBase::musicID> townMusics;
  101. void playMusic(musicBase::musicID musicID, int loop=1);
  102. void playMusicFromSet(std::vector<musicBase::musicID> &music_vec, int loop=1);
  103. void stopMusic(int fade_ms=1000);
  104. void musicFinishedCallback(void);
  105. };
  106. #endif // __CMUSICHANDLER_H__