Añadida la textura para el mapa
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "ifdefs.h"
|
||||
#include "const.h"
|
||||
#include "utils.h"
|
||||
#include "asset.h"
|
||||
#include <string>
|
||||
@@ -39,7 +40,8 @@ private:
|
||||
std::vector<int> 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
|
||||
|
||||
Reference in New Issue
Block a user