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

Author Topic: RectangleShape setFillColor strange behaviour in std::vector  (Read 1074 times)

0 Members and 2 Guests are viewing this topic.

KingGilgamesh

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
RectangleShape setFillColor strange behaviour in std::vector
« on: March 08, 2014, 11:30:18 pm »
hi,
I am really clueless about the following:

I got my game-loop in which I call the draw method of my Map class.
Additionally, I call setFillColor on a certain sf::RectangleShape of my Map class.
The debugger tells me that the color has succesfully changed.

However, it will only be visually updated in the following draw method:

void Map::draw(sf::RenderWindow* w)
{
    for (int x = 0; x < MAP_WIDTH; x++)
    {
         for (int y = 0; y < MAP_HEIGHT; y++)
         {
                int i = x * MAP_HEIGHT+ y;
                w->draw(tiles[i]);
         }
    }
}
 

Initially, I used the following method to draw my tiles which didn't work:
void Map::draw(sf::RenderWindow* w)
{
    for (int i = 0; i < tiles.size(); i++)
    {
         w->draw(tiles[i]);
    }
}
 

It draws all tiles, but it doesn't draw the updated FillColor, even though the FillColor for the certain Tile changed its value succesfully.

I double checked the values of both i variables(both, in debugger and I printed them in a log file).
They are exactly the same.
Both starts at 0 and increase up to 63.
It just makes no sense to me.

As mentioned, it is working with the first method but not with the second and I'm kinda afraid that I do something horrible wrong.


My tiles are just a member of my Map Class:
std::vector<sf::RectangleShape> tiles;
 

For references the init method for my map:
bool Map::initialize()
{
        for (int x = 0; x < MAP_WIDTH; x++)
        {
                for (int y = 0; y < MAP_HEIGHT; y++)
                {
                        sf::RectangleShape tile;
                        tile.setFillColor(sf::Color::Cyan);
                        tile.setOutlineColor(sf::Color::Blue);
                        tile.setOutlineThickness(1.0f);

                        tile.setPosition(START_X + x*tile_width, START_Y + y*tile_height);
                        tile.setSize(sf::Vector2f(tile_width, tile_height));

                        tiles.push_back(tile);
                }
        }
        return true;
}
 

and the update of the fill color:
void GsGame::highlightTile()
{
        PositionComponent* pos = entities[0].getComponent<PositionComponent>();

        //it returns the right tile, checked it
        sf::RectangleShape* tile = map.positionToTile(pos->x, pos->y);
       
        //breakpoint at setFillColor is only reached once, so the FillColor must have changed succesully.
        if (tile->getFillColor() != sf::Color::Yellow)
                tile->setFillColor(sf::Color::Yellow);

}
 

And finally, my game draw method:
void GsGame::draw()
{
        window->clear(clearColor);
        highlightTile();
        map.draw(window);
        window->display();
}
 


Edit://
Might also add:
I am using Visual Studio Ultimate 2013
and the latest source of sfml 2.1
« Last Edit: March 08, 2014, 11:41:36 pm by KingGilgamesh »

Doodlemeat

  • Guest
Re: RectangleShape setFillColor strange behaviour in std::vector
« Reply #1 on: March 09, 2014, 12:33:25 pm »
Hello.
This might not be the answer you are looking for, but I can suggest using a vertex array to draw your tiles.
The second last example in this page: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

And if you want to simlulate a 2 dimensional list of tiles you will use this:

int tileIndex = y * map_width + x;

I'll hope it will fix your problem.

 

anything