diff --git a/source/common/animatedsprite.cpp b/source/common/animatedsprite.cpp index da71ee3..58c7612 100644 --- a/source/common/animatedsprite.cpp +++ b/source/common/animatedsprite.cpp @@ -1,7 +1,7 @@ #include "animatedsprite.h" // Carga la animación desde un fichero -animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath) +animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) { // Inicializa variables animatedSprite_t as; @@ -19,7 +19,10 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath) if (file.good()) { // Procesa el fichero linea a linea - std::cout << "Animation loaded: " << filename << std::endl; + if (verbose) + { + std::cout << "Animation loaded: " << filename << std::endl; + } while (std::getline(file, line)) { // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación @@ -133,12 +136,12 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath) // El fichero no se puede abrir else { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + if (verbose) + { + std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + } } - // Pone un valor por defecto - // setRect({0, 0, frameWidth, frameHeight}); - return as; } @@ -149,11 +152,10 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st setTexture(texture); setRenderer(renderer); - animatedSprite_t as; // Carga las animaciones if (file != "") { - as = loadFromFile(file); + animatedSprite_t as = loadAnimationFromFile(texture, file); // Copia los datos de las animaciones for (auto animation : as.animations) @@ -337,151 +339,6 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) return animation.at(indexA).frames.at(indexF); } -// Carga la animación desde un fichero -animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath) -{ - // Inicializa variables - animatedSprite_t as; - int framesPerRow = 0; - int frameWidth = 0; - int frameHeight = 0; - int maxTiles = 0; - - const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1); - std::ifstream file(filePath); - std::string line; - - // El fichero se puede abrir - if (file.good()) - { - // Procesa el fichero linea a linea - std::cout << "Animation loaded: " << filename << std::endl; - while (std::getline(file, line)) - { - // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación - if (line == "[animation]") - { - animation_t buffer; - buffer.counter = 0; - buffer.currentFrame = 0; - buffer.completed = false; - - do - { - std::getline(file, line); - - // Encuentra la posición del caracter '=' - int pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != (int)line.npos) - { - if (line.substr(0, pos) == "name") - { - buffer.name = line.substr(pos + 1, line.length()); - } - - else if (line.substr(0, pos) == "speed") - { - buffer.speed = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "loop") - { - buffer.loop = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frames") - { - // Se introducen los valores separados por comas en un vector - std::stringstream ss(line.substr(pos + 1, line.length())); - std::string tmp; - SDL_Rect rect = {0, 0, frameWidth, frameHeight}; - while (getline(ss, tmp, ',')) - { - // Comprueba que el tile no sea mayor que el maximo indice permitido - const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp); - rect.x = (numTile % framesPerRow) * frameWidth; - rect.y = (numTile / framesPerRow) * frameHeight; - buffer.frames.push_back(rect); - } - } - - else - { - std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; - } - } - } while (line != "[/animation]"); - - // Añade la animación al vector de animaciones - as.animations.push_back(buffer); - // animation.push_back(buffer); - } - - // En caso contrario se parsea el fichero para buscar las variables y los valores - else - { - // Encuentra la posición del caracter '=' - int pos = line.find("="); - - // Procesa las dos subcadenas - if (pos != (int)line.npos) - { - if (line.substr(0, pos) == "framesPerRow") - { - framesPerRow = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frameWidth") - { - frameWidth = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frameHeight") - { - frameHeight = std::stoi(line.substr(pos + 1, line.length())); - } - - else - { - std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; - } - - // Normaliza valores - if (framesPerRow == 0 && frameWidth > 0) - { - framesPerRow = texture->getWidth() / frameWidth; - } - - if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) - { - const int w = texture->getWidth() / frameWidth; - const int h = texture->getHeight() / frameHeight; - maxTiles = w * h; - } - } - } - } - - // Cierra el fichero - file.close(); - } - // El fichero no se puede abrir - else - { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; - } - - // Pone un valor por defecto - setRect({0, 0, frameWidth, frameHeight}); - - // Añade los punteros - as.texture = texture; - - return as; -} - // Carga la animación desde un vector bool AnimatedSprite::loadFromVector(std::vector *source) { diff --git a/source/common/animatedsprite.h b/source/common/animatedsprite.h index efb30e3..3851e51 100644 --- a/source/common/animatedsprite.h +++ b/source/common/animatedsprite.h @@ -29,7 +29,7 @@ struct animatedSprite_t }; // Carga la animación desde un fichero -animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath); +animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose = false); class AnimatedSprite : public MovingSprite { @@ -80,9 +80,6 @@ public: // Obtiene el indice de la animación a partir del nombre int getIndex(std::string name); - // Carga la animación desde un fichero - animatedSprite_t loadFromFile(std::string filePath); - // Carga la animación desde un vector bool loadFromVector(std::vector *source); diff --git a/source/common/asset.cpp b/source/common/asset.cpp index 711f043..7d13122 100644 --- a/source/common/asset.cpp +++ b/source/common/asset.cpp @@ -2,9 +2,9 @@ #include // Constructor -Asset::Asset(std::string path) +Asset::Asset(std::string executablePath) { - executablePath = path; + this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/")); longestName = 0; verbose = true; } diff --git a/source/common/asset.h b/source/common/asset.h index 4f3762f..a715c79 100644 --- a/source/common/asset.h +++ b/source/common/asset.h @@ -40,7 +40,7 @@ private: bool verbose; // Indica si ha de mostrar información por pantalla // Comprueba que existe un fichero - bool checkFile(std::string path); + bool checkFile(std::string executablePath); // Devuelve el nombre del tipo de recurso std::string getTypeName(int type); diff --git a/source/common/input.cpp b/source/common/input.cpp index f6cdbbf..77388ba 100644 --- a/source/common/input.cpp +++ b/source/common/input.cpp @@ -19,9 +19,6 @@ Input::Input(std::string file) gameControllerBindings.resize(17, gcb); verbose = true; - - // Comprueba si hay un mando conectado - discoverGameController(); } // Asigna inputs a teclas @@ -175,7 +172,7 @@ bool Input::checkAnyInput(int device, int index) return false; } -// Comprueba si hay un mando conectado +// Busca si hay un mando conectado bool Input::discoverGameController() { bool found = false; diff --git a/source/common/input.h b/source/common/input.h index 9ff3c55..6c916ce 100644 --- a/source/common/input.h +++ b/source/common/input.h @@ -59,9 +59,6 @@ private: std::string dbPath; // Ruta al archivo gamecontrollerdb.txt bool verbose; // Indica si ha de mostrar mensajes - // Comprueba si hay un mando conectado - bool discoverGameController(); - public: // Constructor Input(std::string file); @@ -78,6 +75,9 @@ public: // Comprueba si hay almenos un input activo bool checkAnyInput(int device, int index); + // Busca si hay un mando conectado + bool discoverGameController(); + // Comprueba si hay algun mando conectado bool gameControllerFound(); diff --git a/source/common/resource.cpp b/source/common/resource.cpp index 7d8e7ec..65cc912 100644 --- a/source/common/resource.cpp +++ b/source/common/resource.cpp @@ -22,7 +22,7 @@ void Resource::loadTextures(std::vector list) res_texture_t t; t.name = l; - t.texture = new Texture(renderer, asset->get(l)); + t.texture = new Texture(renderer, asset->get(l), options->console); textures.push_back(t); } } @@ -45,7 +45,7 @@ void Resource::loadAnimations(std::vector list) res_animation_t as; as.name = l; - as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(l))); + as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(l), options->console)); animations.push_back(as); } } @@ -57,7 +57,7 @@ void Resource::loadOffsets(std::vector list) { res_textOffset_t to; to.name = l; - to.textFile = new textFile_t(LoadTextFile(asset->get(l))); + to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console)); offsets.push_back(to); } } @@ -69,7 +69,7 @@ void Resource::loadTileMaps(std::vector list) { res_tileMap_t tm; tm.name = l; - tm.tileMap = new std::vector(loadRoomTileFile(asset->get(l))); + tm.tileMap = new std::vector(loadRoomTileFile(asset->get(l), options->console)); tileMaps.push_back(tm); } } @@ -81,7 +81,7 @@ void Resource::loadRooms(std::vector list) { res_room_t r; r.name = l; - r.room = new room_t(loadRoomFile(asset->get(l))); + r.room = new room_t(loadRoomFile(asset->get(l), options->console)); r.room->tileMap = getTileMap(r.room->tileMapFile); for (auto &e : r.room->enemies) { diff --git a/source/common/text.cpp b/source/common/text.cpp index bd7dc69..901d38a 100644 --- a/source/common/text.cpp +++ b/source/common/text.cpp @@ -4,7 +4,7 @@ #include // Llena una estructuta textFile_t desde un fichero -textFile_t LoadTextFile(std::string file) +textFile_t LoadTextFile(std::string file, bool verbose) { textFile_t tf; @@ -50,14 +50,20 @@ textFile_t LoadTextFile(std::string file) }; // Cierra el fichero - std::cout << "Text loaded: " << filename.c_str() << std::endl; + if (verbose) + { + std::cout << "Text loaded: " << filename.c_str() << std::endl; + } rfile.close(); } // El fichero no se puede abrir else { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + if (verbose) + { + std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + } } // Establece las coordenadas para cada caracter ascii de la cadena y su ancho @@ -74,7 +80,17 @@ textFile_t LoadTextFile(std::string file) Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) { // Carga los offsets desde el fichero - initOffsetFromFile(textFile); + textFile_t tf = LoadTextFile(textFile); + + // Inicializa variables desde la estructura + boxHeight = tf.boxHeight; + boxWidth = tf.boxWidth; + for (int i = 0; i < 128; ++i) + { + offset[i].x = tf.offset[i].x; + offset[i].y = tf.offset[i].y; + offset[i].w = tf.offset[i].w; + } // Crea los objetos sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); @@ -198,69 +214,6 @@ int Text::lenght(std::string text, int kerning) return shift - kerning; } -// Inicializa el vector de offsets desde un fichero -void Text::initOffsetFromFile(std::string file) -{ - // Inicializa a cero el vector con las coordenadas - for (int i = 0; i < 128; ++i) - { - offset[i].x = 0; - offset[i].y = 0; - offset[i].w = 0; - } - - // Abre el fichero para leer los valores - const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str(); - std::ifstream rfile(file); - - if (rfile.is_open() && rfile.good()) - { - std::string buffer; - - // Lee los dos primeros valores del fichero - std::getline(rfile, buffer); - std::getline(rfile, buffer); - boxWidth = std::stoi(buffer); - - std::getline(rfile, buffer); - std::getline(rfile, buffer); - boxHeight = std::stoi(buffer); - - // lee el resto de datos del fichero - int index = 32; - int line_read = 0; - while (std::getline(rfile, buffer)) - { - // Almacena solo las lineas impares - if (line_read % 2 == 1) - { - offset[index++].w = std::stoi(buffer); - } - - // Limpia el buffer - buffer.clear(); - line_read++; - }; - - // Cierra el fichero - std::cout << "Text loaded: " << filename.c_str() << std::endl; - rfile.close(); - } - - // El fichero no se puede abrir - else - { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; - } - - // Establece las coordenadas para cada caracter ascii de la cadena y su ancho - for (int i = 32; i < 128; ++i) - { - offset[i].x = ((i - 32) % 15) * boxWidth; - offset[i].y = ((i - 32) / 15) * boxHeight; - } -} - // Devuelve el valor de la variable int Text::getCharacterSize() { diff --git a/source/common/text.h b/source/common/text.h index 7e834aa..b6703b2 100644 --- a/source/common/text.h +++ b/source/common/text.h @@ -26,7 +26,7 @@ struct textFile_t }; // Llena una estructuta textFile_t desde un fichero -textFile_t LoadTextFile(std::string file); +textFile_t LoadTextFile(std::string file, bool verbose = false); // Clase texto. Pinta texto en pantalla a partir de un bitmap class Text @@ -40,9 +40,6 @@ private: int boxHeight; // Altura de la caja de cada caracter en el png offset_t offset[128]; // Vector con las posiciones y ancho de cada letra - // Inicializa el vector de offsets desde un fichero - void initOffsetFromFile(std::string file); - public: // Constructor Text(std::string textFile, Texture *texture, SDL_Renderer *renderer); diff --git a/source/common/texture.cpp b/source/common/texture.cpp index dd0597d..0c96e92 100644 --- a/source/common/texture.cpp +++ b/source/common/texture.cpp @@ -5,7 +5,7 @@ #include // Constructor -Texture::Texture(SDL_Renderer *renderer, std::string path) +Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) { // Copia punteros this->renderer = renderer; @@ -19,7 +19,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path) // Carga el fichero en la textura if (path != "") { - loadFromFile(path, renderer); + loadFromFile(path, renderer, verbose); } } @@ -31,7 +31,7 @@ Texture::~Texture() } // Carga una imagen desde un fichero -bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) +bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) { const std::string filename = path.substr(path.find_last_of("\\/") + 1); int req_format = STBI_rgb_alpha; @@ -44,7 +44,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) } else { - std::cout << "Image loaded: " << filename.c_str() << std::endl; + if (verbose) + { + std::cout << "Image loaded: " << filename.c_str() << std::endl; + } } int depth, pitch; @@ -72,7 +75,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format); if (loadedSurface == nullptr) { - std::cout << "Unable to load image " << path.c_str() << std::endl; + if (verbose) + { + std::cout << "Unable to load image " << path.c_str() << std::endl; + } } else { @@ -80,7 +86,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); if (newTexture == nullptr) { - std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl; + if (verbose) + { + std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl; + } } else { diff --git a/source/common/texture.h b/source/common/texture.h index 52e8ce9..487e2a8 100644 --- a/source/common/texture.h +++ b/source/common/texture.h @@ -21,13 +21,13 @@ private: public: // Constructor - Texture(SDL_Renderer *renderer, std::string path = ""); + Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false); // Destructor ~Texture(); // Carga una imagen desde un fichero - bool loadFromFile(std::string path, SDL_Renderer *renderer); + bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false); // Crea una textura en blanco bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); diff --git a/source/director.cpp b/source/director.cpp index ecea013..1e62eeb 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -11,9 +11,15 @@ Director::Director(int argc, char *argv[]) section.name = SECTION_PROG_GAME; + // Crea e inicializa las opciones del programa + iniOptions(); + + // Comprueba los parametros del programa + checkProgramArguments(argc, argv); + // Crea el objeto que controla los ficheros de recursos - executablePath = argv[0]; - asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/"))); + asset = new Asset(executablePath); + asset->setVerbose(options->console); // Si falta algún fichero no inicia el programa if (!setFileList()) @@ -21,13 +27,6 @@ Director::Director(int argc, char *argv[]) section.name = SECTION_PROG_QUIT; } - // Crea e inicializa las opciones del programa - iniOptions(); - - // Comprueba los parametros del programa - checkProgramArguments(argc, argv); - asset->setVerbose(options->console); - // Inicializa variables desde el fichero de configuración loadConfig(); @@ -92,7 +91,11 @@ void Director::iniOptions() // Comprueba los parametros del programa void Director::checkProgramArguments(int argc, char *argv[]) { - for (int i = 0; i < argc; ++i) + // Establece la ruta del programa + executablePath = argv[0]; + + // Comprueba el resto de parametros + for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--console") == 0) { @@ -661,6 +664,9 @@ void Director::initInput() // Establece si ha de mostrar mensajes input->setVerbose(options->console); + // Busca si hay un mando conectado + input->discoverGameController(); + // Asigna inputs a teclas input->bindKey(INPUT_UP, SDL_SCANCODE_UP); input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); diff --git a/source/room.cpp b/source/room.cpp index b0a0067..ecaa419 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -4,7 +4,7 @@ #include // Carga las variables y texturas desde un fichero de mapa de tiles -std::vector loadRoomTileFile(std::string file_path) +std::vector loadRoomTileFile(std::string file_path, bool verbose) { std::vector tileMapFile; const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1); @@ -37,20 +37,26 @@ std::vector loadRoomTileFile(std::string file_path) } // Cierra el fichero - std::cout << "TileMap loaded: " << filename.c_str() << std::endl; + if (verbose) + { + std::cout << "TileMap loaded: " << filename.c_str() << std::endl; + } file.close(); } else { // El fichero no se puede abrir - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + if (verbose) + { + std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + } } return tileMapFile; } // Carga las variables desde un fichero de mapa -room_t loadRoomFile(std::string file_path) +room_t loadRoomFile(std::string file_path, bool verbose) { room_t room; room.itemColor1 = "yellow"; @@ -84,7 +90,10 @@ room_t loadRoomFile(std::string file_path) // Procesa las dos subcadenas if (!setEnemy(&enemy, line.substr(0, pos), line.substr(pos + 1, line.length()))) { - std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; + if (verbose) + { + std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; + } } } while (line != "[/enemy]"); @@ -110,6 +119,7 @@ room_t loadRoomFile(std::string file_path) // Procesa las dos subcadenas if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) { + if (verbose) { std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; } @@ -129,6 +139,7 @@ room_t loadRoomFile(std::string file_path) // Procesa las dos subcadenas if (!setVars(&room, line.substr(0, pos), line.substr(pos + 1, line.length()))) { + if (verbose) { std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; } @@ -137,6 +148,7 @@ room_t loadRoomFile(std::string file_path) } // Cierra el fichero + if (verbose) { std::cout << "Room loaded: " << filename.c_str() << std::endl; } diff --git a/source/room.h b/source/room.h index 061472b..81764eb 100644 --- a/source/room.h +++ b/source/room.h @@ -56,10 +56,10 @@ struct room_t }; // Carga las variables desde un fichero de mapa -room_t loadRoomFile(std::string file); +room_t loadRoomFile(std::string file, bool verbose = false); // Carga las variables y texturas desde un fichero de mapa de tiles -std::vector loadRoomTileFile(std::string file_path); +std::vector loadRoomTileFile(std::string file_path, bool verbose = false); // Asigna variables a partir de dos cadenas bool setVars(room_t *room, std::string var, std::string value);