From 0a24af663fe1c5f860144304acb1390942b3115e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 3 Jul 2022 08:54:57 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adida=20la=20textura=20para=20el=20mapa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/game.cpp | 2 +- source/room.cpp | 31 +++++++++++++++++++++++++++++-- source/room.h | 11 ++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index d8e2dd8..cce99c5 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -102,7 +102,7 @@ section_t Game::run() mScreen->clean(color); mScreen->clean(mRoom->getBGColor()); - mRoom->draw(); + mRoom->drawMap(); // Escribe las medidas de ancho y alto de la pantalla // mText->writeCentered(GAMECANVAS_CENTER_X, 0, std::to_string(GAMECANVAS_WIDTH), -1); diff --git a/source/room.cpp b/source/room.cpp index a3d6d9c..a09e7c1 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -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); } \ No newline at end of file diff --git a/source/room.h b/source/room.h index 9811b7a..5c73d53 100644 --- a/source/room.h +++ b/source/room.h @@ -1,5 +1,6 @@ #pragma once #include "ifdefs.h" +#include "const.h" #include "utils.h" #include "asset.h" #include @@ -39,7 +40,8 @@ private: std::vector item_list; // Listado con los items que hay en la habitación LTexture *texture; // Textura con los graficos de la habitación Asset *asset; // Objeto con la ruta a todos los ficheros de recursos - SDL_Renderer *renderer; // El renderizador de la ventana + SDL_Renderer *renderer; // El renderizador de la ventana + SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación // Carga una habitación desde un fichero bool load(std::string _file_path); @@ -47,6 +49,9 @@ private: // Asigna variables a partir de dos cadenas bool setVars(std::string _var, std::string _value); + // Pinta el mapa de la habitación en la textura + void fillMapTexture(); + public: // Constructor Room(std::string _file_path, SDL_Renderer *_renderer, Asset *_asset); @@ -60,8 +65,8 @@ public: // Devuelve el color de la habitación color_t getBGColor(); - // Dibuja la habitación en pantalla - void draw(); + // Dibuja el mapa en pantalla + void drawMap(); }; #endif