forked from jaildesigner-jailgames/jaildoctors_dilemma
Optimizaciones de código
This commit is contained in:
179
source/room.cpp
179
source/room.cpp
@@ -4,13 +4,12 @@
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *itemTracker, int *items, Debug *debug)
|
||||
Room::Room(std::string file, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *itemTracker, int *items, Debug *debug)
|
||||
{
|
||||
// Inicializa variables
|
||||
tileSize = 8;
|
||||
mapWidth = 32;
|
||||
mapHeight = 16;
|
||||
tilesetWidth = 20;
|
||||
paused = false;
|
||||
itemColor1 = stringToColor("magenta");
|
||||
itemColor2 = stringToColor("yellow");
|
||||
@@ -24,13 +23,14 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
|
||||
this->debug = debug;
|
||||
|
||||
// Crea los objetos
|
||||
load(file_path);
|
||||
loadMapFile(file);
|
||||
texture = new LTexture(renderer, asset->get(tileset));
|
||||
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
|
||||
tilesetWidth = texture->getWidth() / tileSize;
|
||||
|
||||
// debug->clearLog();
|
||||
// debug->addToLog(tileset);
|
||||
// Calcula las superficies
|
||||
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
|
||||
loadMapTileFile(asset->get(tileMapFile));
|
||||
|
||||
// Calcula las superficies
|
||||
setBottomSurfaces();
|
||||
setTopSurfaces();
|
||||
setLeftSurfaces();
|
||||
@@ -40,8 +40,11 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
|
||||
|
||||
// Crea la textura para el mapa de tiles de la habitación
|
||||
mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (mapTexture == NULL)
|
||||
if (mapTexture == nullptr)
|
||||
{
|
||||
printf("Error: mapTexture could not be created!\nSDL Error: %s\n", SDL_GetError());
|
||||
}
|
||||
SDL_SetTextureBlendMode(mapTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Pinta el mapa de la habitación en la textura
|
||||
fillMapTexture();
|
||||
@@ -69,13 +72,10 @@ Room::~Room()
|
||||
}
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero
|
||||
bool Room::load(std::string file_path)
|
||||
// Carga las variables desde un fichero de mapa
|
||||
bool Room::loadMapFile(std::string file_path)
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
|
||||
@@ -83,7 +83,7 @@ bool Room::load(std::string file_path)
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
printf("Reading file %s\n\n", filename.c_str());
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
|
||||
@@ -104,7 +104,6 @@ bool Room::load(std::string file_path)
|
||||
if (!setEnemy(&enemy, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
} while (line != "[/enemy]");
|
||||
|
||||
@@ -113,7 +112,7 @@ bool Room::load(std::string file_path)
|
||||
}
|
||||
|
||||
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
|
||||
else if (line == "[tilemap]")
|
||||
/*else if (line == "[tilemap]")
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -159,7 +158,7 @@ bool Room::load(std::string file_path)
|
||||
}
|
||||
}
|
||||
} while (line != "[/tilemap]");
|
||||
}
|
||||
}*/
|
||||
|
||||
// Si la linea contiene el texto [item] se realiza el proceso de carga de un item
|
||||
else if (line == "[item]")
|
||||
@@ -177,12 +176,13 @@ bool Room::load(std::string file_path)
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
} while (line != "[/item]");
|
||||
|
||||
// Añade el item al vector de items
|
||||
@@ -202,7 +202,6 @@ bool Room::load(std::string file_path)
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -215,10 +214,58 @@ bool Room::load(std::string file_path)
|
||||
else
|
||||
{
|
||||
printf("Warning: Unable to open %s file\n", filename.c_str());
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
return success;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carga las variables y texturas desde un fichero de mapa de tiles
|
||||
bool Room::loadMapTileFile(std::string file_path)
|
||||
{
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
printf("Reading file %s\n\n", filename.c_str());
|
||||
|
||||
while (std::getline(file, line))
|
||||
{ // Lee el fichero linea a linea
|
||||
if (line.find("data encoding") != std::string::npos)
|
||||
{
|
||||
// Lee la primera linea
|
||||
std::getline(file, line);
|
||||
while (line != "</data>")
|
||||
{ // Procesa lineas mientras haya
|
||||
std::stringstream ss(line);
|
||||
std::string tmp;
|
||||
while (getline(ss, tmp, ','))
|
||||
{
|
||||
tilemap.push_back(std::stoi(tmp) - 1);
|
||||
}
|
||||
|
||||
// Lee la siguiente linea
|
||||
std::getline(file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
printf("Closing file %s\n\n", filename.c_str());
|
||||
file.close();
|
||||
}
|
||||
|
||||
else
|
||||
{ // El fichero no se puede abrir
|
||||
printf("Warning: Unable to open %s file\n", filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
@@ -227,7 +274,12 @@ bool Room::setVars(std::string var, std::string value)
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "name")
|
||||
if (var == "tilemap")
|
||||
{
|
||||
tileMapFile = value;
|
||||
}
|
||||
|
||||
else if (var == "name")
|
||||
{
|
||||
name = value;
|
||||
}
|
||||
@@ -453,34 +505,36 @@ color_t Room::getBorderColor()
|
||||
void Room::fillMapTexture()
|
||||
{
|
||||
SDL_SetRenderTarget(renderer, mapTexture);
|
||||
SDL_SetTextureBlendMode(mapTexture, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// 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++)
|
||||
SDL_Rect clip = {0, 0, tileSize, tileSize};
|
||||
for (int y = 0; y < mapHeight; ++y)
|
||||
for (int x = 0; x < mapWidth; ++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
|
||||
const int index = (y * 32) + x;
|
||||
const int index = (y * mapWidth) + x;
|
||||
if (index > -1)
|
||||
{
|
||||
clip.x = (tilemap[index] % 20) * 8;
|
||||
clip.y = (tilemap[index] / 20) * 8;
|
||||
texture->render(renderer, x * 8, y * 8, &clip);
|
||||
clip.x = (tilemap[index] % tilesetWidth) * tileSize;
|
||||
clip.y = (tilemap[index] / tilesetWidth) * tileSize;
|
||||
texture->render(renderer, x * tileSize, y * tileSize, &clip);
|
||||
|
||||
// ****
|
||||
if (debug->getEnabled())
|
||||
{
|
||||
if (clip.x != -8)
|
||||
if (clip.x != -tileSize)
|
||||
{
|
||||
clip.x = x * 8;
|
||||
clip.y = y * 8;
|
||||
clip.x = x * tileSize;
|
||||
clip.y = y * tileSize;
|
||||
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 224);
|
||||
SDL_RenderFillRect(renderer, &clip);
|
||||
}
|
||||
}
|
||||
// ****
|
||||
}
|
||||
}
|
||||
|
||||
@@ -560,7 +614,7 @@ void Room::renderMap()
|
||||
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
||||
|
||||
// Dibuja la textura con el mapa en pantalla
|
||||
SDL_RenderCopy(renderer, mapTexture, &rect, NULL);
|
||||
SDL_RenderCopy(renderer, mapTexture, &rect, nullptr);
|
||||
}
|
||||
|
||||
// Dibuja los enemigos en pantalla
|
||||
@@ -691,8 +745,7 @@ bool Room::enemyCollision(SDL_Rect &rect)
|
||||
// Indica si hay colision con un objeto a partir de un rectangulo
|
||||
bool Room::itemCollision(SDL_Rect &rect)
|
||||
{
|
||||
bool collision = false;
|
||||
for (int i = 0; i < items.size(); i++)
|
||||
for (int i = 0; i < (int)items.size(); ++i)
|
||||
{
|
||||
if (checkCollision(rect, items[i]->getCollider()))
|
||||
{
|
||||
@@ -701,11 +754,11 @@ bool Room::itemCollision(SDL_Rect &rect)
|
||||
items.erase(items.begin() + i);
|
||||
JA_PlaySound(itemSound);
|
||||
*itemsPicked = *itemsPicked + 1;
|
||||
collision = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return collision;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recarga la textura
|
||||
@@ -763,7 +816,7 @@ 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 < tilemap.size() - mapWidth; i++)
|
||||
for (int i = 0; i < (int)tilemap.size() - mapWidth; ++i)
|
||||
{
|
||||
if (getTile(i) == t_wall && getTile(i + mapWidth) != t_wall)
|
||||
{
|
||||
@@ -780,28 +833,26 @@ void Room::setBottomSurfaces()
|
||||
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
|
||||
int i = 0;
|
||||
int lastOne = 0;
|
||||
while (i < tile.size())
|
||||
while (i < (int)tile.size())
|
||||
{
|
||||
h_line_t line;
|
||||
line.x1 = (tile[i] % mapWidth) * tileSize;
|
||||
line.y = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
|
||||
lastOne = i;
|
||||
i++;
|
||||
++i;
|
||||
|
||||
while (tile[i] == tile[i - 1] + 1)
|
||||
{
|
||||
lastOne = i;
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
|
||||
line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1;
|
||||
bottomSurfaces.push_back(line);
|
||||
if (tile[i] == -1)
|
||||
{ // Si el siguiente elemento es un separador, hay que saltarlo
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
|
||||
// debug->addToLog("B: " + std::to_string(line.x1) + "," + std::to_string(line.y) + "," + std::to_string(line.x2) + "," + std::to_string(line.y));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -810,11 +861,9 @@ 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++)
|
||||
for (int i = mapWidth; i < (int)tilemap.size(); ++i)
|
||||
{
|
||||
if ((getTile(i) == t_wall || getTile(i) == t_passable) && getTile(i - mapWidth) != t_wall)
|
||||
{
|
||||
@@ -831,28 +880,26 @@ void Room::setTopSurfaces()
|
||||
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
|
||||
int i = 0;
|
||||
int lastOne = 0;
|
||||
while (i < tile.size())
|
||||
while (i < (int)tile.size())
|
||||
{
|
||||
h_line_t line;
|
||||
line.x1 = (tile[i] % mapWidth) * tileSize;
|
||||
line.y = (tile[i] / mapWidth) * tileSize;
|
||||
lastOne = i;
|
||||
i++;
|
||||
++i;
|
||||
|
||||
while (tile[i] == tile[i - 1] + 1)
|
||||
{
|
||||
lastOne = i;
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
|
||||
line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1;
|
||||
topSurfaces.push_back(line);
|
||||
if (tile[i] == -1)
|
||||
{ // Si el siguiente elemento es un separador, hay que saltarlo
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
|
||||
// debug->addToLog("T: " + std::to_string(line.x1) + "," + std::to_string(line.y) + "," + std::to_string(line.x2) + "," + std::to_string(line.y));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -879,18 +926,18 @@ void Room::setLeftSurfaces()
|
||||
// (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth)
|
||||
// para localizar las superficies
|
||||
int i = 0;
|
||||
while (i < tile.size())
|
||||
while (i < (int)tile.size())
|
||||
{
|
||||
v_line_t line;
|
||||
line.x = (tile[i] % mapWidth) * tileSize;
|
||||
line.y1 = ((tile[i] / mapWidth) * tileSize);
|
||||
while (tile[i] + mapWidth == tile[i + 1])
|
||||
{
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
line.y2 = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
|
||||
leftSurfaces.push_back(line);
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -917,18 +964,18 @@ void Room::setRightSurfaces()
|
||||
// (Los tiles de la misma columna, la diferencia entre ellos es de mapWidth)
|
||||
// para localizar las superficies
|
||||
int i = 0;
|
||||
while (i < tile.size())
|
||||
while (i < (int)tile.size())
|
||||
{
|
||||
v_line_t line;
|
||||
line.x = ((tile[i] % mapWidth) * tileSize) + tileSize - 1;
|
||||
line.y1 = ((tile[i] / mapWidth) * tileSize);
|
||||
while (tile[i] + mapWidth == tile[i + 1])
|
||||
{
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
line.y2 = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
|
||||
rightSurfaces.push_back(line);
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -937,7 +984,7 @@ void Room::setLeftSlopes()
|
||||
{
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_l
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < tilemap.size(); ++i)
|
||||
for (int i = 0; i < (int)tilemap.size(); ++i)
|
||||
{
|
||||
if (getTile(i) == t_slope_l)
|
||||
{
|
||||
@@ -957,7 +1004,7 @@ void Room::setLeftSlopes()
|
||||
int lookingFor = found[0] + mapWidth + 1;
|
||||
int lastOneFound = found[0];
|
||||
found.erase(found.begin());
|
||||
for (int i = 0; i < found.size(); i++)
|
||||
for (int i = 0; i < (int)found.size(); ++i)
|
||||
{
|
||||
if (found[i] == lookingFor)
|
||||
{
|
||||
@@ -970,7 +1017,6 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -979,7 +1025,7 @@ void Room::setRightSlopes()
|
||||
{
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_r
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < tilemap.size(); ++i)
|
||||
for (int i = 0; i < (int)tilemap.size(); ++i)
|
||||
{
|
||||
if (getTile(i) == t_slope_r)
|
||||
{
|
||||
@@ -999,7 +1045,7 @@ void Room::setRightSlopes()
|
||||
int lookingFor = found[0] + mapWidth - 1;
|
||||
int lastOneFound = found[0];
|
||||
found.erase(found.begin());
|
||||
for (int i = 0; i < found.size(); i++)
|
||||
for (int i = 0; i < (int)found.size(); ++i)
|
||||
{
|
||||
if (found[i] == lookingFor)
|
||||
{
|
||||
@@ -1012,7 +1058,6 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user