La música la tiene ahora la clase Director

This commit is contained in:
2024-07-05 07:20:20 +02:00
parent 8051cbbc39
commit 2b628d2d61
12 changed files with 169 additions and 45 deletions

View File

@@ -187,4 +187,20 @@ std::string Asset::getTypeName(int type)
void Asset::setVerbose(bool value) void Asset::setVerbose(bool value)
{ {
verbose = value; verbose = value;
}
// Devuelve la lista de recursos de un tipo
std::vector<std::string> Asset::getListByType(assetType type)
{
std::vector<std::string> list;
for (auto f : fileList)
{
if (f.type == type)
{
list.push_back(f.file);
}
}
return list;
} }

View File

@@ -61,6 +61,9 @@ public:
// Establece si ha de mostrar texto por pantalla // Establece si ha de mostrar texto por pantalla
void setVerbose(bool value); void setVerbose(bool value);
// Devuelve la lista de recursos de un tipo
std::vector<std::string> getListByType(assetType type);
}; };
#endif #endif

View File

@@ -183,4 +183,32 @@ std::string toLower(std::string str)
std::string nova(lower); std::string nova(lower);
free(lower); free(lower);
return nova; return nova;
}
// Obtiene el fichero de sonido a partir de un nombre
JA_Sound_t *getSound(std::vector<sound_file_t> 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_file_t> music, std::string name)
{
for (auto m : music)
{
if (m.name == name)
{
return m.file;
}
}
return nullptr;
} }

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "texture.h" #include "texture.h"
#include "jail_audio.h"
#include <string> #include <string>
#include <vector> #include <vector>
@@ -163,6 +164,7 @@ struct options_t
op_audio_t audio; // Opciones para el audio op_audio_t audio; // Opciones para el audio
}; };
// Estructura para almacenar todos los parámetros del juego
struct param_t struct param_t
{ {
int gameWidth; // Ancho de la resolucion nativa del juego int gameWidth; // Ancho de la resolucion nativa del juego
@@ -181,6 +183,20 @@ struct param_t
int titleCounter; // Tiempo de inactividad del titulo 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 // Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2); double distanceSquared(int x1, int y1, int x2, int y2);
@@ -205,4 +221,10 @@ std::string boolToString(bool value);
// Convierte una cadena a minusculas // Convierte una cadena a minusculas
std::string toLower(std::string str); std::string toLower(std::string str);
// Obtiene el fichero de sonido a partir de un nombre
JA_Sound_t *getSound(std::vector<sound_file_t> sounds, std::string name);
// Obtiene el fichero de música a partir de un nombre
JA_Music_t *getMusic(std::vector<music_file_t> music, std::string name);
#endif #endif

View File

@@ -62,6 +62,12 @@ Director::Director(int argc, char *argv[])
initInput(); initInput();
screen = new Screen(window, renderer, asset, options); screen = new Screen(window, renderer, asset, options);
// Carga los sonidos del juego
loadSounds();
// Carga las musicas del juego
loadMusics();
} }
Director::~Director() Director::~Director()
@@ -76,6 +82,9 @@ Director::~Director()
delete param; delete param;
delete section; delete section;
deleteSounds();
deleteMusics();
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
@@ -664,6 +673,62 @@ bool Director::saveConfigFile()
return success; return success;
} }
// Carga los sonidos del juego
void Director::loadSounds()
{
// Obtiene la lista con las rutas a los ficheros de sonidos
std::vector<std::string> 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<std::string> 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 // Ejecuta la seccion de juego con el logo
void Director::runLogo() void Director::runLogo()
{ {
@@ -675,7 +740,7 @@ void Director::runLogo()
// Ejecuta la seccion de juego de la introducción // Ejecuta la seccion de juego de la introducción
void Director::runIntro() 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(); intro->run();
delete intro; delete intro;
} }
@@ -683,7 +748,7 @@ void Director::runIntro()
// Ejecuta la seccion de juego con el titulo y los menus // Ejecuta la seccion de juego con el titulo y los menus
void Director::runTitle() 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(); title->run();
delete title; delete title;
} }
@@ -692,7 +757,7 @@ void Director::runTitle()
void Director::runGame() void Director::runGame()
{ {
const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2; 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(); game->run();
delete game; delete game;
} }
@@ -716,7 +781,7 @@ void Director::runInstructions()
// Ejecuta el juego en modo demo // Ejecuta el juego en modo demo
void Director::runDemoGame() 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(); demoGame->run();
delete demoGame; delete demoGame;
} }

View File

@@ -43,10 +43,12 @@ private:
section_t *section; // Sección y subsección actual del programa; section_t *section; // Sección y subsección actual del programa;
// Variables // Variables
options_t *options; // Variable con todas las opciones del programa options_t *options; // Variable con todas las opciones del programa
param_t *param; // Variable con todos los parametros del programa param_t *param; // Variable con todos los parametros del programa
std::string executablePath; // Path del ejecutable std::string executablePath; // Path del ejecutable
std::string systemFolder; // Carpeta del sistema donde guardar datos std::string systemFolder; // Carpeta del sistema donde guardar datos
std::vector<sound_file_t> sounds; // Vector con los sonidos
std::vector<music_file_t> musics; // Vector con las musicas
// Inicializa jail_audio // Inicializa jail_audio
void initJailAudio(); void initJailAudio();
@@ -75,6 +77,18 @@ private:
// Guarda el fichero de configuración // Guarda el fichero de configuración
bool saveConfigFile(); 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 // Comprueba los parametros del programa
void checkProgramArguments(int argc, char *argv[]); void checkProgramArguments(int argc, char *argv[]);

View File

@@ -3,7 +3,7 @@
#define DEATH_COUNTER 350 #define DEATH_COUNTER 350
// Constructor // 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 // Copia los punteros
this->renderer = renderer; this->renderer = renderer;
@@ -14,6 +14,7 @@ Game::Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *scree
this->param = param; this->param = param;
this->options = options; this->options = options;
this->section = section; this->section = section;
this->music = music;
// Pasa variables // Pasa variables
this->demo.enabled = demo; this->demo.enabled = demo;
@@ -150,8 +151,6 @@ Game::~Game()
JA_DeleteSound(clockSound); JA_DeleteSound(clockSound);
JA_DeleteSound(powerBallSound); JA_DeleteSound(powerBallSound);
JA_DeleteSound(coffeeMachineSound); JA_DeleteSound(coffeeMachineSound);
JA_DeleteMusic(gameMusic);
} }
// Inicializa las variables necesarias para la sección 'Game' // 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()); stageChangeSound = JA_LoadSound(asset->get("stage_change.wav").c_str());
coffeeMachineSound = JA_LoadSound(asset->get("title.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) if (options->console)
{ {
std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl
@@ -3001,7 +2997,7 @@ void Game::run()
{ {
if (players[0]->isAlive()) if (players[0]->isAlive())
{ {
JA_PlayMusic(gameMusic); JA_PlayMusic(music);
} }
} }
} }

View File

@@ -171,7 +171,7 @@ private:
JA_Sound_t *powerBallSound; // Sonido para cuando se explota una Power Ball 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_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 // Variables
int numPlayers; // Numero de jugadores int numPlayers; // Numero de jugadores
@@ -469,7 +469,7 @@ private:
public: public:
// Constructor // 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 // Destructor
~Game(); ~Game();

View File

@@ -1,7 +1,7 @@
#include "intro.h" #include "intro.h"
// Constructor // 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 // Copia los punteros
this->renderer = renderer; this->renderer = renderer;
@@ -11,15 +11,13 @@ Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input,
this->input = input; this->input = input;
this->param = param; this->param = param;
this->section = section; this->section = section;
this->music = music;
// Reserva memoria para los objetos // Reserva memoria para los objetos
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
texture = new Texture(renderer, asset->get("intro.png")); texture = new Texture(renderer, asset->get("intro.png"));
text = new Text(asset->get("nokia.png"), asset->get("nokia.txt"), renderer); text = new Text(asset->get("nokia.png"), asset->get("nokia.txt"), renderer);
// Carga los recursos
loadMedia();
// Inicializa variables // Inicializa variables
section->name = SECTION_PROG_INTRO; section->name = SECTION_PROG_INTRO;
section->subsection = 0; section->subsection = 0;
@@ -161,17 +159,6 @@ Intro::~Intro()
{ {
delete text; 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 // Comprueba los eventos

View File

@@ -45,9 +45,6 @@ private:
// Dibuja el objeto en pantalla // Dibuja el objeto en pantalla
void render(); void render();
// Carga los recursos
bool loadMedia();
// Comprueba los eventos // Comprueba los eventos
void checkEvents(); void checkEvents();
@@ -59,7 +56,7 @@ private:
public: public:
// Constructor // 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 // Destructor
~Intro(); ~Intro();

View File

@@ -1,7 +1,7 @@
#include "title.h" #include "title.h"
// Constructor // 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 // Copia las direcciones de los punteros y objetos
this->renderer = renderer; this->renderer = renderer;
@@ -12,6 +12,7 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset,
this->lang = lang; this->lang = lang;
this->param = param; this->param = param;
this->section = section; this->section = section;
this->music = music;
// Reserva memoria y crea los objetos // Reserva memoria y crea los objetos
eventHandler = new SDL_Event(); 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 = new GameLogo(renderer, screen, asset, param, GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QUARTER_Y + 20);
gameLogo->enable(); gameLogo->enable();
// Musicas
titleMusic = JA_LoadMusic(asset->get("title.ogg").c_str());
// Inicializa los valores // Inicializa los valores
init(); init();
} }
@@ -59,8 +57,6 @@ Title::~Title()
delete background; delete background;
delete tiledbg; delete tiledbg;
delete gameLogo; delete gameLogo;
JA_DeleteMusic(titleMusic);
} }
// Inicializa los valores de las variables // Inicializa los valores de las variables
@@ -139,7 +135,7 @@ void Title::update()
// Reproduce la música // Reproduce la música
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) 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 // Actualiza el logo con el título del juego

View File

@@ -49,7 +49,7 @@ private:
Text *text2; // Objeto de texto para poder escribir textos en pantalla Text *text2; // Objeto de texto para poder escribir textos en pantalla
Fade *fade; // Objeto para realizar fundidos 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 // Variable
int counter; // Temporizador para la pantalla de titulo int counter; // Temporizador para la pantalla de titulo
@@ -82,7 +82,7 @@ private:
public: public:
// Constructor // 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 // Destructor
~Title(); ~Title();