# I have problems with spacing tiles

**URL:** <https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018>\
**Category:** Questions\
**Created:** [February 8, 2017, 7:55pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018 "2017-02-08T19:55:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 7:55pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/1 "2017-02-08T19:55:36Z")

</div>

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/mapeditor/original/2X/9/988acbc7a6df53a3b61baa775fa339c439978c54.JPG) ![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/mapeditor/original/2X/8/8b6de4ba5e4c8e6e4093d0df55951b68e01f0443.JPG)

I need your help guys!  
So i have sprite sheet (70 x 70 each sprite), i created map with correct title size, and i uploaded my sprite sheet then i have problems with displaying titles in editor, but i solved this problem (i added spacing - 2 px). Ok, then i tried to draw my map and i’m seeing the mess of the sprites.

---

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 8:05pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/2 "2017-02-08T20:05:36Z")

</div>

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/mapeditor/original/2X/d/d27dd8183a998cb65a8dcc1e726273a313816f04.JPG)

Solved problem with adding spacinig - 2px, but i had problems with drawing map (i sended screenshot with drawn map)

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [February 8, 2017, 8:48pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/3 "2017-02-08T20:48:03Z")

</div>

What are you using to draw the map? It looks like it may be a library that does not handle the spacing value correctly.

---

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 9:06pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/4 "2017-02-08T21:06:33Z")

</div>

tinyxml and special class

---

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 9:10pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/5 "2017-02-08T21:10:21Z")

</div>

```auto
struct Object
{
	int GetPropertyInt(std::string name);//номер свойства объекта в нашем списке
	float GetPropertyFloat(std::string name);
	std::string GetPropertyString(std::string name);

	std::string name;//объявили переменную name типа string
	std::string type;//а здесь переменную type типа string
	sf::Rect<float> rect;//тип Rect с нецелыми значениями
	std::map<std::string, std::string> properties;//создаём ассоциатиный массив. ключ - строковый тип, значение - строковый

	sf::Sprite sprite;//объявили спрайт
};

struct Layer//слои
{
	int opacity;//непрозрачность слоя
	std::vector<sf::Sprite> tiles;//закидываем в вектор тайлы
};

class Level//главный класс - уровень
{
public:
	int levelNumber;//номер уровня
	bool LoadFromFile(std::string filename);//возвращает false если не получилось загрузить
	Object GetObject(std::string name);
	std::vector<Object> GetObjects(std::string name);//выдаем объект в наш уровень
	std::vector<Object> GetAllObjects();//выдаем все объекты в наш уровень
	void Draw(sf::RenderWindow &window);//рисуем в окно
	sf::Vector2i GetTileSize();//получаем размер тайла

private:
	int width, height, tileWidth, tileHeight;//в tmx файле width height в начале,затем размер тайла
	int firstTileID;//получаем айди первого тайла
	sf::Rect<float> drawingBounds;//размер части карты которую рисуем
	sf::Texture tilesetImage;//текстура карты
	std::vector<Object> objects;//массив типа Объекты, который мы создали
	std::vector<Layer> layers;
};

```

---

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 9:11pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/6 "2017-02-08T21:11:41Z")

</div>

```auto
int Object::GetPropertyInt(std::string name)//возвращаем номер свойства в нашем списке
{
	return atoi(properties[name].c_str());
}

float Object::GetPropertyFloat(std::string name)
{
	return strtod(properties[name].c_str(), NULL);
}

std::string Object::GetPropertyString(std::string name)//получить имя в виде строки.вроде понятно
{
	return properties[name];
}

bool Level::LoadFromFile(std::string filename)//двоеточия-обращение к методам класса вне класса 
{
	TiXmlDocument levelFile(filename.c_str());//загружаем файл в TiXmlDocument

											  // загружаем XML-карту
	if (!levelFile.LoadFile())//если не удалось загрузить карту
	{
		std::cout << "Loading level \"" << filename << "\" failed." << std::endl;//выдаем ошибку
		return false;
	}

	// работаем с контейнером map
	TiXmlElement *map;
	map = levelFile.FirstChildElement("map");

	// пример карты: <map version="1.0" orientation="orthogonal"
	// width="10" height="10" tilewidth="34" tileheight="34">
	width = atoi(map->Attribute("width"));//извлекаем из нашей карты ее свойства
	height = atoi(map->Attribute("height"));//те свойства, которые задавали при работе в 
	tileWidth = atoi(map->Attribute("tilewidth"));//тайлмап редакторе
	tileHeight = atoi(map->Attribute("tileheight"));

	// Берем описание тайлсета и идентификатор первого тайла
	TiXmlElement *tilesetElement;
	tilesetElement = map->FirstChildElement("tileset");
	firstTileID = atoi(tilesetElement->Attribute("firstgid"));

	// source - путь до картинки в контейнере image
	TiXmlElement *image;
	image = tilesetElement->FirstChildElement("image");
	std::string imagepath = image->Attribute("source");

	// пытаемся загрузить тайлсет
	sf::Image img;

	if (!img.loadFromFile(imagepath))
	{
		std::cout << "Failed to load tile sheet." << std::endl;//если не удалось загрузить тайлсет-выводим ошибку в консоль
		return false;
	}

	img.createMaskFromColor(sf::Color(255, 255, 255));//для маски цвета.сейчас нет маски
	tilesetImage.loadFromImage(img);
	tilesetImage.setSmooth(false);//сглаживание

								  // получаем количество столбцов и строк тайлсета
	int columns = tilesetImage.getSize().x / tileWidth;
	int rows = tilesetImage.getSize().y / tileHeight;

	// вектор из прямоугольников изображений (TextureRect)
	std::vector<sf::Rect<int>> subRects;

	for (int y = 0; y < rows; y++)
		for (int x = 0; x < columns; x++)
		{
			sf::Rect<int> rect;

			rect.top = y * tileHeight;
			rect.height = tileHeight;
			rect.left = x * tileWidth;
			rect.width = tileWidth;

			subRects.push_back(rect);
		}

	// работа со слоями
	TiXmlElement *layerElement;
	layerElement = map->FirstChildElement("layer");
	while (layerElement)
	{
		Layer layer;

		// если присутствует opacity, то задаем прозрачность слоя, иначе он полностью непрозрачен
		if (layerElement->Attribute("opacity") != NULL)
		{
			float opacity = strtod(layerElement->Attribute("opacity"), NULL);
			layer.opacity = 255 * opacity;
		}
		else
		{
			layer.opacity = 255;
		}

		// контейнер <data> 
		TiXmlElement *layerDataElement;
		layerDataElement = layerElement->FirstChildElement("data");

		if (layerDataElement == NULL)
		{
			std::cout << "Bad map. No layer information found." << std::endl;
		}

		// контейнер <tile> - описание тайлов каждого слоя
		TiXmlElement *tileElement;
		tileElement = layerDataElement->FirstChildElement("tile");

		if (tileElement == NULL)
		{
			std::cout << "Bad map. No tile information found." << std::endl;
			return false;
		}

		int x = 0;
		int y = 0;

		while (tileElement)
		{
			int tileGID = atoi(tileElement->Attribute("gid"));
			int subRectToUse = tileGID - firstTileID;

			// Устанавливаем TextureRect каждого тайла
			if (subRectToUse >= 0)
			{
				sf::Sprite sprite;
				sprite.setTexture(tilesetImage);
				sprite.setTextureRect(subRects[subRectToUse]);
				sprite.setPosition(x * tileWidth, y * tileHeight);
				sprite.setColor(sf::Color(255, 255, 255, layer.opacity));

				layer.tiles.push_back(sprite);//закидываем в слой спрайты тайлов
			}

			tileElement = tileElement->NextSiblingElement("tile");

			x++;
			if (x >= width)
			{
				x = 0;
				y++;
				if (y >= height)
					y = 0;
			}
		}

		layers.push_back(layer);

		layerElement = layerElement->NextSiblingElement("layer");
	}

	// работа с объектами
	TiXmlElement *objectGroupElement;

	// если есть слои объектов
	if (map->FirstChildElement("objectgroup") != NULL)
	{
		objectGroupElement = map->FirstChildElement("objectgroup");
		while (objectGroupElement)
		{
			// контейнер <object>
			TiXmlElement *objectElement;
			objectElement = objectGroupElement->FirstChildElement("object");

			while (objectElement)
			{
				// получаем все данные - тип, имя, позиция, и тд
				std::string objectType;
				if (objectElement->Attribute("type") != NULL)
				{
					objectType = objectElement->Attribute("type");
				}
				std::string objectName;
				if (objectElement->Attribute("name") != NULL)
				{
					objectName = objectElement->Attribute("name");
				}
				int x = atoi(objectElement->Attribute("x"));
				int y = atoi(objectElement->Attribute("y"));

				int width, height;

				sf::Sprite sprite;
				sprite.setTexture(tilesetImage);
				sprite.setTextureRect(sf::Rect<int>(0, 0, 0, 0));
				sprite.setPosition(x, y);

				if (objectElement->Attribute("width") != NULL)
				{
					width = atoi(objectElement->Attribute("width"));
					height = atoi(objectElement->Attribute("height"));
				}
				else
				{
					width = subRects[atoi(objectElement->Attribute("gid")) - firstTileID].width;
					height = subRects[atoi(objectElement->Attribute("gid")) - firstTileID].height;
					sprite.setTextureRect(subRects[atoi(objectElement->Attribute("gid")) - firstTileID]);
				}

				// экземпляр объекта
				Object object;
				object.name = objectName;
				object.type = objectType;
				object.sprite = sprite;

				sf::Rect <float> objectRect;
				objectRect.top = y;
				objectRect.left = x;
				objectRect.height = height;
				objectRect.width = width;
				object.rect = objectRect;

				// "переменные" объекта
				TiXmlElement *properties;
				properties = objectElement->FirstChildElement("properties");
				if (properties != NULL)
				{
					TiXmlElement *prop;
					prop = properties->FirstChildElement("property");
					if (prop != NULL)
					{
						while (prop)
						{
							std::string propertyName = prop->Attribute("name");
							std::string propertyValue = prop->Attribute("value");

							object.properties[propertyName] = propertyValue;

							prop = prop->NextSiblingElement("property");
						}
					}
				}

				objects.push_back(object);

				objectElement = objectElement->NextSiblingElement("object");
			}
			objectGroupElement = objectGroupElement->NextSiblingElement("objectgroup");
		}
	}
	else
	{
		std::cout << "No object layers found..." << std::endl;
	}

	return true;
}

Object Level::GetObject(std::string name)
{
	// только первый объект с заданным именем
	for (int i = 0; i < objects.size(); i++)
		if (objects[i].name == name)
			return objects[i];
}

std::vector<Object> Level::GetObjects(std::string name)
{
	// все объекты с заданным именем
	std::vector<Object> vec;
	for (int i = 0; i < objects.size(); i++)
		if (objects[i].name == name)
			vec.push_back(objects[i]);

	return vec;
}

std::vector<Object> Level::GetAllObjects()
{
	return objects;
};

sf::Vector2i Level::GetTileSize()
{
	return sf::Vector2i(tileWidth, tileHeight);
}

void Level::Draw(sf::RenderWindow &window)
{
	// рисуем все тайлы (объекты не рисуем!)
	for (int layer = 0; layer < layers.size(); layer++)
		for (int tile = 0; tile < layers[layer].tiles.size(); tile++)
			window.draw(layers[layer].tiles[tile]);
}

```

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [February 8, 2017, 9:37pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/7 "2017-02-08T21:37:52Z")

</div>

You forgot to do anything with the tile spacing in your own code. If you just ignore those 2 pixels in between each tile, your `columns` and `rows` are calculated wrong and all `subRects` will be off.

Btw, you’ll want to pass `std::string` as `const std::string &` whenever possible, otherwise the string data will get copied.

---

<div class="post-metadata">

**Author:** ![Vindmax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/vindmax/32/1023_2.png) [@Vindmax](https://discourse.mapeditor.org/u/Vindmax)\
**Post date:** [February 8, 2017, 11:07pm UTC](https://discourse.mapeditor.org/t/i-have-problems-with-spacing-tiles/2018/9 "2017-02-08T23:07:27Z")

</div>

I dont reall how to calcucate all this things including spacing, its my first experience with xml.  
Can you suggest me something? Thank you  
By the way this class wasnt created by me.
