TileMaps now render and move with camera

This commit is contained in:
Alan Youngblood 2022-04-04 18:16:20 -04:00
parent 871e6d19c6
commit c8aae59f26
3 changed files with 39 additions and 16 deletions

View File

@ -47,7 +47,7 @@
20,21,22,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,11,12,13 20,21,22,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,11,12,13
</data> </data>
</layer> </layer>
<layer id="3" name="Collision" width="42" height="42"> <layer id="3" name="Collision" width="42" height="42" visible="0">
<data encoding="csv"> <data encoding="csv">
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2, 2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2, 2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,

Binary file not shown.

View File

@ -15,6 +15,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <tuple> #include <tuple>
// #include <algorithm>
// #include <iterator>
#include "tmxparser.h" #include "tmxparser.h"
class TileMapComponent : public Component class TileMapComponent : public Component
@ -29,6 +31,7 @@ public:
int globalScale; int globalScale;
std::vector<SDL_Rect> tileSet; std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects; std::vector<SDL_Rect> destRects;
// std::array destRects;
int totalTiles; int totalTiles;
TileMapComponent() = default; TileMapComponent() = default;
@ -59,22 +62,39 @@ public:
tileSet[element] = srcRect; tileSet[element] = srcRect;
} }
} }
destRects.resize(totalTiles); destRects.resize(map.width*map.height);
for (int i=0;i<totalTiles-1;i++){ // std::cout << "Number of Tiles on Map: " << destRects.size() << std::endl;
SDL_Rect myRect = SDL_Rect(); // for (int i=0;i<totalTiles-1;i++){
myRect.w = myRect.h = map.tileWidth * gScale; // SDL_Rect myRect = SDL_Rect();
myRect.x = myRect.y = 0; // myRect.w = myRect.h = map.tileWidth * gScale;
destRects[i] = myRect; // myRect.x = i*map.tilesetCollection[0].tileWidth;
// myRect.y = 0;
// destRects.push_back(myRect);
// }
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-1;c++){
int elem = c+r*map.width;
SDL_Rect thisRect = SDL_Rect();
thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
thisRect.w = thisRect.h = map.tileWidth * globalScale;
// destRects.push_back(thisRect);
destRects[elem] = thisRect;
// std::cout << "destRects[" << elem << "].x = " << destRects[elem].x << "].y = " << destRects[elem].y << "].w = " << destRects[elem].w << std::endl;
}
} }
destRect.w = destRect.h = map.tileWidth * gScale; destRect.w = destRect.h = map.tileWidth * gScale;
} }
void update() override void update() override
{ {
// for (int i=0;i<totalTiles-1;i++){ for (int i=0;i<destRects.size()-1;i++){
// destRects[i].x = destRects[i].x - Game::camera.x; // SDL_Rect thisRect = destRects[i];
// destRects[i].y = destRects[i].y - Game::camera.y; destRects[i].x = destRects[i].x - Game::camera.x;
// } destRects[i].y = destRects[i].y - Game::camera.y;
}
} }
void draw() override void draw() override
@ -85,12 +105,15 @@ public:
// Next cycle through each column or tile in that row: // Next cycle through each column or tile in that row:
for (int c = 0;c<map.width-1;c++){ for (int c = 0;c<map.width-1;c++){
int i = r*map.width+c; int i = r*map.width+c;
int elem = c+r*map.width;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1; int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale; // SDL_Rect thisRect = SDL_Rect();
destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale; // thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
// destRects[i].x = 8; // thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
// destRects[i].y = 32; // thisRect.w = thisRect.h = map.tileWidth * globalScale;
TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE); // destRects[elem] = thisRect;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
// }
} }
} }