MusicModule.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * author: Patrick-Christopher Mattulat
  3. * dev-tools: JetBrains CLion
  4. * created: 23.08.2018
  5. * last updated: 12.09.2018
  6. */
  7. #include <SDL_mixer.h>
  8. #include "MusicModule.h"
  9. #include "../../Error/ErrorCodes.h"
  10. #include "../../Module/Module.h"
  11. //////////////////////////////////////////////////
  12. //////////////////////////////////////////////////
  13. //// constructor / destructor
  14. //////////////////////////////////////////////////
  15. //////////////////////////////////////////////////
  16. MusicModule::MusicModule(uint32_t id):
  17. protectedFromReplay(false),
  18. loops(0),
  19. module(MODULE_MUSIC),
  20. volume(MIX_MAX_VOLUME)
  21. {
  22. this->id.store(id);
  23. }
  24. MusicModule::~MusicModule() = default;
  25. //////////////////////////////////////////////////
  26. //////////////////////////////////////////////////
  27. //// public
  28. //////////////////////////////////////////////////
  29. //////////////////////////////////////////////////
  30. int MusicModule::fadeIn(int ms)
  31. {
  32. int result = LE_NO_ERROR;
  33. if(Mix_FadeInMusic(this->music, this->loops.load(), ms)) {
  34. result = LE_SDL_FADEIN_MUSIC;
  35. }
  36. return result;
  37. }
  38. int MusicModule::fadeOut(int ms)
  39. {
  40. int result = LE_NO_ERROR;
  41. if(!Mix_FadeOutMusic(ms)) {
  42. result = LE_SDL_FADEOUT_MUSIC;
  43. }
  44. return result;
  45. }
  46. uint32_t MusicModule::getId()
  47. {
  48. return this->id.load();
  49. }
  50. uint8_t MusicModule::getModule()
  51. {
  52. return this->module.load();
  53. }
  54. uint8_t MusicModule::getVolume()
  55. {
  56. return this->volume.load();
  57. }
  58. bool MusicModule::isLocked()
  59. {
  60. return this->protectedFromReplay.load();
  61. }
  62. bool MusicModule::isPaused()
  63. {
  64. return (bool) Mix_PausedMusic();
  65. }
  66. bool MusicModule::isPlaying()
  67. {
  68. return (bool) Mix_PlayingMusic();
  69. }
  70. int MusicModule::load(const std::string& file)
  71. {
  72. int result = LE_NO_ERROR;
  73. this->music = Mix_LoadMUS(file.c_str());
  74. if(this->music == nullptr) {
  75. result = LE_SDL_LOAD_MUS;
  76. }
  77. return result;
  78. }
  79. void MusicModule::lock(bool lock)
  80. {
  81. this->protectedFromReplay.store(lock);
  82. }
  83. void MusicModule::pause()
  84. {
  85. Mix_PauseMusic();
  86. }
  87. int MusicModule::play(int loops)
  88. {
  89. int result = LE_NO_ERROR;
  90. if(!this->protectedFromReplay.load()) {
  91. this->loops.store(loops);
  92. Mix_VolumeMusic(this->volume.load());
  93. if(Mix_PlayMusic(this->music, this->loops.load())) {
  94. result = LE_SDL_PLAY_MUSIC;
  95. }
  96. }
  97. return result;
  98. }
  99. void MusicModule::resume()
  100. {
  101. Mix_ResumeMusic();
  102. }
  103. void MusicModule::setVolume(uint8_t volume)
  104. {
  105. this->volume.store(volume);
  106. Mix_VolumeMusic(this->volume.load());
  107. }