TileMaps now render and move with camera
This commit is contained in:
parent
871e6d19c6
commit
c8aae59f26
@ -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
|
||||
</data>
|
||||
</layer>
|
||||
<layer id="3" name="Collision" width="42" height="42">
|
||||
<layer id="3" name="Collision" width="42" height="42" visible="0">
|
||||
<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,
|
||||
|
Binary file not shown.
@ -15,6 +15,8 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
// #include <algorithm>
|
||||
// #include <iterator>
|
||||
#include "tmxparser.h"
|
||||
|
||||
class TileMapComponent : public Component
|
||||
@ -29,6 +31,7 @@ public:
|
||||
int globalScale;
|
||||
std::vector<SDL_Rect> tileSet;
|
||||
std::vector<SDL_Rect> destRects;
|
||||
// std::array destRects;
|
||||
int totalTiles;
|
||||
|
||||
TileMapComponent() = default;
|
||||
@ -59,22 +62,39 @@ 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;
|
||||
destRects.resize(map.width*map.height);
|
||||
// std::cout << "Number of Tiles on Map: " << destRects.size() << std::endl;
|
||||
// for (int i=0;i<totalTiles-1;i++){
|
||||
// SDL_Rect myRect = SDL_Rect();
|
||||
// myRect.w = myRect.h = map.tileWidth * gScale;
|
||||
// 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;
|
||||
}
|
||||
|
||||
void update() override
|
||||
{
|
||||
// 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;
|
||||
// }
|
||||
for (int i=0;i<destRects.size()-1;i++){
|
||||
// SDL_Rect thisRect = destRects[i];
|
||||
destRects[i].x = destRects[i].x - Game::camera.x;
|
||||
destRects[i].y = destRects[i].y - Game::camera.y;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() override
|
||||
@ -85,12 +105,15 @@ public:
|
||||
// Next cycle through each column or tile in that row:
|
||||
for (int c = 0;c<map.width-1;c++){
|
||||
int i = r*map.width+c;
|
||||
int elem = c+r*map.width;
|
||||
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);
|
||||
// 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[elem] = thisRect;
|
||||
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user