TileMaps now render each row correctly

This commit is contained in:
2022-03-27 23:59:12 -04:00
parent 72b4ac9229
commit 871e6d19c6
9 changed files with 152 additions and 162 deletions

View File

@ -23,11 +23,13 @@ public:
// TransformComponent *transform;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
Vector2D position;
// Vector2D position;
// std::tuple <SDL_Rect,SDL_Rect> tile;
tmxparser::TmxMap map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
int totalTiles;
TileMapComponent() = default;
@ -40,10 +42,10 @@ public:
{
map = loadedMap;
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
Game::assets->AddTexture(loadedMap.tilesetCollection[0].name, texturePath.c_str());
Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());
setTex(map.tilesetCollection[0].name);
globalScale = gScale;
int totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
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++){
@ -57,25 +59,37 @@ public:
tileSet[element] = srcRect;
}
}
destRects.resize(totalTiles);
for (int i=0;i<totalTiles-1;i++){
SDL_Rect myRect = SDL_Rect();
myRect.w = myRect.h = map.tileWidth * gScale;
myRect.x = myRect.y = 0;
destRects[i] = myRect;
}
destRect.w = destRect.h = map.tileWidth * gScale;
}
void update() override
{
destRect.x = position.x - Game::camera.x;
destRect.y = position.y - Game::camera.y;
// for (int i=0;i<totalTiles-1;i++){
// destRects[i].x = destRects[i].x - Game::camera.x;
// destRects[i].y = destRects[i].y - Game::camera.y;
// }
}
void draw() override
{
//iterate through rows and columns of the map to draw the tiles
// First cycle through rows
for (int r = 0;r<map.height;r++){
for (int r = 0;r<map.height-1;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;
for (int c = 0;c<map.width-1;c++){
int i = r*map.width+c;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
// destRects[i].x = 8;
// destRects[i].y = 32;
TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE);
}
}