简体   繁体   中英

SFML Texture Transforming (Enlarging)

#include <iostream>
#include <SFML/Graphics.hpp>    

int main()
{
    int desktopwidth = sf::VideoMode::getDesktopMode().width;
    int desktopheight = sf::VideoMode::getDesktopMode().height;
    sf::ContextSettings settings;settings.antialiasingLevel = 8;
    sf::RenderWindow window(sf::VideoMode(desktopwidth, desktopheight), "Texture Transforming!", sf::Style::Fullscreen, settings);
    while (window.isOpen())
    {
        sf::Image image;
        if (!(image.loadFromFile("texture.jpg")))
            std::cout << "Cannot load image";   //Load Image
        sf::Texture texture;
        texture.loadFromImage(image);  //Load Texture from image
        sf::Sprite sprite;
        sprite.setTexture(texture);
        sprite.setTextureRect({ 0, 0, desktopwidth, desktopheight });
        window.clear();
        window.draw(sprite);
        window.display();
    }
    return 0;
}

What I want to do is stretch the texture to the full size of the screen when the screen is bigger than the image. Using setTextureRect all I have achieved is stretching out the last pixel to the new size which isn't a great look for what i am trying to do, as image resize was removed, does anyone know of a work around to resize images or transform texture size/scales?

sf::Sprite::setTextureRect() defines the part of the texture that's visible as the sprite. It doesn't define the actual display size.

For this purpose you might want to use sf::Sprite::setScale() , which allows you to define a scaling ratio. For example, calling sprite.setScale(2, 2); will make the displayed sprite twice as wide/high.

If you want to downscale/upscale your whole scene, you might want looking into sf::View though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM