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.
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.