Primera implementación de la clase asset

This commit is contained in:
2021-09-08 18:26:22 +02:00
parent a0b14f5071
commit 94a06dcc2d
7 changed files with 114 additions and 123 deletions

View File

@@ -3,6 +3,7 @@
// Constructor // Constructor
Asset::Asset(std::string path) Asset::Asset(std::string path)
{ {
mExecutablePath = path;
} }
// Destructor // Destructor
@@ -11,10 +12,56 @@ Asset::~Asset()
} }
// Añade un elemento a la lista // Añade un elemento a la lista
void Asset::add(std::string file, enum assetType type) void Asset::add(std::string file, enum assetType type, bool required)
{ {
item_t temp; item_t temp;
temp.file = file; temp.file = mExecutablePath + "/.." + file;
temp.type = type; temp.type = type;
temp.required = required;
mFileList.push_back(temp); mFileList.push_back(temp);
}
// Devuelve un elemento de la lista a partir de una cadena
std::string Asset::get(std::string text)
{
for (int i = 0; i < mFileList.size(); i++)
if (mFileList[i].file.find(text) != std::string::npos)
return mFileList[i].file;
return "";
}
// Comprueba que existen todos los elementos
bool Asset::check()
{
bool success = true;
for (int i = 0; i < mFileList.size(); i++)
if (mFileList[i].required)
success &= checkFile(mFileList[i].file);
return success;
}
// Comprueba que existe un fichero
bool Asset::checkFile(std::string path)
{
bool success = true;
// Comprueba si existe el fichero
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b");
if (file != NULL)
{
printf("Checking file %-20s [OK]\n", filename.c_str());
SDL_RWclose(file);
}
else
{
printf("Checking file %-20s [ERROR]\n", filename.c_str());
success = false;
}
return success;
} }

View File

@@ -6,14 +6,15 @@
#ifndef ASSET_H #ifndef ASSET_H
#define ASSET_H #define ASSET_H
enum assetType {bitmap, music, sound, data, font, lang}; enum assetType
{
#define ASSET_TYPE_BITMAP 0 bitmap,
#define ASSET_TYPE_MUSIC 1 music,
#define ASSET_TYPE_SOUND 2 sound,
#define ASSET_TYPE_DATA 3 data,
#define ASSET_TYPE_FONT 4 font,
#define ASSET_TYPE_LANG 5 lang
};
// Clase Asset // Clase Asset
class Asset class Asset
@@ -22,11 +23,16 @@ private:
// Estructura para definir un item // Estructura para definir un item
struct item_t struct item_t
{ {
std::string file; std::string file; // Ruta del fichero desde la raiz del directorio
enum assetType type; enum assetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
}; };
std::vector<item_t> mFileList; std::vector<item_t> mFileList;
std::string mExecutablePath;
// Comprueba que existe un fichero
bool checkFile(std::string path);
public: public:
// Constructor // Constructor
@@ -36,7 +42,13 @@ public:
~Asset(); ~Asset();
// Añade un elemento a la lista // Añade un elemento a la lista
void add(std::string file, enum assetType type); void add(std::string file, enum assetType type, bool required = true);
// Devuelve un elemento de la lista a partir de una cadena
std::string get(std::string text);
// Comprueba que existen todos los elementos
bool check();
}; };
#endif #endif

View File

@@ -10,19 +10,19 @@ Director::Director(std::string path)
// Inicializa la ruta // Inicializa la ruta
setExecutablePath(path); setExecutablePath(path);
// Establece la lista de ficheros
setFileList();
// Crea el objeto que controla los ficheros de recursos // Crea el objeto que controla los ficheros de recursos
mAsset = new Asset(mExecutablePath); mAsset = new Asset(mExecutablePath);
// Si falta algún fichero no inicies el programa // Establece la lista de ficheros
setFileList();
// Si falta algún fichero no inicia el programa
Uint8 section = PROG_SECTION_GAME; Uint8 section = PROG_SECTION_GAME;
if (!checkFileList()) if (!mAsset->check())
section = PROG_SECTION_QUIT; section = PROG_SECTION_QUIT;
// Inicializa el objeto de idioma // Inicializa el objeto de idioma
mLang = new Lang(mFileList); mLang = new Lang(mAsset);
// Crea el puntero a la estructura y carga el fichero de configuración // Crea el puntero a la estructura y carga el fichero de configuración
mOptions = new options_t; mOptions = new options_t;
@@ -39,7 +39,7 @@ Director::Director(std::string path)
} }
// Crea los objetos // Crea los objetos
mInput = new Input(mFileList[11]); mInput = new Input(mAsset->get("gamecontrollerdb.txt"));
// Inicializa SDL // Inicializa SDL
initSDL(); initSDL();
@@ -187,91 +187,19 @@ bool Director::initSDL()
// Crea el indice de ficheros // Crea el indice de ficheros
void Director::setFileList() void Director::setFileList()
{ {
// Inicializa el vector mAsset->add("/media/font/8bithud.png", font);
for (int i = 0; i < MAX_FILE_LIST; i++) mAsset->add("/media/font/8bithud.txt", font);
mFileList[i] = ""; mAsset->add("/media/font/nokia.png", font);
mAsset->add("/media/font/nokia.txt", font);
// Fuentes mAsset->add("/media/font/nokia2.png", font);
mFileList[0] = mExecutablePath + "/" + "../media/font/8bithud.png"; mAsset->add("/media/font/nokia2.txt", font);
mFileList[1] = mExecutablePath + "/" + "../media/font/8bithud.txt"; mAsset->add("/media/font/smb2.png", font);
mFileList[2] = mExecutablePath + "/" + "../media/font/nokia.png"; mAsset->add("/media/font/smb2.txt", font);
mFileList[3] = mExecutablePath + "/" + "../media/font/nokia.txt"; mAsset->add("/media/lang/es_ES.txt", lang);
mFileList[4] = mExecutablePath + "/" + "../media/font/nokia2.png"; mAsset->add("/media/lang/en_UK.txt", lang);
mFileList[5] = mExecutablePath + "/" + "../media/font/nokia2.txt"; mAsset->add("/media/lang/ba_BA.txt", lang);
mFileList[6] = mExecutablePath + "/" + "../media/font/smb2.png"; mAsset->add("/data/gamecontrollerdb.txt", data);
mFileList[7] = mExecutablePath + "/" + "../media/font/smb2.txt"; mAsset->add("/data/config.bin", data, false);
// Textos
mFileList[8] = mExecutablePath + "/" + "../media/lang/es_ES.txt";
mFileList[9] = mExecutablePath + "/" + "../media/lang/en_UK.txt";
mFileList[10] = mExecutablePath + "/" + "../media/lang/ba_BA.txt";
// DATA
mFileList[11] = mExecutablePath + "/" + "../data/gamecontrollerdb.txt";
mFileList[12] = mExecutablePath + "/" + "../data/config.bin";
}
// Comprueba los ficheros del vector de ficheros que coinciden con una ruta dada
bool Director::checkFolder(std::string name, std::string path)
{
bool success = true;
std::string p;
std::string filename;
SDL_RWops *file;
// Comprueba los ficheros de la carpeta
printf("\n>> %s FILES\n", name.c_str());
for (int i = 3; i < MAX_FILE_LIST; i++)
{
if (mFileList[i].find(path.c_str()) != std::string::npos)
{
p = mFileList[i].c_str();
filename = p.substr(p.find_last_of("\\/") + 1);
file = SDL_RWFromFile(p.c_str(), "r+b");
if (file != NULL)
{
printf("Checking file %-20s [OK]\n", filename.c_str());
}
else
{
printf("Checking file %-20s [ERROR]\n", filename.c_str());
success = false;
break;
}
SDL_RWclose(file);
}
}
return success;
}
// Comprueba que todos los ficheros existen
bool Director::checkFileList()
{
bool success = true;
printf("Checking files...\n");
if (success)
success &= checkFolder("MUSIC", "/media/music/");
if (success)
success &= checkFolder("SOUND", "/media/sound/");
if (success)
success &= checkFolder("BITMAP", "/media/gfx/");
if (success)
success &= checkFolder("FONT", "/media/font/");
if (success)
success &= checkFolder("LANG", "/media/lang/");
// Resultado
if (success)
printf("\n** All files OK.\n\n");
else
printf("\n** A file is missing. Exiting.\n\n");
return success;
} }
// Carga el fichero de configuración // Carga el fichero de configuración
@@ -290,7 +218,7 @@ bool Director::loadConfigFile()
// Indicador de éxito en la carga // Indicador de éxito en la carga
bool success = true; bool success = true;
const std::string p = mFileList[12]; const std::string p = mAsset->get("config.bin");
std::string filename = p.substr(p.find_last_of("\\/") + 1); std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b"); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
@@ -359,7 +287,7 @@ bool Director::loadConfigFile()
bool Director::saveConfigFile() bool Director::saveConfigFile()
{ {
bool success = true; bool success = true;
const std::string p = mFileList[12]; const std::string p = mAsset->get("config.bin");
std::string filename = p.substr(p.find_last_of("\\/") + 1); std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b"); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL) if (file != NULL)
@@ -424,7 +352,7 @@ void Director::runTitle()
void Director::runGame() void Director::runGame()
{ {
mGame = new Game(mRenderer, mFileList, mLang, mInput); mGame = new Game(mRenderer, mAsset, mLang, mInput);
setSection(mGame->run()); setSection(mGame->run());
delete mGame; delete mGame;
} }

View File

@@ -1,25 +1,25 @@
#include "game.h" #include "game.h"
// Constructor // Constructor
Game::Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input) Game::Game(SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input)
{ {
// Copia los punteros // Copia los punteros
mRenderer = renderer; mRenderer = renderer;
mFileList = filelist; mAsset = asset;
mLang = lang; mLang = lang;
mInput = input; mInput = input;
mScreen = new Screen(renderer); mScreen = new Screen(renderer);
mEventHandler = new SDL_Event(); mEventHandler = new SDL_Event();
mTextureText = new LTexture(); mTextureText = new LTexture();
mText = new Text(mFileList[5], mTextureText, renderer); mText = new Text(mAsset->get("nokia2.txt"), mTextureText, renderer);
mFade = new Fade(renderer); mFade = new Fade(renderer);
} }
Game::~Game() Game::~Game()
{ {
mRenderer = nullptr; mRenderer = nullptr;
mFileList = nullptr; mAsset = nullptr;
mLang = nullptr; mLang = nullptr;
mInput = nullptr; mInput = nullptr;
@@ -59,7 +59,7 @@ bool Game::loadMedia()
bool success = true; bool success = true;
// Texturas // Texturas
success &= loadTextureFromFile(mTextureText, mFileList[4], mRenderer); success &= loadTextureFromFile(mTextureText, mAsset->get("nokia2.png"), mRenderer);
return success; return success;
} }

View File

@@ -14,6 +14,7 @@
#include "fade.h" #include "fade.h"
#include "lang.h" #include "lang.h"
#include "screen.h" #include "screen.h"
#include "asset.h"
#include "jail_audio.h" #include "jail_audio.h"
#ifndef GAME_H #ifndef GAME_H
@@ -24,8 +25,9 @@ class Game
{ {
private: private:
SDL_Renderer *mRenderer; // El renderizador de la ventana SDL_Renderer *mRenderer; // El renderizador de la ventana
Screen *mScreen; // El objeto encargado de manejar el renderizador
SDL_Event *mEventHandler; // Manejador de eventos SDL_Event *mEventHandler; // Manejador de eventos
Screen *mScreen; // El objeto encargado de manejar el renderizador
Asset *mAsset; // Objeto con la ruta a todos los ficheros de recursos
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
Input *mInput; // Manejador de entrada Input *mInput; // Manejador de entrada
Text *mText; // Fuente para los textos del juego Text *mText; // Fuente para los textos del juego
@@ -43,7 +45,7 @@ private:
public: public:
// Constructor // Constructor
Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input); Game(SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);
// Destructor // Destructor
~Game(); ~Game();

View File

@@ -3,14 +3,15 @@
#include <fstream> #include <fstream>
// Constructor // Constructor
Lang::Lang(std::string *fileList) Lang::Lang(Asset *asset)
{ {
mFileList = fileList; mAsset = asset;
} }
// Destructor // Destructor
Lang::~Lang() Lang::~Lang()
{ {
mAsset = nullptr;
} }
// Inicializa los textos del juego en el idioma seleccionado // Inicializa los textos del juego en el idioma seleccionado
@@ -21,19 +22,19 @@ bool Lang::setLang(Uint8 lang)
switch (lang) switch (lang)
{ {
case es_ES: case es_ES:
file = mFileList[49]; file = mAsset->get("es_ES.txt");
break; break;
case en_UK: case en_UK:
file = mFileList[50]; file = mAsset->get("en_UK.txt");
break; break;
case ba_BA: case ba_BA:
file = mFileList[51]; file = mAsset->get("ba_BA.txt");
break; break;
default: default:
file = mFileList[50]; file = mAsset->get("en_UK.txt");
break; break;
} }

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "ifdefs.h" #include "ifdefs.h"
#include "asset.h"
#include <string> #include <string>
#ifndef LANG_H #ifndef LANG_H
@@ -18,12 +19,12 @@
class Lang class Lang
{ {
private: private:
std::string *mFileList; // Lista de ficheros con los recursos Asset *mAsset;
std::string mTextStrings[MAX_TEXT_STRINGS]; std::string mTextStrings[MAX_TEXT_STRINGS];
public: public:
// Constructor // Constructor
Lang(std::string *fileList); Lang(Asset *asset);
// Destructor // Destructor
~Lang(); ~Lang();