Add audio

master
John Montagu, the 4th Earl of Sandvich 2026-03-16 13:26:45 -07:00
parent bd622d1873
commit b74b0dbb9d
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 93 additions and 1 deletions

View File

@ -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

View File

@ -1,5 +1,6 @@
#pragma once
#include "components/AudioComponent.hpp"
#include "components/CollectibleComponent.hpp"
#include "components/ColliderComponent.hpp"
#include "components/GravityReceiverComponent.hpp"

View File

@ -3,6 +3,7 @@
#include "Components.hpp"
#include "EnergyBarRaygui.hpp"
#include "raylib-cpp.hpp"
#include "raylib.h"
#include <algorithm>
@ -257,6 +258,9 @@ std::shared_ptr<Entity> CreateWorld() {
std::shared_ptr<Entity> CreateStats() {
auto e = std::make_shared<Entity>();
e->AddComponent<StatsComponent>();
auto &audio = e->AddComponent<AudioComponent>();
audio.volume = 0.6f;
audio.Load("assets/audio/ping.wav");
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto stats = e->GetComponent<StatsComponent>();

View File

@ -0,0 +1,73 @@
#pragma once
#include "Component.hpp"
#include "raylib-cpp.hpp"
#include <algorithm>
#include <string>
/**
* 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;
};

View File

@ -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<AudioComponent>();
if (audio) {
audio->get().Play();
}
}
});
}

View File

@ -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<StartMenuScene>();

View File

@ -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<GameplayScene>();
return;
}