Guest User

Untitled

a guest
Dec 3rd, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. TiXmlElement* layerElement;
  2.     layerElement = map->FirstChildElement("layer");
  3.  
  4.     while (layerElement)
  5.     {
  6.         Layer layer;
  7.  
  8.         if (layerElement->Attribute("opacity") != NULL)
  9.         {
  10.             float opacity = strtod(layerElement->Attribute("opacity"), NULL);
  11.             layer.opacity = 255 * opacity;
  12.         }
  13.         else
  14.         {
  15.             layer.opacity = 255;
  16.         }
  17.  
  18.         TiXmlElement* layerDataElement;
  19.         layerDataElement = layerElement->FirstChildElement("data");
  20.  
  21.         if (layerDataElement == NULL)
  22.         {
  23.             std::cout << "Bad map. No layer information found." << std::endl;
  24.         }
  25.  
  26.         string editTileMatrix = layerDataElement->GetText();
  27.         for (int i = 0; i < editTileMatrix.size(); i++)
  28.         {
  29.             if (editTileMatrix[i] == ' ')
  30.             {
  31.                 editTileMatrix[i] = ',';
  32.             }
  33.         }
  34.         istringstream tileMatrix(editTileMatrix);
  35.         string saveElementTileMatrix;
  36.         while (getline(tileMatrix, saveElementTileMatrix, ','))
  37.         {
  38.             if(saveElementTileMatrix.size()!=0)
  39.             matrix.push_back(stoi(saveElementTileMatrix));
  40.         }
  41.  
  42.         string var = layerElement->Attribute("name");
  43.  
  44.         int x = 0;
  45.         int y = 0;
  46.         for (int i = 0; i < matrix.size(); i++)
  47.         {
  48.             int tileGID = matrix[i];
  49.             int subRectToUse = tileGID - firstTileID;
  50.  
  51.             if (subRectToUse >= 0)
  52.             {
  53.                 sf::Sprite sprite;
  54.                 sprite.setTexture(tilesetImage);
  55.                 sprite.setTextureRect(subRects[subRectToUse]);
  56.                 sprite.setPosition(x * tileWidth, y * tileHeight);
  57.                 sprite.setColor(sf::Color(255, 255, 255, layer.opacity));
  58.  
  59.                 layer.tiles.push_back(sprite);
  60.                 cout << layer.tiles.size() << endl;
  61.             }
  62.  
  63.             x++;
  64.             if (x >= width)
  65.             {
  66.                 x = 0;
  67.                 y++;
  68.                 if (y >= height)
  69.                     y = 0;
  70.             }
  71.         }
  72.    
  73.         if (var == "Tree")
  74.             objects = layer.tiles;
  75.  
  76.  
  77.         layers.push_back(layer);
  78.  
  79.         layerElement = layerElement->NextSiblingElement("layer");
  80.  
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment