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)
{
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
void setVerbose(bool value);
// Devuelve la lista de recursos de un tipo
std::vector<std::string> getListByType(assetType type);
};
#endif

View File

@@ -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<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 "texture.h"
#include "jail_audio.h"
#include <string>
#include <vector>
@@ -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<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

View File

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

View File

@@ -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<sound_file_t> sounds; // Vector con los sonidos
std::vector<music_file_t> 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[]);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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