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

Author Topic: Bug with local sf::Image declarations?  (Read 4408 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Bug with local sf::Image declarations?
« on: December 18, 2010, 02:39:27 am »
Hello, I use the SVN revision 1691 of SFML 2 and I compile with Visual Studio 2010. I just encountered a very strange behaviour. Given the following code, a blue sprite is drawn as expected:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow app(sf::VideoMode(200, 200), "Title");
app.SetFramerateLimit(30);

sf::Image image;
image.Create(30, 30, sf::Color::Blue);

for (;;)
{
sf::Event event;
while (app.GetEvent(event))
{
if (event.Type == sf::Event::KeyPressed)
return 0;
}

app.Clear();

sf::Sprite sprite(image);

app.Draw(sprite);
app.Display();
}
}

Now let's declare the sf::Image locally inside the loop (which may be inefficient, but not fundamentally wrong), like this:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow app(sf::VideoMode(200, 200), "Title");
app.SetFramerateLimit(30);

for (;;)
{
sf::Event event;
while (app.GetEvent(event))
{
if (event.Type == sf::Event::KeyPressed)
return 0;
}

app.Clear();

sf::Image image;
image.Create(30, 30, sf::Color::Blue);
sf::Sprite sprite(image);

app.Draw(sprite);
app.Display();
}
}

And what I get is a white sprite. :shock:

Actually, by observing it more precisely, I see it blue for a single frame (the first one), then it becomes white for the rest of the time.

I'm confused because the image is still in scope when the sprite is drawn. Must it exist any longer?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Bug with local sf::Image declarations?
« Reply #1 on: December 18, 2010, 02:47:58 am »
try declaring sprite and image outside of loop

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bug with local sf::Image declarations?
« Reply #2 on: December 18, 2010, 02:53:41 am »
Quote from: "Fierce_Dutch"
try declaring sprite and image outside of loop
This is beside the point, because I'm not searching for workarounds. I would rather like to understand the problem with the second code.

By the way, a full quote isn't always necessary. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Bug with local sf::Image declarations?
« Reply #3 on: December 18, 2010, 08:52:47 am »
Quote
By the way, a full quote isn't always necessary.

Yes. Please please please, stop doing this. The quoted messages is just above, we don't need it to be duplicated N times. We'd like to see your answer directly rather than having to scroll and search within tons of quoted messages ;)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Bug with local sf::Image declarations?
« Reply #4 on: December 18, 2010, 08:57:20 am »
Quote
I'm confused because the image is still in scope when the sprite is drawn. Must it exist any longer?

Nop, this is a bug in the sf::Renderer's cache. When binding an image, if both the image instance and OpenGL texture ID are the same, the image is not bound again. So what happens here is that you have a new image but with the same address in memory and same texture ID, so the renderer doesn't see that it's a new one and keeps the previous (destroyed) one bound.

Hopefully I can get rid of these insane optimizations after the new drawable system is written, it's causing so much troubles.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Bug with local sf::Image declarations?
« Reply #5 on: December 18, 2010, 01:33:27 pm »
Okay, this explains the issue. Wouldn't it be possible to unregister the sf::Image from the cache, when its destructor is called?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Bug with local sf::Image declarations?
« Reply #6 on: December 18, 2010, 01:57:02 pm »
Quote
Okay, this explains the issue. Wouldn't it be possible to unregister the sf::Image from the cache, when its destructor is called?

Nope, because it's in the renderer's cache, and every RenderTarget has its own renderer. So there no link between the image and whatever renderer it is cached in.

But I'm tired of fixing this stuff, the code gets uglier and uglier. It also makes mixing SFML and OpenGL harder than it was in SFML 1.
I'll definitely have to redesign something cleaner.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Bug with local sf::Image declarations?
« Reply #7 on: December 18, 2010, 07:32:28 pm »
Okay. Don't worry, it's not a topic with very high priority, as the bug appears rarely enough.

Nevertheless, do you have some tips how to avoid that bug until it is fixed (respectively, until a better way is found)? Should we just be careful that a image memory address isn't "reused"?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Drektar

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Bug with local sf::Image declarations?
« Reply #8 on: December 18, 2010, 07:43:03 pm »
The address can be used again... If the texture is not the same.

Normally you declare your image before the loop so he don't have problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Bug with local sf::Image declarations?
« Reply #9 on: December 18, 2010, 07:46:37 pm »
Quote
Nevertheless, do you have some tips how to avoid that bug until it is fixed (respectively, until a better way is found)? Should we just be careful that a image memory address isn't "reused"?

You know what causes the bug, now the solution depends on what you do in your real code ;)

Quote
The address can be used again... If the texture is not the same.

Usually, if an image address is reused, then the corresponding OpenGL texture ID is likely to be recycled too.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Bug with local sf::Image declarations?
« Reply #10 on: December 18, 2010, 08:27:19 pm »
I'm asking because maybe there are still some hidden pitfalls of which one doesn't think in the first moment (the not obvious cases). But probably there are not too much of them... I mean it took quite long until I came across the bug (and it resulted from a temporary experiment for debugging). Many SFML users are likely not to recognize it at all ;)

Thanks for the answers! And don't forget to inform us about news :D
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: