TileMapComponent now takes a TMX to render tilemap
This commit is contained in:
@ -15,16 +15,20 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include "tmxparser.h"
|
||||
|
||||
class TileMapComponent : public Component
|
||||
{
|
||||
public:
|
||||
TransformComponent *transform;
|
||||
// TransformComponent *transform;
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect srcRect, destRect;
|
||||
Vector2D position;
|
||||
std::tuple <SDL_Rect,SDL_Rect> tile;
|
||||
|
||||
// std::tuple <SDL_Rect,SDL_Rect> tile;
|
||||
tmxparser::TmxMap map;
|
||||
int globalScale;
|
||||
std::vector<SDL_Rect> tileSet;
|
||||
|
||||
TileMapComponent() = default;
|
||||
|
||||
~TileMapComponent()
|
||||
@ -32,19 +36,28 @@ public:
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
TileMapComponent(std::string id, int tsize, int tscale)
|
||||
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale)
|
||||
{
|
||||
setTex(id);
|
||||
// position.x = xpos;
|
||||
// position.y = ypos;
|
||||
//
|
||||
// srcRect.x = srcX;
|
||||
// srcRect.y = srcY;
|
||||
srcRect.w = srcRect.h = tsize;
|
||||
|
||||
// destRect.x = xpos;
|
||||
// destRect.y = ypos;
|
||||
destRect.w = destRect.h = tsize * tscale;
|
||||
map = loadedMap;
|
||||
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
|
||||
Game::assets->AddTexture(loadedMap.tilesetCollection[0].name, texturePath.c_str());
|
||||
setTex(map.tilesetCollection[0].name);
|
||||
globalScale = gScale;
|
||||
int totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
|
||||
// std::cout << "Number of Tiles: " << totalTiles << std::endl;
|
||||
tileSet.resize(totalTiles);
|
||||
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
|
||||
for (int c=0;c<map.tilesetCollection[0].colCount;c++){
|
||||
srcRect.x = c*map.tilesetCollection[0].tileWidth;
|
||||
srcRect.y = r*map.tilesetCollection[0].tileHeight;
|
||||
srcRect.w = srcRect.h = map.tileWidth;
|
||||
int element = r*map.tilesetCollection[0].colCount+c;
|
||||
// std::cout << "element " << element << std::endl;
|
||||
// std::cout << "srcRect= x: " << srcRect.x << " y: " << srcRect.y << " srcRect.w & h: " << srcRect.w << std::endl;
|
||||
tileSet[element] = srcRect;
|
||||
}
|
||||
}
|
||||
destRect.w = destRect.h = map.tileWidth * gScale;
|
||||
}
|
||||
|
||||
void update() override
|
||||
@ -56,19 +69,19 @@ public:
|
||||
void draw() override
|
||||
{
|
||||
//iterate through rows and columns of the map to draw the tiles
|
||||
|
||||
TextureManager::Draw(texture, srcRect, destRect, SDL_FLIP_NONE);
|
||||
// First cycle through rows
|
||||
for (int r = 0;r<map.height;r++){
|
||||
// Next cycle through each column or tile in that row:
|
||||
for (int c = 0;c<map.width;c++){
|
||||
int tileToDraw = map.layerCollection[0].tiles[c].gid-1;
|
||||
destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::tuple<SDL_Rect,SDL_Rect> getTile(char currentTile, int i)
|
||||
{
|
||||
std::tuple<SDL_Rect,SDL_Rect> tileTuple;
|
||||
|
||||
|
||||
tileTuple = std::make_tuple(srcRect,destRect);
|
||||
return tileTuple;
|
||||
}
|
||||
|
||||
|
||||
void setTex(std::string id)
|
||||
{
|
||||
texture = Game::assets->GetTexture(id);
|
||||
|
Reference in New Issue
Block a user