Tile an image with SFML
Asked Answered
D

1

5

I have a background image and it's only 256 x 256 when my window is 800 x 600. I'm not sure how to get the image to repeat itself across the whole window. I currently load the image in with:

sf::Texture Bkg;
if(!Bkg.loadFromFile("darkPurple.png"))
{
    return -1;
}

sf::Sprite Sprite;
Sprite.setTexture(Bkg);

and the draw it later with:

window.draw(Bkg);

I tried to use:

texture.setRepeated(true);

but that didn't seem to help.

Thanks!

Delp answered 1/2, 2014 at 20:9 Comment(1)
As a side note, if you're trying to color your background, you don't need a jpg, you can just use this: window.clear(sf::Color(128,0,128)); provided that "window" is a RenderWindow object.Culbertson
S
7

After you load the image you need to call setReapeted:

    texture.setRepeated(true);

And after that, when load texture in your sprite, set the texture rect to be to your size of screen:

    sprite.setTexture(texture);
    sprite.setTextureRect(sf::IntRect(0,0,800,600);
Sst answered 2/2, 2014 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.