SoundEffects.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Copyright (c) 2008-2014 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/UI/Button.h>
  24. #include <Urho3D/Engine/Engine.h>
  25. #include <Urho3D/UI/Font.h>
  26. #include <Urho3D/Input/Input.h>
  27. #include <Urho3D/IO/Log.h>
  28. #include <Urho3D/Resource/ResourceCache.h>
  29. #include <Urho3D/Scene/Scene.h>
  30. #include <Urho3D/UI/Slider.h>
  31. #include <Urho3D/Audio/Sound.h>
  32. #include <Urho3D/Audio/SoundSource.h>
  33. #include <Urho3D/UI/Text.h>
  34. #include <Urho3D/UI/UI.h>
  35. #include <Urho3D/UI/UIEvents.h>
  36. #include "SoundEffects.h"
  37. #include <Urho3D/Container/DebugNew.h>
  38. // Custom variable identifier for storing sound effect name within the UI element
  39. static const StringHash VAR_SOUNDRESOURCE("SoundResource");
  40. static const unsigned NUM_SOUNDS = 3;
  41. static String soundNames[] = {
  42. "Fist",
  43. "Explosion",
  44. "Power-up"
  45. };
  46. static String soundResourceNames[] = {
  47. "Sounds/PlayerFistHit.wav",
  48. "Sounds/BigExplosion.wav",
  49. "Sounds/Powerup.wav"
  50. };
  51. DEFINE_APPLICATION_MAIN(SoundEffects)
  52. SoundEffects::SoundEffects(Context* context) :
  53. Sample(context)
  54. {
  55. }
  56. void SoundEffects::Start()
  57. {
  58. // Execute base class startup
  59. Sample::Start();
  60. // Enable OS cursor
  61. GetSubsystem<Input>()->SetMouseVisible(true);
  62. // Create the user interface
  63. CreateUI();
  64. }
  65. void SoundEffects::CreateUI()
  66. {
  67. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  68. scene_ = new Scene(context_);
  69. UIElement* root = GetSubsystem<UI>()->GetRoot();
  70. ResourceCache* cache = GetSubsystem<ResourceCache>();
  71. XMLFile* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  72. // Set style to the UI root so that elements will inherit it
  73. root->SetDefaultStyle(uiStyle);
  74. // Create buttons for playing back sounds
  75. for (unsigned i = 0; i < NUM_SOUNDS; ++i)
  76. {
  77. Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
  78. // Store the sound effect resource name as a custom variable into the button
  79. button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]);
  80. SubscribeToEvent(button, E_PRESSED, HANDLER(SoundEffects, HandlePlaySound));
  81. }
  82. // Create buttons for playing/stopping music
  83. Button* button = CreateButton(20, 80, 120, 40, "Play Music");
  84. SubscribeToEvent(button, E_RELEASED, HANDLER(SoundEffects, HandlePlayMusic));
  85. button = CreateButton(160, 80, 120, 40, "Stop Music");
  86. SubscribeToEvent(button, E_RELEASED, HANDLER(SoundEffects, HandleStopMusic));
  87. Audio* audio = GetSubsystem<Audio>();
  88. // Create sliders for controlling sound and music master volume
  89. Slider* slider = CreateSlider(20, 140, 200, 20, "Sound Volume");
  90. slider->SetValue(audio->GetMasterGain(SOUND_EFFECT));
  91. SubscribeToEvent(slider, E_SLIDERCHANGED, HANDLER(SoundEffects, HandleSoundVolume));
  92. slider = CreateSlider(20, 200, 200, 20, "Music Volume");
  93. slider->SetValue(audio->GetMasterGain(SOUND_MUSIC));
  94. SubscribeToEvent(slider, E_SLIDERCHANGED, HANDLER(SoundEffects, HandleMusicVolume));
  95. }
  96. Button* SoundEffects::CreateButton(int x, int y, int xSize, int ySize, const String& text)
  97. {
  98. UIElement* root = GetSubsystem<UI>()->GetRoot();
  99. ResourceCache* cache = GetSubsystem<ResourceCache>();
  100. Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  101. // Create the button and center the text onto it
  102. Button* button = root->CreateChild<Button>();
  103. button->SetStyleAuto();
  104. button->SetPosition(x, y);
  105. button->SetSize(xSize, ySize);
  106. Text* buttonText = button->CreateChild<Text>();
  107. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  108. buttonText->SetFont(font, 12);
  109. buttonText->SetText(text);
  110. return button;
  111. }
  112. Slider* SoundEffects::CreateSlider(int x, int y, int xSize, int ySize, const String& text)
  113. {
  114. UIElement* root = GetSubsystem<UI>()->GetRoot();
  115. ResourceCache* cache = GetSubsystem<ResourceCache>();
  116. Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  117. // Create text and slider below it
  118. Text* sliderText = root->CreateChild<Text>();
  119. sliderText->SetPosition(x, y);
  120. sliderText->SetFont(font, 12);
  121. sliderText->SetText(text);
  122. Slider* slider = root->CreateChild<Slider>();
  123. slider->SetStyleAuto();
  124. slider->SetPosition(x, y + 20);
  125. slider->SetSize(xSize, ySize);
  126. // Use 0-1 range for controlling sound/music master volume
  127. slider->SetRange(1.0f);
  128. return slider;
  129. }
  130. void SoundEffects::HandlePlaySound(StringHash eventType, VariantMap& eventData)
  131. {
  132. Button* button = static_cast<Button*>(GetEventSender());
  133. const String& soundResourceName = button->GetVar(VAR_SOUNDRESOURCE).GetString();
  134. // Get the sound resource
  135. ResourceCache* cache = GetSubsystem<ResourceCache>();
  136. Sound* sound = cache->GetResource<Sound>(soundResourceName);
  137. if (sound)
  138. {
  139. // Create a scene node with a SoundSource component for playing the sound. The SoundSource component plays
  140. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  141. // SoundSource3D component would be used instead
  142. Node* soundNode = scene_->CreateChild("Sound");
  143. SoundSource* soundSource = soundNode->CreateComponent<SoundSource>();
  144. soundSource->Play(sound);
  145. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  146. soundSource->SetGain(0.75f);
  147. // Set the sound component to automatically remove its scene node from the scene when the sound is done playing
  148. soundSource->SetAutoRemove(true);
  149. }
  150. }
  151. void SoundEffects::HandlePlayMusic(StringHash eventType, VariantMap& eventData)
  152. {
  153. // Check if the music player node/component already exist
  154. if (scene_->GetChild("Music"))
  155. return;
  156. ResourceCache* cache = GetSubsystem<ResourceCache>();
  157. Sound* music = cache->GetResource<Sound>("Music/Ninja Gods.ogg");
  158. // Set the song to loop
  159. music->SetLooped(true);
  160. // Create a scene node and a sound source for the music
  161. Node* musicNode = scene_->CreateChild("Music");
  162. SoundSource* musicSource = musicNode->CreateComponent<SoundSource>();
  163. // Set the sound type to music so that master volume control works correctly
  164. musicSource->SetSoundType(SOUND_MUSIC);
  165. musicSource->Play(music);
  166. }
  167. void SoundEffects::HandleStopMusic(StringHash eventType, VariantMap& eventData)
  168. {
  169. // Remove the music player node from the scene
  170. scene_->RemoveChild(scene_->GetChild("Music"));
  171. }
  172. void SoundEffects::HandleSoundVolume(StringHash eventType, VariantMap& eventData)
  173. {
  174. using namespace SliderChanged;
  175. float newVolume = eventData[P_VALUE].GetFloat();
  176. GetSubsystem<Audio>()->SetMasterGain(SOUND_EFFECT, newVolume);
  177. }
  178. void SoundEffects::HandleMusicVolume(StringHash eventType, VariantMap& eventData)
  179. {
  180. using namespace SliderChanged;
  181. float newVolume = eventData[P_VALUE].GetFloat();
  182. GetSubsystem<Audio>()->SetMasterGain(SOUND_MUSIC, newVolume);
  183. }