This is the code that reproduce the problem:
#include <vector>
#include <SFML/Graphics.hpp>
class UI : public sf::Drawable
{
virtual void draw(sf::RenderTarget & target, sf::RenderStates states) const
{
target.draw(sprite);
}
sf::RenderTexture view;
sf::Sprite sprite;
public:
UI()
{
}
void draw(const sf::Drawable & drawable)
{
view.draw(drawable);
}
void setSize(int x, int y)
{
view.create(x, y);
view.clear();
}
void update(const std::vector<sf::RectangleShape>& squares)
{
for (int i = 0; i < squares.size(); i++)
view.draw(squares[i]);
view.display();
sprite.setTexture(view.getTexture());
}
};
int main()
{
sf::RectangleShape shape;
shape.setSize(sf::Vector2f(20, 20));
shape.setFillColor(sf::Color::White);
int actualPosition = 0;
std::vector <sf::RectangleShape> shapes;
UI ui;
for (int i = 0; i < 10; i++)
{
shape.setPosition(actualPosition, 0);
shapes.push_back(shape);
actualPosition += 21;
}
ui.setSize(21 * shapes.size(), 20);
ui.update(shapes);
sf::RenderWindow window(sf::VideoMode(21*shapes.size(), 20), "test");
window.setFramerateLimit(20);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Space)
{
shape.setPosition(actualPosition, 0);
shapes.push_back(shape);
actualPosition += 21;
ui.setSize(21 * shapes.size(), 20);
ui.update(shapes);
}
if (event.key.code == sf::Keyboard::B)
{
shapes.pop_back();
actualPosition -= 21;
ui.setSize(21 * shapes.size(), 20);
ui.update(shapes);
}
window.create(sf::VideoMode(21 * shapes.size(), 20), "test");
window.setFramerateLimit(20);
}
}
window.clear();
window.draw(ui);
window.display();
}
return 0;
}
When we remove square from std::vector everything is good.
When we add square to std::vector everthing is good.
When we add another square that will be more than starting 10 (or any number defined before main program loop) squares, window will resize but RenderTarget will don't draw squares that is more than 10.
What am I doing wrong?