-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
121 lines (107 loc) · 2.49 KB
/
map.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
#include "map.h"
#include "Tile.h"
#include <fstream>
namespace SSJ {
Map::Map() : GameLayer("Mapa", false)
{
}
Map::Map(string file) : GameLayer("Mapa", false)
{
this->load(file);
}
void Map::load(string file)
{
fstream mapFile;
mapFile.open(file.c_str(), ios::in);
if(!mapFile.good())
{
cout << "Nie znaleziono mapy" << endl;
return;
}
int width, height, tileWidth;
int fileWidth, fileHeight;
string textures;
mapFile >> width;
mapFile >> height;
mapFile >> textures;
mapFile >> tileWidth;
w = width;
h = height;
this->tileWidth = tileWidth;
this->texture.loadFromFile(textures);
fileWidth = texture.getSize().x;
fileHeight = texture.getSize().y;
for(int i = 0 ; i < height ; ++ i )
{
for( int j = 0 ; j < width ; ++j)
{
int tileCount;
char block;
bool bBlock;
int x, y;
int xClip = 0, yClip = 0;
x = j*tileWidth;
y = i*tileWidth;
mapFile >> tileCount;
mapFile >> block;
if (block == 'x')
bBlock = true;
else
bBlock = false;
while(tileCount >= fileWidth / tileWidth)
{
tileCount -= fileWidth / tileWidth;
yClip ++;
}
xClip = tileCount;
Tile *tile;
if(tileCount == -1)
{
tile = new Tile(x, y, tileWidth, bBlock);
}
else
{
sf::IntRect clip;
clip.left = xClip * tileWidth;
clip.top = yClip * tileWidth;
clip.width = tileWidth;
clip.height = tileWidth;
tile = new Tile(x, y, tileWidth, bBlock, clip, &this->texture);
}
this->addObject(tile);
}
}
}
void Map::DrawLayer(){
int x;
int y;
int first;
int last;
int offset = 3;
int screenTileHeight = ( DataContainer::ScreenHeight / this->tileWidth ) + 2*offset;
x = ( ( DataContainer::ScreenPosition.x - fmod(DataContainer::ScreenPosition.x, tileWidth) )/
this->tileWidth ) - offset;
y = ( ( DataContainer::ScreenPosition.y - fmod(DataContainer::ScreenPosition.y, tileWidth) )/
this->tileWidth ) - offset;
if( x < 0 )
x = 0;
if( y < 0 )
y = 0;
int tempFirst;
for(int i = 0 ; i < screenTileHeight; i++ )
{
first = y * (this->w) + x + i*w;;
if(i==0)
tempFirst=first;
last = first + ( DataContainer::ScreenWidth / this->tileWidth ) + offset*2;
if (last >= (y+i+1) * this->w)
last = (y+i) * this->w-1;
if( last >= this->getObjects()->size() )
last = this->getObjects()->size() -1;
for( int j = first ; j < last ; ++j)
{
this->getObjects()->at(j)->draw();
}
}
}
}