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

Author Topic: (Solved!) texture NOT updating from window  (Read 4011 times)

0 Members and 1 Guest are viewing this topic.

burnack

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
(Solved!) texture NOT updating from window
« on: November 04, 2016, 03:15:46 pm »
Hello, I'm facing a problem of a sf::Texture NOT updating from sf::RenderWindow in SFML 2.4.
Here's the code example:
#pragma once

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

int main ()
{
        sf::RenderWindow window (sf::VideoMode (1280, 720), sf::String ("100%"));
        sf::Texture grassTexture;
        if (!grassTexture.loadFromFile ("grass.jpg", sf::IntRect (0, 0, 1280, 720)))
        {
                std::cout << "Could not load image" << std::endl;
                return -1;
        }

        sf::Sprite background (grassTexture, sf::IntRect { 0, 0, 1280, 720 });

        while (window.isOpen ())
        {
                sf::Event event;
                while (window.pollEvent (event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close ();
                       
                        if (event.type == sf::Event::MouseButtonReleased &&
                                event.mouseButton.button == sf::Mouse::Left)
                        {
                                window.clear (sf::Color::White);
                                window.display ();
                                grassTexture.update (window, 0, 0);
                                sf::Image tmpImg = grassTexture.copyToImage ();
                                tmpImg.saveToFile ("tmp.jpg");

                                background = sf::Sprite (grassTexture);
                        }
                }

                window.clear (sf::Color::White);
                window.draw (background);
       
                window.display ();

                if (sf::Keyboard::isKeyPressed (sf::Keyboard::Escape))
                {
                        window.close ();
                }
        }

        return 0;
}
 
So after a left-click the texture should update with the white screen. I even save it to an image which shows that it's still the grass image loaded initially (it's attached) and not a white image.

The udpate() function description says:
    /// No additional check is performed on the size of the window,
    /// passing an invalid combination of window size and offset
    /// will lead to an undefined behavior.
    ///
    /// This function does nothing if either the texture or the window
    /// was not previously created.
 
These both are not the case as you can see.

I'm looking forward to any help.
« Last Edit: November 05, 2016, 10:29:09 pm by burnack »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: texture NOT updating from window
« Reply #1 on: November 05, 2016, 11:45:02 am »
Removing the call to display on the window (or calling it twice) fixes this.
i.e.
                                window.clear(sf::Color::White);
                                grassTexture.update(window, 0, 0); // you don't need this offset though since it's (0, 0)

Your "background" sprite should not need updating at all, by the way, as it's already a sprite that uses the grassTexture texture. It would only need updating here if the texture changed size.
« Last Edit: November 05, 2016, 11:47:13 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: texture NOT updating from window
« Reply #2 on: November 05, 2016, 03:44:53 pm »
I know the reason why this happens:
 SFML window is always double buffered, what does it mean? You draw to the back buffer and then swap it with read buffer(window.display() does the swapping).

What is thiat code doing(rather fragment of code)? Let's imagine, that "a" the first buffer and "b" the second buffer. Then we can describe code's work with following list of actions:
1.) write some stuff to "a"
2.) swap "a" with "b". Now all operations such as "draw" and "read screen" are now operating on "b".
3.) try to read data from "b", which contains previous rendered frame.

burnack

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: texture NOT updating from window
« Reply #3 on: November 05, 2016, 10:27:38 pm »
Thank you Hapax and Mr_Blame for the explanations, deleting the display() call has solved the problem!