Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include "TileMap.h"
  2. #include <SFML/Graphics.hpp>
  3. #include "GameEngine.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <ctime>
  7. #include <random>
  8.  
  9. GameEngine engine;
  10. extern GameEngine engine;
  11.  
  12. TileMap::TileMap()
  13. {
  14. }
  15.  
  16. void TileMap::init(int width, int height, TileID * level, sf::Vector2u tileSize, FillStyle fillStyle, std::string fileName)
  17. {
  18. //Load texture
  19. _texture.loadFromFile(fileName);
  20.  
  21. //Find size of each tile in texture | total size of texture devided by the number of different textures
  22. _textureSize.x = (_texture.getSize().x) / (int)TileID::NUMBER_OF_TYPES;
  23. _textureSize.y = _textureSize.x;
  24.  
  25. //Set other variables
  26. _width = width;
  27. _height = height;
  28. _level = level;
  29. _fillStyle = fillStyle;
  30. _tileSize = tileSize;
  31. _textureFileName = fileName;
  32.  
  33. //If set to fill screen ignore tile size
  34. if (_fillStyle == FillStyle::FILL)
  35. {
  36. _tileSize.x = _textureSize.x;
  37. _tileSize.y = _textureSize.y;
  38. }
  39.  
  40. //Get size of game screen from engine (Sizes of everything are relitive to this as a way of handling resolutions)
  41. _gameBorder = engine.getGameBorder();
  42.  
  43. create();
  44. }
  45.  
  46.  
  47. int TileMap::createVariation(int x, int y)
  48. {
  49. TileID tileType = _level[x + y*_width];
  50. sf::Image image = _texture.copyToImage();
  51. int numberOfVariations = -1;
  52.  
  53. //_texture.getSize().y / _height = the maximum number of tile variations for each tile
  54. for (int i = 0; i < _tileSize.y; i++)
  55. {
  56. //If *current tile* pixel is transparent(the tile doesn't exist)
  57. if (image.getPixel((int)(tileType)*_tileSize.x, i*_tileSize.y).a == 0)
  58. {
  59. //That is the number of tile variations
  60. numberOfVariations = i;
  61. //End for loop
  62. break;
  63. }
  64. }
  65.  
  66. //Random generator
  67. std::default_random_engine generator;
  68.  
  69. generator.seed((++x)*(++y)+(++x));
  70.  
  71. //need to multiply by tile size, as to make it work
  72. std::uniform_int_distribution<int> distribution(1, numberOfVariations);
  73.  
  74. int variation = distribution(generator);
  75. return variation;
  76. }
  77.  
  78. void TileMap::create()
  79. {
  80. //Set up vertex array
  81. _vertex.setPrimitiveType(sf::Quads);
  82. _vertex.resize(_width * _height * 4);
  83.  
  84. for (unsigned int i = 0; i < _width; ++i)
  85. {
  86. for (unsigned int j = 0; j < _height; ++j)
  87. {
  88. int variation = createVariation(i, j);
  89. // get the current tile number
  90. int tileNumber = (int)_level[i + j * _width];
  91.  
  92. // find its position in the tileset texture - need to be square!!!
  93. int texSize = std::floor(_texture.getSize().x / _textureSize.x);
  94. int tu = tileNumber % texSize;
  95.  
  96. // get a pointer to the current tile's quad
  97. sf::Vertex* quad = &_vertex[(i + j * _width) * 4];
  98.  
  99. // define its 4 corners
  100. quad[0].position = sf::Vector2f(std::round(i * _textureSize.x), std::round(j * _textureSize.y));
  101. quad[1].position = sf::Vector2f(std::round((i + 1) * _textureSize.x), std::round(j * _textureSize.y));
  102. quad[2].position = sf::Vector2f(std::round((i + 1) * _textureSize.x), std::round((j + 1) * _textureSize.y));
  103. quad[3].position = sf::Vector2f(std::round(i * _textureSize.x), std::round((j + 1) * _textureSize.y));
  104.  
  105. // define its 4 texture coordinates
  106. quad[0].texCoords = sf::Vector2f(std::round(tu * _textureSize.x), std::round(_textureSize.y*(variation-1)));
  107. quad[1].texCoords = sf::Vector2f(std::round((tu + 1) * _textureSize.x), std::round(_textureSize.y*(variation-1)));
  108. quad[2].texCoords = sf::Vector2f(std::round((tu + 1) * _textureSize.x), std::round(_textureSize.y*variation));
  109. quad[3].texCoords = sf::Vector2f(std::round(tu * _textureSize.x),std::round(_textureSize.y*variation) );
  110. }
  111. if (i % 50 == 0)
  112. {
  113. std::cout << i << std::endl;
  114. }
  115. }
  116. std::cout << _textureSize.x << std::endl;
  117. std::cout << _textureSize.y << std::endl;
  118. }
  119.  
  120. //////////////////////////////////////////////////////////////////////
  121. ///draw: Only use if you are planning on using a fillstyle, or another preprocessing option
  122. void TileMap::draw(sf::RenderWindow &window)
  123. {
  124. window.draw(*this);
  125.  
  126. }
  127.  
  128. void TileMap::update()
  129. {
  130. // sf::Thread update_thread(&create);
  131. // update_thread.launch();
  132. }
  133.  
  134. void TileMap::updateTextures(std::string fileName)
  135. {
  136. if (fileName != _textureFileName)
  137. {
  138. //Load texture
  139. _texture.loadFromFile("./textures/" + fileName);
  140.  
  141. //Find size of each tile in texture | total size of texture devided by the number of different textures | must be square
  142. _textureSize.x = (_texture.getSize().x) / (int)TileID::NUMBER_OF_TYPES;
  143. _textureSize.y = _textureSize.x;
  144. }
  145. }
  146.  
  147. TileID* TileMap::getTileData()
  148. {
  149. return _level;
  150. }
  151.  
  152. void TileMap::findOccupingTiles(Entity &entity)
  153. {
  154. //TBA
  155. }
  156.  
  157. TileMap::~TileMap()
  158. {
  159. }
  160.  
  161. void TileMap::draw(sf::RenderTarget & target, sf::RenderStates states) const
  162. {
  163. //Apply transforfmation
  164. states.transform *= getTransform();
  165.  
  166. // apply the tileset texture
  167. states.texture = &_texture;
  168.  
  169. // draw the vertex array
  170. target.draw(_vertex, states);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement