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

Author Topic: setRepeated over a rectangle?  (Read 1481 times)

0 Members and 1 Guest are viewing this topic.

klefmung

  • Newbie
  • *
  • Posts: 12
    • View Profile
setRepeated over a rectangle?
« on: November 02, 2017, 05:01:34 am »
Sorry, I'm pretty new at SFML (though I tried it once a few years ago and never got any traction). I am sure I am just misunderstanding this feature, but here goes. I tried to get the code down to the simplest version of the problem.

From the documentation, it seemed that if the shape was set to be larger than the texture or selected texture rect, and setRepeated was set to true, it would tile it repeatedly, but for me it just stretches. Here's some code that I believe outlines my problem along with the output screenshotted:

#include <SFML/Graphics.hpp>

int main()
{
    sf::Texture tex;
    if(!tex.loadFromFile("crossed lines.png"))
    {

    }
    tex.setRepeated(true);

    sf::RectangleShape rect1;
    sf::Vector2f rectsize1(50, 50);
    rect1.setSize(rectsize1);
    rect1.setTexture(&tex, false);
    rect1.setPosition(0, 0);

    sf::RectangleShape rect2;
    sf::Vector2f rectsize2(100, 100);
    rect2.setSize(rectsize2);
    rect2.setTexture(&tex, false);
    rect2.setPosition(100, 100);



    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);

        window.draw(rect1);
        window.draw(rect2);

        window.display();
    }

    return 0;
}

 

The output is attached.

So I guess my question is, how can I get this to repeat over rect2 instead of just stretch it?
« Last Edit: November 02, 2017, 06:38:07 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: setRepeated over a rectangle?
« Reply #1 on: November 02, 2017, 06:39:15 am »
The texture rect has to be larger than the texture. If it's twice as large, then it will repeat twice, etc.
Laurent Gomila - SFML developer

klefmung

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: setRepeated over a rectangle?
« Reply #2 on: November 02, 2017, 02:58:23 pm »
I get it now. It's working great. Thanks!