// Copyright (c) 2008-2023 the Urho3D project // License: MIT #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "SoundEffects.h" #include // Custom variable identifier for storing sound effect name within the UI element static const StringHash VAR_SOUNDRESOURCE("SoundResource"); static const unsigned NUM_SOUNDS = 3; static const char* soundNames[] = { "Fist", "Explosion", "Power-up" }; static const char* soundResourceNames[] = { "Sounds/PlayerFistHit.wav", "Sounds/BigExplosion.wav", "Sounds/Powerup.wav" }; URHO3D_DEFINE_APPLICATION_MAIN(SoundEffects) SoundEffects::SoundEffects(Context* context) : Sample(context), musicSource_(nullptr) { } void SoundEffects::Setup() { // Modify engine startup parameters Sample::Setup(); engineParameters_[EP_SOUND] = true; } void SoundEffects::Start() { // Execute base class startup Sample::Start(); // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds scene_ = new Scene(context_); // Create music sound source musicSource_ = scene_->CreateComponent(); // Set the sound type to music so that master volume control works correctly musicSource_->SetSoundType(SOUND_MUSIC); // Enable OS cursor GetSubsystem()->SetMouseVisible(true); // Create the user interface CreateUI(); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_FREE); } void SoundEffects::CreateUI() { UIElement* root = GetSubsystem()->GetRoot(); auto* cache = GetSubsystem(); auto* uiStyle = cache->GetResource("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); // Create buttons for playing back sounds for (unsigned i = 0; i < NUM_SOUNDS; ++i) { Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]); // Store the sound effect resource name as a custom variable into the button button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]); SubscribeToEvent(button, E_PRESSED, URHO3D_HANDLER(SoundEffects, HandlePlaySound)); } // Create buttons for playing/stopping music Button* button = CreateButton(20, 80, 120, 40, "Play Music"); SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandlePlayMusic)); button = CreateButton(160, 80, 120, 40, "Stop Music"); SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandleStopMusic)); auto* audio = GetSubsystem