From deb820015fcf216ff5531276e720cecf65272c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 31 Oct 2022 10:07:35 +0100 Subject: [PATCH] Resueltos muchos bugs de vector out of range en la busqueda de superficies --- source/demo.cpp | 66 ++++++++++++++- source/demo.h | 1 + source/room.cpp | 207 ++++++++++++++++++++++++++++-------------------- 3 files changed, 188 insertions(+), 86 deletions(-) diff --git a/source/demo.cpp b/source/demo.cpp index 6bd48be..69b40ba 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -11,6 +11,68 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as rooms.push_back("45.room"); rooms.push_back("60.room"); rooms.push_back("58.room"); + + //rooms.push_back("01.room"); + //rooms.push_back("02.room"); + //rooms.push_back("03.room"); + //rooms.push_back("04.room"); + //rooms.push_back("05.room"); + //rooms.push_back("06.room"); + //rooms.push_back("07.room"); + //rooms.push_back("08.room"); + //rooms.push_back("09.room"); + //rooms.push_back("10.room"); + //rooms.push_back("11.room"); + //rooms.push_back("12.room"); + //rooms.push_back("13.room"); + //rooms.push_back("14.room"); + //rooms.push_back("15.room"); + //rooms.push_back("16.room"); + //rooms.push_back("17.room"); + //rooms.push_back("18.room"); + //rooms.push_back("19.room"); + //rooms.push_back("20.room"); + //rooms.push_back("21.room"); + //rooms.push_back("22.room"); + //rooms.push_back("23.room"); + //rooms.push_back("24.room"); + //rooms.push_back("25.room"); + //rooms.push_back("26.room"); + //rooms.push_back("27.room"); + //rooms.push_back("28.room"); + //rooms.push_back("29.room"); + //rooms.push_back("30.room"); + //rooms.push_back("31.room"); + //rooms.push_back("32.room"); + //rooms.push_back("33.room"); + //rooms.push_back("34.room"); + //rooms.push_back("35.room"); + //rooms.push_back("36.room"); + //rooms.push_back("37.room"); + //rooms.push_back("38.room"); + //rooms.push_back("39.room"); + //rooms.push_back("40.room"); + //rooms.push_back("41.room"); + //rooms.push_back("42.room"); + //rooms.push_back("43.room"); + //rooms.push_back("44.room"); + //rooms.push_back("45.room"); + //rooms.push_back("46.room"); + //rooms.push_back("47.room"); + //rooms.push_back("48.room"); + //rooms.push_back("49.room"); + //rooms.push_back("50.room"); + //rooms.push_back("51.room"); + //rooms.push_back("52.room"); + //rooms.push_back("53.room"); + //rooms.push_back("54.room"); + //rooms.push_back("55.room"); + //rooms.push_back("56.room"); + //rooms.push_back("57.room"); + //rooms.push_back("58.room"); + //rooms.push_back("59.room"); + //rooms.push_back("60.room"); + roomIndex = 0; currentRoom = rooms.at(roomIndex); @@ -31,6 +93,8 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as // Inicializa el resto de variables counter = 0; + roomTime = 400; + roomTime = 200; ticks = 0; ticksSpeed = 15; board.lives = 9; @@ -240,7 +304,7 @@ bool Demo::changeRoom(std::string file) void Demo::checkRoomChange() { counter++; - if (counter == 400) + if (counter == roomTime) { counter = 0; roomIndex++; diff --git a/source/demo.h b/source/demo.h index a835730..4811afa 100644 --- a/source/demo.h +++ b/source/demo.h @@ -42,6 +42,7 @@ private: std::string currentRoom; // Fichero de la habitación actual board_t board; // Estructura con los datos del marcador int counter; // Contador para el modo demo + int roomTime; // Tiempo que se muestra cada habitacion int roomIndex; // Indice para el vector de habitaciones std::vector rooms; // Listado con los mapas de la demo diff --git a/source/room.cpp b/source/room.cpp index 17b5793..1501b95 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -882,33 +882,42 @@ void Room::setBottomSurfaces() } // Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies - int i = 0; - int lastOne = 0; - do + if ((int)tile.size() > 0) { - h_line_t line; - line.x1 = (tile.at(i) % mapWidth) * tileSize; - line.y = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; - lastOne = i; - i++; - - while (tile.at(i) == tile.at(i - 1) + 1) + int i = 0; + int lastOne = 0; + do { + h_line_t line; + line.x1 = (tile.at(i) % mapWidth) * tileSize; + line.y = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; lastOne = i; - if (i == (int)tile.size() - 1) - { - break; - } i++; - } - line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; - bottomSurfaces.push_back(line); - if (tile.at(i) == -1) - { // Si el siguiente elemento es un separador, hay que saltarlo - i++; - } - } while (i < (int)tile.size() - 1); + if (i < (int)tile.size() - 1) + { + while (tile.at(i) == tile.at(i - 1) + 1) + { + lastOne = i; + if (i == (int)tile.size() - 1) + { + break; + } + i++; + } + } + + line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; + bottomSurfaces.push_back(line); + if (i < (int)tile.size() - 1) + { + if (tile.at(i) == -1) + { // Si el siguiente elemento es un separador, hay que saltarlo + i++; + } + } + } while (i < (int)tile.size() - 1); + } } // Calcula las superficies superiores @@ -933,33 +942,42 @@ void Room::setTopSurfaces() } // Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies - int i = 0; - int lastOne = 0; - do + if ((int)tile.size() > 0) { - h_line_t line; - line.x1 = (tile.at(i) % mapWidth) * tileSize; - line.y = (tile.at(i) / mapWidth) * tileSize; - lastOne = i; - i++; - - while (tile.at(i) == tile.at(i - 1) + 1) + int i = 0; + int lastOne = 0; + do { + h_line_t line; + line.x1 = (tile.at(i) % mapWidth) * tileSize; + line.y = (tile.at(i) / mapWidth) * tileSize; lastOne = i; - if (i == (int)tile.size() - 1) - { - break; - } i++; - } - line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; - topSurfaces.push_back(line); - if (tile.at(i) == -1) - { // Si el siguiente elemento es un separador, hay que saltarlo - i++; - } - } while (i < (int)tile.size() - 1); + if (i < (int)tile.size() - 1) + { + while (tile.at(i) == tile.at(i - 1) + 1) + { + lastOne = i; + if (i == (int)tile.size() - 1) + { + break; + } + i++; + } + } + + line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; + topSurfaces.push_back(line); + if (i < (int)tile.size() - 1) + { + if (tile.at(i) == -1) + { // Si el siguiente elemento es un separador, hay que saltarlo + i++; + } + } + } while (i < (int)tile.size() - 1); + } } // Calcula las superficies laterales izquierdas @@ -984,24 +1002,27 @@ void Room::setLeftSurfaces() // Recorre el vector de tiles buscando tiles consecutivos // (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth) // para localizar las superficies - int i = 0; - do + if ((int)tile.size() > 0) { - v_line_t line; - line.x = (tile.at(i) % mapWidth) * tileSize; - line.y1 = ((tile.at(i) / mapWidth) * tileSize); - while (tile.at(i) + mapWidth == tile[i + 1]) + int i = 0; + do { - if (i == (int)tile.size() - 1) + v_line_t line; + line.x = (tile.at(i) % mapWidth) * tileSize; + line.y1 = ((tile.at(i) / mapWidth) * tileSize); + while (tile.at(i) + mapWidth == tile[i + 1]) { - break; + if (i == (int)tile.size() - 1) + { + break; + } + i++; } + line.y2 = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; + leftSurfaces.push_back(line); i++; - } - line.y2 = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; - leftSurfaces.push_back(line); - i++; - } while (i < (int)tile.size() - 1); + } while (i < (int)tile.size() - 1); + } } // Calcula las superficies laterales derechas @@ -1026,24 +1047,27 @@ void Room::setRightSurfaces() // Recorre el vector de tiles buscando tiles consecutivos // (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth) // para localizar las superficies - int i = 0; - do + if ((int)tile.size() > 0) { - v_line_t line; - line.x = ((tile.at(i) % mapWidth) * tileSize) + tileSize - 1; - line.y1 = ((tile.at(i) / mapWidth) * tileSize); - while (tile.at(i) + mapWidth == tile[i + 1]) + int i = 0; + do { - if (i == (int)tile.size() - 1) + v_line_t line; + line.x = ((tile.at(i) % mapWidth) * tileSize) + tileSize - 1; + line.y1 = ((tile.at(i) / mapWidth) * tileSize); + while (tile.at(i) + mapWidth == tile[i + 1]) { - break; + if (i == (int)tile.size() - 1) + { + break; + } + i++; } + line.y2 = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; + rightSurfaces.push_back(line); i++; - } - line.y2 = ((tile.at(i) / mapWidth) * tileSize) + tileSize - 1; - rightSurfaces.push_back(line); - i++; - } while (i < (int)tile.size() - 1); + } while (i < (int)tile.size() - 1); + } } // Encuentra todas las rampas que suben hacia la izquierda @@ -1152,26 +1176,39 @@ void Room::setAutoSurfaces() // Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies int i = 0; int lastOne = 0; - while (i < (int)tile.size()) + if ((int)tile.size() > 0) { - h_line_t line; - line.x1 = (tile.at(i) % mapWidth) * tileSize; - line.y = (tile.at(i) / mapWidth) * tileSize; - lastOne = i; - i++; - - while (tile.at(i) == tile.at(i - 1) + 1) + do { + h_line_t line; + line.x1 = (tile.at(i) % mapWidth) * tileSize; + line.y = (tile.at(i) / mapWidth) * tileSize; lastOne = i; i++; - } - line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; - autoSurfaces.push_back(line); - if (tile.at(i) == -1) - { // Si el siguiente elemento es un separador, hay que saltarlo - i++; - } + if (i < (int)tile.size() - 1) + { + while (tile.at(i) == tile.at(i - 1) + 1) + { + lastOne = i; + if (i == (int)tile.size() - 1) + { + break; + } + i++; + } + } + + line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; + autoSurfaces.push_back(line); + if (i < (int)tile.size() - 1) + { + if (tile.at(i) == -1) + { // Si el siguiente elemento es un separador, hay que saltarlo + i++; + } + } + } while (i < (int)tile.size() - 1); } }