Foreground.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "Foreground.hpp"
  18. #include <iostream>
  19. #include <cmath>
  20. Foreground::~Foreground()
  21. {
  22. for(unsigned i = 0; i < _sprites.size(); ++i)
  23. _sprites[i].clear();
  24. _sprites.clear();
  25. _tiles.clear();
  26. _normalSprites.clear();
  27. _normalTiles.clear();
  28. }
  29. const sf::Vector2u Foreground::getSize() const
  30. {
  31. return {_tileCount.x * _tileSize, _tileCount.y * _tileSize};
  32. }
  33. unsigned const Foreground::tileAlpha(unsigned x, unsigned y, unsigned pixelX, unsigned pixelY)
  34. {
  35. return _tiles[y * _tileCount.x + x].first.getPixel(pixelX, pixelY).a;
  36. }
  37. void Foreground::reloadFromTile(int x, int y, bool neighbouring)
  38. {
  39. if(x < 0 or y < 0 or x >= (int)_tileCount.x or y >= (int)_tileCount.y)
  40. return;
  41. if(neighbouring)
  42. {
  43. for(int i = -1; i <= 1; ++i)
  44. {
  45. if(x + i < 0 or x + i >= (int)_tileCount.x)
  46. continue;
  47. for(int j = -1; j <= 1; ++j)
  48. {
  49. if(y + j < 0 or y + j >= (int)_tileCount.y)
  50. continue;
  51. unsigned id = (y + j) * _tileCount.x + (x + i);
  52. _tiles[id].second.loadFromImage(_tiles[id].first);
  53. _normalTiles[id].second.loadFromImage(_normalTiles[id].first);
  54. }
  55. }
  56. }
  57. else
  58. {
  59. unsigned id = y * _tileCount.x + x;
  60. _tiles[id].second.loadFromImage(_tiles[id].first);
  61. _normalTiles[id].second.loadFromImage(_normalTiles[id].first);
  62. }
  63. }
  64. void Foreground::reloadFromPixel(int x, int y, bool neighbouring)
  65. {
  66. reloadFromTile((x / _tileSize), (y / _tileSize), neighbouring);
  67. }
  68. bool Foreground::destroy(unsigned x, unsigned y)
  69. {
  70. if((x / _tileSize) < 0 or (y / _tileSize) < 0 or (x / _tileSize) >= _tileCount.x or (y / _tileSize) >= _tileCount.y)
  71. return true;
  72. unsigned id = ((y / _tileSize) * _tileCount.x) + (x / _tileSize);
  73. x %= _tileSize;
  74. y %= _tileSize;
  75. int a = _tiles[id].first.getPixel(x, y).a;
  76. if(a > 0)
  77. {
  78. _tiles[id].first.setPixel(x, y, sf::Color::Transparent);
  79. _normalTiles[id].first.setPixel(x, y, sf::Color::Transparent);
  80. return true;
  81. }
  82. else
  83. return false;
  84. }
  85. const unsigned Foreground::getTileSize() const
  86. {
  87. return _tileSize;
  88. }
  89. void Foreground::create(sf::Image& foreground, sf::Image& normal, sf::Image& map)
  90. {
  91. _tileSize = 256;
  92. _position.x = 0; _position.y = 0;
  93. _tileCount.x = std::ceil(static_cast<float>(map.getSize().x) / _tileSize);
  94. _tileCount.y = std::ceil(static_cast<float>(map.getSize().y) / _tileSize);
  95. _tiles.clear();
  96. _tiles.resize(_tileCount.x * _tileCount.y);
  97. _sprites.resize(_tileCount.x);
  98. _normalTiles.clear();
  99. _normalTiles.resize(_tileCount.x * _tileCount.y);
  100. _normalSprites.resize(_tileCount.x);
  101. for(unsigned i = 0; i < _tileCount.x; ++i)
  102. {
  103. _sprites[i].resize(_tileCount.y);
  104. _normalSprites[i].resize(_tileCount.y);
  105. }
  106. for(unsigned i = 0; i < _tiles.size(); ++i)
  107. {
  108. // std::get instead of .first and .second because it used to be a tuple
  109. // and I did not rewrite this.
  110. std::get<0>(_tiles[i]).create(_tileSize, _tileSize, sf::Color::Yellow);
  111. std::get<0>(_normalTiles[i]).create(_tileSize, _tileSize, sf::Color::Yellow);
  112. }
  113. for(unsigned i = 0; i < _tileCount.x * _tileSize; ++i)
  114. for(unsigned j = 0; j < _tileCount.y * _tileSize; ++j)
  115. {
  116. sf::Color color = foreground.getPixel(i % foreground.getSize().x, j % foreground.getSize().y);
  117. std::get<0>(_tiles[j / _tileSize * _tileCount.x + i / _tileSize])
  118. .setPixel(i % _tileSize, j % _tileSize, sf::Color(color.r, color.g, color.b, i < map.getSize().x and j < map.getSize().y ? 255 - map.getPixel(i, j).r : 255));
  119. color = normal.getPixel(i % normal.getSize().x, j % normal.getSize().y);
  120. std::get<0>(_normalTiles[j / _tileSize * _tileCount.x + i / _tileSize])
  121. .setPixel(i % _tileSize, j % _tileSize, sf::Color(color.r, color.g, color.b, i < map.getSize().x and j < map.getSize().y ? 255 - map.getPixel(i, j).r : 255));
  122. }
  123. for(unsigned i = 0; i < _tiles.size(); ++i)
  124. {
  125. std::get<1>(_tiles[i]).loadFromImage(std::get<0>(_tiles[i]));
  126. _sprites[i % _tileCount.x][i / _tileCount.x].setTexture(std::get<1>(_tiles[i]));
  127. _sprites[i % _tileCount.x][i / _tileCount.x].setPosition((i % _tileCount.x) * _tileSize, (i / _tileCount.x) * _tileSize);
  128. std::get<1>(_normalTiles[i]).loadFromImage(std::get<0>(_normalTiles[i]));
  129. _normalSprites[i % _tileCount.x][i / _tileCount.x].setTexture(std::get<1>(_normalTiles[i]));
  130. _normalSprites[i % _tileCount.x][i / _tileCount.x].setPosition((i % _tileCount.x) * _tileSize, (i / _tileCount.x) * _tileSize);
  131. }
  132. }
  133. void Foreground::create(std::string foreground, std::string normal, std::string map)
  134. {
  135. sf::Image image1, image2, image3;
  136. image1.loadFromFile(foreground);
  137. image2.loadFromFile(normal);
  138. image3.loadFromFile(map);
  139. create(image1, image2, image3);
  140. }
  141. void Foreground::setResolution(sf::Vector2u resolution)
  142. {
  143. sf::Vector2f temp;
  144. temp.x = static_cast<float>(resolution.x) / static_cast<float>(_tileSize);
  145. temp.y = static_cast<float>(resolution.y) / static_cast<float>(_tileSize);
  146. _renderCount = sf::Vector2u(std::ceil(temp.x) + 1, std::ceil(temp.y) + 1);
  147. }
  148. void Foreground::setPosition(sf::Vector2f position)
  149. {
  150. _position.x = position.x >= 0 ? position.x : 0;
  151. _position.y = position.y >= 0 ? position.y : 0;
  152. }
  153. void Foreground::renderNormals(bool render)
  154. {
  155. _renderNormals = render;
  156. }
  157. void Foreground::move(sf::Vector2f movement)
  158. {
  159. for(unsigned i = 0; i < _renderCount.x; ++i)
  160. for(unsigned j = 0; j < _renderCount.y; ++j)
  161. _sprites[i][j].move(movement);
  162. }
  163. void Foreground::rotate(float rotation)
  164. {
  165. for(unsigned i = 0; i < _renderCount.x; ++i)
  166. for(unsigned j = 0; j < _renderCount.y; ++j)
  167. _sprites[i][j].rotate(rotation);
  168. }
  169. void Foreground::draw(sf::RenderTarget& target, sf::RenderStates states) const
  170. {
  171. unsigned x = _position.x / _tileSize, y = _position.y / _tileSize;
  172. for(unsigned i = 0; i < _renderCount.x; ++i)
  173. for(unsigned j = 0; j < _renderCount.y; ++j)
  174. if(i + x < _tileCount.x and j + y < _tileCount.y)
  175. {
  176. if(!_renderNormals)
  177. target.draw(_sprites[i + x][j + y], states);
  178. else
  179. target.draw(_normalSprites[i + x][j + y], states);
  180. }
  181. }