diff --git a/source/common/debug.cpp b/source/common/debug.cpp index 1b68d25..c3b1bc8 100644 --- a/source/common/debug.cpp +++ b/source/common/debug.cpp @@ -1,16 +1,16 @@ #include "debug.h" // Constructor -Debug::Debug(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset) +Debug::Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset) { // Copia la dirección de los objetos - this->resource = resource; this->renderer = renderer; this->screen = screen; this->asset = asset; // Reserva memoria para los punteros - text = new Text("debug.png", asset->get("debug.txt"), resource, renderer); + texture = new Texture(renderer, asset->get("debug.png")); + text = new Text(asset->get("debug.txt"), texture, renderer); // Inicializa variables x = 0; @@ -21,6 +21,7 @@ Debug::Debug(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset * // Destructor Debug::~Debug() { + delete texture; delete text; } diff --git a/source/common/debug.h b/source/common/debug.h index a6ddc4a..3d6f396 100644 --- a/source/common/debug.h +++ b/source/common/debug.h @@ -5,6 +5,7 @@ #include "asset.h" #include "screen.h" #include "text.h" +#include "texture.h" #include "utils.h" #include #include @@ -19,9 +20,9 @@ private: // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla - Resource *resource; // Objeto con los recursos Asset *asset; // Objeto con los ficheros de recursos Text *text; // Objeto encargado de escribir texto en pantalla + Texture *texture; // Textura para el texto // Variables std::vector slot; // Vector con los textos a escribir @@ -32,7 +33,7 @@ private: public: // Constructor - Debug(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset); + Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset); // Destructor ~Debug(); diff --git a/source/common/menu.cpp b/source/common/menu.cpp index 759985e..a4886b3 100644 --- a/source/common/menu.cpp +++ b/source/common/menu.cpp @@ -154,7 +154,7 @@ bool Menu::load(std::string file_path) // Crea el objeto text tan pronto como se pueda. Necesario para añadir items if (font_png != "" && font_txt != "" && !textAllocated) { - text = new Text(font_png, asset->get(font_txt), resource, renderer); + text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer); textAllocated = true; } } @@ -977,6 +977,6 @@ void Menu::setText(std::string font_png, std::string font_txt) { if (!text) { - text = new Text(font_png, font_txt, resource, renderer); + text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer); } } \ No newline at end of file diff --git a/source/common/resource.cpp b/source/common/resource.cpp index 198e1b7..19efac2 100644 --- a/source/common/resource.cpp +++ b/source/common/resource.cpp @@ -16,7 +16,7 @@ void Resource::loadTextures(std::vector list) { texture_t t; t.name = l; - t.texture = new Texture(renderer, asset->get(t.name)); + t.texture = new Texture(renderer, asset->get(l)); textures.push_back(t); } } @@ -33,6 +33,18 @@ void Resource::loadAnimations(std::vector list) } } +// Carga los offsets desde una lista +void Resource::loadOffsets(std::vector list) +{ + for (auto l : list) + { + textOffset_t to; + to.name = l; + to.textFile = new textFile_t(LoadTextFile(asset->get(l))); + offsets.push_back(to); + } +} + // Recarga las texturas void Resource::reLoadTextures() { @@ -62,11 +74,22 @@ void Resource::freeAnimations() animations.clear(); } +// Libera los offsets +void Resource::freeOffsets() +{ + for (auto o : offsets) + { + delete o.textFile; + } + offsets.clear(); +} + // Libera todos los recursos void Resource::free() { freeTextures(); freeAnimations(); + freeOffsets(); } // Obtiene una textura @@ -97,6 +120,22 @@ animatedSprite_t *Resource::getAnimation(std::string name) } } + std::cout << "NOT FOUND: " << name << std::endl; + return nullptr; +} + +// Obtiene un offset +textFile_t *Resource::getOffset(std::string name) +{ + for (auto offset : offsets) + { + if (offset.name.find(name) != std::string::npos) + { + std::cout << "CACHE: " << name << std::endl; + return offset.textFile; + } + } + std::cout << "NOT FOUND: " << name << std::endl; return nullptr; } \ No newline at end of file diff --git a/source/common/resource.h b/source/common/resource.h index 1760c27..fb8cb4d 100644 --- a/source/common/resource.h +++ b/source/common/resource.h @@ -3,6 +3,7 @@ #include #include "animatedsprite.h" #include "asset.h" +#include "text.h" #include "texture.h" #include "utils.h" #include @@ -19,10 +20,17 @@ struct texture_t struct animation_t { - std::string name; // Nombre de la textura + std::string name; // Nombre de la textura animatedSprite_t *animation; // La animación }; +struct textOffset_t +{ + std::string name; // Nombre del offeset + textFile_t *textFile; // Los offsets de la fuente +}; + +// Clase Resource. Almacena recursos de disco en memoria class Resource { private: @@ -34,6 +42,7 @@ private: // Variables std::vector textures; std::vector animations; + std::vector offsets; public: // Constructor @@ -45,6 +54,9 @@ public: // Carga las animaciones desde una lista void loadAnimations(std::vector list); + // Carga los offsets desde una lista + void loadOffsets(std::vector list); + // Recarga las texturas void reLoadTextures(); @@ -54,6 +66,9 @@ public: // Libera las animaciones void freeAnimations(); + // Libera los offsets + void freeOffsets(); + // Libera todos los recursos void free(); @@ -62,6 +77,9 @@ public: // Obtiene una animación animatedSprite_t *getAnimation(std::string name); + + // Obtiene un offset + textFile_t *getOffset(std::string name); }; #endif diff --git a/source/common/text.cpp b/source/common/text.cpp index c072261..3e5d371 100644 --- a/source/common/text.cpp +++ b/source/common/text.cpp @@ -3,17 +3,97 @@ #include #include -// Constructor -Text::Text(std::string bitmapFile, std::string textFile, Resource *resource, SDL_Renderer *renderer) +// Llena una estructuta textFile_t desde un fichero +textFile_t LoadTextFile(std::string file) { - // Copia punteros - this->resource = resource; + textFile_t tf; + // Inicializa a cero el vector con las coordenadas + for (int i = 0; i < 128; ++i) + { + tf.offset[i].x = 0; + tf.offset[i].y = 0; + tf.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); + tf.boxWidth = std::stoi(buffer); + + std::getline(rfile, buffer); + std::getline(rfile, buffer); + tf.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) + { + tf.offset[index++].w = std::stoi(buffer); + } + + // Limpia el buffer + buffer.clear(); + line_read++; + }; + + // Cierra el fichero + printf("Text loaded: %s\n", filename.c_str()); + rfile.close(); + } + + // El fichero no se puede abrir + else + { + printf("Warning: Unable to open %s file\n", filename.c_str()); + } + + // Establece las coordenadas para cada caracter ascii de la cadena y su ancho + for (int i = 32; i < 128; ++i) + { + tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth; + tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight; + } + + return tf; +} + +// Constructor +Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) +{ // Carga los offsets desde el fichero initOffsetFromFile(textFile); // Crea los objetos - texture = resource->getTexture(bitmapFile); + sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); +} + +// Constructor +Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) +{ + // Inicializa variables desde la estructura + boxHeight = textFile->boxHeight; + boxWidth = textFile->boxWidth; + for (int i = 0; i < 128; ++i) + { + offset[i].x = textFile->offset[i].x; + offset[i].y = textFile->offset[i].y; + offset[i].w = textFile->offset[i].w; + } + + // Crea los objetos sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); } @@ -190,5 +270,5 @@ int Text::getCharacterSize() // Recarga la textura void Text::reLoadTexture() { - texture->reLoad(); + sprite->getTexture()->reLoad(); } \ No newline at end of file diff --git a/source/common/text.h b/source/common/text.h index 0649727..7e834aa 100644 --- a/source/common/text.h +++ b/source/common/text.h @@ -1,7 +1,6 @@ #pragma once #include "sprite.h" -#include "resource.h" #include "utils.h" #ifndef TEXT_H @@ -12,21 +11,29 @@ #define TXT_CENTER 4 #define TXT_STROKE 8 +struct offset_t +{ + int x; + int y; + int w; +}; + +struct textFile_t +{ + int boxWidth; // Anchura de la caja de cada caracter en el png + 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 +}; + +// Llena una estructuta textFile_t desde un fichero +textFile_t LoadTextFile(std::string file); + // Clase texto. Pinta texto en pantalla a partir de un bitmap class Text { private: - struct offset_t - { - int x; - int y; - int w; - }; - // Objetos y punteros - Resource *resource; // Objeto con los recursos Sprite *sprite; // Objeto con los graficos para el texto - Texture *texture; // Textura con los bitmaps del texto // Variables int boxWidth; // Anchura de la caja de cada caracter en el png @@ -38,7 +45,8 @@ private: public: // Constructor - Text(std::string bitmapFile, std::string textFile, Resource *resource, SDL_Renderer *renderer); + Text(std::string textFile, Texture *texture, SDL_Renderer *renderer); + Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer); // Destructor ~Text(); diff --git a/source/credits.cpp b/source/credits.cpp index 3782762..64303bb 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -12,7 +12,7 @@ Credits::Credits(SDL_Renderer *renderer, Screen *screen, Resource *resource, Ass // Reserva memoria para los punteros eventHandler = new SDL_Event(); - text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); texture = resource->getTexture("shine.png"); sprite = new AnimatedSprite(renderer, resource->getAnimation("shine.ani")); diff --git a/source/demo.cpp b/source/demo.cpp index 9260ad9..13554d4 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -27,7 +27,7 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as scoreboard = new ScoreBoard(renderer, resource, asset, options, &board); room = new Room(asset->get(currentRoom), renderer, screen, resource, asset, options, itemTracker, &board.items, debug); eventHandler = new SDL_Event(); - text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); // Inicializa el resto de variables counter = 0; diff --git a/source/director.cpp b/source/director.cpp index c63c7f0..770b265 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -36,7 +36,7 @@ Director::Director(std::string path) screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); screen->setBorderColor(borderColor); screen->setVideoMode(options->fullScreenMode); - debug = new Debug(renderer, screen, resource, asset); + debug = new Debug(renderer, screen, asset); music = JA_LoadMusic(asset->get("title.ogg").c_str()); } @@ -181,7 +181,7 @@ bool Director::saveConfig() // Carga los recursos void Director::loadResources(section_t section) { - std::cout << "** LOAD RESOURCES" << std::endl; + std::cout << "** LOAD RESOURCES" << std::endl; if (section.name == SECTION_PROG_LOGO) { @@ -211,6 +211,12 @@ void Director::loadResources(section_t section) textureList.push_back("smb2.png"); resource->loadTextures(textureList); + + // Offsets + std::vector offsetsList; + offsetsList.push_back("smb2.txt"); + + resource->loadOffsets(offsetsList); } else if (section.name == SECTION_PROG_CREDITS) @@ -222,10 +228,17 @@ void Director::loadResources(section_t section) resource->loadTextures(textureList); + // Animaciones std::vector animationList; animationList.push_back("shine"); resource->loadAnimations(animationList); + + // Offsets + std::vector offsetsList; + offsetsList.push_back("smb2.txt"); + + resource->loadOffsets(offsetsList); } else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO) @@ -293,7 +306,7 @@ void Director::loadResources(section_t section) // Animaciones std::vector animationList; - + // Jugador animationList.push_back("player"); @@ -340,9 +353,16 @@ void Director::loadResources(section_t section) animationList.push_back("bat"); resource->loadAnimations(animationList); + + // Offsets + std::vector offsetsList; + offsetsList.push_back("smb2.txt"); + offsetsList.push_back("debug.txt"); + + resource->loadOffsets(offsetsList); } - std::cout << "** RESOURCES LOADED" << std::endl; + std::cout << "** RESOURCES LOADED" << std::endl; } // Asigna variables a partir de dos cadenas diff --git a/source/game.cpp b/source/game.cpp index b87c7a6..a812854 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -33,7 +33,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as room = new Room(asset->get(currentRoom), renderer, screen, resource, asset, options, itemTracker, &board.items, debug); player = new Player(spawnPoint, "player.png", "player.ani", renderer, resource, asset, options, input, room, debug); eventHandler = new SDL_Event(); - text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); music = JA_LoadMusic(asset->get("game.ogg").c_str()); deathSound = JA_LoadSound(asset->get("death.wav").c_str()); test = new Test(renderer, screen, asset, debug); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index cb2b39d..3610d74 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -17,7 +17,7 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset, itemTexture = resource->getTexture("items.png"); sprite = new AnimatedSprite(renderer, resource->getAnimation("player.ani")); sprite->setCurrentAnimation("walk_menu"); - text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); // Inicializa las variables counter = 0; diff --git a/source/title.cpp b/source/title.cpp index 89963c0..d4fb3d6 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -21,7 +21,7 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset * texture = resource->getTexture("loading_screen_color_zxarne.png"); } sprite = new Sprite(0, 0, texture->getWidth(), texture->getHeight(), texture, renderer); - text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); // Inicializa variables counter = 0; diff --git a/source/title.h b/source/title.h index b2e7f24..132d19e 100644 --- a/source/title.h +++ b/source/title.h @@ -27,7 +27,7 @@ private: // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla - Resource *resource; // Objeto con los recursos + Resource *resource; // Objeto con los recursos Asset *asset; // Objeto con los ficheros de recursos SDL_Event *eventHandler; // Manejador de eventos Texture *texture; // Textura con los graficos