-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.cpp
executable file
·544 lines (436 loc) · 14.7 KB
/
tile.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "tile.h"
#include "pugixml.hpp"
level::level(int areaid , GameEngine* myengine)
{
engine = myengine;
LoadLevel( (char*) PALLET );
/*Leagacy Code*/
// int * mapdata;
// int * lampmap;
// //get level data
// level_in( areaid , mapdata, lampmap, levelx, levely);
// //load pallet town tilest and store in level tileset var
// loadMedia( (char*)PALLET /*tile_set_path[ areaid ]*/ , tileSet, engine->get_screen() );
// LoadLevel( (char*)PALLET );
// //mainmap = tilesetup(levelx , levely, mapdata,lampmap );
// spritemap = spritesetup( levelx, levely , mainmap);
}
/*
Grabs the names and loads the tile sets for the level
spider must be able to get to the "tileset" tags in one edge
stores them in the level::tileset.vector
*/
void level::create_tilesets( pugi::xml_node spider )
{
//temporary container for tileset image pointer
SDL_Surface* temp;
int first, Twidth ,id , last;
//holds path to source
char str[256];
pugi::xml_node name;
//traverses the Tileset tags in the TMX file
for( spider = spider.child("tileset"); spider != 0; spider = spider.next_sibling("tileset") )
{
//spider is in the <tileset> tag
//sets first so we can know which prototile we are setting for each tileset
first = spider.attribute("firstgid").as_int();
Twidth = spider.attribute("tilewidth").as_int();
//std::cout << "present tileset first Gid " << temp.first << std::endl;
name = spider.child("image");
//makes it so we can open the TMX files
strcpy(str, "assets/");
strcat(str , (char*)name.attribute("source").value() );
if( loadMedia( str , temp , engine->get_screen()) )
{
//add to vector of tilesets
tilesets.push_back(temp);
//tiles in this tileset
last = name.attribute("width").as_int()/Twidth ;
prototile.resize ( prototile.size() + last);
std::cout <<"prototile is "<< prototile.size() << " tiles big.\n";
//set up our prototiles with some basic attributes
for(int i = first; i < prototile.size(); i++)
{
prototile[i] = tile_helper( temp, 16, i-first);
}
//name.attribute("width").as_int()/Twidth ;
//runs through the tile properties and creates prototile vector
for( name = name.next_sibling("tile"); name != 0; name = name.next_sibling("tile") );
{//in tag <tile> if exists
id = name.attribute("id").as_int();
//std::cout << "tile id:" << id << " has colision attribute : " << name.child("properties").child_value("property") << std::endl;
//prototile[first+id].solid = name.child("properties").child("property").attribute("value").as_int();
}
}
else
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Tileset creation error",
"Now you fucked up This was ment to happen\n go change teh false thing",
NULL);
exit( 1 );
}
}
}
tile level::tile_helper( SDL_Surface * mysurface, int tilesize , int tilenum)
{
tile mytile;
mytile.solid;
mytile.occupied;
mytile.portal = false;
mytile.tenent = nullptr;
mytile.Tile_Sprite->src = mysurface;
SDL_Rect myrect = { ((tilenum-1) * tilesize) , 0 , tilesize , tilesize };
mytile.Tile_Sprite->srcloc = myrect;
return mytile;
}
void level::LoadLevel( char * xmlfile )
{
//begining xmlreader implementation
/*
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file("assets/pallet.tmx");
pugi::xml_node spider = doc.child("map");
std::cout << spider.attribute("height").value();
spider = spider.child("tileset");
std::cout << spider.attribute("name").value() << std::endl;
spider = spider.next_sibling("tileset");
std::cout << spider.attribute("name").value() << std::endl;
std::cout << spider;
spider = spider.next_sibling("tileset");
std::cout << spider.attribute("name").value() << std::endl;
*/
int height, width;
//open file
pugi::xml_document doc;
pugi::xml_parse_result result;
//pugi::xml_parse_result result = doc.load_file( xmlfile );
//TODO: assets pallet needs to be based on a lookup table
try
{
result = doc.load_file("assets/pallet.tmx");
}
catch( ... )
{
exit(1);
}
if (result)
{
std::cout << "Load Result: " << result.description() << std::endl;
}
else
{
}
//get width and hight
//and make layer maps
//sets spider on the map tag
pugi::xml_node spider = doc.child("map");
//grabs width and height from xml
height = spider.attribute("height").as_int();
width = spider.attribute( "width").as_int();
//std::cout << height << " " << width << std::endl;
create_tilesets( spider );
std::cout << "tilesets created\n";
//make tile rules
pugi::xml_node datafinder;
//read in tile layers
spider = spider.child("layer");
//std::cout << "\nusing " << spider.attribute("name").value();
datafinder = spider;
bottom = layer_set( datafinder, width, height );
spider = spider.next_sibling("layer");
//std::cout << "\nusing " << spider.attribute("name").value();
datafinder = spider;
mid = layer_set( datafinder, width, height );
spider = spider.next_sibling("layer");
//std::cout << "\nusing " << spider.attribute("name").value();
datafinder = spider;
top = layer_set( datafinder, width, height );
}
tile ** level::layer_set( pugi::xml_node node, int width, int height)
{
node = node.child("data");
/*
if(node != 1)
std::cout << "layerget\n";
*/
tile ** vcr = new(nothrow) tile*[width];
if(vcr == nullptr)
{
std::cout << "Level Layer dynamic allocation failed, way to fail dicknuts";
exit(0);
}
for(int i = 0; i < width ; i++ )
{
vcr[i] = new(nothrow) tile[height];
if(vcr[i] == nullptr)
{
std::cout << "Level Layer dynamic allocation failed, way to fail dicknuts";
exit(0);
}
}
node = node.child("tile");
for( int i = 0; i < width; i++)
for( int j = 0; j < height; j++)
{
vcr[i][j].tile_type = node.attribute("gid").as_int();
node = node.next_sibling("tile");
//std::cout << vcr[i][j].tile_type;
}
// std::cout << endl;
return vcr;
}
bool level::level_in( int areaid ,int * &floor, int * &lamp, int &x , int &y )
{
int errnum = 0;
int temp; // for trash data
char delim;
fstream fin;
fin.open(area[ areaid ]); //get area id from structure and get path to level data
if(fin)//check if it opened
{
fin >> x >> y;//get x y of main level
//printf("%d\n %d\n",x,y);
/*
//if there are
if(adj)
{
//read in the id and xywh of the anchor to the adj
for(int i = 0; i < adj; i++)
{
fin >> adjid[i];
fin >> anchor[i][0].x;
fin >> anchor[i][0].y;
fin >> anchor[i][0].w;
fin >> anchor[i][0].h;
//open stream to adjecent level
next[i].open(area [ adjid[i] ] );
//if it opens
if(next[i])
{
//get the x and y values of the adjcent area
next[i] >> adj_x[i] >> adj_y[i];
printf("Route 1 opened recived x %d, y %d\n", adj_x[i],adj_y[i]);
//reads the amount of areas next to ajd area(doesnt matter)
next[i] >> temp;
if(temp == 0)
{
printf("Level data error\n");
}
//reads the id of an adjcent area and checks if its the main area
//if not it tests the next one
next[i] >> temp;
while(temp != areaid)
{
next[i] >> temp >> temp >> temp >> temp >> temp;
}
if(temp == areaid)
{
next[i] >> anchor[i][1].x;
next[i] >> anchor[i][1].y;
next[i] >> anchor[i][1].w;
next[i] >> anchor[i][1].h;
}
printf("anchor point x %d\ny %d\nw %d\nh %d\n", anchor[i][1].x,anchor[i][1].y,anchor[i][1].w,anchor[i][1].h );
}
else
{
printf("Missing Next area!\n");
return false;
}
next[i].close();
if( n > ( anchor[i][0].y - anchor[i][1].y ) )
{
n = anchor[i][0].y - anchor[i][1].y;
}
if( e < ( anchor[i][0].x - anchor[i][1].x +adj_x[i] ) )
{
e = anchor[i][0].x - anchor[i][1].x + adj_x[i];
}
if( s < ( anchor[i][0].y - anchor[i][1].y + adj_y[i] ) )
{
s = anchor[i][0].y - anchor[i][1].y + adj_y[i];
}
if( w > ( anchor[i][0].x - anchor[i][1].x ) )
{
w = anchor[i][0].x - anchor[i][1].x;
}
printf ("Level params are n = %d, e = %d, s = %d, w = %d\n",n,e,s,w);
}
x = -1*w + e;
y = -1*n + s;
}
printf( "estimated level size x = %d, y = %d", x ,y );
*/
//make floor and lamp pointers
floor = new (nothrow)int[x*y];
lamp = new (nothrow)int[x*y];
if(floor == nullptr)
{
printf("Floor did not dynamically allocate correctly\n");
return false;
}
if(lamp == nullptr)
{
printf("Lamp did not dynamically allocate correctly\n");
return false;
}
//printf("x is %d, y is %d\n",x,y);
for(int i = 0; i < x*y; i++)
{
// if(i%x == 0)
//printf("\n");
fin >> floor[i];
// printf ( "%d",floor[i]);
}
for(int i = 0; i < x*y; i++)
{
//if(i%x == 0)
// printf("\n");
fin >> lamp[i];
// printf ( "%d",lamp[i]);
}
mainmap = tilesetup(levelx , levely, floor ,lamp);
// //get number of transportign blocks
// int portals;
// fin >> portals;
// coord portalspot;
// for( int i = 0; i < portals; i++)
// {
// //read where the portal spot will be
// fin >> portalspot.x >> portalspot.y;
// mainmap[portalspot.x][portalspot.y].portal = true;
// //get where the portal will go then place
// fin >> temp;
// mainmap[portalspot.x][portalspot.y].world = temp;
// //get x coord of where we end up
// fin >> temp;
// mainmap[portalspot.x][portalspot.y].out.x = temp;
// //get y coord of where we end up
// fin >> temp;
// mainmap[portalspot.x][portalspot.y].out.y = temp;
// }
fin.close();
return true;
}
else
{
printf("Level FinError!\n");
return false;
}
}
Sprite *** level::spritesetup(int x ,int y, tile ** tilemap )
{
Sprite *** retsprite = new (nothrow) Sprite **[x];
//map = makesprites()
for(int i = 0; i < x; i++)
{
retsprite[i] = new (nothrow) Sprite*[y];
}
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
{
retsprite[i][j] = new (nothrow) Sprite[2];
}
//printf("tile size = %d, square params are %d,%d,%d,%d",TILE_SIZE,SQUARE.w,SQUARE.h,SQUARE.y,SQUARE.x);
for(int i = 0; i < x ; i++)
for( int j = 0; j < y; j++)
{
retsprite[i][j][0].depth = 1;
//setting the job's tileset
retsprite[i][j][0].src = tileSet;
//sprite source size
retsprite[i][j][0].srcloc = SQUARE;
retsprite[i][j][0].srcloc.y = 0;
retsprite[i][j][0].srcloc.x = TILE_SIZE * (mainmap[i][j].tile_type-1);
//sprite destination size
retsprite[i][j][0].destloc = SQUARE;
retsprite[i][j][0].destloc.x = TILE_SIZE * i;
retsprite[i][j][0].destloc.y = TILE_SIZE * j;
//destination on camera size
retsprite[i][j][0].camloc.w = TILE_SIZE;
retsprite[i][j][0].camloc.h = TILE_SIZE;
retsprite[i][j][0].draw = true;
//printf("spite map params src whxy= %d,%d,%d,%d",retsprite[i][j][0].destloc.w,retsprite[i][j][0].destloc.h,retsprite[i][j][0].destloc.x,retsprite[i][j][0].destloc.y);
//top layer jobs
retsprite[i][j][1].depth = 2;
if(mainmap[i][j].top_type >= 1)
{
retsprite[i][j][1].src = tileSet;
retsprite[i][j][1].srcloc = SQUARE;
retsprite[i][j][1].srcloc.y = TILE_SIZE;
retsprite[i][j][1].srcloc.x = TILE_SIZE * (mainmap[i][j].top_type-1);
//sprite destination size
retsprite[i][j][1].destloc = SQUARE;
retsprite[i][j][1].destloc.x = TILE_SIZE * i;
retsprite[i][j][1].destloc.y = TILE_SIZE * j;
//destination on camera size
retsprite[i][j][1].camloc.w = TILE_SIZE;
retsprite[i][j][1].camloc.h = TILE_SIZE;
retsprite[i][j][1].draw = true;
}
else
{
retsprite[i][j][1].draw = false;
}
}
return retsprite;
}
tile ** level::tilesetup( int x ,int y, int * mapdata, int * lampmap )
{
tile ** retmap = new (nothrow) tile *[x];
for(int i = 0; i < x; i++)
{
retmap[i] = new (nothrow) tile[y];
if (retmap[i] == nullptr)
{
printf("could not load map");
}
}
int k = 0;
for(int i = 0; i < y; i++)
for(int j = 0; j < x; j++,k++)
{
if(mapdata[k] > 14)
{
retmap[j][i].solid = true;
}
else
{
retmap[j][i].solid = false;
}
retmap[j][i].tile_type = mapdata[k];
retmap[j][i].occupied = false;
retmap[j][i].portal = false;
retmap[j][i].top_type = lampmap[k];
}
return retmap;
}
tile ** level::get_map()
{
return mainmap;
}
void level::show()
{
for(int i = 0; i < levelx ; i++)
for( int j = 0; j < levely; j++)
{
if( checkCollision( &spritemap[i][j][0].destloc , engine->get_camera()) )
{
spritemap[i][j][0].camloc.x = spritemap[i][j][0].destloc.x - engine->get_camera()->x;
spritemap[i][j][0].camloc.y = spritemap[i][j][0].destloc.y - engine->get_camera()->y;
engine->get_batch()->push_front(&spritemap[i][j][0]);
//printf( "floor w = %d, h = %d, y =%d, x = %d\n", engine->get_batch()->front()->srcloc.w , engine->get_batch()->front()->srcloc.h , engine->get_batch()->front()->srcloc.y , engine->get_batch()->front()->srcloc.x);
}
if( spritemap[i][j][1].draw && checkCollision( &spritemap[i][j][1].destloc , engine->get_camera()) )
{
spritemap[i][j][1].camloc.x = spritemap[i][j][1].destloc.x - engine->get_camera()->x;
spritemap[i][j][1].camloc.y = spritemap[i][j][1].destloc.y - engine->get_camera()->y;
engine->get_batch()->push_front(&spritemap[i][j][1]);
// printf( "lamp w = %d, h = %d, y =%d, x = %d\n", engine->get_batch()->front()->srcloc.w , engine->get_batch()->front()->srcloc.h , engine->get_batch()->front()->srcloc.y , engine->get_batch()->front()->srcloc.x);
}
}
}
void level::update()
{
}