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

Author Topic: [SOLVED] How to draw a repeated pattern?  (Read 5351 times)

0 Members and 1 Guest are viewing this topic.

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
[SOLVED] How to draw a repeated pattern?
« on: February 19, 2013, 01:50:48 pm »
Hi,

I want to display a background using a tiled pattern.
I know this can be achieve by using a sprite with a repeated texture, but I want to avoid using one image per pattern, because I'd like to group them in a single sprite sheet (I intend to deal with many small patterns so a single image would be much more efficient and convenient).

For example, this is a 64x64 spritesheet with 4 32x32 patterns.

Let's say I want to draw a 640x480 background with the top-left pattern.
I tried using a RectangleShape and a repeated texture:

sf::Texture patterns;
patterns.loadFromFile("patterns.png");
patterns.setRepeated(true);

sf::RectangleShape background;
background.setTexture(&patterns);
background.setTextureRect(sf::IntRect(0, 0, 32, 32));
background.setSize(sf::Vector2f(640, 480));

But I just ended with a streched/scaled background:


Any advices?
« Last Edit: February 19, 2013, 07:06:14 pm by Haze »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a repeated pattern?
« Reply #1 on: February 19, 2013, 02:23:41 pm »
To repeat a texture, you must use a texture rectangle which is bigger than it. So you can only repeat the whole texture, not a sub-rectangle (remember, you call setRepeated on the texture itself).
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How to draw a repeated pattern?
« Reply #2 on: February 19, 2013, 02:38:58 pm »
To repeat a texture, you must use a texture rectangle which is bigger than it. So you can only repeat the whole texture, not a sub-rectangle (remember, you call setRepeated on the texture itself).
Yes that's what I thought, but I was still hoping there was some magic trick I didn't hear about.
So that's mean that if I want to stick with my pattern sprite sheet, I need to code the tiling background myself by drawing the sprite several times?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a repeated pattern?
« Reply #3 on: February 19, 2013, 02:55:42 pm »
You can use a vertex array, if you want it to be optimal.
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How to draw a repeated pattern?
« Reply #4 on: February 19, 2013, 07:04:49 pm »
Ok thank you, I'll do that (or maybe use a RenderTexture).

 

anything