SoundEffects.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Copyright (c) 2008-2021 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Audio/Audio.h>
  23. #include <Urho3D/Audio/AudioEvents.h>
  24. #include <Urho3D/Audio/Sound.h>
  25. #include <Urho3D/Audio/SoundSource.h>
  26. #include <Urho3D/Engine/Engine.h>
  27. #include <Urho3D/Input/Input.h>
  28. #include <Urho3D/IO/Log.h>
  29. #include <Urho3D/Resource/ResourceCache.h>
  30. #include <Urho3D/Scene/Scene.h>
  31. #include <Urho3D/UI/Button.h>
  32. #include <Urho3D/UI/Font.h>
  33. #include <Urho3D/UI/Slider.h>
  34. #include <Urho3D/UI/Text.h>
  35. #include <Urho3D/UI/UI.h>
  36. #include <Urho3D/UI/UIEvents.h>
  37. #include "SoundEffects.h"
  38. #include <Urho3D/DebugNew.h>
  39. // Custom variable identifier for storing sound effect name within the UI element
  40. static const StringHash VAR_SOUNDRESOURCE("SoundResource");
  41. static const unsigned NUM_SOUNDS = 3;
  42. static const char* soundNames[] = {
  43. "Fist",
  44. "Explosion",
  45. "Power-up"
  46. };
  47. static const char* soundResourceNames[] = {
  48. "Sounds/PlayerFistHit.wav",
  49. "Sounds/BigExplosion.wav",
  50. "Sounds/Powerup.wav"
  51. };
  52. URHO3D_DEFINE_APPLICATION_MAIN(SoundEffects)
  53. SoundEffects::SoundEffects(Context* context) :
  54. Sample(context),
  55. musicSource_(nullptr)
  56. {
  57. }
  58. void SoundEffects::Setup()
  59. {
  60. // Modify engine startup parameters
  61. Sample::Setup();
  62. engineParameters_[EP_SOUND] = true;
  63. }
  64. void SoundEffects::Start()
  65. {
  66. // Execute base class startup
  67. Sample::Start();
  68. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  69. scene_ = new Scene(context_);
  70. // Create music sound source
  71. musicSource_ = scene_->CreateComponent<SoundSource>();
  72. // Set the sound type to music so that master volume control works correctly
  73. musicSource_->SetSoundType(SOUND_MUSIC);
  74. // Enable OS cursor
  75. GetSubsystem<Input>()->SetMouseVisible(true);
  76. // Create the user interface
  77. CreateUI();
  78. // Set the mouse mode to use in the sample
  79. Sample::InitMouseMode(MM_FREE);
  80. }
  81. void SoundEffects::CreateUI()
  82. {
  83. UIElement* root = GetSubsystem<UI>()->GetRoot();
  84. auto* cache = GetSubsystem<ResourceCache>();
  85. auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  86. // Set style to the UI root so that elements will inherit it
  87. root->SetDefaultStyle(uiStyle);
  88. // Create buttons for playing back sounds
  89. for (unsigned i = 0; i < NUM_SOUNDS; ++i)
  90. {
  91. Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
  92. // Store the sound effect resource name as a custom variable into the button
  93. button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]);
  94. SubscribeToEvent(button, E_PRESSED, URHO3D_HANDLER(SoundEffects, HandlePlaySound));
  95. }
  96. // Create buttons for playing/stopping music
  97. Button* button = CreateButton(20, 80, 120, 40, "Play Music");
  98. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandlePlayMusic));
  99. button = CreateButton(160, 80, 120, 40, "Stop Music");
  100. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandleStopMusic));
  101. auto* audio = GetSubsystem<Audio>();
  102. // Create sliders for controlling sound and music master volume
  103. Slider* slider = CreateSlider(20, 140, 200, 20, "Sound Volume");
  104. slider->SetValue(audio->GetMasterGain(SOUND_EFFECT));
  105. SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleSoundVolume));
  106. slider = CreateSlider(20, 200, 200, 20, "Music Volume");
  107. slider->SetValue(audio->GetMasterGain(SOUND_MUSIC));
  108. SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleMusicVolume));
  109. }
  110. Button* SoundEffects::CreateButton(int x, int y, int xSize, int ySize, const String& text)
  111. {
  112. UIElement* root = GetSubsystem<UI>()->GetRoot();
  113. auto* cache = GetSubsystem<ResourceCache>();
  114. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  115. // Create the button and center the text onto it
  116. auto* button = root->CreateChild<Button>();
  117. button->SetStyleAuto();
  118. button->SetPosition(x, y);
  119. button->SetSize(xSize, ySize);
  120. auto* buttonText = button->CreateChild<Text>();
  121. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  122. buttonText->SetFont(font, 12);
  123. buttonText->SetText(text);
  124. return button;
  125. }
  126. Slider* SoundEffects::CreateSlider(int x, int y, int xSize, int ySize, const String& text)
  127. {
  128. UIElement* root = GetSubsystem<UI>()->GetRoot();
  129. auto* cache = GetSubsystem<ResourceCache>();
  130. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  131. // Create text and slider below it
  132. auto* sliderText = root->CreateChild<Text>();
  133. sliderText->SetPosition(x, y);
  134. sliderText->SetFont(font, 12);
  135. sliderText->SetText(text);
  136. auto* slider = root->CreateChild<Slider>();
  137. slider->SetStyleAuto();
  138. slider->SetPosition(x, y + 20);
  139. slider->SetSize(xSize, ySize);
  140. // Use 0-1 range for controlling sound/music master volume
  141. slider->SetRange(1.0f);
  142. return slider;
  143. }
  144. void SoundEffects::HandlePlaySound(StringHash eventType, VariantMap& eventData)
  145. {
  146. auto* button = static_cast<Button*>(GetEventSender());
  147. const String& soundResourceName = button->GetVar(VAR_SOUNDRESOURCE).GetString();
  148. // Get the sound resource
  149. auto* cache = GetSubsystem<ResourceCache>();
  150. auto* sound = cache->GetResource<Sound>(soundResourceName);
  151. if (sound)
  152. {
  153. // Create a SoundSource component for playing the sound. The SoundSource component plays
  154. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  155. // SoundSource3D component would be used instead
  156. auto* soundSource = scene_->CreateComponent<SoundSource>();
  157. // Component will automatically remove itself when the sound finished playing
  158. soundSource->SetAutoRemoveMode(REMOVE_COMPONENT);
  159. soundSource->Play(sound);
  160. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  161. soundSource->SetGain(0.75f);
  162. }
  163. }
  164. void SoundEffects::HandlePlayMusic(StringHash eventType, VariantMap& eventData)
  165. {
  166. auto* cache = GetSubsystem<ResourceCache>();
  167. auto* music = cache->GetResource<Sound>("Music/Ninja Gods.ogg");
  168. // Set the song to loop
  169. music->SetLooped(true);
  170. musicSource_->Play(music);
  171. }
  172. void SoundEffects::HandleStopMusic(StringHash eventType, VariantMap& eventData)
  173. {
  174. // Remove the music player node from the scene
  175. musicSource_->Stop();
  176. }
  177. void SoundEffects::HandleSoundVolume(StringHash eventType, VariantMap& eventData)
  178. {
  179. using namespace SliderChanged;
  180. float newVolume = eventData[P_VALUE].GetFloat();
  181. GetSubsystem<Audio>()->SetMasterGain(SOUND_EFFECT, newVolume);
  182. }
  183. void SoundEffects::HandleMusicVolume(StringHash eventType, VariantMap& eventData)
  184. {
  185. using namespace SliderChanged;
  186. float newVolume = eventData[P_VALUE].GetFloat();
  187. GetSubsystem<Audio>()->SetMasterGain(SOUND_MUSIC, newVolume);
  188. }