Añadida una capa estática de fondo

This commit is contained in:
2022-09-24 07:32:48 +02:00
parent 330185720d
commit 45f9a5b494
3 changed files with 83 additions and 11 deletions

View File

@@ -119,6 +119,9 @@ void Game::render()
screen->start(); screen->start();
screen->clean(); screen->clean();
// Dibuja la capa BG
map->renderLayerBG();
// Dibuja la capa 0 // Dibuja la capa 0
map->renderLayer0(); map->renderLayer0();

View File

@@ -24,6 +24,10 @@ Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset, ItemTracker *it
tileset_width = texture_tile->getWidth() / tile_size; tileset_width = texture_tile->getWidth() / tile_size;
// Crea las texturas para dibujar el mapa // 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());
map_layer0 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT); map_layer0 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
if (map_layer0 == NULL) if (map_layer0 == NULL)
printf("Error: map_layer0 could not be created!\nSDL Error: %s\n", SDL_GetError()); printf("Error: map_layer0 could not be created!\nSDL Error: %s\n", SDL_GetError());
@@ -81,10 +85,36 @@ bool Map::load(std::string file_path)
if (file2.good()) if (file2.good())
{ {
bool map_col_read = false; bool map_col_read = false;
bool map_BG_read = false;
bool map_l0_read = false; bool map_l0_read = false;
bool map_l1_read = false; bool map_l1_read = false;
while (std::getline(file2, line)) // Lee el fichero linea a linea 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) if (!map_l0_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa { // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0; int pos = 0;
@@ -428,10 +458,10 @@ void Map::fillMapTexture()
// Crea variables // Crea variables
SDL_Rect clip = {0, 0, tile_size, tile_size}; SDL_Rect clip = {0, 0, tile_size, tile_size};
// Rellena la capa 0 // Rellena la capa BG
{ {
SDL_SetRenderTarget(renderer, map_layer0); SDL_SetRenderTarget(renderer, map_layerBG);
SDL_SetTextureBlendMode(map_layer0, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(map_layerBG, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
@@ -448,6 +478,27 @@ void Map::fillMapTexture()
SDL_RenderDrawLine(renderer, 0, i, 319, i); SDL_RenderDrawLine(renderer, 0, i, 319, i);
} }
// 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 // Dibuja el mapeado de tiles
for (int y = 0; y < map_height; ++y) for (int y = 0; y < map_height; ++y)
for (int x = 0; x < map_width; ++x) for (int x = 0; x < map_width; ++x)
@@ -459,6 +510,8 @@ void Map::fillMapTexture()
clip.y = ((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); texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
} }
tilemap_l0.clear();
} }
// Rellena la capa 1 // Rellena la capa 1
@@ -479,6 +532,8 @@ void Map::fillMapTexture()
clip.y = ((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); texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
} }
tilemap_l1.clear();
} }
// Vuelve a colocar el renderizador // Vuelve a colocar el renderizador
@@ -488,11 +543,20 @@ void Map::fillMapTexture()
// Dibuja todos los elementos del mapa // Dibuja todos los elementos del mapa
void Map::render() void Map::render()
{ {
renderLayerBG();
renderLayer0(); renderLayer0();
renderLayer1(); renderLayer1();
renderActors(); renderActors();
} }
// Dibuja la capa BG
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);
}
// Dibuja la capa 0 // Dibuja la capa 0
void Map::renderLayer0() void Map::renderLayer0()
{ {
@@ -512,7 +576,7 @@ void Map::renderLayer0()
else else
{ {
// SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT}; // SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT};
SDL_RenderCopy(renderer, map_layer0, NULL, NULL); SDL_RenderCopy(renderer, map_layer0, nullptr, nullptr);
} }
} }
@@ -520,8 +584,8 @@ void Map::renderLayer0()
void Map::renderLayer1() void Map::renderLayer1()
{ {
// Dibuja la textura con el mapa en pantalla // Dibuja la textura con el mapa en pantalla
SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT}; //SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT};
SDL_RenderCopy(renderer, map_layer1, NULL, &rect); SDL_RenderCopy(renderer, map_layer1, nullptr, nullptr);
} }
// Dibuja los actores // Dibuja los actores

View File

@@ -44,10 +44,12 @@ private:
std::string room_right; // Identificador de la habitación que se encuentra a la derecha 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 enemy_file; // Fichero con los enemigos para la habitación
std::string tileset_img; // Imagen con los graficos 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_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::vector<int> tilemap_l1; // Indice de los tiles a dibujar de la capa 1 en la habitación
std::vector<int> collisionmap; // Indice con los tipos de tile de la habitación 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 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
SDL_Texture *map_layer0; // Textura para dibujar la capa 0 del mapa de la habitación SDL_Texture *map_layer0; // Textura para dibujar la capa 0 del mapa de la habitación
SDL_Texture *map_layer1; // Textura para dibujar la capa 1 del mapa de la habitación SDL_Texture *map_layer1; // Textura para dibujar la capa 1 del mapa de la habitación
std::vector<Actor *> actors; // Listado con los actores de la habitación std::vector<Actor *> actors; // Listado con los actores de la habitación
@@ -87,6 +89,9 @@ public:
// Dibuja todos los elementos del mapa // Dibuja todos los elementos del mapa
void render(); void render();
// Dibuja la capa BG
void renderLayerBG();
// Dibuja la capa 1 // Dibuja la capa 1
void renderLayer0(); void renderLayer0();