SoundEffects.cpp 7.9 KB

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