-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtileimage.cpp
134 lines (124 loc) · 3.59 KB
/
tileimage.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "tileimage.h"
TileImage::TileImage(Tile *t) : parent(t) {
piece = NULL;
}
TileImage::~TileImage() {
for (int i = 0; i < imageLayer.size(); ++i) {
delete imageLayer.at(i);
}
}
int TileImage::getRealRotation(int rot, int pos) {
rot += imageLayer.at(pos)->rotation;
if (rot >= 4) {
rot -= 4;
}
return rot;
}
vector<int> TileImage::getRotationMatrix(int rot, int size) {
vector<int> matrix;
switch (rot) {
case 0:
matrix.push_back(1);
matrix.push_back(0);
matrix.push_back(0);
matrix.push_back(1);
matrix.push_back(0);
matrix.push_back(0);
break;
case 1:
matrix.push_back(0);
matrix.push_back(1);
matrix.push_back(-1);
matrix.push_back(0);
matrix.push_back(size);
matrix.push_back(0);
break;
case 2:
matrix.push_back(-1);
matrix.push_back(0);
matrix.push_back(0);
matrix.push_back(-1);
matrix.push_back(size);
matrix.push_back(size);
break;
case 3:
matrix.push_back(0);
matrix.push_back(-1);
matrix.push_back(1);
matrix.push_back(0);
matrix.push_back(0);
matrix.push_back(size);
break;
}
return matrix;
}
void TileImage::addImageLayer(ImageLayer *l) {
imageLayer.push_back(l);
}
void TileImage::addFollower(Follower *f) {
followers.push_back(f);
}
void TileImage::removeFollower(Follower *f) {
for (int i = 0; i < followers.size(); ++i) {
if (followers.at(i) == f) {
followers.erase(followers.begin() + i);
return;
}
}
}
void TileImage::removeFollowers() {
while (!followers.empty()) {
followers.pop_back();
}
}
void TileImage::setPiece(Piece *p) {
piece = p;
}
QImage* TileImage::getImage(int size) {
// First create tile from layers
double zoomScale = 1.0 * size / IMG_DEFAULT_SIZE;
QImage *image = new QImage(size, size, IMG_FORMAT); // Deletion is up to receiver
QPainter painter(image);
for (int i = 0; i < imageLayer.size(); ++i) {
int realRot = getRealRotation(parent->getRotation(), i);
vector<int> matrix = getRotationMatrix(realRot, size);
QTransform trans(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
trans.scale(imageLayer.at(i)->scale, imageLayer.at(i)->scale);
trans.translate(imageLayer.at(i)->x * zoomScale / imageLayer.at(i)->scale, imageLayer.at(i)->y * zoomScale / imageLayer.at(i)->scale);
if (!imageLayer.at(i)->stationary) {
painter.setTransform(trans);
}
imageLayer.at(i)->renderer->render(&painter);
}
// Then add followers on top
for (int i = 0; i < followers.size(); ++i) {
int realRot = getRealRotation(parent->getRotation(), i);
vector<int> matrix = getRotationMatrix(realRot, size);
QTransform trans(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
ImageLayer *il = followers.at(i)->getImage();
trans.scale(il->scale, il->scale);
trans.translate(il->x * zoomScale / il->scale, il->y * zoomScale / il->scale);
painter.setTransform(trans);
il->renderer->render(&painter);
QTransform trans2(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
il = followers.at(i)->getOccupationImage();
if (il == NULL) {
continue;
}
trans2.scale(il->scale, il->scale);
trans2.translate(il->x * zoomScale / il->scale, il->y * zoomScale / il->scale);
painter.setTransform(trans2);
il->renderer->render(&painter);
}
// Finally add piece, if any
if (piece != NULL) {
vector<int> matrix = getRotationMatrix(0, size);
QTransform trans(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
ImageLayer *il = piece->getImage();
trans.scale(il->scale, il->scale);
trans.translate(il->x * zoomScale / il->scale, il->y * zoomScale / il->scale);
painter.setTransform(trans);
il->renderer->render(&painter);
}
return image;
}