Hello,
I've made it so that when I click, I get the closest Tile and use
sf::Color prev = Tile.getFillColor();
Tile.setFillColor(sf::Color(prev.r, prev.g + 32, prev.b, 255));
When printing this using cout, it changes when I click, however, visually nothing happens.
Here is the code snippet:
#include <SFML/Graphics.hpp>
#include <iostream>
using std::vector;
using std::cout;
using std::string;
string nl = "\n";
class Tile : public sf::Drawable
{
public:
sf::RectangleShape shape;
bool canMove;
Tile(sf::RectangleShape rect, bool can = true) : shape(rect), canMove(can)
{
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(shape);
}
};
int main()
{
sf::RectangleShape defaultShape(sf::Vector2f(16, 16));
Tile defaultTile(defaultShape);
vector<vector<Tile>> tiles(40, vector<Tile>(40, defaultTile));
sf::RenderWindow window(sf::VideoMode(640, 640), "SFML works!");
window.setFramerateLimit(30);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::MouseButtonPressed)
{
sf::Vector2i pixel = sf::Mouse::getPosition(window);
sf::Vector2f world = window.mapPixelToCoords(pixel);
int tileX = (int) world.x/16;
int tileY = (int) world.y / 16;
sf::Color color = tiles[tileX][tileY].shape.getFillColor();
tiles[tileX][tileY].shape.setFillColor(sf::Color(color.r, color.g + 32, color.b, 255));
cout << (int) tiles[tileX][tileY].shape.getFillColor().g << nl;
}
}
window.clear(sf::Color::White);
for(uint32_t x = 0; x < tiles.size(); x++)
{
for(uint32_t y = 0; y < tiles.size(); y++)
{
window.draw(tiles[x][y]);
}
}
window.display();
}
return 0;
}
As you can see, in the console it prints different values per click, but it stays the same on the screen.