Foreground.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  27. const sf::Vector2u Foreground::getSize() const
  28. {
  29. return {_tileCount.x * _tileSize, _tileCount.y * _tileSize};
  30. }
  31. unsigned const Foreground::tileAlpha(unsigned x, unsigned y, unsigned pixelX, unsigned pixelY)
  32. {
  33. return _tiles[y * _tileCount.x + x].first.getPixel(pixelX, pixelY).a;
  34. }
  35. void Foreground::reloadFromTile(int x, int y, bool neighbouring)
  36. {
  37. if(x < 0 or y < 0 or x >= (int)_tileCount.x or y >= (int)_tileCount.y)
  38. return;
  39. if(neighbouring)
  40. {
  41. for(int i = -1; i <= 1; ++i)
  42. {
  43. if(x + i < 0 or x + i >= (int)_tileCount.x)
  44. continue;
  45. for(int j = -1; j <= 1; ++j)
  46. {
  47. if(y + j < 0 or y + j >= (int)_tileCount.y)
  48. continue;
  49. unsigned id = (y + j) * _tileCount.x + (x + i);
  50. _tiles[id].second.loadFromImage(_tiles[id].first);
  51. }
  52. }
  53. }
  54. else
  55. {
  56. unsigned id = y * _tileCount.x + x;
  57. _tiles[id].second.loadFromImage(_tiles[id].first);
  58. }
  59. }
  60. void Foreground::reloadFromPixel(int x, int y, bool neighbouring)
  61. {
  62. reloadFromTile((x / _tileSize), (y / _tileSize), neighbouring);
  63. }
  64. bool Foreground::destroy(unsigned x, unsigned y)
  65. {
  66. if((x / _tileSize) < 0 or (y / _tileSize) < 0 or (x / _tileSize) >= _tileCount.x or (y / _tileSize) >= _tileCount.y)
  67. return true;
  68. unsigned id = ((y / _tileSize) * _tileCount.x) + (x / _tileSize);
  69. x %= _tileSize;
  70. y %= _tileSize;
  71. int a = _tiles[id].first.getPixel(x, y).a;
  72. if(a > 0)
  73. {
  74. _tiles[id].first.setPixel(x, y, sf::Color::Transparent);
  75. return true;
  76. }
  77. else
  78. return false;
  79. }
  80. const unsigned Foreground::getTileSize() const
  81. {
  82. return _tileSize;
  83. }
  84. void Foreground::create(sf::Image& foreground, sf::Image& map)
  85. {
  86. _tileSize = 512;
  87. _position.x = 0; _position.y = 0;
  88. _tileCount.x = std::ceil(static_cast<float>(map.getSize().x) / _tileSize);
  89. _tileCount.y = std::ceil(static_cast<float>(map.getSize().y) / _tileSize);
  90. _tiles.clear();
  91. _tiles.resize(_tileCount.x * _tileCount.y);
  92. _sprites.resize(_tileCount.x);
  93. for(unsigned i = 0; i < _tileCount.x; ++i)
  94. _sprites[i].resize(_tileCount.y);
  95. for(unsigned i = 0; i < _tiles.size(); ++i)
  96. std::get<0>(_tiles[i]).create(_tileSize, _tileSize, sf::Color::Yellow);
  97. for(unsigned i = 0; i < _tileCount.x * _tileSize; ++i)
  98. for(unsigned j = 0; j < _tileCount.y * _tileSize; ++j)
  99. {
  100. sf::Color color = foreground.getPixel(i % foreground.getSize().x, j % foreground.getSize().y);
  101. std::get<0>(_tiles[j / _tileSize * _tileCount.x + i / _tileSize])
  102. .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));
  103. }
  104. for(unsigned i = 0; i < _tiles.size(); ++i)
  105. {
  106. std::get<1>(_tiles[i]).loadFromImage(std::get<0>(_tiles[i]));
  107. _sprites[i % _tileCount.x][i / _tileCount.x].setTexture(std::get<1>(_tiles[i]));
  108. _sprites[i % _tileCount.x][i / _tileCount.x].setPosition((i % _tileCount.x) * _tileSize, (i / _tileCount.x) * _tileSize);
  109. //_sprites[i % _tileCount.x][i / _tileCount.x].setColor(sf::Color(rand() % 256,rand() % 256,rand() % 256, 255)); //Debug
  110. //std::get<2>(_tiles[i]).setTexture(std::get<1>(_tiles[i]));
  111. //std::get<2>(_tiles[i]).setPosition((i % _tileCount.x) * _tileSize, (i / _tileCount.x) * _tileSize);
  112. }
  113. }
  114. void Foreground::create(std::string foreground, std::string map)
  115. {
  116. sf::Image image1, image2;
  117. image1.loadFromFile(foreground);
  118. image2.loadFromFile(map);
  119. create(image1, image2);
  120. }
  121. void Foreground::setResolution(sf::Vector2u resolution)
  122. {
  123. sf::Vector2f temp;
  124. temp.x = static_cast<float>(resolution.x) / static_cast<float>(_tileSize);
  125. temp.y = static_cast<float>(resolution.y) / static_cast<float>(_tileSize);
  126. _renderCount = sf::Vector2u(std::ceil(temp.x) + 1, std::ceil(temp.y) + 1);
  127. }
  128. void Foreground::setPosition(sf::Vector2f position)
  129. {
  130. _position.x = position.x >= 0 ? position.x : 0;
  131. _position.y = position.y >= 0 ? position.y : 0;
  132. }
  133. void Foreground::draw(sf::RenderTarget& target, sf::RenderStates states) const
  134. {
  135. unsigned x = _position.x / _tileSize, y = _position.y / _tileSize;
  136. for(unsigned i = 0; i < _renderCount.x; ++i)
  137. for(unsigned j = 0; j < _renderCount.y; ++j)
  138. if(i + x < _tileCount.x and j + y < _tileCount.y)
  139. target.draw(_sprites[i + x][j + y], states);
  140. }