Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SoundManager.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*!
  2. * \file src/SoundManager.cpp
  3. * \brief This is the audio manager Implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "global.h"
  9. #include "system/Sound.h"
  10. #include "SoundManager.h"
  11. std::vector<SoundSource> SoundManager::soundSources;
  12. std::vector<int> SoundManager::soundMap;
  13. std::vector<SoundDetail> SoundManager::soundDetails;
  14. std::vector<int> SoundManager::sampleIndices;
  15. void SoundManager::clear() {
  16. soundSources.clear();
  17. soundMap.clear();
  18. soundDetails.clear();
  19. sampleIndices.clear();
  20. Sound::clear();
  21. }
  22. int SoundManager::prepareSources() {
  23. for (int i = 0; i < soundSources.size(); i++) {
  24. float vol;
  25. int index = getIndex(soundSources.at(i).id, &vol);
  26. int ret = Sound::addSource(index, vol, false, true);
  27. assertEqual(ret, i);
  28. float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
  29. ret = Sound::sourceAt(i, pos);
  30. assertEqual(ret, 0);
  31. Sound::play(i, false);
  32. }
  33. for (int i = 0; i < soundMap.size(); i++) {
  34. float vol;
  35. int index = getIndex(i, &vol);
  36. if ((index >= 0) && (index < Sound::numBuffers())) {
  37. int ret = Sound::addSource(index, vol, true, false);
  38. assertGreaterThanEqual(ret, 0);
  39. }
  40. }
  41. return 0;
  42. }
  43. void SoundManager::addSoundSource(float x, float y, float z, int id, int flags) {
  44. soundSources.emplace_back(x, y, z, id, flags);
  45. }
  46. void SoundManager::addSoundMapEntry(int id) {
  47. soundMap.push_back(id);
  48. }
  49. void SoundManager::addSoundDetail(int sample, float volume) {
  50. soundDetails.emplace_back(sample, volume);
  51. }
  52. void SoundManager::addSampleIndex(int index) {
  53. sampleIndices.push_back(index);
  54. }
  55. int SoundManager::sizeSoundMap() {
  56. return soundMap.size();
  57. }
  58. int SoundManager::getIndex(int index, float* volume) {
  59. if (index <= -1)
  60. return -1;
  61. if (index >= soundMap.size())
  62. return -2; // SoundMap not big enough
  63. index = soundMap.at(index);
  64. if (index <= -1)
  65. return -3; // SoundMap has no entry here (-1)
  66. if (index >= soundDetails.size())
  67. return -4; // SoundMap entry is bigger than SoundDetails
  68. SoundDetail s = soundDetails.at(index);
  69. if (volume != nullptr)
  70. *volume = s.volume;
  71. if (s.sample <= -1)
  72. return -5; // SoundDetail has no entry here (-1)
  73. if (s.sample >= sampleIndices.size())
  74. return -6; // SoundDetail entry is bigger than SampleIndices
  75. index = sampleIndices.at(s.sample);
  76. if (index <= -1)
  77. return -7; // SampleIndices has no entry here (-1)
  78. return index;
  79. }
  80. int SoundManager::playSound(int index) {
  81. if ((index >= 0) && (index < soundMap.size())) {
  82. if (soundMap.at(index) == -1)
  83. return 0;
  84. int c = 1;
  85. for (int i = 0; i < index; i++)
  86. if (soundMap.at(i) != -1)
  87. c++;
  88. Sound::play(c, true);
  89. return 0;
  90. } else {
  91. return -1;
  92. }
  93. }
  94. void SoundManager::display() {
  95. if (ImGui::CollapsingHeader("Sound Map Player")) {
  96. if (!Sound::getEnabled()) {
  97. ImGui::Text("Please enable Sound first!");
  98. if (ImGui::Button("Enable Sound!")) {
  99. Sound::setEnabled(true);
  100. }
  101. } else if (Sound::numBuffers() == 0) {
  102. ImGui::Text("Please load a level!");
  103. } else {
  104. static int index = 0;
  105. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  106. ImGui::SliderInt("##soundslide", &index, 0, sizeSoundMap() - 1);
  107. ImGui::PopItemWidth();
  108. ImGui::SameLine();
  109. if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
  110. if (index < (sizeSoundMap() - 1))
  111. index++;
  112. else
  113. index = 0;
  114. }
  115. ImGui::SameLine();
  116. if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
  117. if (index > 0)
  118. index--;
  119. else
  120. index = sizeSoundMap() - 1;
  121. }
  122. ImGui::SameLine();
  123. if (ImGui::Button("Play##soundplay")) {
  124. playSound(index);
  125. }
  126. ImGui::Text("Index: %d", getIndex(index));
  127. }
  128. }
  129. }