From 94a06dcc2d3b898d46708d4fe31dddaf85dd31e0 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 8 Sep 2021 18:26:22 +0200 Subject: [PATCH] =?UTF-8?q?Primera=20implementaci=C3=B3n=20de=20la=20clase?= =?UTF-8?q?=20asset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/asset.cpp | 51 ++++++++++++++++++- source/asset.h | 34 ++++++++----- source/director.cpp | 118 +++++++++----------------------------------- source/game.cpp | 10 ++-- source/game.h | 6 ++- source/lang.cpp | 13 ++--- source/lang.h | 5 +- 7 files changed, 114 insertions(+), 123 deletions(-) diff --git a/source/asset.cpp b/source/asset.cpp index f473907..11884ab 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -3,6 +3,7 @@ // Constructor Asset::Asset(std::string path) { + mExecutablePath = path; } // Destructor @@ -11,10 +12,56 @@ Asset::~Asset() } // 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; - temp.file = file; + temp.file = mExecutablePath + "/.." + file; temp.type = type; + temp.required = required; 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; } \ No newline at end of file diff --git a/source/asset.h b/source/asset.h index 187b7ef..21e5f7e 100644 --- a/source/asset.h +++ b/source/asset.h @@ -6,14 +6,15 @@ #ifndef ASSET_H #define ASSET_H -enum assetType {bitmap, music, sound, data, font, lang}; - -#define ASSET_TYPE_BITMAP 0 -#define ASSET_TYPE_MUSIC 1 -#define ASSET_TYPE_SOUND 2 -#define ASSET_TYPE_DATA 3 -#define ASSET_TYPE_FONT 4 -#define ASSET_TYPE_LANG 5 +enum assetType +{ + bitmap, + music, + sound, + data, + font, + lang +}; // Clase Asset class Asset @@ -22,11 +23,16 @@ private: // Estructura para definir un item struct item_t { - std::string file; - enum assetType type; + 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 }; std::vector mFileList; + std::string mExecutablePath; + + // Comprueba que existe un fichero + bool checkFile(std::string path); public: // Constructor @@ -36,7 +42,13 @@ public: ~Asset(); // 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 diff --git a/source/director.cpp b/source/director.cpp index 17edf17..ff44267 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -10,19 +10,19 @@ Director::Director(std::string path) // Inicializa la ruta setExecutablePath(path); - // Establece la lista de ficheros - setFileList(); - // Crea el objeto que controla los ficheros de recursos 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; - if (!checkFileList()) + if (!mAsset->check()) section = PROG_SECTION_QUIT; // 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 mOptions = new options_t; @@ -39,7 +39,7 @@ Director::Director(std::string path) } // Crea los objetos - mInput = new Input(mFileList[11]); + mInput = new Input(mAsset->get("gamecontrollerdb.txt")); // Inicializa SDL initSDL(); @@ -187,91 +187,19 @@ bool Director::initSDL() // Crea el indice de ficheros void Director::setFileList() { - // Inicializa el vector - for (int i = 0; i < MAX_FILE_LIST; i++) - mFileList[i] = ""; - - // Fuentes - mFileList[0] = mExecutablePath + "/" + "../media/font/8bithud.png"; - mFileList[1] = mExecutablePath + "/" + "../media/font/8bithud.txt"; - mFileList[2] = mExecutablePath + "/" + "../media/font/nokia.png"; - mFileList[3] = mExecutablePath + "/" + "../media/font/nokia.txt"; - mFileList[4] = mExecutablePath + "/" + "../media/font/nokia2.png"; - mFileList[5] = mExecutablePath + "/" + "../media/font/nokia2.txt"; - mFileList[6] = mExecutablePath + "/" + "../media/font/smb2.png"; - mFileList[7] = mExecutablePath + "/" + "../media/font/smb2.txt"; - - // 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; + mAsset->add("/media/font/8bithud.png", font); + mAsset->add("/media/font/8bithud.txt", font); + mAsset->add("/media/font/nokia.png", font); + mAsset->add("/media/font/nokia.txt", font); + mAsset->add("/media/font/nokia2.png", font); + mAsset->add("/media/font/nokia2.txt", font); + mAsset->add("/media/font/smb2.png", font); + mAsset->add("/media/font/smb2.txt", font); + mAsset->add("/media/lang/es_ES.txt", lang); + mAsset->add("/media/lang/en_UK.txt", lang); + mAsset->add("/media/lang/ba_BA.txt", lang); + mAsset->add("/data/gamecontrollerdb.txt", data); + mAsset->add("/data/config.bin", data, false); } // Carga el fichero de configuración @@ -290,7 +218,7 @@ bool Director::loadConfigFile() // Indicador de éxito en la carga 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); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b"); @@ -359,7 +287,7 @@ bool Director::loadConfigFile() bool Director::saveConfigFile() { 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); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b"); if (file != NULL) @@ -424,7 +352,7 @@ void Director::runTitle() void Director::runGame() { - mGame = new Game(mRenderer, mFileList, mLang, mInput); + mGame = new Game(mRenderer, mAsset, mLang, mInput); setSection(mGame->run()); delete mGame; } diff --git a/source/game.cpp b/source/game.cpp index a2aef64..bc1ac00 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,25 +1,25 @@ #include "game.h" // 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 mRenderer = renderer; - mFileList = filelist; + mAsset = asset; mLang = lang; mInput = input; mScreen = new Screen(renderer); mEventHandler = new SDL_Event(); mTextureText = new LTexture(); - mText = new Text(mFileList[5], mTextureText, renderer); + mText = new Text(mAsset->get("nokia2.txt"), mTextureText, renderer); mFade = new Fade(renderer); } Game::~Game() { mRenderer = nullptr; - mFileList = nullptr; + mAsset = nullptr; mLang = nullptr; mInput = nullptr; @@ -59,7 +59,7 @@ bool Game::loadMedia() bool success = true; // Texturas - success &= loadTextureFromFile(mTextureText, mFileList[4], mRenderer); + success &= loadTextureFromFile(mTextureText, mAsset->get("nokia2.png"), mRenderer); return success; } diff --git a/source/game.h b/source/game.h index d81bbe5..8d95492 100644 --- a/source/game.h +++ b/source/game.h @@ -14,6 +14,7 @@ #include "fade.h" #include "lang.h" #include "screen.h" +#include "asset.h" #include "jail_audio.h" #ifndef GAME_H @@ -24,8 +25,9 @@ class Game { private: SDL_Renderer *mRenderer; // El renderizador de la ventana - Screen *mScreen; // El objeto encargado de manejar el renderizador 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 Input *mInput; // Manejador de entrada Text *mText; // Fuente para los textos del juego @@ -43,7 +45,7 @@ private: public: // Constructor - Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input); + Game(SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input); // Destructor ~Game(); diff --git a/source/lang.cpp b/source/lang.cpp index c739dd3..c89f096 100644 --- a/source/lang.cpp +++ b/source/lang.cpp @@ -3,14 +3,15 @@ #include // Constructor -Lang::Lang(std::string *fileList) +Lang::Lang(Asset *asset) { - mFileList = fileList; + mAsset = asset; } // Destructor Lang::~Lang() { + mAsset = nullptr; } // Inicializa los textos del juego en el idioma seleccionado @@ -21,19 +22,19 @@ bool Lang::setLang(Uint8 lang) switch (lang) { case es_ES: - file = mFileList[49]; + file = mAsset->get("es_ES.txt"); break; case en_UK: - file = mFileList[50]; + file = mAsset->get("en_UK.txt"); break; case ba_BA: - file = mFileList[51]; + file = mAsset->get("ba_BA.txt"); break; default: - file = mFileList[50]; + file = mAsset->get("en_UK.txt"); break; } diff --git a/source/lang.h b/source/lang.h index 228e1c9..d30cf65 100644 --- a/source/lang.h +++ b/source/lang.h @@ -1,5 +1,6 @@ #pragma once #include "ifdefs.h" +#include "asset.h" #include #ifndef LANG_H @@ -18,12 +19,12 @@ class Lang { private: - std::string *mFileList; // Lista de ficheros con los recursos + Asset *mAsset; std::string mTextStrings[MAX_TEXT_STRINGS]; public: // Constructor - Lang(std::string *fileList); + Lang(Asset *asset); // Destructor ~Lang();