afegit sistema de audio
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
|
||||
@@ -51,6 +52,9 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
||||
return;
|
||||
}
|
||||
|
||||
// Reproducir sonido de explosión
|
||||
Audio::get()->playSound(Defaults::Sound::EXPLOSION, Audio::Group::GAME);
|
||||
|
||||
// Obtenir centre de la forma per a transformacions
|
||||
const Punt& shape_centre = shape->get_centre();
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
@@ -50,6 +51,9 @@ void Bala::disparar(const Punt& posicio, float angle) {
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
velocitat_ = 140.0f;
|
||||
|
||||
// Reproducir sonido de disparo láser
|
||||
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
|
||||
}
|
||||
|
||||
void Bala::actualitzar(float delta_time) {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "../../core/audio/audio.hpp"
|
||||
#include "../../core/rendering/line_renderer.hpp"
|
||||
#include "../../core/system/gestor_escenes.hpp"
|
||||
#include "../../core/system/global_events.hpp"
|
||||
@@ -73,6 +74,9 @@ void EscenaJoc::executar() {
|
||||
// Actualitzar física del joc amb delta_time real
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar sistema d'audio
|
||||
Audio::update();
|
||||
|
||||
// Actualitzar colors oscil·lats
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
|
||||
@@ -39,6 +39,14 @@ void init() {
|
||||
// Rendering
|
||||
rendering.vsync = Defaults::Rendering::VSYNC_DEFAULT;
|
||||
|
||||
// Audio
|
||||
audio.enabled = Defaults::Audio::ENABLED;
|
||||
audio.volume = Defaults::Audio::VOLUME;
|
||||
audio.music.enabled = Defaults::Music::ENABLED;
|
||||
audio.music.volume = Defaults::Music::VOLUME;
|
||||
audio.sound.enabled = Defaults::Sound::ENABLED;
|
||||
audio.sound.volume = Defaults::Sound::VOLUME;
|
||||
|
||||
// Version
|
||||
version = std::string(Project::VERSION);
|
||||
}
|
||||
@@ -200,6 +208,71 @@ static void loadRenderingConfigFromYaml(const fkyaml::node& yaml) {
|
||||
}
|
||||
}
|
||||
|
||||
static void loadAudioConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("audio")) {
|
||||
const auto& aud = yaml["audio"];
|
||||
|
||||
if (aud.contains("enabled")) {
|
||||
try {
|
||||
audio.enabled = aud["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.enabled = Defaults::Audio::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("volume")) {
|
||||
try {
|
||||
float val = aud["volume"].get_value<float>();
|
||||
audio.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Audio::VOLUME;
|
||||
} catch (...) {
|
||||
audio.volume = Defaults::Audio::VOLUME;
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("music")) {
|
||||
const auto& mus = aud["music"];
|
||||
|
||||
if (mus.contains("enabled")) {
|
||||
try {
|
||||
audio.music.enabled = mus["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.music.enabled = Defaults::Music::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (mus.contains("volume")) {
|
||||
try {
|
||||
float val = mus["volume"].get_value<float>();
|
||||
audio.music.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Music::VOLUME;
|
||||
} catch (...) {
|
||||
audio.music.volume = Defaults::Music::VOLUME;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("sound")) {
|
||||
const auto& snd = aud["sound"];
|
||||
|
||||
if (snd.contains("enabled")) {
|
||||
try {
|
||||
audio.sound.enabled = snd["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.sound.enabled = Defaults::Sound::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (snd.contains("volume")) {
|
||||
try {
|
||||
float val = snd["volume"].get_value<float>();
|
||||
audio.sound.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Sound::VOLUME;
|
||||
} catch (...) {
|
||||
audio.sound.volume = Defaults::Sound::VOLUME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carregar configuració des del fitxer YAML
|
||||
auto loadFromFile() -> bool {
|
||||
const std::string CONFIG_VERSION = std::string(Project::VERSION);
|
||||
@@ -246,6 +319,7 @@ auto loadFromFile() -> bool {
|
||||
loadPhysicsConfigFromYaml(yaml);
|
||||
loadGameplayConfigFromYaml(yaml);
|
||||
loadRenderingConfigFromYaml(yaml);
|
||||
loadAudioConfigFromYaml(yaml);
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config carregada correctament des de: " << config_file_path
|
||||
@@ -309,7 +383,18 @@ auto saveToFile() -> bool {
|
||||
|
||||
file << "# RENDERITZACIÓ\n";
|
||||
file << "rendering:\n";
|
||||
file << " vsync: " << rendering.vsync << " # 0=disabled, 1=enabled\n";
|
||||
file << " vsync: " << rendering.vsync << " # 0=disabled, 1=enabled\n\n";
|
||||
|
||||
file << "# AUDIO\n";
|
||||
file << "audio:\n";
|
||||
file << " enabled: " << (audio.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.volume << " # 0.0 to 1.0\n";
|
||||
file << " music:\n";
|
||||
file << " enabled: " << (audio.music.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.music.volume << " # 0.0 to 1.0\n";
|
||||
file << " sound:\n";
|
||||
file << " enabled: " << (audio.sound.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.sound.volume << " # 0.0 to 1.0\n";
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
@@ -31,6 +31,23 @@ struct Rendering {
|
||||
int vsync{1}; // 0=disabled, 1=enabled
|
||||
};
|
||||
|
||||
struct Music {
|
||||
bool enabled{true};
|
||||
float volume{0.8f};
|
||||
};
|
||||
|
||||
struct Sound {
|
||||
bool enabled{true};
|
||||
float volume{1.0f};
|
||||
};
|
||||
|
||||
struct Audio {
|
||||
Music music{};
|
||||
Sound sound{};
|
||||
bool enabled{true};
|
||||
float volume{1.0f};
|
||||
};
|
||||
|
||||
// Variables globals (inline per evitar ODR violations)
|
||||
|
||||
inline std::string version{}; // Versió del config per validació
|
||||
@@ -39,6 +56,7 @@ inline Window window{};
|
||||
inline Physics physics{};
|
||||
inline Gameplay gameplay{};
|
||||
inline Rendering rendering{};
|
||||
inline Audio audio{};
|
||||
|
||||
inline std::string config_file_path{}; // Establert per setConfigFile()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user