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

Author Topic: RenderTexture bug?  (Read 3541 times)

0 Members and 1 Guest are viewing this topic.

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
RenderTexture bug?
« on: October 09, 2012, 09:03:15 am »
I'm using master from git. I'm new to SFML! So I'm sorry if this is just a newb mistake. This is just a small excerpt of code that I could get to exhibit what appears to be a bug. I'm posting the source with comments that should make it clear what I'm expecting and what I'm actually getting. Any help is much appreciated!

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

int main()
{
    sf::RenderWindow window(sf::VideoMode(320, 240), "Test");
   
    sf::RenderTexture buffer;
    buffer.create(320, 240);

    sf::RenderTexture buffer2;
    buffer2.create(64, 64);
   
    // Pink box
    buffer.clear(sf::Color(255, 0, 255));
    buffer.display();
   
    // Blue box
    buffer2.clear(sf::Color(0, 255, 255));
    buffer2.display();
   
    // Should draw a light blue box in the upper left corner with a pink
    // background. OK!
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
   
    // White box.
    buffer.clear(sf::Color(255, 255, 255));
    buffer.display();
   
    // Should draw a light blue box in the upper left corner with a white
    // background. NOPE! Pure white background...
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
   
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: RenderTexture bug?
« Reply #1 on: October 09, 2012, 09:19:33 am »
Well it seems quite odd to call multiple this display() in one iteration.

The process of draw things should always be:
window.clear();
window.draw(xyz);
window.display();

You never clear the window which can/will lead to strange behavior and you're calling display twice or even more.
Additionally you should clear() draw() and display() for the window in the main loop. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: RenderTexture bug?
« Reply #2 on: October 09, 2012, 09:27:19 am »
Yep! I know that what I'm doing here is weird looking. The reason is because this is just meant to exhibit the bug, I wouldn't really use SFML like this, I promise!

Here's the code with clear calls before drawing is done on the window, with the same (wrong?) behavior...

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

int main()
{
    sf::RenderWindow window(sf::VideoMode(320, 240), "Test");
    window.clear();
   
    sf::RenderTexture buffer;
    buffer.create(320, 240);

    sf::RenderTexture buffer2;
    buffer2.create(64, 64);
   
    // Pink box
    buffer.clear(sf::Color(255, 0, 255));
    buffer.display();
   
    // Blue box
    buffer2.clear(sf::Color(0, 255, 255));
    buffer2.display();
   
    // Should draw a light blue box in the upper left corner with a pink
    // background. OK!
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
    window.clear();
   
    // White box.
    buffer.clear(sf::Color(255, 255, 255));
    buffer.display();
   
    // Should draw a light blue box in the upper left corner with a white
    // background. NOPE! Pure white background...
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
   
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
    }
}
 

For a little background, I'm creating a JavaScript game engine here: https://github.com/cha0s/avocado and I was interested in implementing a graphics backend using SFML. I've been testing it and this behavior is messing up my progress! :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: RenderTexture bug?
« Reply #3 on: October 09, 2012, 09:52:00 am »
You didn't read right, you need to always call clear before display and not just once somewhere in the middle. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: RenderTexture bug?
« Reply #4 on: October 09, 2012, 09:55:48 am »
*Sigh*

I did read right, and that's why I posted the second code. Here's new code that does the exact same thing, and exhibits the exact same bug, with a couple of things shuffled around to make you happy:

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

int main()
{
    sf::RenderWindow window(sf::VideoMode(320, 240), "Test");
   
    sf::RenderTexture buffer;
    buffer.create(320, 240);

    sf::RenderTexture buffer2;
    buffer2.create(64, 64);
   
    // Pink box
    buffer.clear(sf::Color(255, 0, 255));
    buffer.display();
   
    // Blue box
    buffer2.clear(sf::Color(0, 255, 255));
    buffer2.display();
   
    // Should draw a light blue box in the upper left corner with a pink
    // background. OK!
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.clear();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
   
    // White box.
    buffer.clear(sf::Color(255, 255, 255));
    buffer.display();
   
    // Should draw a light blue box in the upper left corner with a white
    // background. NOPE! Pure white background...
    buffer.draw(sf::Sprite(buffer2.getTexture()));
    buffer.display();
    window.clear();
    window.draw(sf::Sprite(buffer.getTexture()));
    window.display();
   
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
    }
}
 

No offense, but could you help instead of making me do busy work by shuffling my code around into the semantic equivalent over and over? :P

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: RenderTexture bug?
« Reply #5 on: October 09, 2012, 10:39:42 am »
Well you just don't realise that I am helping. Writing wrong code to reproduce a problem won't get you anywhere and this has nothing to do with nice coding or anything your old code is just wrong and I'm not gonna try to fix things on top of wrong code, since one never knows what acutally causes the problem.
Besides fixing things always goes with writing things newly/differently, if you don't want that, then don't ask for help...
Also if you post wrong code but have applied it correctly to your code how should I know it? ::)

As for the problem, what happens if you clear the buffer before drawing buffer2 to it?
And does pure white background referre to the whole window or just the rendertexture box?
« Last Edit: October 09, 2012, 10:43:04 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: RenderTexture bug?
« Reply #6 on: October 09, 2012, 11:36:05 am »
you can save texture to disc, and look where it goes wrong.

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: RenderTexture bug?
« Reply #7 on: October 09, 2012, 03:40:52 pm »
what happens if you clear the buffer before drawing buffer2 to it?

Please explain how that is not what I'm doing, I genuinely don't understand. I clear buffer both times before I render buffer2 on it.

And does pure white background referre to the whole window or just the rendertexture box?

Pure white refers to what you see if you run the program, it is the end state of 'window' after all operations have been applied.

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: RenderTexture bug?
« Reply #8 on: October 09, 2012, 04:15:38 pm »
I just did some more testing and I found that commenting the first
window.draw(sf::Sprite(buffer.getTexture()));
line causes it to work. I'm very confused! It really feels like a bug. I think I'll go to the stable version and try that.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture bug?
« Reply #9 on: October 09, 2012, 05:54:09 pm »
Your last example works for me, I see a light blue square on a white background when I run it.

What's your OS and graphics card? Are your graphics drivers up to date? (by the way, you should have started with this ;))
Laurent Gomila - SFML developer

cha0s

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: RenderTexture bug?
« Reply #10 on: October 09, 2012, 06:21:32 pm »
Thanks for the hint, Laurent! It was my fault all along. Somewhere along the line (probably during a kernel update: running Xubuntu 12.04 btw) I missed updating my nvidia drivers. Updating them made everything work lovely!

Thanks and I'll try to limit the noob questions... :P