forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMapComponent.cpp
More file actions
93 lines (83 loc) · 2.02 KB
/
TileMapComponent.cpp
File metadata and controls
93 lines (83 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "TileMapComponent.h"
#include "Actor.h"
#include <fstream>
#include <sstream>
#include <iostream>
TileMapComponent::TileMapComponent(Actor* owner, int drawOrder)
:SpriteComponent(owner, drawOrder)
{
}
void TileMapComponent::Draw(SDL_Renderer* renderer)
{
if (mTexture)
{
SDL_Rect dsrect;
dsrect.w = mTileSize;
dsrect.h = mTileSize;
for (int i = 0; i < mLoadedCSV.size(); i++)
{
dsrect.y = i * mTileSize;
for (int j = 0; j < mLoadedCSV[i].size(); j++)
{
if (mLoadedCSV[i][j] > -1)
{
dsrect.x = j * mTileSize;
// Draw (have to convert angle from radians to degrees, and clockwise to counter)
SDL_RenderCopyEx(renderer,
mTexture,
&mTileRects[mLoadedCSV[i][j]],
&dsrect,
-Math::ToDegrees(mOwner->GetRotation()),
nullptr,
SDL_FLIP_NONE);
}
}
}
}
}
void TileMapComponent::LoadTilemap(std::string&& CSVPath, SDL_Texture* tilemapTexture, int tileSize)
{
mTexture = tilemapTexture;
mTileSize = tileSize;
SDL_QueryTexture(tilemapTexture, nullptr, nullptr, &mTexWidth, &mTexHeight);
// Split the texture in to tiles to use with a csv tilemap.
int tilesW = mTexWidth / tileSize;
int tilesH = mTexHeight / tileSize;
for (int i = 0; i < tilesH; i++)
{
for (int j = 0; j < tilesW; j++)
{
SDL_Rect tilerect;
tilerect.x = j * tileSize;
tilerect.y = i * tileSize;
tilerect.w = tileSize;
tilerect.h = tileSize;
mTileRects.emplace_back(std::move(tilerect));
}
}
std::cout << "Tiles loaded: " << mTileRects.size() << std::endl;
// Load desired tiles from csv file.
std::ifstream fin(std::move(CSVPath));
std::string s;
char cstring[4];
while (std::getline(fin, s))
{
std::istringstream ss;
ss.str(s);
std::vector<int> CSVLine;
while (ss.getline(cstring, 4, ','))
{
CSVLine.emplace_back(stoi(std::string(cstring)));
}
mLoadedCSV.emplace_back(std::move(CSVLine));
}
// Print loaded tilemap to console.
for (auto& intvec : mLoadedCSV)
{
for (int i : intvec)
{
std::cout << i;
}
std::cout << std::endl;
}
}