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

Author Topic: SFML sprite crops for some reason  (Read 5924 times)

0 Members and 1 Guest are viewing this topic.

Nemecis

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML sprite crops for some reason
« on: June 26, 2014, 10:45:00 pm »
I have small code to render Mandelbrot set. The actual set rendering, and image are correct (I saved it on HDD, and it displayed correct). My problem however is, that for some reason, when resizing window, the image behaves weirdly. Firstly if I do nothing with the scale (which should be automatic if I understand correctly), the image scales different rate than window (although a new image has been generated to match the new window size). If I do handle the scale, bottom part and left side of the set is cropped out as black (if I save the image, the bottom part is there). What am I doing wrong?


Main file, where pretty much everything happens:
int main()
{
        int windowWidth = 1500, windowHeight = 800;
        sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Mandelbrot");
        window.setFramerateLimit(60);

       
        sf::Texture texture;
        std::cout << "create MB \n";
        sf::Image MB = createMBPicture(windowWidth, windowHeight);
       
       



        texture.loadFromImage(MB, sf::IntRect());

        sf::Sprite sprite;

        sprite.setTexture(texture);
        int i = 0;
    while (window.isOpen())
    {
               

        sf::Event event;

        while (window.pollEvent(event))
        {

            if (event.type == sf::Event::Closed)
                window.close();
                        if (event.type == sf::Event::Resized){

                                windowWidth = event.size.width;
                                windowHeight = event.size.height;
                                std::cout << "W: " << windowWidth << "H: " << windowHeight << " \n";
                               
                                MB = createMBPicture(windowWidth, windowHeight);
                                MB.saveToFile("E:\\Mandelbrot.jpg");
                                //Problem somewhere near here.
                                texture.loadFromImage(MB, sf::IntRect());
                               
                                sprite.setTexture(texture);


                               
                                sprite.setScale(1500.0f/(float)windowWidth, 800.0f/(float)windowHeight); //Scaling handling here
                                std::cout << "W: " << sprite.getScale().x << "H: " << sprite.getScale().y << " \n";
                               

                        }
        }

        window.clear();

                window.draw(sprite);

        window.display();
    }

    return 0;
       
}

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML sprite crops for some reason
« Reply #1 on: June 26, 2014, 11:23:26 pm »
When the window is resized, its view doesn't change, so even if the window is smaller or bigger, it still displays the exact same area of the 2D scene. So if you did nothing at all in your code regarding the Resized event, it would work perfectly.

But since you handle the Resized event, I assume that you want to regenerate a pixel-perfect image rather than displaying a (automatically in this case) scaled one. If you resize your image to match the new window size, you must adjust the view as well. Or, since there's only a single element displayed in the window, adjust its scale -- the result will be the same.

So what exactly is wrong when you handle the scale? Your code looks good.
Laurent Gomila - SFML developer

Nemecis

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML sprite crops for some reason
« Reply #2 on: June 27, 2014, 08:03:11 am »
I banged my head for a while, and rewrote that exactly the same code, and found out, that there was an option bool resetRect = false, which probably was the reason it cropped parts of the image, since when I changed the code to:

//........
//Problem somewhere near here.
                texture.loadFromImage(MB, sf::IntRect());
               
                sprite.setTexture(texture, true); // <-- Here the problem was
//....

And the croping was no more. So there was only one boolean that I forgot, typical mistake, sorry for your inconvenience, and thank you.

 

anything