From 2b628d2d618ea85e86abba477f2a3bec2af6aa74 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 5 Jul 2024 07:20:20 +0200 Subject: [PATCH] =?UTF-8?q?La=20m=C3=BAsica=20la=20tiene=20ahora=20la=20cl?= =?UTF-8?q?ase=20Director?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/common/asset.cpp | 16 +++++++++ source/common/asset.h | 3 ++ source/common/utils.cpp | 28 ++++++++++++++++ source/common/utils.h | 22 +++++++++++++ source/director.cpp | 73 ++++++++++++++++++++++++++++++++++++++--- source/director.h | 22 ++++++++++--- source/game.cpp | 10 ++---- source/game.h | 4 +-- source/intro.cpp | 17 ++-------- source/intro.h | 5 +-- source/title.cpp | 10 ++---- source/title.h | 4 +-- 12 files changed, 169 insertions(+), 45 deletions(-) diff --git a/source/common/asset.cpp b/source/common/asset.cpp index 1202129..acc0fcd 100644 --- a/source/common/asset.cpp +++ b/source/common/asset.cpp @@ -187,4 +187,20 @@ std::string Asset::getTypeName(int type) void Asset::setVerbose(bool value) { verbose = value; +} + +// Devuelve la lista de recursos de un tipo +std::vector Asset::getListByType(assetType type) +{ + std::vector list; + + for (auto f : fileList) + { + if (f.type == type) + { + list.push_back(f.file); + } + } + + return list; } \ No newline at end of file diff --git a/source/common/asset.h b/source/common/asset.h index 2eb5bca..c14ec01 100644 --- a/source/common/asset.h +++ b/source/common/asset.h @@ -61,6 +61,9 @@ public: // Establece si ha de mostrar texto por pantalla void setVerbose(bool value); + + // Devuelve la lista de recursos de un tipo + std::vector getListByType(assetType type); }; #endif diff --git a/source/common/utils.cpp b/source/common/utils.cpp index efffdfe..94f740b 100644 --- a/source/common/utils.cpp +++ b/source/common/utils.cpp @@ -183,4 +183,32 @@ std::string toLower(std::string str) std::string nova(lower); free(lower); return nova; +} + +// Obtiene el fichero de sonido a partir de un nombre +JA_Sound_t *getSound(std::vector sounds, std::string name) +{ + for (auto s : sounds) + { + if (s.name == name) + { + return s.file; + } + } + + return nullptr; +} + +// Obtiene el fichero de música a partir de un nombre +JA_Music_t *getMusic(std::vector music, std::string name) +{ + for (auto m : music) + { + if (m.name == name) + { + return m.file; + } + } + + return nullptr; } \ No newline at end of file diff --git a/source/common/utils.h b/source/common/utils.h index 47c4922..30adfbf 100644 --- a/source/common/utils.h +++ b/source/common/utils.h @@ -2,6 +2,7 @@ #include #include "texture.h" +#include "jail_audio.h" #include #include @@ -163,6 +164,7 @@ struct options_t op_audio_t audio; // Opciones para el audio }; +// Estructura para almacenar todos los parámetros del juego struct param_t { int gameWidth; // Ancho de la resolucion nativa del juego @@ -181,6 +183,20 @@ struct param_t int titleCounter; // Tiempo de inactividad del titulo }; +// Estructura para almacenar ficheros de sonido y su nombre +struct sound_file_t +{ + std::string name; // Nombre del sonido + JA_Sound_t *file; // Fichero con el sonido +}; + +// Estructura para almacenar ficheros musicales y su nombre +struct music_file_t +{ + std::string name; // Nombre de la musica + JA_Music_t *file; // Fichero con la música +}; + // Calcula el cuadrado de la distancia entre dos puntos double distanceSquared(int x1, int y1, int x2, int y2); @@ -205,4 +221,10 @@ std::string boolToString(bool value); // Convierte una cadena a minusculas std::string toLower(std::string str); +// Obtiene el fichero de sonido a partir de un nombre +JA_Sound_t *getSound(std::vector sounds, std::string name); + +// Obtiene el fichero de música a partir de un nombre +JA_Music_t *getMusic(std::vector music, std::string name); + #endif \ No newline at end of file diff --git a/source/director.cpp b/source/director.cpp index b2e9286..344e3b5 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -62,6 +62,12 @@ Director::Director(int argc, char *argv[]) initInput(); screen = new Screen(window, renderer, asset, options); + + // Carga los sonidos del juego + loadSounds(); + + // Carga las musicas del juego + loadMusics(); } Director::~Director() @@ -76,6 +82,9 @@ Director::~Director() delete param; delete section; + deleteSounds(); + deleteMusics(); + SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); @@ -664,6 +673,62 @@ bool Director::saveConfigFile() return success; } +// Carga los sonidos del juego +void Director::loadSounds() +{ + // Obtiene la lista con las rutas a los ficheros de sonidos + std::vector list = asset->getListByType(t_sound); + sounds.clear(); + + for (auto l : list) + { + const size_t lastIndex = l.find_last_of("/") + 1; + const std::string name = l.substr(lastIndex, std::string::npos); + sound_file_t temp; + temp.name = name; // Añade el nombre del fichero + temp.file = JA_LoadSound(l.c_str()); // Carga el fichero de audio + sounds.push_back(temp); + } +} + +// Carga las musicas del juego +void Director::loadMusics() +{ + // Obtiene la lista con las rutas a los ficheros musicales + std::vector list = asset->getListByType(t_music); + musics.clear(); + + for (auto l : list) + { + const size_t lastIndex = l.find_last_of("/") + 1; + const std::string name = l.substr(lastIndex, std::string::npos); + music_file_t temp; + temp.name = name; // Añade el nombre del fichero + temp.file = JA_LoadMusic(l.c_str()); // Carga el fichero de audio + musics.push_back(temp); + } +} + +// Descarga los sonidos del juego +void Director::deleteSounds() +{ + for (auto s : sounds) + { + JA_DeleteSound(s.file); + } + sounds.clear(); +} + +// Descarga las músicas del juego +void Director::deleteMusics() +{ + for (auto m : musics) + { + JA_DeleteMusic(m.file); + } + musics.clear(); +} + // Ejecuta la seccion de juego con el logo void Director::runLogo() { @@ -675,7 +740,7 @@ void Director::runLogo() // Ejecuta la seccion de juego de la introducción void Director::runIntro() { - intro = new Intro(renderer, screen, asset, input, lang, param, section); + intro = new Intro(renderer, screen, asset, input, lang, param, section, getMusic(musics, "intro.ogg")); intro->run(); delete intro; } @@ -683,7 +748,7 @@ void Director::runIntro() // Ejecuta la seccion de juego con el titulo y los menus void Director::runTitle() { - title = new Title(renderer, screen, input, asset, options, lang, param, section); + title = new Title(renderer, screen, input, asset, options, lang, param, section, getMusic(musics, "title.ogg")); title->run(); delete title; } @@ -692,7 +757,7 @@ void Director::runTitle() void Director::runGame() { const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2; - game = new Game(numPlayers, 0, renderer, screen, asset, lang, input, false, param, options, section); + game = new Game(numPlayers, 0, renderer, screen, asset, lang, input, false, param, options, section, getMusic(musics, "playing.ogg")); game->run(); delete game; } @@ -716,7 +781,7 @@ void Director::runInstructions() // Ejecuta el juego en modo demo void Director::runDemoGame() { - demoGame = new Game(1, 0, renderer, screen, asset, lang, input, true, param, options, section); + demoGame = new Game(1, 0, renderer, screen, asset, lang, input, true, param, options, section, nullptr); demoGame->run(); delete demoGame; } diff --git a/source/director.h b/source/director.h index 09b9643..144d583 100644 --- a/source/director.h +++ b/source/director.h @@ -43,10 +43,12 @@ private: section_t *section; // Sección y subsección actual del programa; // Variables - options_t *options; // Variable con todas las opciones del programa - param_t *param; // Variable con todos los parametros del programa - std::string executablePath; // Path del ejecutable - std::string systemFolder; // Carpeta del sistema donde guardar datos + options_t *options; // Variable con todas las opciones del programa + param_t *param; // Variable con todos los parametros del programa + std::string executablePath; // Path del ejecutable + std::string systemFolder; // Carpeta del sistema donde guardar datos + std::vector sounds; // Vector con los sonidos + std::vector musics; // Vector con las musicas // Inicializa jail_audio void initJailAudio(); @@ -75,6 +77,18 @@ private: // Guarda el fichero de configuración bool saveConfigFile(); + // Carga los sonidos del juego + void loadSounds(); + + // Carga las musicas del juego + void loadMusics(); + + // Descarga los sonidos del juego + void deleteSounds(); + + // Descarga las músicas del juego + void deleteMusics(); + // Comprueba los parametros del programa void checkProgramArguments(int argc, char *argv[]); diff --git a/source/game.cpp b/source/game.cpp index 896a665..92ebba8 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -3,7 +3,7 @@ #define DEATH_COUNTER 350 // Constructor -Game::Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section) +Game::Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section, JA_Music_t *music) { // Copia los punteros this->renderer = renderer; @@ -14,6 +14,7 @@ Game::Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *scree this->param = param; this->options = options; this->section = section; + this->music = music; // Pasa variables this->demo.enabled = demo; @@ -150,8 +151,6 @@ Game::~Game() JA_DeleteSound(clockSound); JA_DeleteSound(powerBallSound); JA_DeleteSound(coffeeMachineSound); - - JA_DeleteMusic(gameMusic); } // Inicializa las variables necesarias para la sección 'Game' @@ -499,9 +498,6 @@ void Game::loadMedia() stageChangeSound = JA_LoadSound(asset->get("stage_change.wav").c_str()); coffeeMachineSound = JA_LoadSound(asset->get("title.wav").c_str()); - // Musicas - gameMusic = JA_LoadMusic(asset->get("playing.ogg").c_str()); - if (options->console) { std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl @@ -3001,7 +2997,7 @@ void Game::run() { if (players[0]->isAlive()) { - JA_PlayMusic(gameMusic); + JA_PlayMusic(music); } } } diff --git a/source/game.h b/source/game.h index 32eeaef..bcdc342 100644 --- a/source/game.h +++ b/source/game.h @@ -171,7 +171,7 @@ private: JA_Sound_t *powerBallSound; // Sonido para cuando se explota una Power Ball JA_Sound_t *coffeeMachineSound; // Sonido para cuando la máquina de café toca el suelo - JA_Music_t *gameMusic; // Musica de fondo + JA_Music_t *music; // Musica de fondo // Variables int numPlayers; // Numero de jugadores @@ -469,7 +469,7 @@ private: public: // Constructor - Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section); + Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section, JA_Music_t *music); // Destructor ~Game(); diff --git a/source/intro.cpp b/source/intro.cpp index f78fc2c..090a96f 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -1,7 +1,7 @@ #include "intro.h" // Constructor -Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section) +Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section, JA_Music_t *music) { // Copia los punteros this->renderer = renderer; @@ -11,15 +11,13 @@ Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, this->input = input; this->param = param; this->section = section; + this->music = music; // Reserva memoria para los objetos eventHandler = new SDL_Event(); texture = new Texture(renderer, asset->get("intro.png")); text = new Text(asset->get("nokia.png"), asset->get("nokia.txt"), renderer); - // Carga los recursos - loadMedia(); - // Inicializa variables section->name = SECTION_PROG_INTRO; section->subsection = 0; @@ -161,17 +159,6 @@ Intro::~Intro() { delete text; } - - JA_DeleteMusic(music); -} - -// Carga los recursos -bool Intro::loadMedia() -{ - // Musicas - music = JA_LoadMusic(asset->get("intro.ogg").c_str()); - - return true; } // Comprueba los eventos diff --git a/source/intro.h b/source/intro.h index 1748c0f..7ccef96 100644 --- a/source/intro.h +++ b/source/intro.h @@ -45,9 +45,6 @@ private: // Dibuja el objeto en pantalla void render(); - // Carga los recursos - bool loadMedia(); - // Comprueba los eventos void checkEvents(); @@ -59,7 +56,7 @@ private: public: // Constructor - Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section); + Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section, JA_Music_t *music); // Destructor ~Intro(); diff --git a/source/title.cpp b/source/title.cpp index 52b0f0f..74c5744 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -1,7 +1,7 @@ #include "title.h" // Constructor -Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, param_t *param, section_t *section) +Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, param_t *param, section_t *section, JA_Music_t *music) { // Copia las direcciones de los punteros y objetos this->renderer = renderer; @@ -12,6 +12,7 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, this->lang = lang; this->param = param; this->section = section; + this->music = music; // Reserva memoria y crea los objetos eventHandler = new SDL_Event(); @@ -35,9 +36,6 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, gameLogo = new GameLogo(renderer, screen, asset, param, GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QUARTER_Y + 20); gameLogo->enable(); - // Musicas - titleMusic = JA_LoadMusic(asset->get("title.ogg").c_str()); - // Inicializa los valores init(); } @@ -59,8 +57,6 @@ Title::~Title() delete background; delete tiledbg; delete gameLogo; - - JA_DeleteMusic(titleMusic); } // Inicializa los valores de las variables @@ -139,7 +135,7 @@ void Title::update() // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { - JA_PlayMusic(titleMusic); + JA_PlayMusic(music); } // Actualiza el logo con el título del juego diff --git a/source/title.h b/source/title.h index 4206df5..3ceaf68 100644 --- a/source/title.h +++ b/source/title.h @@ -49,7 +49,7 @@ private: Text *text2; // Objeto de texto para poder escribir textos en pantalla Fade *fade; // Objeto para realizar fundidos en pantalla - JA_Music_t *titleMusic; // Musica para el titulo + JA_Music_t *music; // Musica para el titulo // Variable int counter; // Temporizador para la pantalla de titulo @@ -82,7 +82,7 @@ private: public: // Constructor - Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, param_t *param, section_t *section); + Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, param_t *param, section_t *section, JA_Music_t *music); // Destructor ~Title();