-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleitor.cpp
407 lines (356 loc) · 9.84 KB
/
leitor.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "leitor.h"
//using namespace std;
string nomeArquivoSvg;
string nomeArquivoPmg;
string nomeArquivoPmg2;
string variavelNome1;
string variavelNome2;
string variavelNome3 = ".";
string variavelNome4;
int flag = 0;
int flagArquivo = 0;
int widthPmg;
int heightPmg;
float menorx = 1000;
float menory = 1000;
int** matrizMobility;
int** matrizDistance;
float corMax;
bool flagMatriz = true;
float shootSpeed;
float tankSpeed;
float shootsRate;
vector <Circulo> vetorCirculo;
Circulo circo;
vector <Retangulo> vetorRetangulo;
Retangulo reto;
const unsigned int NUM_INDENTS_PER_SPACE=2;
static int Getdec(char hexchar)
{
if ((hexchar >= '0') && (hexchar <= '9')) return hexchar - '0';
if ((hexchar >= 'A') && (hexchar <= 'F')) return hexchar - 'A' + 10;
if ((hexchar >= 'a') && (hexchar <= 'f')) return hexchar - 'a' + 10;
return -1; // Wrong character
}
float HexColorToFloat(string HexColor)
{
string HexColorPtr = HexColor;
float IntColor;
IntColor = (Getdec(HexColorPtr[0]) * 16) +
Getdec(HexColorPtr[1]);
IntColor = IntColor / 255.0;
return IntColor;
}
void drawRectangulo (Retangulo r)
{
glColor3f(r.R,r.G,r.B);
glBegin(GL_QUADS);
glVertex2f (r.x - menorx, heightPmg - (-r.y + heightPmg + menory));
glVertex2f (r.x - menorx, heightPmg - (-r.y - r.altura + heightPmg + menory));
glVertex2f (r.x - menorx + r.largura, heightPmg - (-r.y - r.altura + heightPmg + menory));
glVertex2f (r.x - menorx + r.largura, heightPmg -(-r.y + heightPmg + menory));
glEnd();
}
void drawCircle(Circulo c)
{
// Player: 0.784314 // player < 0.8 /// inimigo > 0.8 float!!!
float x = c.raio;
float y = 0;
float theta = 2*3.1415/c.raio;
float cose = cos(theta);
float seno = sin(theta);
float trans;
glColor3f(c.R,c.G,c.B);
glBegin(GL_POLYGON);
for(int i = 0; i < 360; i++)
{
glVertex2f(x+c.x - menorx, heightPmg - (y+-c.y + heightPmg + menory));
trans = x;
x = cose * x - seno * y;
y = seno * trans + cose * y;
}
glEnd();
}
void putInRectangulo (float height, float width, float x, float y, float R, float G, float B)
{
reto.altura = height;
reto.largura = width;
reto.x = x;
reto.y = y;
reto.R = R;
reto.G = G;
reto.B = B;
if(B > 0.4)
reto.teletransporte = true;
else
reto.teletransporte = false;
vetorRetangulo.push_back(reto);
}
void putInCircle(float cx, float cy, float raio, float R, float G, float B)
{
circo.x = cx;
circo.y = cy;
circo.raio = raio;
circo.R = R;
circo.G = G;
circo.B = B;
inicializaTank(circo.x, circo.y, circo.R, circo.raio);
//cout << "BUIIBVIBUBU\n";
vetorCirculo.push_back(circo);
}
void seeRect(TiXmlElement* pElement) {
float width, height, x, y, R, G, B;
stringstream t;
TiXmlAttribute* pAttrib = pElement->FirstAttribute();
while (pAttrib)
{
if (!strcmp(pAttrib->Name(), "style")) {
string value = pAttrib->Value();
string colorHex = value.substr(6, 6);
string Rh = colorHex.substr(0, 2); istringstream(Rh) >> hex >> R;
string Gh = colorHex.substr(2, 2); istringstream(Gh) >> hex >> G;
string Bh = colorHex.substr(4, 2); istringstream(Bh) >> hex >> B;
R = HexColorToFloat(Rh);
G = HexColorToFloat(Gh);
B = HexColorToFloat(Bh);
} else if (!strcmp(pAttrib->Name(), "width")) {
width = atof(pAttrib->Value());
} else if (!strcmp(pAttrib->Name(), "height")) {
height = atof(pAttrib->Value());
} else if (!strcmp(pAttrib->Name(), "x")) {
x = atof(pAttrib->Value());
} else if (!strcmp(pAttrib->Name(), "y")) {
y = atof(pAttrib->Value());
}
pAttrib = pAttrib->Next();
}
if (x < menorx)
menorx = x;
if (y < menory)
menory = y;
//drawRectangulo(height, width, x, y, R, G, B);
putInRectangulo(height, width, x, y, R, G, B);
}
void seeCircle(TiXmlElement* pElement) {
float cx, cy, raio, R, G, B;
TiXmlAttribute* pAttrib = pElement->FirstAttribute();
while (pAttrib)
{
if (!strcmp(pAttrib->Name(), "fill")) {
string value = pAttrib->Value();
string colorHex = value.substr(1, 6);
string Rh = colorHex.substr(0, 2); istringstream(Rh) >> hex >> R;
string Gh = colorHex.substr(2, 2); istringstream(Gh) >> hex >> G;
string Bh = colorHex.substr(4, 2); istringstream(Bh) >> hex >> B;
R = HexColorToFloat(Rh);
G = HexColorToFloat(Gh);
B = HexColorToFloat(Bh);
} else if (!strcmp(pAttrib->Name(), "cx")) {
cx = atof(pAttrib->Value());
} else if (!strcmp(pAttrib->Name(), "cy")) {
cy = atof(pAttrib->Value());
} else if (!strcmp(pAttrib->Name(), "r")){
raio = atof(pAttrib->Value());
}
pAttrib = pAttrib->Next();
}
//drawCircle(cx, cy, raio, R, G, B);
putInCircle(cx, cy, raio, R, G, B);
}
const char * getIndent( unsigned int numIndents )
{
static const char * pINDENT=" + ";
static const unsigned int LENGTH=strlen( pINDENT );
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if ( n > LENGTH ) n = LENGTH;
return &pINDENT[ LENGTH-n ];
}
// same as getIndent but no "+" at the end
const char * getIndentAlt( unsigned int numIndents )
{
static const char * pINDENT=" ";
static const unsigned int LENGTH=strlen( pINDENT );
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if ( n > LENGTH ) n = LENGTH;
return &pINDENT[ LENGTH-n ];
}
int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
if ( !pElement ) return 0;
TiXmlAttribute* pAttrib=pElement->FirstAttribute();
int i=0;
int ival;
double dval;
const char* pIndent=getIndent(indent);
//printf("\n");
while (pAttrib)
{
// Receber o nome dos arquivos
if (flagArquivo != 4){
if (flag == 0){
variavelNome2 = pAttrib->Value();
//variavelNome2[0] = toupper(variavelNome2[0]); //Mudei
flag = 1;
}
else if (flag == 1){
variavelNome4 = pAttrib->Value();
flag = 2;
}
else if (flag == 2){
variavelNome1 = pAttrib->Value();
if (flagArquivo == 0){
nomeArquivoSvg = variavelNome1 + variavelNome2 + variavelNome3 + variavelNome4;
flagArquivo = 1;
}
else if (flagArquivo == 1){
nomeArquivoPmg = variavelNome1 + variavelNome2 + variavelNome3 + variavelNome4;
flagArquivo = 2;
}
else if (flagArquivo == 2){
nomeArquivoPmg2 = variavelNome1 + variavelNome2 + variavelNome3 + variavelNome4;
flagArquivo = 3;
}
else if (flagArquivo == 3){
shootsRate = atof(variavelNome1.c_str());
shootSpeed = atof(variavelNome2.c_str());
tankSpeed = atof(variavelNome4.c_str());
flagArquivo = 4;
}
flag = 0;
}
}
//if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) printf( " int=%d", ival);
//if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
//printf( "\n" );
i++;
pAttrib=pAttrib->Next();
}
return i;
}
void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
if ( !pParent ) return;
TiXmlNode* pChild;
TiXmlText* pText;
int t = pParent->Type();
//printf( "%s", getIndent(indent));
int num;
switch ( t )
{
case TiXmlNode::TINYXML_DOCUMENT:
//printf( "Document" );
break;
case TiXmlNode::TINYXML_ELEMENT:
//printf( "Element [%s]", pParent->Value() );
if (!strcmp(pParent->Value(), "rect")) {
seeRect(pParent->ToElement());
}
if (!strcmp(pParent->Value(), "circle")) {
seeCircle(pParent->ToElement());
}
num=dump_attribs_to_stdout(pParent->ToElement(), indent+1);
switch(num)
{
//case 0: printf( " (No attributes)"); break;
//case 1: printf( "%s1 attribute", getIndentAlt(indent)); break;
//default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
}
break;
case TiXmlNode::TINYXML_COMMENT:
//printf( "Comment: [%s]", pParent->Value());
break;
case TiXmlNode::TINYXML_UNKNOWN:
//printf( "Unknown" );
break;
case TiXmlNode::TINYXML_TEXT:
pText = pParent->ToText();
//printf( "Text: [%s]", pText->Value() );
break;
case TiXmlNode::TINYXML_DECLARATION:
//printf( "Declaration" );
break;
default:
break;
}
//printf( "\n" );
for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
dump_to_stdout( pChild, indent+1 );
}
}
//Carregar a imagem SVG
void openSvg(const char* pFilename)
{
TiXmlDocument doc(pFilename);
bool loadOkay = doc.LoadFile();
if (loadOkay)
{
//printf(" Open Success! \n%s:\n", pFilename);
dump_to_stdout( &doc ); // defined later in the tutorial
}
else
{
printf("Failed to load file \"%s\"\n", pFilename);
}
}
//Carregar a imagem PMG
void openPgm(const char* pFilename)
{
ifstream doc2;
doc2.open(pFilename);
string linha, largura, altura;
if (doc2.is_open()){
printf("Show to load file \"%s\"\n", pFilename);
getline( doc2 , linha );
getline( doc2 , linha );
getline( doc2 , linha );
stringstream stream(linha.c_str());
stream >> largura;
stream >> altura;
widthPmg = atof(largura.c_str());
heightPmg = atof(altura.c_str());
getline(doc2, linha);
corMax = atof(linha.c_str());
if (flagMatriz)
{
matrizMobility = (int**)malloc(sizeof(int*)*(heightPmg));
for (int cont = 0; cont < heightPmg; cont++){
matrizMobility[cont] = (int*)malloc(sizeof(int)*(widthPmg));
for (int cont2 = 0; cont2 < widthPmg; cont2++)
{
getline(doc2, linha);
matrizMobility[cont][cont2] = atof(linha.c_str());
}
}
flagMatriz = false;
}
else
{
matrizDistance = (int**)malloc(sizeof(int*)*(heightPmg));
for (int cont = 0; cont < heightPmg; cont++){
matrizDistance[cont] = (int*)malloc(sizeof(int)*(widthPmg));
for (int cont2 = 0; cont2 < widthPmg; cont2++)
{
getline(doc2, linha);
matrizDistance[cont][cont2] = atof(linha.c_str());
}
}
}
}
else
printf("Failed to load file \"%s\"\n", pFilename);
}
void openXml(const char* pFilename)
{
TiXmlDocument docX(pFilename);
bool loadOkay = docX.LoadFile();
if (loadOkay)
{
printf(" Open Success! \n%s:\n", pFilename);
dump_to_stdout( &docX ); // defined later in the tutorial
}
else
{
printf("Failed to load file \"%s\"\n", pFilename);
}
}