diff --git a/source/demo.cpp b/source/demo.cpp index 4c42b10e..d95c619f 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -41,9 +41,9 @@ Demo::Demo() currentRoom = rooms[roomIndex]; // Crea los objetos - itemTracker = std::make_shared(); + ItemTracker::init(); scoreboard = std::make_shared(&board); - room = std::make_shared(resource->getRoom(currentRoom), itemTracker, &board.items, false); + room = std::make_shared(resource->getRoom(currentRoom), &board.items, false); text = resource->getText("smb2"); // Inicializa el resto de variables @@ -62,6 +62,12 @@ Demo::Demo() options.section.subsection = Subsection::NONE; } +// Destructor +Demo::~Demo() +{ + ItemTracker::destroy(); +} + // Comprueba los eventos de la cola void Demo::checkEvents() { @@ -183,7 +189,7 @@ bool Demo::changeRoom(std::string file) if (asset->get(file) != "") { // Crea un objeto habitación a partir del fichero - room = std::make_shared(resource->getRoom(file), itemTracker, &board.items, false); + room = std::make_shared(resource->getRoom(file), &board.items, false); // Pone el color del marcador en función del color del borde de la habitación setScoreBoardColor(); diff --git a/source/demo.h b/source/demo.h index d074b6a3..5f0c613d 100644 --- a/source/demo.h +++ b/source/demo.h @@ -28,7 +28,6 @@ private: std::shared_ptr room; // Objeto encargado de gestionar cada habitación del juego std::shared_ptr text; // Objeto para los textos del juego std::shared_ptr scoreboard; // Objeto encargado de gestionar el marcador - std::shared_ptr itemTracker; // Lleva el control de los objetos recogidos // Variables Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa @@ -75,7 +74,7 @@ public: Demo(); // Destructor - ~Demo() = default; + ~Demo(); // Bucle para el juego void run(); diff --git a/source/item.cpp b/source/item.cpp index c42ac7bb..14b88efa 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -1,59 +1,57 @@ #include "item.h" +#include "resource.h" #include "sprite.h" // Para Sprite #include "texture.h" // Para Texture // Constructor -Item::Item(item_t item) +Item::Item(ItemData item) { constexpr int ITEMSIZE = 8; // Crea objetos; - sprite = std::make_shared(item.texture, item.x, item.y, ITEMSIZE, ITEMSIZE); + sprite_ = std::make_shared(Resource::get()->getTexture(item.tile_set_file), item.x, item.y, ITEMSIZE, ITEMSIZE); // Inicia variables - sprite->setClip((item.tile % 10) * ITEMSIZE, (item.tile / 10) * ITEMSIZE, ITEMSIZE, ITEMSIZE); - collider = sprite->getRect(); - colorChangeSpeed = 4; - counter = item.counter * colorChangeSpeed; + sprite_->setClip((item.tile % 10) * ITEMSIZE, (item.tile / 10) * ITEMSIZE, ITEMSIZE, ITEMSIZE); + collider_ = sprite_->getRect(); + change_color_speed = 4; + counter_ = item.counter * change_color_speed; // Inicializa los colores - Color c = item.color1; - color.push_back(c); - color.push_back(c); + color_.push_back(item.color1); + color_.push_back(item.color1); - c = item.color2; - color.push_back(c); - color.push_back(c); + color_.push_back(item.color2); + color_.push_back(item.color2); } // Pinta el objeto en pantalla void Item::render() { - const int index = (counter / colorChangeSpeed) % color.size(); - sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b); - sprite->render(); - sprite->getTexture()->setColor(255, 255, 255); + const int index = (counter_ / change_color_speed) % color_.size(); + sprite_->getTexture()->setColor(color_[index].r, color_[index].g, color_[index].b); + sprite_->render(); + sprite_->getTexture()->setColor(255, 255, 255); } // Obtiene su ubicación SDL_Point Item::getPos() { - const SDL_Point p = {sprite->getX(), sprite->getY()}; + const SDL_Point p = {sprite_->getX(), sprite_->getY()}; return p; } - // Asigna los colores del objeto void Item::setColors(Color col1, Color col2) { // Reinicializa el vector de colores - color.clear(); + color_.clear(); // Añade el primer color - color.push_back(col1); - color.push_back(col1); + color_.push_back(col1); + color_.push_back(col1); // Añade el segundo color - color.push_back(col2); - color.push_back(col2); + color_.push_back(col2); + color_.push_back(col2); } \ No newline at end of file diff --git a/source/item.h b/source/item.h index 88ac3de8..5857c831 100644 --- a/source/item.h +++ b/source/item.h @@ -1,45 +1,43 @@ #pragma once -#include // for SDL_Rect, SDL_Point -#include // for SDL_Renderer -#include // for shared_ptr, __shared_ptr_access -#include // for string -#include // for vector -#include "sprite.h" // for Sprite -#include "texture.h" // for Texture -#include "utils.h" // for Color +#include // for SDL_Rect, SDL_Point +#include // for SDL_Renderer +#include // for shared_ptr, __shared_ptr_access +#include // for string +#include // for vector +#include "sprite.h" // for Sprite +#include "texture.h" // for Texture +#include "utils.h" // for Color -struct item_t +struct ItemData { - SDL_Renderer *renderer; // El renderizador de la ventana - std::shared_ptr texture; // Textura con los gráficos del item - std::string tileSetFile; // Ruta al fichero con los gráficos del item - int x; // Posición del item en pantalla - int y; // Posición del item en pantalla - int tile; // Número de tile dentro de la textura - int counter; // Contador inicial. Es el que lo hace cambiar de color - Color color1; // Uno de los dos colores que se utiliza para el item - Color color2; // Uno de los dos colores que se utiliza para el item + std::string tile_set_file; // Ruta al fichero con los gráficos del item + int x; // Posición del item en pantalla + int y; // Posición del item en pantalla + int tile; // Número de tile dentro de la textura + int counter; // Contador inicial. Es el que lo hace cambiar de color + Color color1; // Uno de los dos colores que se utiliza para el item + Color color2; // Uno de los dos colores que se utiliza para el item - // Constructor por defecto - item_t() : renderer(nullptr), texture(nullptr), x(0), y(0), tile(0), counter(0), color1(), color2() {} + // Constructor + ItemData() : x(0), y(0), tile(0), counter(0), color1(), color2() {} }; class Item { private: // Objetos y punteros - std::shared_ptr sprite; // Sprite del objeto + std::shared_ptr sprite_; // Sprite del objeto // Variables - std::vector color; // Vector con los colores del objeto - int counter; // Contador interno - SDL_Rect collider; // Rectangulo de colisión - int colorChangeSpeed; // Cuanto mas alto, mas tarda en cambiar de color + std::vector color_; // Vector con los colores del objeto + int counter_; // Contador interno + SDL_Rect collider_; // Rectangulo de colisión + int change_color_speed; // Cuanto mas alto, mas tarda en cambiar de color public: // Constructor - Item(item_t item); + Item(ItemData item); // Destructor ~Item() = default; @@ -48,18 +46,17 @@ public: void render(); // Actualiza las variables del objeto - void update() { counter++; } + void update() { counter_++; } // Obtiene el rectangulo de colision del objeto - SDL_Rect &getCollider() { return collider; } + SDL_Rect &getCollider() { return collider_; } // Obtiene su ubicación SDL_Point getPos(); // Recarga la textura - void reLoadTexture() { sprite->getTexture()->reLoad(); } + void reLoadTexture() { sprite_->getTexture()->reLoad(); } // Asigna los colores del objeto void setColors(Color col1, Color col2); - }; \ No newline at end of file diff --git a/source/item_tracker.h b/source/item_tracker.h index e84bac33..ac5aad97 100644 --- a/source/item_tracker.h +++ b/source/item_tracker.h @@ -4,13 +4,13 @@ #include // Para string, basic_string #include // Para vector -struct ItemData +struct ItemTrackerData { std::string name; // Nombre de la habitación donde se encuentra el objeto std::vector pos; // Lista de objetos cogidos de la habitación // Constructor - ItemData(const std::string& name, const SDL_Point& position) + ItemTrackerData(const std::string& name, const SDL_Point& position) : name(name) { pos.push_back(position); @@ -24,7 +24,7 @@ private: static ItemTracker *item_tracker_; // Variables - std::vector list; // Lista con todos los objetos recogidos + std::vector list; // Lista con todos los objetos recogidos // Busca una entrada en la lista por nombre int findByName(std::string name); diff --git a/source/room.cpp b/source/room.cpp index f279184c..578e150b 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -15,9 +15,11 @@ #include "sprite.h" // Para Sprite #include "texture.h" // Para Texture #include "options.h" +#include "utils.h" // Para stringToBool, stringToColor +#include "resource.h" // Carga las variables y texturas desde un fichero de mapa de tiles -std::vector loadRoomTileFile(std::string file_path, bool verbose) +std::vector loadRoomTileFile(const std::string &file_path, bool verbose) { std::vector tileMapFile; const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1); @@ -69,7 +71,7 @@ std::vector loadRoomTileFile(std::string file_path, bool verbose) } // Carga las variables desde un fichero de mapa -RoomData loadRoomFile(std::string file_path, bool verbose) +RoomData loadRoomFile(const std::string &file_path, bool verbose) { RoomData room; room.item_color1 = "yellow"; @@ -104,13 +106,13 @@ RoomData loadRoomFile(std::string file_path, bool verbose) int pos = line.find("="); // Procesa las dos subcadenas - if (!setEnemy(&enemy, line.substr(0, pos), line.substr(pos + 1, line.length()))) - { + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1, line.length()); + if (!setEnemy(&enemy, key, value)) if (verbose) { std::cout << "Warning: file " << fileName.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; } - } } while (line != "[/enemy]"); // Añade el enemigo al vector de enemigos @@ -120,7 +122,7 @@ RoomData loadRoomFile(std::string file_path, bool verbose) // Si la linea contiene el texto [item] se realiza el proceso de carga de un item else if (line == "[item]") { - item_t item; + ItemData item; item.counter = 0; item.color1 = stringToColor(Palette::ZXSPECTRUM, "yellow"); item.color2 = stringToColor(Palette::ZXSPECTRUM, "magenta"); @@ -133,7 +135,9 @@ RoomData loadRoomFile(std::string file_path, bool verbose) int pos = line.find("="); // Procesa las dos subcadenas - if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1, line.length()); + if (!setItem(&item, key, value)) { if (verbose) { @@ -153,7 +157,9 @@ RoomData loadRoomFile(std::string file_path, bool verbose) int pos = line.find("="); // Procesa las dos subcadenas - if (!setVars(&room, line.substr(0, pos), line.substr(pos + 1, line.length()))) + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1, line.length()); + if (!setRoom(&room, key, value)) { if (verbose) { @@ -181,226 +187,210 @@ RoomData loadRoomFile(std::string file_path, bool verbose) return room; } -// Asigna variables a partir de dos cadenas -bool setVars(RoomData *room, std::string var, std::string value) +// Asigna variables a una estructura RoomData +bool setRoom(RoomData *room, const std::string &key, const std::string &value) { // Indicador de éxito en la asignación bool success = true; - if (var == "tileMapFile") + try { - room->tile_map_file = value; - } - - else if (var == "name") - { - room->name = value; - } - - else if (var == "bgColor") - { - room->bg_color = value; - } - - else if (var == "border") - { - room->border_color = value; - } - - else if (var == "itemColor1") - { - room->item_color1 = value; - } - - else if (var == "itemColor2") - { - room->item_color2 = value; - } - - else if (var == "tileSetFile") - { - room->tile_set_file = value; - } - - else if (var == "roomUp") - { - room->room_top = value; - } - - else if (var == "roomDown") - { - room->room_bottom = value; - } - - else if (var == "roomLeft") - { - room->room_left = value; - } - - else if (var == "roomRight") - { - room->room_right = value; - } - - else if (var == "autoSurface") - { - if (value == "right") + if (key == "tileMapFile") { - room->auto_surface_direction = 1; + room->tile_map_file = value; + } + else if (key == "name") + { + room->name = value; + } + else if (key == "bgColor") + { + room->bg_color = value; + } + else if (key == "border") + { + room->border_color = value; + } + else if (key == "itemColor1") + { + room->item_color1 = value; + } + else if (key == "itemColor2") + { + room->item_color2 = value; + } + else if (key == "tileSetFile") + { + room->tile_set_file = value; + } + else if (key == "roomUp") + { + room->room_top = value; + } + else if (key == "roomDown") + { + room->room_bottom = value; + } + else if (key == "roomLeft") + { + room->room_left = value; + } + else if (key == "roomRight") + { + room->room_right = value; + } + else if (key == "autoSurface") + { + room->auto_surface_direction = (value == "right") ? 1 : -1; + } + else if (key == "" || key.substr(0, 1) == "#") + { + // No se realiza ninguna acción para estas claves } else { - room->auto_surface_direction = -1; + success = false; } } - - else if (var == "" || var.substr(0, 1) == "#") - { - } - - else + catch (const std::exception &e) { + std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl; success = false; } return success; } -// Asigna variables a una estructura enemy_t -bool setEnemy(EnemyData *enemy, std::string var, std::string value) +// Asigna variables a una estructura EnemyData +bool setEnemy(EnemyData *enemy, const std::string &key, const std::string &value) { // Indicador de éxito en la asignación bool success = true; - if (var == "tileSetFile") + try { - enemy->texture_path = value; + if (key == "tileSetFile") + { + enemy->texture_path = value; + } + else if (key == "animation") + { + enemy->animation_path = value; + } + else if (key == "width") + { + enemy->w = std::stoi(value); + } + else if (key == "height") + { + enemy->h = std::stoi(value); + } + else if (key == "x") + { + enemy->x = std::stof(value) * BLOCK; + } + else if (key == "y") + { + enemy->y = std::stof(value) * BLOCK; + } + else if (key == "vx") + { + enemy->vx = std::stof(value); + } + else if (key == "vy") + { + enemy->vy = std::stof(value); + } + else if (key == "x1") + { + enemy->x1 = std::stoi(value) * BLOCK; + } + else if (key == "x2") + { + enemy->x2 = std::stoi(value) * BLOCK; + } + else if (key == "y1") + { + enemy->y1 = std::stoi(value) * BLOCK; + } + else if (key == "y2") + { + enemy->y2 = std::stoi(value) * BLOCK; + } + else if (key == "flip") + { + enemy->flip = stringToBool(value); + } + else if (key == "mirror") + { + enemy->mirror = stringToBool(value); + } + else if (key == "color") + { + enemy->color = value; + } + else if (key == "frame") + { + enemy->frame = std::stoi(value); + } + else if (key == "[/enemy]" || key == "tileSetFile" || key.substr(0, 1) == "#") + { + // No se realiza ninguna acción para estas claves + } + else + { + success = false; + } } - - else if (var == "animation") - { - enemy->animation_path = value; - } - - else if (var == "width") - { - enemy->w = std::stof(value); - } - - else if (var == "height") - { - enemy->h = std::stof(value); - } - - else if (var == "x") - { - enemy->x = std::stof(value) * BLOCK; - } - - else if (var == "y") - { - enemy->y = std::stof(value) * BLOCK; - } - - else if (var == "vx") - { - enemy->vx = std::stof(value); - } - - else if (var == "vy") - { - enemy->vy = std::stof(value); - } - - else if (var == "x1") - { - enemy->x1 = std::stoi(value) * BLOCK; - } - - else if (var == "x2") - { - enemy->x2 = std::stoi(value) * BLOCK; - } - - else if (var == "y1") - { - enemy->y1 = std::stoi(value) * BLOCK; - } - - else if (var == "y2") - { - enemy->y2 = std::stoi(value) * BLOCK; - } - - else if (var == "flip") - { - enemy->flip = stringToBool(value); - } - - else if (var == "mirror") - { - enemy->mirror = stringToBool(value); - } - - else if (var == "color") - { - enemy->color = value; - } - - else if (var == "frame") - { - enemy->frame = std::stoi(value); - } - - else if (var == "[/enemy]" || var == "tileSetFile" || var.substr(0, 1) == "#") - { - } - - else + catch (const std::exception &e) { + std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl; success = false; } return success; } -// Asigna variables a una estructura item_t -bool setItem(item_t *item, std::string var, std::string value) +// Asigna variables a una estructura ItemData +bool setItem(ItemData *item, const std::string &key, const std::string &value) { // Indicador de éxito en la asignación bool success = true; - if (var == "tileSetFile") + try { - item->tileSetFile = value; + if (key == "tileSetFile") + { + item->tile_set_file = value; + } + else if (key == "counter") + { + item->counter = std::stoi(value); + } + else if (key == "x") + { + item->x = std::stof(value) * BLOCK; + } + else if (key == "y") + { + item->y = std::stof(value) * BLOCK; + } + else if (key == "tile") + { + item->tile = std::stof(value); + } + else if (key == "[/item]") + { + // No se realiza ninguna acción para esta clave + } + else + { + success = false; + } } - - else if (var == "counter") - { - item->counter = std::stoi(value); - } - - else if (var == "x") - { - item->x = std::stof(value) * BLOCK; - } - - else if (var == "y") - { - item->y = std::stof(value) * BLOCK; - } - - else if (var == "tile") - { - item->tile = std::stof(value); - } - - else if (var == "[/item]") - { - } - - else + catch (const std::exception &e) { + std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl; success = false; } @@ -408,7 +398,7 @@ bool setItem(item_t *item, std::string var, std::string value) } // Constructor -Room::Room(std::shared_ptr room, int *itemsPicked, bool jailEnabled) +Room::Room(std::shared_ptr room, int *itemsPicked, bool jail_is_open) : screen_(Screen::get()), renderer_(Screen::get()->getRenderer()), asset_(Asset::get()), @@ -428,17 +418,12 @@ Room::Room(std::shared_ptr room, int *itemsPicked, bool jailEnabled) tile_set_file_ = room->tile_set_file; tile_map_file_ = room->tile_map_file; auto_surface_direction_ = room->auto_surface_direction; - textureA_ = room->textureA; - textureB_ = room->textureB; - tile_map_ = room->tile_map; - texture_ = (options.video.palette == Palette::ZXSPECTRUM) ? textureA_ : textureB_; - jail_is_open_ = jailEnabled; + tile_map_ = Resource::get()->getTileMap(room->tile_map_file); + texture_ = (options.video.palette == Palette::ZXSPECTRUM) ? Resource::get()->getTexture(room->tile_set_file) : Resource::get()->getTexture(room->tile_set_file); + jail_is_open_ = jail_is_open; // Inicializa variables - tile_size_ = 8; - tile_set_width_ = texture_->getWidth() / tile_size_; - map_width_ = 32; - map_height_ = 16; + tile_set_width_ = texture_->getWidth() / TILE_SIZE_; paused_ = false; counter_ = 0; @@ -455,7 +440,6 @@ Room::Room(std::shared_ptr room, int *itemsPicked, bool jailEnabled) if (!ItemTracker::get()->hasBeenPicked(room->name, itemPos)) { - item.renderer = renderer_; item.color1 = stringToColor(options.video.palette, item_color1_); item.color2 = stringToColor(options.video.palette, item_color2_); items_.emplace_back(std::make_shared(item)); @@ -466,7 +450,7 @@ Room::Room(std::shared_ptr room, int *itemsPicked, bool jailEnabled) item_sound_ = JA_LoadSound(asset_->get("item.wav").c_str()); // Abre la jail para poder entrar - if (jailEnabled) + if (jail_is_open) { openTheJail(); } @@ -536,121 +520,117 @@ void Room::fillMapTexture() // Los tileSetFiles son de 20x20 tiles. El primer tile es el 0. Cuentan hacia la derecha y hacia abajo - SDL_Rect clip = {0, 0, tile_size_, tile_size_}; - for (int y = 0; y < map_height_; ++y) - for (int x = 0; x < map_width_; ++x) + SDL_Rect clip = {0, 0, TILE_SIZE_, TILE_SIZE_}; + for (int y = 0; y < MAP_HEIGHT_; ++y) + for (int x = 0; x < MAP_WIDTH_; ++x) { // Tiled pone los tiles vacios del mapa como cero y empieza a contar de 1 a n. // Al cargar el mapa en memoria, se resta uno, por tanto los tiles vacios son -1 // Tampoco hay que dibujar los tiles animados que estan en la fila 19 (indices) - const int index = (y * map_width_) + x; + const int index = (y * MAP_WIDTH_) + x; const bool a = (tile_map_[index] >= 18 * tile_set_width_) && (tile_map_[index] < 19 * tile_set_width_); const bool b = tile_map_[index] > -1; if (b && !a) { - clip.x = (tile_map_[index] % tile_set_width_) * tile_size_; - clip.y = (tile_map_[index] / tile_set_width_) * tile_size_; - texture_->render(x * tile_size_, y * tile_size_, &clip); + clip.x = (tile_map_[index] % tile_set_width_) * TILE_SIZE_; + clip.y = (tile_map_[index] / tile_set_width_) * TILE_SIZE_; + texture_->render(x * TILE_SIZE_, y * TILE_SIZE_, &clip); #ifdef DEBUG - // **** - if (debug->getEnabled()) + if (debug_->getEnabled()) { - if (clip.x != -tileSize) + if (clip.x != -TILE_SIZE_) { - clip.x = x * tileSize; - clip.y = y * tileSize; - SDL_SetRenderDrawColor(renderer, 64, 64, 64, 224); - SDL_RenderFillRect(renderer, &clip); + clip.x = x * TILE_SIZE_; + clip.y = y * TILE_SIZE_; + SDL_SetRenderDrawColor(renderer_, 64, 64, 64, 224); + SDL_RenderFillRect(renderer_, &clip); } } -// **** #endif } } #ifdef DEBUG - // **** - if (debug->getEnabled()) + if (debug_->getEnabled()) { // BottomSurfaces if (true) { - for (auto l : bottomSurfaces) + for (auto l : bottom_surfaces_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0xFF); - SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 255, 0, 0, 0xFF); + SDL_RenderDrawLine(renderer_, l.x1, l.y, l.x2, l.y); } } // TopSurfaces if (true) { - for (auto l : topSurfaces) + for (auto l : top_surfaces_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0xFF); - SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 0, 255, 0, 0xFF); + SDL_RenderDrawLine(renderer_, l.x1, l.y, l.x2, l.y); } } // LeftSurfaces if (true) { - for (auto l : leftSurfaces) + for (auto l : left_surfaces_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 128, 128, 255, 0xFF); - SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 128, 128, 255, 0xFF); + SDL_RenderDrawLine(renderer_, l.x, l.y1, l.x, l.y2); } } // RightSurfaces if (true) { - for (auto l : rightSurfaces) + for (auto l : right_surfaces_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF); - SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 255, 255, 0, 0xFF); + SDL_RenderDrawLine(renderer_, l.x, l.y1, l.x, l.y2); } } // LeftSlopes if (true) { - for (auto l : leftSlopes) + for (auto l : left_slopes_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 0, 255, 255, 0xFF); - SDL_RenderDrawLine(renderer, l.x1, l.y1, l.x2, l.y2); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 0, 255, 255, 0xFF); + SDL_RenderDrawLine(renderer_, l.x1, l.y1, l.x2, l.y2); } } // RightSlopes if (true) { - for (auto l : rightSlopes) + for (auto l : right_slopes_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_SetRenderDrawColor(renderer, 255, 0, 255, 0xFF); - SDL_RenderDrawLine(renderer, l.x1, l.y1, l.x2, l.y2); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_SetRenderDrawColor(renderer_, 255, 0, 255, 0xFF); + SDL_RenderDrawLine(renderer_, l.x1, l.y1, l.x2, l.y2); } } // AutoSurfaces if (true) { - for (auto l : autoSurfaces) + for (auto l : auto_surfaces_) { - SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); - SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y); + SDL_SetRenderDrawColor(renderer_, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_RenderDrawLine(renderer_, l.x1, l.y, l.x2, l.y); } } } - // **** #endif SDL_SetRenderTarget(renderer_, nullptr); @@ -665,7 +645,7 @@ void Room::renderMap() // Dibuja los tiles animados #ifdef DEBUG - if (!debug->getEnabled()) + if (!debug_->getEnabled()) { renderAnimatedTiles(); } @@ -747,7 +727,7 @@ std::string Room::getRoom(int border) // Devuelve el tipo de tile que hay en ese pixel tile_e Room::getTile(SDL_Point point) { - const int pos = ((point.y / tile_size_) * map_width_) + (point.x / tile_size_); + const int pos = ((point.y / TILE_SIZE_) * MAP_WIDTH_) + (point.x / TILE_SIZE_); return getTile(pos); } @@ -867,7 +847,7 @@ void Room::reLoadPalette() screen_->setBorderColor(stringToColor(options.video.palette, border_color_)); // Cambia la textura - texture_ = (options.video.palette == Palette::ZXSPECTRUM) ? textureA_ : textureB_; + //texture_ = (options.video.palette == Palette::ZXSPECTRUM) ? Resource::get()->getTexture(room->tile_set_file) : Resource::get()->getTexture(room->tile_set_file); // Pone la nueva textura a los tiles animados for (auto tile : animated_tiles_) @@ -882,22 +862,22 @@ void Room::reLoadPalette() // Obten el tamaño del tile int Room::getTileSize() { - return tile_size_; + return TILE_SIZE_; } // Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile int Room::getSlopeHeight(SDL_Point p, tile_e slope) { // Calcula la base del tile - int base = ((p.y / tile_size_) * tile_size_) + tile_size_; + int base = ((p.y / TILE_SIZE_) * TILE_SIZE_) + TILE_SIZE_; #ifdef DEBUG - debug->add("BASE = " + std::to_string(base)); + debug_->add("BASE = " + std::to_string(base)); #endif // Calcula cuanto se ha entrado en el tile horizontalmente - const int pos = (p.x % tile_size_); // Esto da un valor entre 0 y 7 + const int pos = (p.x % TILE_SIZE_); // Esto da un valor entre 0 y 7 #ifdef DEBUG - debug->add("POS = " + std::to_string(pos)); + debug_->add("POS = " + std::to_string(pos)); #endif // Se resta a la base la cantidad de pixeles pos en funcion de la rampa @@ -905,14 +885,14 @@ int Room::getSlopeHeight(SDL_Point p, tile_e slope) { base -= pos + 1; #ifdef DEBUG - debug->add("BASE_R = " + std::to_string(base)); + debug_->add("BASE_R = " + std::to_string(base)); #endif } else { - base -= (tile_size_ - pos); + base -= (TILE_SIZE_ - pos); #ifdef DEBUG - debug->add("BASE_L = " + std::to_string(base)); + debug_->add("BASE_L = " + std::to_string(base)); #endif } @@ -926,14 +906,14 @@ void Room::setBottomSurfaces() // Busca todos los tiles de tipo muro que no tengan debajo otro muro // Hay que recorrer la habitación por filas (excepto los de la última fila) - for (int i = 0; i < (int)tile_map_.size() - map_width_; ++i) + for (int i = 0; i < (int)tile_map_.size() - MAP_WIDTH_; ++i) { - if (getTile(i) == t_wall && getTile(i + map_width_) != t_wall) + if (getTile(i) == t_wall && getTile(i + MAP_WIDTH_) != t_wall) { tile.push_back(i); // Si llega al final de la fila, introduce un separador - if (i % map_width_ == map_width_ - 1) + if (i % MAP_WIDTH_ == MAP_WIDTH_ - 1) { tile.push_back(-1); } @@ -951,8 +931,8 @@ void Room::setBottomSurfaces() do { h_line_t line; - line.x1 = (tile[i] % map_width_) * tile_size_; - line.y = ((tile[i] / map_width_) * tile_size_) + tile_size_ - 1; + line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_; + line.y = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; lastOne = i; i++; @@ -969,7 +949,7 @@ void Room::setBottomSurfaces() } } - line.x2 = ((tile[lastOne] % map_width_) * tile_size_) + tile_size_ - 1; + line.x2 = ((tile[lastOne] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; bottom_surfaces_.push_back(line); if (i <= (int)tile.size() - 1) { @@ -989,14 +969,14 @@ void Room::setTopSurfaces() // Busca todos los tiles de tipo muro o pasable que no tengan encima un muro // Hay que recorrer la habitación por filas (excepto los de la primera fila) - for (int i = map_width_; i < (int)tile_map_.size(); ++i) + for (int i = MAP_WIDTH_; i < (int)tile_map_.size(); ++i) { - if ((getTile(i) == t_wall || getTile(i) == t_passable) && getTile(i - map_width_) != t_wall) + if ((getTile(i) == t_wall || getTile(i) == t_passable) && getTile(i - MAP_WIDTH_) != t_wall) { tile.push_back(i); // Si llega al final de la fila, introduce un separador - if (i % map_width_ == map_width_ - 1) + if (i % MAP_WIDTH_ == MAP_WIDTH_ - 1) { tile.push_back(-1); } @@ -1014,8 +994,8 @@ void Room::setTopSurfaces() do { h_line_t line; - line.x1 = (tile[i] % map_width_) * tile_size_; - line.y = (tile[i] / map_width_) * tile_size_; + line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_; + line.y = (tile[i] / MAP_WIDTH_) * TILE_SIZE_; lastOne = i; i++; @@ -1032,7 +1012,7 @@ void Room::setTopSurfaces() } } - line.x2 = ((tile[lastOne] % map_width_) * tile_size_) + tile_size_ - 1; + line.x2 = ((tile[lastOne] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; top_surfaces_.push_back(line); if (i <= (int)tile.size() - 1) { @@ -1052,11 +1032,11 @@ void Room::setLeftSurfaces() // Busca todos los tiles de tipo muro que no tienen a su izquierda un tile de tipo muro // Hay que recorrer la habitación por columnas (excepto los de la primera columna) - for (int i = 1; i < map_width_; ++i) + for (int i = 1; i < MAP_WIDTH_; ++i) { - for (int j = 0; j < map_height_; ++j) + for (int j = 0; j < MAP_HEIGHT_; ++j) { - const int pos = (j * map_width_ + i); + const int pos = (j * MAP_WIDTH_ + i); if (getTile(pos) == t_wall && getTile(pos - 1) != t_wall) { tile.push_back(pos); @@ -1076,9 +1056,9 @@ void Room::setLeftSurfaces() do { v_line_t line; - line.x = (tile[i] % map_width_) * tile_size_; - line.y1 = ((tile[i] / map_width_) * tile_size_); - while (tile[i] + map_width_ == tile[i + 1]) + line.x = (tile[i] % MAP_WIDTH_) * TILE_SIZE_; + line.y1 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_); + while (tile[i] + MAP_WIDTH_ == tile[i + 1]) { if (i == (int)tile.size() - 1) { @@ -1086,7 +1066,7 @@ void Room::setLeftSurfaces() } i++; } - line.y2 = ((tile[i] / map_width_) * tile_size_) + tile_size_ - 1; + line.y2 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; left_surfaces_.push_back(line); i++; } while (i < (int)tile.size() - 1); @@ -1100,11 +1080,11 @@ void Room::setRightSurfaces() // Busca todos los tiles de tipo muro que no tienen a su derecha un tile de tipo muro // Hay que recorrer la habitación por columnas (excepto los de la última columna) - for (int i = 0; i < map_width_ - 1; ++i) + for (int i = 0; i < MAP_WIDTH_ - 1; ++i) { - for (int j = 0; j < map_height_; ++j) + for (int j = 0; j < MAP_HEIGHT_; ++j) { - const int pos = (j * map_width_ + i); + const int pos = (j * MAP_WIDTH_ + i); if (getTile(pos) == t_wall && getTile(pos + 1) != t_wall) { tile.push_back(pos); @@ -1124,9 +1104,9 @@ void Room::setRightSurfaces() do { v_line_t line; - line.x = ((tile[i] % map_width_) * tile_size_) + tile_size_ - 1; - line.y1 = ((tile[i] / map_width_) * tile_size_); - while (tile[i] + map_width_ == tile[i + 1]) + line.x = ((tile[i] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; + line.y1 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_); + while (tile[i] + MAP_WIDTH_ == tile[i + 1]) { if (i == (int)tile.size() - 1) { @@ -1134,7 +1114,7 @@ void Room::setRightSurfaces() } i++; } - line.y2 = ((tile[i] / map_width_) * tile_size_) + tile_size_ - 1; + line.y2 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; right_surfaces_.push_back(line); i++; } while (i < (int)tile.size() - 1); @@ -1161,9 +1141,9 @@ void Room::setLeftSlopes() while (found.size() > 0) { d_line_t line; - line.x1 = (found[0] % map_width_) * tile_size_; - line.y1 = (found[0] / map_width_) * tile_size_; - int lookingFor = found[0] + map_width_ + 1; + line.x1 = (found[0] % MAP_WIDTH_) * TILE_SIZE_; + line.y1 = (found[0] / MAP_WIDTH_) * TILE_SIZE_; + int lookingFor = found[0] + MAP_WIDTH_ + 1; int lastOneFound = found[0]; found.erase(found.begin()); for (int i = 0; i < (int)found.size(); ++i) @@ -1171,13 +1151,13 @@ void Room::setLeftSlopes() if (found[i] == lookingFor) { lastOneFound = lookingFor; - lookingFor += map_width_ + 1; + lookingFor += MAP_WIDTH_ + 1; found.erase(found.begin() + i); i--; } } - line.x2 = ((lastOneFound % map_width_) * tile_size_) + tile_size_ - 1; - line.y2 = ((lastOneFound / map_width_) * tile_size_) + tile_size_ - 1; + line.x2 = ((lastOneFound % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; + line.y2 = ((lastOneFound / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; left_slopes_.push_back(line); } } @@ -1202,9 +1182,9 @@ void Room::setRightSlopes() while (found.size() > 0) { d_line_t line; - line.x1 = ((found[0] % map_width_) * tile_size_) + tile_size_ - 1; - line.y1 = (found[0] / map_width_) * tile_size_; - int lookingFor = found[0] + map_width_ - 1; + line.x1 = ((found[0] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; + line.y1 = (found[0] / MAP_WIDTH_) * TILE_SIZE_; + int lookingFor = found[0] + MAP_WIDTH_ - 1; int lastOneFound = found[0]; found.erase(found.begin()); for (int i = 0; i < (int)found.size(); ++i) @@ -1212,13 +1192,13 @@ void Room::setRightSlopes() if (found[i] == lookingFor) { lastOneFound = lookingFor; - lookingFor += map_width_ - 1; + lookingFor += MAP_WIDTH_ - 1; found.erase(found.begin() + i); i--; } } - line.x2 = (lastOneFound % map_width_) * tile_size_; - line.y2 = ((lastOneFound / map_width_) * tile_size_) + tile_size_ - 1; + line.x2 = (lastOneFound % MAP_WIDTH_) * TILE_SIZE_; + line.y2 = ((lastOneFound / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; right_slopes_.push_back(line); } } @@ -1230,14 +1210,14 @@ void Room::setAutoSurfaces() // Busca todos los tiles de tipo animado // Hay que recorrer la habitación por filas (excepto los de la primera fila) - for (int i = map_width_; i < (int)tile_map_.size(); ++i) + for (int i = MAP_WIDTH_; i < (int)tile_map_.size(); ++i) { if (getTile(i) == t_animated) { tile.push_back(i); // Si llega al final de la fila, introduce un separador - if (i % map_width_ == map_width_ - 1) + if (i % MAP_WIDTH_ == MAP_WIDTH_ - 1) { tile.push_back(-1); } @@ -1252,8 +1232,8 @@ void Room::setAutoSurfaces() do { h_line_t line; - line.x1 = (tile[i] % map_width_) * tile_size_; - line.y = (tile[i] / map_width_) * tile_size_; + line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_; + line.y = (tile[i] / MAP_WIDTH_) * TILE_SIZE_; lastOne = i; i++; @@ -1270,7 +1250,7 @@ void Room::setAutoSurfaces() } } - line.x2 = ((tile[lastOne] % map_width_) * tile_size_) + tile_size_ - 1; + line.x2 = ((tile[lastOne] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1; auto_surfaces_.push_back(line); if (i <= (int)tile.size() - 1) { @@ -1292,12 +1272,12 @@ void Room::setAnimatedTiles() if (getTile(i) == t_animated) { // La i es la ubicación - const int x = (i % map_width_) * tile_size_; - const int y = (i / map_width_) * tile_size_; + const int x = (i % MAP_WIDTH_) * TILE_SIZE_; + const int y = (i / MAP_WIDTH_) * TILE_SIZE_; // TileMap[i] es el tile a poner - const int xc = (tile_map_[i] % tile_set_width_) * tile_size_; - const int yc = (tile_map_[i] / tile_set_width_) * tile_size_; + const int xc = (tile_map_[i] % tile_set_width_) * TILE_SIZE_; + const int yc = (tile_map_[i] / tile_set_width_) * TILE_SIZE_; aTile_t at; at.sprite = std::make_shared(texture_, x, y, 8, 8); @@ -1315,11 +1295,11 @@ void Room::updateAnimatedTiles() int offset = 0; if (auto_surface_direction_ == -1) { - offset = ((counter_ / 3) % numFrames * tile_size_); + offset = ((counter_ / 3) % numFrames * TILE_SIZE_); } else { - offset = ((numFrames - 1 - ((counter_ / 3) % numFrames)) * tile_size_); + offset = ((numFrames - 1 - ((counter_ / 3) % numFrames)) * TILE_SIZE_); } for (auto &a : animated_tiles_) diff --git a/source/room.h b/source/room.h index eebcccb2..fe9a5a80 100644 --- a/source/room.h +++ b/source/room.h @@ -50,29 +50,32 @@ struct RoomData std::vector tile_map; // Indice de los tiles a dibujar en la habitación int auto_surface_direction; // Sentido en el que arrastran las superficies automáticas de la habitación std::vector enemies; // Listado con los enemigos de la habitación - std::vector items; // Listado con los items que hay en la habitación - std::shared_ptr textureA; // Textura con los graficos de la habitación - std::shared_ptr textureB; // Textura con los graficos de la habitación + std::vector items; // Listado con los items que hay en la habitación }; // Carga las variables desde un fichero de mapa -RoomData loadRoomFile(std::string file, bool verbose = false); +RoomData loadRoomFile(const std::string &file_path, bool verbose = false); // Carga las variables y texturas desde un fichero de mapa de tiles -std::vector loadRoomTileFile(std::string file_path, bool verbose = false); +std::vector loadRoomTileFile(const std::string &file_path, bool verbose = false); -// Asigna variables a partir de dos cadenas -bool setVars(RoomData *room, std::string var, std::string value); +// Asigna variables a una estructura RoomData +bool setRoom(RoomData *room, const std::string &key, const std::string &value); -// Asigna variables a una estructura enemy_t -bool setEnemy(EnemyData *enemy, std::string var, std::string value); +// Asigna variables a una estructura EnemyData +bool setEnemy(EnemyData *enemy, const std::string &key, const std::string &value); -// Asigna variables a una estructura item_t -bool setItem(item_t *item, std::string var, std::string value); +// Asigna variables a una estructura ItemData +bool setItem(ItemData *item, const std::string &key, const std::string &value); class Room { private: + // Constantes + static constexpr int TILE_SIZE_ = 8; // Ancho del tile en pixels + static constexpr int MAP_WIDTH_ = 32; // Ancho del mapa en tiles + static constexpr int MAP_HEIGHT_ = 16; // Alto del mapa en tiles + // Objetos y punteros Screen *screen_; // Objeto encargado de dibujar en pantalla SDL_Renderer *renderer_; // El renderizador de la ventana @@ -81,8 +84,6 @@ private: std::vector> enemies_; // Listado con los enemigos de la habitación std::vector> items_; // Listado con los items que hay en la habitación std::shared_ptr texture_; // Textura con los graficos de la habitación - std::shared_ptr textureA_; // Textura con los graficos de la habitación - std::shared_ptr textureB_; // Textura con los graficos de la habitación SDL_Texture *map_texture_; // Textura para dibujar el mapa de la habitación int *items_picked_; // Puntero a la cantidad de items recogidos que lleva el juego @@ -112,9 +113,6 @@ private: bool paused_; // Indica si el mapa esta en modo pausa std::vector animated_tiles_; // Vector con los indices de tiles animados std::vector auto_surfaces_; // Lista con las superficies automaticas de la habitación - int tile_size_; // Ancho del tile en pixels - int map_width_; // Ancho del mapa en tiles - int map_height_; // Alto del mapa en tiles int tile_set_width_; // Ancho del tileset en tiles bool jail_is_open_; // Indica si hay acceso a la Jail