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
60 lines (54 loc) · 1.51 KB
/
Copy pathTileMapComponent.cpp
File metadata and controls
60 lines (54 loc) · 1.51 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
#include "TileMapComponent.h"
#include "Actor.h"
TileMapComponent::TileMapComponent(Actor * owner, const TileMapTexture& tilemapTexture, int drawOrder)
: SpriteComponent(owner, drawOrder), tilemapTexture(tilemapTexture) {
}
void TileMapComponent::Draw(SDL_Renderer * renderer) {
int tw = GetTexWidth();
int th = GetTexHeight();
int cw = tw / tilemapTexture.GetColumnCount();
int ch = th / tilemapTexture.GetRowCount();
int x = 0;
int y = 0;
for (int i = 0; i < tilemapTexture.GetLayerCount(); i++) {
const CSV<int>& csv = tilemapTexture.GetLayer(i);
x = 0;
y = 0;
for (int j = 0; j < csv.Count(); j++) {
const CSVRow<int> row = csv.GetRowAt(j);
for (int k = 0; k < row.Count(); k++) {
int index = row.GetElementAt(k);
drawCell(renderer, index, x, y, cw, ch);
x += cw;
}
y += ch;
x = 0;
}
}
}
void TileMapComponent::SetTileMapTexture(const TileMapTexture & tilemapTexture) {
this->tilemapTexture = tilemapTexture;
}
void TileMapComponent::drawCell(SDL_Renderer* renderer, int index, int x, int y, int cw, int ch) {
if (index == -1) {
return;
}
SDL_Rect r;
r.x = x;
r.y = y;
r.w = cw;
r.h = ch;
SDL_Rect srcrect;
srcrect.w = cw;
srcrect.h = ch;
srcrect.x = (index % tilemapTexture.GetColumnCount()) * cw;
srcrect.y = (index / tilemapTexture.GetColumnCount()) * ch;
// Draw (have to convert angle from radians to degrees, and clockwise to counter)
SDL_RenderCopyEx(renderer,
mTexture,
&srcrect,
&r,
-Math::ToDegrees(mOwner->GetRotation()),
nullptr,
SDL_FLIP_NONE);
}