Repeating texture to fit certain size in SFML

牧云@^-^@ 提交于 2019-12-05 10:34:24

You can use sf::Texture::setRepeated to do that.

However, you'll need to copy that part of your bigger image into an independant texture.

Here is an example:

#include <SFML/Graphics.hpp>

int main(int, char const**)
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    sf::Image img;
    img.create(20, 20);
    for (auto i = 0; i < 20; ++i) {
        for (auto j = 0; j < 20; ++j) {
            img.setPixel(i, j, sf::Color(255 * i / 20, 255 * j / 20, 255 * i / 20 * j / 20));
        }
    }

    sf::Texture texture;
    texture.loadFromImage(img);
    texture.setRepeated(true);

    sf::Sprite sprite;
    sprite.setTexture(texture);
    sprite.setTextureRect({ 0, 0, 800, 600 });

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

            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }

        window.clear();
        window.draw(sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!