CMusicHandler.h 3.5 KB

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