Search code examples
c++sfml

Sprite Image getting clipped/cropped in SFML window


I'm using this book called "Beginning C++ game programming" to learn, well, game programming. The following is a snippet from the book which display the background of the first game in an SFML window. (Apologies for my use of namespace I'm just following the book).

#include <SFML/Graphics.hpp>

using namespace sf;

int main() 
{
    RenderWindow window(VideoMode(1920, 1080), "Timber!!", Style::Fullscreen);

    Texture textureBackground;
    textureBackground.loadFromFile("graphics/background.png");
    Sprite spriteBackground;
    spriteBackground.setTexture(textureBackground);
    spriteBackground.setPosition(0,0);

    while (window.isOpen()) 
    {
        if (Keyboard::isKeyPressed(Keyboard::Escape)) {
            window.close();
        }
        window.clear();

        window.draw(spriteBackground);

        window.display();
    }

    return 0;
}

The problem that I'm facing is no image I try in loadFromFile shows up fully in the window. It shows up zoomed towards the top left, even though I'm using the same window size as the resolution of the image.

Any help/advice is appreciated. Cheers!

EDIT: Added images

What I see:(Apologies for the bad picture my screenshot tool doesn't seem to be working when this is on) enter image description here

Original image: enter image description here


Solution

  • Your laptop doesn't have a 1980x1080 resolution. Set the available resolution. Then change the view to fit the whole picture:

    Vector2f textureSize(1980, 1080); // or texture.getSize()
    VideoMode videomode(1980, 1080);
    if (!videomode.isValid()) {
        videomode = VideoMode::getDesktopMode();
    }
    
    RenderWindow window(videomode, "Timber!!", Style::Fullscreen);
    window.setView(View(FloatRect(0, 0, textureSize.x, textureSize.y)));