1
Graphics / 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:
Initially, I used the following method to draw my tiles which didn't work:
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:
For references the init method for my map:
and the update of the fill color:
And finally, my game draw method:
Edit://
Might also add:
I am using Visual Studio Ultimate 2013
and the latest source of sfml 2.1
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]);
}
}
}
{
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]);
}
}
{
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;
}
{
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);
}
{
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();
}
{
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