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

Author Topic: [Solved] Problem with changing size RenderTexture  (Read 1639 times)

0 Members and 1 Guest are viewing this topic.

ifinox

  • Newbie
  • *
  • Posts: 9
    • View Profile
[Solved] Problem with changing size RenderTexture
« on: February 18, 2017, 10:23:41 am »
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?
« Last Edit: February 18, 2017, 11:59:08 am by ifinox »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Problem with changing size RenderTexture
« Reply #1 on: February 18, 2017, 11:58:06 am »
You simply have to adjust the texture rect as well!

sprite.setTexture(view.getTexture(), true);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/