Añadida la textura para el mapa

This commit is contained in:
2022-07-03 08:54:57 +02:00
parent f14388530d
commit 0a24af663f
3 changed files with 38 additions and 6 deletions

View File

@@ -11,6 +11,14 @@ Room::Room(std::string _file_path, SDL_Renderer *_renderer, Asset *_asset)
renderer = _renderer;
load(_file_path);
loadTextureFromFile(texture, asset->get(tileset), renderer);
// Crea la textura para el mapa de tiles de la habitación
map_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (map_texture == NULL)
printf("Error: map_texture could not be created!\nSDL Error: %s\n", SDL_GetError());
// Pinta el mapa de la habitación en la textura
fillMapTexture();
}
// Destructor
@@ -19,6 +27,9 @@ Room::~Room()
texture->unload();
delete texture;
texture = nullptr;
SDL_DestroyTexture(map_texture);
map_texture = nullptr;
}
// Carga una habitación desde un fichero
@@ -158,9 +169,14 @@ color_t Room::getBGColor()
return color;
}
// Dibuja la habitación en pantalla
void Room::draw()
// Crea la textura con el mapeado de la habitación
void Room::fillMapTexture()
{
SDL_SetRenderTarget(renderer, map_texture);
SDL_SetTextureBlendMode(map_texture, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_Rect clip = {0, 0, 8, 8};
for (int y = 0; y < 16; y++)
for (int x = 0; x < 32; x++)
@@ -169,4 +185,15 @@ void Room::draw()
clip.y = tilemap[(y * 16) + x] * 8;
texture->render(renderer, x * 8, y * 8, &clip);
}
SDL_SetRenderTarget(renderer, nullptr);
}
// Dibuja el mapa en pantalla
void Room::drawMap()
{
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
// Dibuja la textura con el mapa en pantalla
SDL_RenderCopy(renderer, map_texture, &rect, NULL);
}