SFML community forums

Help => Graphics => Topic started by: fr33k on August 01, 2014, 09:14:14 pm

Title: sf::RenderTexture problem
Post by: fr33k on August 01, 2014, 09:14:14 pm
Is hope it is not normal for sf::RenderTexture:
http://youtu.be/62WJEN9u5mA
Can I fix it somehow, or what I'm doing wrong ?

Graphics Card: Radeon HD 5570
Lastest drivers.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow app(sf::VideoMode(800,600),"RT Test");
    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile("cb.bmp"))
        return EXIT_FAILURE;
    sf::Sprite sprite(texture);

    sf::RenderTexture rt;
    rt.create(800,600);

    sf::Sprite surface(rt.getTexture());

        // Start the game loop
    while (app.isOpen())
    {
        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                app.close();
        }
        sprite.move(0.1f,0);
        // app.resetGLStates(); // with this, all work
        rt.clear(sf::Color::White);
        rt.draw(sprite);
        rt.display();


        // Clear screen
        app.clear();

        // Draw the sprite
        app.draw(surface);

        // Update the window
        app.display();
    }

    return 0;
}
 
Title: Re: sf::RenderTexture problem
Post by: Hapax on August 02, 2014, 12:02:37 am
Why not post the complete code? It can't be much more than what you posted  :o

I just put this in a main() with the require #include and created app as a render window. Not sure why you didn't just post those lines too. It runs fine and doesn't show the artifacts you demonstrated in your video. Which version of SFML are you using, although I would've first thought it was a driver issue?

Worst.texture.ever.
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 02, 2014, 05:21:57 am
Quote
Worst.texture.ever.
Just Code::Blocks SFML project, for test.  :D
I'm using SFML 2.1 :)
Title: Re: sf::RenderTexture problem
Post by: Laurent on August 02, 2014, 08:38:48 am
You can try the latest sources, we made fixes that could solve your problem.
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 02, 2014, 07:11:29 pm
Nope, I still have the same problem.  :-\
Title: Re: sf::RenderTexture problem
Post by: AlexAUT on August 02, 2014, 09:36:59 pm
Try to reset the openGL states before you are drawing/clearing the rendertexture. (I have the same issue)



AlexAUT
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 05:42:56 am
Quote
Try to reset the openGL states before you are drawing/clearing the rendertexture. (I have the same issue)
It's working!  :D
Thanks Alex.
Title: Re: sf::RenderTexture problem
Post by: Laurent on August 03, 2014, 09:33:23 am
Can you show the working code? It could help us to solve this problem.
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 10:13:09 am
Why not.
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")  
#endif // SFML_STATIC

#include <SFML/Graphics.hpp>
#include <SFML\OpenGL.hpp>
#include "include/Defaults.hpp"
#include "include/Debug.hpp"
#include "include/FPS.h"
#include "include/Timer.hpp"
#include "include/Camera.hpp"
#include "include/Animation.hpp"
#include "include/Collision.hpp"
#include "include/Object.hpp"
#include "include/Player.hpp"
#include "include/Scene.hpp"


int main()
{
        sf::RenderWindow window(sf::VideoMode(Default::windowWidth, Default::widnowHeight),
                                                        Default::windowTitle, Default::windowStyle);
        window.setFramerateLimit(60);

        Scene test(sf::Vector2f(400,300));
        sf::Texture * tex = new sf::Texture();
        tex->loadFromFile("gfx/test.png");
        Animation anim;
        anim.setImage(tex, 64);
        anim.setOrigin(32, 32);
        Player hero(sf::Vector2f(64,64),anim);
        test.add(&hero);



        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        hero.eventUpdate(event);
                }
                FPS::update();
                window.resetGLStates();
                hero.update(FPS::dt);
                window.clear(sf::Color::Black);
                test.draw(window);
                window.display();
        }

        delete tex;
        return 0;
}
 

Scene::Scene(sf::Vector2f Size)
{
        previous = NULL; // previous scene pointer
        next = NULL;
        name = "";
        this->Size = Size;
        camera.setCenter(Default::windowWidth / 2, Default::widnowHeight / 2);
        surface.create(Size.x,Size.y, true); // sf::RenderTexture
        surfaceRenderer.setPosition(0, 0); // sprite for render sf::RenderTexture
        surfaceRenderer.setTexture(surface.getTexture());
}
//...
void Scene::draw(sf::RenderWindow & window)
{
        surface.clear(sf::Color::White);
        surface.draw(objects.back()->getAnimation());
        surface.display();
       
        window.draw(surfaceRenderer);
}
 
Title: Re: sf::RenderTexture problem
Post by: Laurent on August 03, 2014, 10:18:45 am
The working version of the code you posted previously would be more helpful :P
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 10:27:54 am
It's the same as in first post, but before app.clear() line, I wrote app.resetGLStates() as Alex proposed.
EDIT: I edited first post.
Title: Re: sf::RenderTexture problem
Post by: Nexus on August 03, 2014, 10:30:46 am
By the way, you should not do that:
sf::Texture * tex = new sf::Texture();
... // big amount of code
delete tex;

The reason is that if you later add a return statement in between, or a function throws an exception, you will have memory leaks.

Instead, use RAII (http://www.bromeon.ch/articles/raii.html):
sf::Texture tex;
... // big amount of code
Title: Re: sf::RenderTexture problem
Post by: AlexAUT on August 03, 2014, 10:46:08 am
I think this should be the same problem as described here: http://en.sfml-dev.org/forums/index.php?topic=14110



AlexAUT
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 10:54:12 am
Thanks Nexus, good to know about RAII :).
EDIT: First video clip is without glResetStates(), second with.
http://youtu.be/q--8EiQdImk
Title: Re: sf::RenderTexture problem
Post by: Hapax on August 03, 2014, 02:47:53 pm
Good to see you fixed the problem!  :)

Just for reporting of this problem, I'm using Radeon HD 5450 and it didn't require the "fix".
Title: Re: sf::RenderTexture problem
Post by: binary1248 on August 03, 2014, 04:21:33 pm
Just for the people who are still clueless because they didn't follow the chain of links AlexAUT provided:

This was fixed in e6b5ce1f27d22aa0150c1376550b2e3f1baec09a (https://github.com/SFML/SFML/commit/e6b5ce1f27d22aa0150c1376550b2e3f1baec09a) a few months ago.

This fix is not in SFML 2.1, so if you can't or don't want to use the latest master, then yes, you will need this "fix".
Title: Re: sf::RenderTexture problem
Post by: Hapax on August 03, 2014, 05:52:01 pm
My fault. I didn't spot that it was fixed inside SFML already.
Title: Re: sf::RenderTexture problem
Post by: Laurent on August 03, 2014, 07:38:31 pm
OP said the latest sources didn't solve his problem.
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 09:17:07 pm
I compiled sources (CMAKE + C::B), created new sfml project in C::B, tried and I have the same problem as before (without resetGLStates()). Maybe I'm doing something wrong, but I didn't want to mislead anyone.  :-X
Or sources from this page, is not what I had to compile: http://sfml-dev.org/download.php ?  ???
If I must get it directly from github, then I'll try again. In edit I'll write if it's working or not.
Title: Re: Re: sf::RenderTexture problem
Post by: Strelok on August 03, 2014, 09:20:43 pm
I compiled sources (CMAKE + C::B), created new sfml project in C::B, tried and I have the same problem as before (without resetGLStates()). Maybe I'm doing something wrong, but I didn't want to mislead anyone.  :-X
Or sources from this page, is not what I had to compile: http://sfml-dev.org/download.php ?  ???
If I must get it directly from github, then I'll try again. In edit I'll write if it's working or not.
The latest stable version (2.1) is far behind from the master branch, clone the repository and try that. I've never tried the snapshots from the site though.
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 09:51:45 pm
Okay, something is wrong:
http://oi60.tinypic.com/2hyyo01.jpg
http://oi58.tinypic.com/28tevj7.jpg
http://oi58.tinypic.com/2s1a9si.jpg

Title: Re: sf::RenderTexture problem
Post by: kralo9 on August 03, 2014, 10:05:31 pm
Okay, something is wrong:
http://oi60.tinypic.com/2hyyo01.jpg
http://oi58.tinypic.com/28tevj7.jpg
http://oi58.tinypic.com/2s1a9si.jpg

Did you link to the new libraries?
Title: Re: sf::RenderTexture problem
Post by: fr33k on August 03, 2014, 10:12:10 pm
Yes, I did.
EDIT: New informations, I setted project to dynamic libs and indeed it's  working without glResetStates().
Title: Re: sf::RenderTexture problem
Post by: Laurent on August 03, 2014, 10:52:43 pm
Quote
Or sources from this page, is not what I had to compile: http://sfml-dev.org/download.php ?
Yeah, compiling the sources of SFML 2.1 is not going to give you something different than the compiled release of SFML 2.1... ;)
When we talk about the latest sources, we mean the current source code as it is today, from the development repository.

Anyway, thanks for doing it and confirming that the bug it fixed :)