Resolviendo bug en setTopSurfaces con los -1

This commit is contained in:
2022-09-15 10:14:32 +02:00
parent d5e5d142a4
commit a3d0eccbb9
2 changed files with 31 additions and 19 deletions

View File

@@ -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 != "</data>");
@@ -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<int> 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));
}
}