Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] RectangleShape has different color than what is being drawn.  (Read 1189 times)

0 Members and 1 Guest are viewing this topic.

ondrej008

  • Newbie
  • *
  • Posts: 8
    • View Profile
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.
« Last Edit: August 20, 2017, 09:55:34 pm by ondrej008 »

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: RectangleShape has different color than what is being drawn.
« Reply #1 on: August 20, 2017, 09:41:43 pm »
In the code you show you are not positioning the tiles so they are all drawn in the same place. The tile that is visible is not necessarily the same one that is modified when clicking.

ondrej008

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: RectangleShape has different color than what is being drawn.
« Reply #2 on: August 20, 2017, 09:55:58 pm »
In the code you show you are not positioning the tiles so they are all drawn in the same place. The tile that is visible is not necessarily the same one that is modified when clicking.
Thank you very much.