afegit sistema de audio

This commit is contained in:
2025-12-02 13:51:54 +01:00
parent 9ceb21c04f
commit e51749dbc6
10 changed files with 5685 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(orni VERSION 0.2.0)
project(orni VERSION 0.3.0)
# Info del proyecto
set(PROJECT_LONG_NAME "Orni Attack")
@@ -45,6 +45,8 @@ set(APP_SOURCES
source/core/graphics/shape.cpp
source/core/graphics/shape_loader.cpp
source/core/graphics/vector_text.cpp
source/core/audio/audio.cpp
source/core/audio/audio_cache.cpp
source/game/options.cpp
source/game/escenes/escena_logo.cpp
source/game/escenes/escena_joc.cpp

BIN
data/sounds/explosion.wav Normal file

Binary file not shown.

BIN
data/sounds/laser_shoot.wav Normal file

Binary file not shown.

5565
source/external/stb_vorbis.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -12,3 +12,4 @@ ORNI TODO:
- Crear progresion: fichero con enemigos para ese nivel. cuando se acaba la lista de niveles -> loop con dificultad aumentada. Pensar si jefe final. Juego no tiene fin
- Menu de servicio? numero de vidas, numero de continues o free play, controles de audio, controles de video, colores, activar parpadeo o cambiar frecuencia
- Cuando se destruye un enemigo, salen dos mas pequeños
- El escalado de pantalla hace cosas raras al salir de full screen: permite ventanas no-4:3