Don melitonada la classe Asset

This commit is contained in:
2024-09-28 13:49:00 +02:00
parent 878518babe
commit fa82758ce1
23 changed files with 99 additions and 56 deletions

View File

@@ -1,6 +1,27 @@
#include "asset.h"
#include <iostream>
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Asset *Asset::asset = nullptr;
// [SINGLETON] Crearemos el objeto asset con esta función estática
void Asset::init(std::string executablePath)
{
Asset::asset = new Asset(executablePath);
}
// [SINGLETON] Destruiremos el objeto asset con esta función estática
void Asset::destroy()
{
delete Asset::asset;
}
// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él
Asset *Asset::get()
{
return Asset::asset;
}
// Constructor
Asset::Asset(std::string executablePath)
{
@@ -13,6 +34,12 @@ Asset::Asset(std::string executablePath)
#endif
}
// Destructor
Asset::~Asset()
{
}
// Añade un elemento a la lista
void Asset::add(std::string file, enum assetType type, bool required, bool absolute)
{

View File

@@ -22,13 +22,16 @@ enum assetType
class Asset
{
private:
// [SINGLETON] Objeto asset privado para Don Melitón
static Asset *asset;
// Estructura para definir un item
struct item_t
{
std::string file; // Ruta del fichero desde la raiz del directorio
enum assetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
//bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
// bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
};
// Variables
@@ -43,10 +46,22 @@ private:
// Devuelve el nombre del tipo de recurso
std::string getTypeName(int type);
public:
// Constructor
Asset(std::string path);
// Destructor
~Asset();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init(std::string path);
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static Asset *get();
// Añade un elemento a la lista
void add(std::string file, enum assetType type, bool required = true, bool absolute = false);

View File

@@ -2,11 +2,11 @@
#include "param.h"
// Constructor
Background::Background(SDL_Renderer *renderer, Asset *asset)
Background::Background(SDL_Renderer *renderer)
{
// Copia los punteros
this->renderer = renderer;
this->asset = asset;
asset = Asset::get();
// Carga las texturas
buildingsTexture = new Texture(renderer, asset->get("game_buildings.png"));

View File

@@ -105,7 +105,7 @@ private:
public:
// Constructor
Background(SDL_Renderer *renderer, Asset *asset);
Background(SDL_Renderer *renderer);
// Destructor
~Background();

View File

@@ -33,7 +33,8 @@ Director::Director(int argc, char *argv[])
createSystemFolder("jailgames/coffee_crisis_arcade_edition");
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(executablePath);
Asset::init(executablePath);
asset = Asset::get();
// Si falta algún fichero no inicia el programa
if (!setFileList())
@@ -72,7 +73,7 @@ Director::Director(int argc, char *argv[])
input = new Input(asset->get("gamecontrollerdb.txt"));
initInput();
Screen::Init(window, renderer, asset, input);
Screen::init(window, renderer, input);
screen = Screen::get();
// Carga los sonidos del juego
@@ -86,9 +87,9 @@ Director::~Director()
{
saveOptionsFile(asset->get("config.txt"));
delete asset;
delete input;
Screen::Destroy();
Asset::destroy();
Screen::destroy();
deleteSounds();
deleteMusics();
@@ -597,7 +598,7 @@ void Director::deleteMusics()
// Ejecuta la sección con el logo
void Director::runLogo()
{
logo = new Logo(asset, input);
logo = new Logo(input);
logo->run();
delete logo;
}
@@ -605,7 +606,7 @@ void Director::runLogo()
// Ejecuta la sección con la secuencia de introducción
void Director::runIntro()
{
intro = new Intro(asset, input, getMusic(musics, "intro.ogg"));
intro = new Intro(input, getMusic(musics, "intro.ogg"));
intro->run();
delete intro;
}
@@ -613,7 +614,7 @@ void Director::runIntro()
// Ejecuta la sección con el titulo del juego
void Director::runTitle()
{
title = new Title(asset, input, getMusic(musics, "title.ogg"));
title = new Title(input, getMusic(musics, "title.ogg"));
title->run();
delete title;
}
@@ -621,9 +622,9 @@ void Director::runTitle()
// Ejecuta la sección donde se juega al juego
void Director::runGame()
{
const int playerID = section::options;
const int playerID = section::options == section::OPTIONS_GAME_PLAY_1P ? 1 : 2;
const int currentStage = 0;
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, asset, input, getMusic(musics, "playing.ogg"));
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, input, getMusic(musics, "playing.ogg"));
game->run();
delete game;
}
@@ -631,7 +632,7 @@ void Director::runGame()
// Ejecuta la sección donde se muestran las instrucciones
void Director::runInstructions()
{
instructions = new Instructions(asset, input, getMusic(musics, "title.ogg"));
instructions = new Instructions(input, getMusic(musics, "title.ogg"));
instructions->run();
delete instructions;
}
@@ -639,7 +640,7 @@ void Director::runInstructions()
// Ejecuta la sección donde se muestra la tabla de puntuaciones
void Director::runHiScoreTable()
{
hiScoreTable = new HiScoreTable(asset, input, getMusic(musics, "title.ogg"));
hiScoreTable = new HiScoreTable(input, getMusic(musics, "title.ogg"));
hiScoreTable->run();
delete hiScoreTable;
}
@@ -649,7 +650,7 @@ void Director::runDemoGame()
{
const int playerID = (rand() % 2) + 1;
const int currentStage = 0;
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, asset, input, nullptr);
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, input, nullptr);
demoGame->run();
delete demoGame;
}

View File

@@ -5,11 +5,11 @@
#define GAME_OVER_COUNTER 350
// Constructor
Game::Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music)
Game::Game(int playerID, int currentStage, bool demo, Input *input, JA_Music_t *music)
{
// Copia los punteros
screen = Screen::get();
this->asset = asset;
asset = Asset::get();
this->input = input;
this->music = music;
renderer = screen->getRenderer();
@@ -23,8 +23,8 @@ Game::Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input
// Crea los objetos
fade = new Fade(renderer);
eventHandler = new SDL_Event();
scoreboard = new Scoreboard(renderer, asset);
background = new Background(renderer, asset);
scoreboard = new Scoreboard(renderer);
background = new Background(renderer);
explosions = new Explosions();
enemyFormations = new EnemyFormations();

View File

@@ -447,7 +447,7 @@ private:
public:
// Constructor
Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music);
Game(int playerID, int currentStage, bool demo, Input *input, JA_Music_t *music);
// Destructor
~Game();

View File

@@ -2,12 +2,12 @@
#include "param.h"
// Constructor
GameLogo::GameLogo(Asset *asset, int x, int y)
GameLogo::GameLogo(int x, int y)
{
// Copia los punteros
screen = Screen::get();
renderer = screen->getRenderer();
this->asset = asset;
asset = Asset::get();
this->x = x;
this->y = y;

View File

@@ -60,7 +60,7 @@ private:
public:
// Constructor
GameLogo(Asset *asset, int x, int y);
GameLogo(int x, int y);
// Destructor
~GameLogo();

View File

@@ -4,11 +4,11 @@
#include <iostream>
// Constructor
HiScoreTable::HiScoreTable(Asset *asset, Input *input, JA_Music_t *music)
HiScoreTable::HiScoreTable(Input *input, JA_Music_t *music)
{
// Copia punteros
screen = Screen::get();
this->asset = asset;
asset = Asset::get();
this->input = input;
this->music = music;
renderer = screen->getRenderer();
@@ -16,7 +16,7 @@ HiScoreTable::HiScoreTable(Asset *asset, Input *input, JA_Music_t *music)
// Objetos
eventHandler = new SDL_Event();
fade = new Fade(renderer);
background = new Background(renderer, asset);
background = new Background(renderer);
text = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer);
// Crea un backbuffer para el renderizador

View File

@@ -79,7 +79,7 @@ private:
public:
// Constructor
HiScoreTable(Asset *asset, Input *input, JA_Music_t *music);
HiScoreTable(Input *input, JA_Music_t *music);
// Destructor
~HiScoreTable();

View File

@@ -4,13 +4,13 @@
#include <iostream>
// Constructor
Instructions::Instructions(Asset *asset, Input *input, JA_Music_t *music)
Instructions::Instructions(Input *input, JA_Music_t *music)
{
// Copia los punteros
screen = Screen::get();
this->asset = asset;
this->input = input;
this->music = music;
screen = Screen::get();
asset = Asset::get();
renderer = screen->getRenderer();
// Crea objetos

View File

@@ -86,7 +86,7 @@ private:
public:
// Constructor
Instructions(Asset *asset, Input *input, JA_Music_t *music);
Instructions(Input *input, JA_Music_t *music);
// Destructor
~Instructions();

View File

@@ -3,13 +3,13 @@
#include "options.h"
// Constructor
Intro::Intro(Asset *asset, Input *input, JA_Music_t *music)
Intro::Intro(Input *input, JA_Music_t *music)
{
// Copia los punteros
screen = Screen::get();
this->asset = asset;
this->input = input;
this->music = music;
asset = Asset::get();
screen = Screen::get();
SDL_Renderer *renderer = screen->getRenderer();
// Reserva memoria para los objetos

View File

@@ -60,7 +60,7 @@ private:
public:
// Constructor
Intro(Asset *asset, Input *input, JA_Music_t *music);
Intro(Input *input, JA_Music_t *music);
// Destructor
~Intro();

View File

@@ -4,12 +4,12 @@
#include <iostream>
// Constructor
Logo::Logo(Asset *asset, Input *input)
Logo::Logo(Input *input)
{
// Copia la dirección de los objetos
screen = Screen::get();
this->asset = asset;
this->input = input;
screen = Screen::get();
asset = Asset::get();
SDL_Renderer *renderer = screen->getRenderer();
// Reserva memoria para los punteros

View File

@@ -71,7 +71,7 @@ private:
public:
// Constructor
Logo(Asset *asset, Input *input);
Logo(Input *input);
// Destructor
~Logo();

View File

@@ -2,11 +2,11 @@
#include <math.h>
// Constructor
Scoreboard::Scoreboard(SDL_Renderer *renderer, Asset *asset)
Scoreboard::Scoreboard(SDL_Renderer *renderer)
{
// Copia los punteros
this->renderer = renderer;
this->asset = asset;
asset = Asset::get();
// Inicializa punteros
gamePowerMeterTexture = nullptr;

View File

@@ -91,7 +91,7 @@ private:
public:
// Constructor
Scoreboard(SDL_Renderer *renderer, Asset *asset);
Scoreboard(SDL_Renderer *renderer);
// Destructor
~Scoreboard();

View File

@@ -13,13 +13,13 @@
Screen *Screen::screen = nullptr;
// [SINGLETON] Crearemos el objeto screen con esta función estática
void Screen::Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input)
void Screen::init(SDL_Window *window, SDL_Renderer *renderer, Input *input)
{
Screen::screen = new Screen(window, renderer, asset, input);
Screen::screen = new Screen(window, renderer, input);
}
// [SINGLETON] Destruiremos el objeto screen con esta función estática
void Screen::Destroy()
void Screen::destroy()
{
delete Screen::screen;
}
@@ -31,13 +31,13 @@ Screen *Screen::get()
}
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input)
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input)
{
// Copia punteros
this->window = window;
this->renderer = renderer;
this->asset = asset;
this->input = input;
asset = Asset::get();
// Inicializa variables
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);

View File

@@ -81,17 +81,17 @@ private:
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
// Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input);
Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input);
// Destructor
~Screen();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input);
static void init(SDL_Window *window, SDL_Renderer *renderer, Input *input);
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void Destroy();
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static Screen *get();

View File

@@ -3,12 +3,12 @@
#include "options.h"
// Constructor
Title::Title(Asset *asset, Input *input, JA_Music_t *music)
Title::Title(Input *input, JA_Music_t *music)
{
// Copia las direcciones de los punteros y objetos
this->input = input;
this->asset = asset;
this->music = music;
asset = Asset::get();
screen = Screen::get();
SDL_Renderer *renderer = screen->getRenderer();
@@ -26,7 +26,7 @@ Title::Title(Asset *asset, Input *input, JA_Music_t *music)
tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM);
gameLogo = new GameLogo(asset, param.game.gameArea.centerX, param.title.titleCCPosition);
gameLogo = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition);
gameLogo->enable();
defineButtons = new DefineButtons(input, text2);

View File

@@ -101,7 +101,7 @@ private:
public:
// Constructor
Title(Asset *asset, Input *input, JA_Music_t *music);
Title(Input *input, JA_Music_t *music);
// Destructor
~Title();