Advertisement
Guest User

Untitled

a guest
Jun 30th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. bool TileMap::load(sf::Texture m_tileset, sf::Vector2u tileSize, vector< vector<Textures::textureTypes> > tiles, unsigned int width, unsigned int height)
  2. {
  3.     // populate the vertex array, with one quad per tile
  4.     for (unsigned int i = 0; i < width; ++i)
  5.         for (unsigned int j = 0; j < height; ++j)
  6.         {
  7.             // get the current tile number
  8.             int tileNumber = tiles[i][j];
  9.             if (tiles[i][j] == Textures::emptyNpcTile)
  10.                 continue;
  11. //            cout << tileNumber;
  12.  
  13.             // find its position in the tileset texture
  14.             int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
  15.             int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
  16.  
  17.             // get a pointer to the current tile's quad
  18.             sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
  19.  
  20.             // define its 4 corners
  21.             quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
  22.             quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
  23.             quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
  24.             quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
  25.  
  26.             // define its 4 texture coordinates
  27.             quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
  28.             quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
  29.             quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
  30.             quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
  31.         }
  32.  
  33.     return true;
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement