Optimizada la carga del mapa

This commit is contained in:
2022-09-24 11:05:22 +02:00
parent 45f9a5b494
commit 911061899d
17 changed files with 369 additions and 309 deletions

View File

@@ -353,25 +353,12 @@ void Game::checkIfPlayerIsAlive()
// Mata al jugador
void Game::killPlayer()
{
// if (player->getInvincible())
//{
// return;
// }
// Decrementa el marcador de vidas
board.lives--;
// Destruye el mapa, el jugador y los enemigos
// delete map;
// Destruye el jugador
delete player;
// delete enemyEngine;
// Sonido
// JA_PlaySound(deathSound);
// setBlackScreen();
// Crea el nuevo mapa, el nuevo jugador y nuevos enemigos
// map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
// Crea el nuevo jugador
player = new Player(spawnPoint, renderer, asset, input, map, debug, &board.diamonds);
// enemyEngine = new EnemyEngine(renderer, asset, player, map, asset->get(map->getEnemyFile()));
}

View File

@@ -18,26 +18,37 @@ Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset, ItemTracker *it
this->renderer = renderer;
this->itemTracker = itemTracker;
// Crea los objetos
load(file);
texture_tile = new LTexture(renderer, asset->get(tileset_img));
tileset_width = texture_tile->getWidth() / tile_size;
// Crea las texturas para dibujar el mapa
map_layerBG = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
if (map_layerBG == NULL)
{
printf("Error: map_layer0 could not be created!\nSDL Error: %s\n", SDL_GetError());
}
SDL_SetTextureBlendMode(map_layerBG, SDL_BLENDMODE_BLEND);
map_layer0 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
if (map_layer0 == NULL)
{
printf("Error: map_layer0 could not be created!\nSDL Error: %s\n", SDL_GetError());
}
SDL_SetTextureBlendMode(map_layer0, SDL_BLENDMODE_BLEND);
map_layer1 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
if (map_layer1 == NULL)
{
printf("Error: map_layer1 could not be created!\nSDL Error: %s\n", SDL_GetError());
}
SDL_SetTextureBlendMode(map_layer1, SDL_BLENDMODE_BLEND);
// Crea los objetos
loadMapFile(file);
texture_tile = new LTexture(renderer, asset->get(tileset_img));
tileset_width = texture_tile->getWidth() / tile_size;
loadMapTileFile(asset->get(tileMapFile));
// Pinta el mapa en las texturas
fillMapTexture();
// fillMapTexture();
}
// Destructor
@@ -56,8 +67,8 @@ Map::~Map()
actors.clear();
}
// Carga las variables desde un fichero
bool Map::load(std::string file_path)
// Carga las variables desde un fichero de mapa
bool Map::loadMapFile(std::string file_path)
{
// Indicador de éxito en la carga
bool success = true;
@@ -73,130 +84,8 @@ bool Map::load(std::string file_path)
printf("Reading file %s\n\n", filename.c_str());
while (std::getline(file, line))
{
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
if (line == "[tilemap]")
{
do
{
std::getline(file, line);
if (line.find(".tmx") != std::string::npos)
{
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
if (file2.good())
{
bool map_col_read = false;
bool map_BG_read = false;
bool map_l0_read = false;
bool map_l1_read = false;
while (std::getline(file2, line)) // Lee el fichero linea a linea
{
if (!map_BG_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
pos = line.find("data encoding");
} while (pos == (int)std::string::npos);
do
{ // Se introducen los valores separados por comas en un vector
map_BG_read = true;
std::getline(file2, line);
if (line != "</data>")
{
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap_BG.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
if (!map_l0_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
pos = line.find("data encoding");
} while (pos == (int)std::string::npos);
do
{ // Se introducen los valores separados por comas en un vector
map_l0_read = true;
std::getline(file2, line);
if (line != "</data>")
{
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap_l0.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
if (!map_l1_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
pos = line.find("data encoding");
} while (pos == (int)std::string::npos);
do
{ // Se introducen los valores separados por comas en un vector
map_l1_read = true;
std::getline(file2, line);
if (line != "</data>")
{
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap_l1.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
if (!map_col_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
pos = line.find("data encoding");
} while (pos == (int)std::string::npos);
do
{ // Se introducen los valores separados por comas en un vector
map_col_read = true;
std::getline(file2, line);
if (line != "</data>")
{
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
collisionmap.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
}
}
}
} while (line != "[/tilemap]");
}
// Si la linea contiene el texto [actor] se realiza el proceso de carga de los actores
else if (line == "[actors]")
if (line == "[actors]")
{
do
{
@@ -295,13 +184,136 @@ bool Map::load(std::string file_path)
return success;
}
// Carga las variables y texturas desde un fichero de mapa de tiles
bool Map::loadMapTileFile(std::string file_path)
{
std::vector<int> tilemap;
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("name=\"estatico\"") != std::string::npos)
{
// Salta una linea y lee la siguiente
std::getline(file, line);
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));
}
// Lee la siguiente linea
std::getline(file, line);
}
fillGradientTexture(*map_layerBG);
fillMapTexture(*map_layerBG, tilemap, false);
tilemap.clear();
}
if (line.find("name=\"fondo\"") != std::string::npos)
{
// Salta una linea y lee la siguiente
std::getline(file, line);
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));
}
// Lee la siguiente linea
std::getline(file, line);
}
fillMapTexture(*map_layer0, tilemap, true);
tilemap.clear();
}
if (line.find("name=\"mapa\"") != std::string::npos)
{
// Salta una linea y lee la siguiente
std::getline(file, line);
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));
}
// Lee la siguiente linea
std::getline(file, line);
}
fillMapTexture(*map_layer1, tilemap, true);
tilemap.clear();
}
if (line.find("name=\"colisiones\"") != std::string::npos)
{
// Salta una linea y lee la siguiente
std::getline(file, line);
std::getline(file, line);
while (line != "</data>")
{ // Procesa lineas mientras haya
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
collisionmap.push_back(std::stoi(tmp));
}
// Lee la siguiente linea
std::getline(file, line);
}
}
}
// Cierra el fichero
printf("Closing file %s\n\n", filename.c_str());
file.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
return false;
}
return true;
}
// Asigna variables a partir de dos cadenas
bool Map::setVars(std::string var, std::string value)
{
// Indicador de éxito en la asignación
bool success = true;
if (var == "tileset_img")
if (var == "tilemap")
{
tileMapFile = value;
}
else if (var == "tileset_img")
{
tileset_img = value;
}
@@ -451,92 +463,58 @@ bool Map::setActor(actor_t *actor, SDL_Point *p1, SDL_Point *p2, std::string var
return success;
}
// Pinta el degradado en la textura
void Map::fillGradientTexture(SDL_Texture &layer)
{
// Cambia el puntero del renderizador a la textura
SDL_SetRenderTarget(renderer, &layer);
// Dibuja el degradado de fondo
const float num_lines = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; ++i)
{
float step = ((float)i / num_lines);
int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step);
int g = bgColor1.g + ((bgColor2.g - bgColor1.g) * step);
int b = bgColor1.b + ((bgColor2.b - bgColor1.b) * step);
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
SDL_RenderDrawLine(renderer, PLAY_AREA_LEFT, i, PLAY_AREA_RIGHT, i);
}
// Vuelve a colocar el renderizador apuntando a la pantalla
SDL_SetRenderTarget(renderer, nullptr);
}
// Crea la textura con el mapeado de la habitación
void Map::fillMapTexture()
void Map::fillMapTexture(SDL_Texture &layer, std::vector<int> tilemap, bool clean)
{
// Crea variables
SDL_Rect clip = {0, 0, tile_size, tile_size};
// Rellena la capa BG
// Cambia el puntero del renderizador a la textura
SDL_SetRenderTarget(renderer, &layer);
// Limpia la textura
if (clean)
{
SDL_SetRenderTarget(renderer, map_layerBG);
SDL_SetTextureBlendMode(map_layerBG, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
}
// Dibuja el degradado de fondo
const float num_lines = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; ++i)
// Dibuja el mapeado de tiles
for (int y = 0; y < map_height; ++y)
for (int x = 0; x < map_width; ++x)
{
float step = ((float)i / num_lines);
int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step);
int g = bgColor1.g + ((bgColor2.g - bgColor1.g) * step);
int b = bgColor1.b + ((bgColor2.b - bgColor1.b) * step);
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
SDL_RenderDrawLine(renderer, 0, i, 319, i);
// Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0.
// El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
// Dibuja el mapeado de tiles
for (int y = 0; y < map_height; ++y)
for (int x = 0; x < map_width; ++x)
{
// Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0.
// El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap_BG[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap_BG[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
tilemap_BG.clear();
}
// Rellena la capa 0
{
SDL_SetRenderTarget(renderer, map_layer0);
SDL_SetTextureBlendMode(map_layer0, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
// Dibuja el mapeado de tiles
for (int y = 0; y < map_height; ++y)
for (int x = 0; x < map_width; ++x)
{
// Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0.
// El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap_l0[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap_l0[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
tilemap_l0.clear();
}
// Rellena la capa 1
{
SDL_SetRenderTarget(renderer, map_layer1);
SDL_SetTextureBlendMode(map_layer1, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
// Dibuja el mapeado de tiles
for (int y = 0; y < map_height; ++y)
for (int x = 0; x < map_width; ++x)
{
// Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0.
// El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap_l1[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap_l1[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
tilemap_l1.clear();
}
// Vuelve a colocar el renderizador
// Vuelve a colocar el renderizador apuntando a la pantalla
SDL_SetRenderTarget(renderer, nullptr);
}
@@ -553,7 +531,6 @@ void Map::render()
void Map::renderLayerBG()
{
// Dibuja la textura con el mapa en pantalla
//SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT};
SDL_RenderCopy(renderer, map_layerBG, nullptr, nullptr);
}
@@ -575,7 +552,6 @@ void Map::renderLayer0()
}
else
{
// SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT};
SDL_RenderCopy(renderer, map_layer0, nullptr, nullptr);
}
}
@@ -584,7 +560,6 @@ void Map::renderLayer0()
void Map::renderLayer1()
{
// Dibuja la textura con el mapa en pantalla
//SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT};
SDL_RenderCopy(renderer, map_layer1, nullptr, nullptr);
}
@@ -802,5 +777,5 @@ std::string Map::getEnemyFile()
void Map::reLoadTextures()
{
texture_tile->reLoad();
fillMapTexture();
// fillMapTexture();
}

View File

@@ -44,9 +44,7 @@ private:
std::string room_right; // Identificador de la habitación que se encuentra a la derecha
std::string enemy_file; // Fichero con los enemigos para la habitación
std::string tileset_img; // Imagen con los graficos para la habitación
std::vector<int> tilemap_BG; // Indice de los tiles a dibujar de la capa BG en la habitación
std::vector<int> tilemap_l0; // Indice de los tiles a dibujar de la capa 0 en la habitación
std::vector<int> tilemap_l1; // Indice de los tiles a dibujar de la capa 1 en la habitación
std::string tileMapFile; // Fichero con el mapa de indices de tile
std::vector<int> collisionmap; // Indice con los tipos de tile de la habitación
LTexture *texture_tile; // Textura con los graficos de los tiles habitación
SDL_Texture *map_layerBG; // Textura para dibujar la capa BG del mapa de la habitación
@@ -64,8 +62,11 @@ private:
int map_height; // Alto del mapa en tiles
int tileset_width; // Ancho del tileset en tiles
// Carga las variables desde un fichero
bool load(std::string file);
// Carga las variables desde un fichero de mapa
bool loadMapFile(std::string file);
// Carga las variables y texturas desde un fichero de mapa de tiles
bool loadMapTileFile(std::string file);
// Asigna variables a partir de dos cadenas
bool setVars(std::string var, std::string value);
@@ -73,8 +74,11 @@ private:
// Asigna variables a una estructura actor_t
bool setActor(actor_t *actor, SDL_Point *p1, SDL_Point *p2, std::string var, std::string value);
// Pinta el mapa de la habitación en la textura
void fillMapTexture();
// Pinta el degradado en la textura
void fillGradientTexture(SDL_Texture &layer);
// Pinta el mapa de tiles en la textura
void fillMapTexture(SDL_Texture &layer, std::vector<int> tilemap, bool clean);
public:
// Constructor