From a3d0eccbb9d45864332563035eda30882db132c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Thu, 15 Sep 2022 10:14:32 +0200 Subject: [PATCH] Resolviendo bug en setTopSurfaces con los -1 --- source/debug.cpp | 2 +- source/room.cpp | 48 ++++++++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/source/debug.cpp b/source/debug.cpp index c0090a6..6e2bd0f 100644 --- a/source/debug.cpp +++ b/source/debug.cpp @@ -49,7 +49,7 @@ void Debug::render() y = 0; for (auto l : log) { - text->writeColored(x, y, l, {255, 255, 255}); + text->writeColored(x + 10, y, l, {255, 255, 255}); y += text->getCharacterSize() + 1; } } diff --git a/source/room.cpp b/source/room.cpp index b064812..12364d1 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -45,7 +45,6 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset // Establece el color del borde screen->setBorderColor(borderColor); - } // Destructor @@ -142,8 +141,8 @@ bool Room::load(std::string file_path) std::stringstream ss(line); std::string tmp; while (getline(ss, tmp, ',')) - { - tilemap.push_back(std::stoi(tmp)); + { // Se resta 1 ya que tiled numera los tiles de 1 a n + tilemap.push_back(std::stoi(tmp) - 1); } } } while (line != ""); @@ -437,23 +436,29 @@ void Room::fillMapTexture() SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); SDL_RenderClear(renderer); - // Los tilesets son de 20x20 tiles. El primer tile es el 1. Cuentan hacia la derecha y hacia abajo + // Los tilesets son de 20x20 tiles. El primer tile es el 0. Cuentan hacia la derecha y hacia abajo SDL_Rect clip = {0, 0, 8, 8}; for (int y = 0; y < 16; y++) for (int x = 0; x < 32; x++) { - clip.x = ((tilemap[(y * 32) + x] - 1) % 20) * 8; - clip.y = ((tilemap[(y * 32) + x] - 1) / 20) * 8; - texture->render(renderer, x * 8, y * 8, &clip); - if (debug->getEnabled()) + // 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 + const int index = (y * 32) + x; + if (index > -1) { - if (clip.x != -8) + clip.x = (tilemap[index] % 20) * 8; + clip.y = (tilemap[index] / 20) * 8; + texture->render(renderer, x * 8, y * 8, &clip); + if (debug->getEnabled()) { - clip.x = x * 8; - clip.y = y * 8; - SDL_SetRenderDrawColor(renderer, 64, 64, 64, 224); - SDL_RenderFillRect(renderer, &clip); + if (clip.x != -8) + { + clip.x = x * 8; + clip.y = y * 8; + SDL_SetRenderDrawColor(renderer, 64, 64, 64, 224); + SDL_RenderFillRect(renderer, &clip); + } } } } @@ -467,6 +472,7 @@ void Room::fillMapTexture() for (auto l : bottomSurfaces) { 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); } } @@ -477,6 +483,7 @@ void Room::fillMapTexture() for (auto l : topSurfaces) { 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); } } @@ -604,7 +611,7 @@ tile_e Room::getTile(SDL_Point point) if (pos < maxTile) { // Las filas 0-7 son de tiles t_wall - if ((tilemap[pos] > 0) && (tilemap[pos] < 8 * tilesetWidth)) + if ((tilemap[pos] >= 0) && (tilemap[pos] < 8 * tilesetWidth)) { return t_wall; } @@ -640,7 +647,7 @@ tile_e Room::getTile(int index) if (index < maxTile) { // Las filas 0-7 son de tiles t_wall - if ((tilemap[index] > 0) && (tilemap[index] < 8 * tilesetWidth)) + if ((tilemap[index] >= 0) && (tilemap[index] < 8 * tilesetWidth)) { return t_wall; } @@ -719,7 +726,7 @@ void Room::reLoadTexture() // Obten el tamaño del tile int Room::getTileSize() { - return 8; + return tileSize; } // Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile @@ -784,6 +791,7 @@ void Room::setBottomSurfaces() line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1; bottomSurfaces.push_back(line); i++; + debug->addToLog("B: " + std::to_string(line.x1) + "," + std::to_string(line.y) + "," + std::to_string(line.x2) + "," + std::to_string(line.y)); } } @@ -792,6 +800,8 @@ void Room::setTopSurfaces() { std::vector tile; + debug->addToLog(std::to_string(tilemap.size())); + // 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 = mapWidth; i < tilemap.size(); i++) @@ -822,6 +832,8 @@ void Room::setTopSurfaces() line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1; topSurfaces.push_back(line); i++; + + debug->addToLog("T: " + std::to_string(line.x1) + "," + std::to_string(line.y) + "," + std::to_string(line.x2) + "," + std::to_string(line.y)); } } @@ -940,7 +952,7 @@ void Room::setLeftSlopes() line.x2 = ((lastOneFound % mapWidth) * tileSize) + tileSize - 1; line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1; leftSlopes.push_back(line); - debug->addToLog("LS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2)); + // debug->addToLog("LS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2)); } } @@ -982,7 +994,7 @@ void Room::setRightSlopes() line.x2 = (lastOneFound % mapWidth) * tileSize; line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1; rightSlopes.push_back(line); - debug->addToLog("RS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2)); + // debug->addToLog("RS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2)); } }