From b74b0dbb9dee4ee8280b1b8f6a1e6632e4b201a0 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Mon, 16 Mar 2026 13:26:45 -0700 Subject: [PATCH] Add audio --- as6/CMakeLists.txt | 6 ++ as6/Components.hpp | 1 + as6/Entities.cpp | 4 ++ as6/components/AudioComponent.hpp | 73 +++++++++++++++++++++++++ as6/components/CollectibleComponent.cpp | 5 ++ as6/main.cpp | 3 + as6/scene/DeathScene.cpp | 2 +- 7 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 as6/components/AudioComponent.hpp diff --git a/as6/CMakeLists.txt b/as6/CMakeLists.txt index 46ab01e..f127ecb 100644 --- a/as6/CMakeLists.txt +++ b/as6/CMakeLists.txt @@ -12,6 +12,12 @@ add_subdirectory(../raylib-cpp raylib) include(../assets/includeable.cmake) +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/assets/audio) +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/../assets/audio/ping.wav + ${CMAKE_CURRENT_BINARY_DIR}/assets/audio/ping.wav + COPYONLY) + add_executable(as6-gravity-surfing main.cpp raygui.cpp diff --git a/as6/Components.hpp b/as6/Components.hpp index bf03144..7b6cd5e 100644 --- a/as6/Components.hpp +++ b/as6/Components.hpp @@ -1,5 +1,6 @@ #pragma once +#include "components/AudioComponent.hpp" #include "components/CollectibleComponent.hpp" #include "components/ColliderComponent.hpp" #include "components/GravityReceiverComponent.hpp" diff --git a/as6/Entities.cpp b/as6/Entities.cpp index 699af40..9787331 100644 --- a/as6/Entities.cpp +++ b/as6/Entities.cpp @@ -3,6 +3,7 @@ #include "Components.hpp" #include "EnergyBarRaygui.hpp" +#include "raylib-cpp.hpp" #include "raylib.h" #include @@ -257,6 +258,9 @@ std::shared_ptr CreateWorld() { std::shared_ptr CreateStats() { auto e = std::make_shared(); e->AddComponent(); + auto &audio = e->AddComponent(); + audio.volume = 0.6f; + audio.Load("assets/audio/ping.wav"); auto &render = e->AddComponent(); render.draw = [e]() { auto stats = e->GetComponent(); diff --git a/as6/components/AudioComponent.hpp b/as6/components/AudioComponent.hpp new file mode 100644 index 0000000..df5e327 --- /dev/null +++ b/as6/components/AudioComponent.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include "Component.hpp" +#include "raylib-cpp.hpp" + +#include +#include + +/** + * Lightweight component that owns a raylib::Sound instance. + * The component exposes a simple API so other gameplay systems can trigger the + * sound without having to track the resource themselves. + */ +struct AudioComponent : public Component { + std::string filePath; + bool playOnStart = false; + float volume = 1.0f; + + void Setup() override { + if (!filePath.empty()) { + Load(filePath); + } + } + + void Update(float) override {} + + void Cleanup() override { + if (sound.IsValid()) { + sound.Unload(); + } + } + + bool Load(const std::string &path) { + if (sound.IsValid()) { + sound.Unload(); + } + filePath = path; + sound.Load(filePath); + if (!sound.IsValid()) { + return false; + } + sound.SetVolume(volume); + sound.SetPitch(1.0f); + if (playOnStart) { + Play(); + } + return true; + } + + void Play() { + if (!sound.IsValid()) { + return; + } + sound.Play(); + } + + void Stop() { + if (!sound.IsValid()) { + return; + } + sound.Stop(); + } + + void SetVolume(float newVolume) { + volume = std::clamp(newVolume, 0.0f, 1.0f); + if (sound.IsValid()) { + sound.SetVolume(volume); + } + } + + private: + raylib::Sound sound; +}; diff --git a/as6/components/CollectibleComponent.cpp b/as6/components/CollectibleComponent.cpp index f350970..3cf61d5 100644 --- a/as6/components/CollectibleComponent.cpp +++ b/as6/components/CollectibleComponent.cpp @@ -1,6 +1,7 @@ #include "components/CollectibleComponent.hpp" #include "Entity.hpp" +#include "components/AudioComponent.hpp" #include "components/ColliderComponent.hpp" #include "components/StatsComponent.hpp" @@ -27,6 +28,10 @@ void CollectibleComponent::Setup() { stats->get().AddValue(stats->get().gainPerStar); stats->get().AddStar(); } + auto audio = context->statsEntity->GetComponent(); + if (audio) { + audio->get().Play(); + } } }); } diff --git a/as6/main.cpp b/as6/main.cpp index a8ea6b4..548bdc2 100644 --- a/as6/main.cpp +++ b/as6/main.cpp @@ -14,6 +14,9 @@ int main() { window.SetState(FLAG_WINDOW_RESIZABLE); window.SetTargetFPS(60); + // Initialize audio device early so we can play sound effects. + raylib::AudioDevice audioDevice; + SceneManager sceneManager; sceneManager.ChangeScene(); diff --git a/as6/scene/DeathScene.cpp b/as6/scene/DeathScene.cpp index a1d40d6..1978d49 100644 --- a/as6/scene/DeathScene.cpp +++ b/as6/scene/DeathScene.cpp @@ -6,7 +6,7 @@ #include "scene/StartMenuScene.hpp" void DeathScene::Update(float) { - if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if (IsKeyPressed(KEY_ENTER)) { manager.QueueSceneChange(); return; }