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

Author Topic: SFML 2.1 & 2.0? issue with rendertexture inside functions? White background?  (Read 3115 times)

0 Members and 1 Guest are viewing this topic.

Lamonte

  • Newbie
  • *
  • Posts: 46
    • View Profile
So I'm playing with SFML 2.1 and trying to get a sf::RenderTexture setup and then pass it to a sprite and output it to the window.

So I ran a test and did the following:

call above int main()
sf::Sprite testing() {
        //scene main texture
        sf::RenderTexture scene_texture;
        scene_texture.create( 800,600 );

        //top menu section
        sf::Texture menubg_texture;
        menubg_texture.create( 800, 107 );
        if( !menubg_texture.loadFromFile( "Assets/Scenes/Default/background_section.png", sf::IntRect( 0, 0, 800, 107 ) ) ) {
                cout << "Couldn't load file" << endl;
        }

        sf::Sprite menubg_sprite( menubg_texture );

        menubg_sprite.setTextureRect( sf::IntRect( 0, 0, 800, 107 ) );
        menubg_sprite.setPosition( 0.f, 0.f );

        //draw menu to scene
        scene_texture.clear( sf::Color( 0, 255, 0 ) );
        scene_texture.draw( menubg_sprite );
        scene_texture.display();

        //sf::Sprite scene_sprite( scene_texture.getTexture() );
       
        sf::Sprite sprite;
        sprite.setTexture( scene_texture.getTexture() );
        return sprite;
}

The main.cpp main() function

int main() {
        sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "Temporary Window", sf::Style::Titlebar | sf::Style::Close );
        sf::RectangleShape background( sf::Vector2f(800.f, 600.f) );
        background.setPosition( 0.f, 0.f );
        background.setFillColor( sf::Color(255, 0, 0) );

        // Start the game loop
        while ( app_window.isOpen() ) {


                // Process events
                sf::Event event;

                while ( app_window.pollEvent( event ) ) {

                        // Close window : exit
                        if ( event.type == sf::Event::Closed ) {
                                app_window.close();
                        }

                        // Mouse Events
                        if( event.type == sf::Event::MouseMoved ) {
                        }

                        if( event.type == sf::Event::MouseButtonPressed ) {
                                if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) ) {
                                }
                        }
                }


                // Clear screen
                app_window.clear( sf::Color( 0, 255, 0) );

                //draw stuff
                app_window.draw( background );
                app_window.draw( testing() );

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

        return EXIT_SUCCESS;
}

So I'm like okay what's going on here.  I literally copy the code from the function and replace the:

app_window.draw( testing() ); with the code from above then do: app_window.draw(sprite);

And the image texture loads perfectly.  What am I doing wrong or is this a bug with SFML 2.1?  About to test SFML 2.0 right now.
« Last Edit: July 30, 2013, 04:39:42 pm by Lamonte »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Please read carefully the end of the sprite/texture tutorial ("The white square problem").
Laurent Gomila - SFML developer

Lamonte

  • Newbie
  • *
  • Posts: 46
    • View Profile
Please read carefully the end of the sprite/texture tutorial ("The white square problem").

God okay I swear I tried this before, created a blank project and just sent the renderwindow reference to the function and it worked.

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

class Test {
public:
        sf::Sprite* sprite;

        Test();
        void load_texture( sf::RenderWindow *);
};

Test::Test() {}

void Test::load_texture( sf::RenderWindow * window) {
       
        //scene render texture
        sf::RenderTexture scene_texture;
        scene_texture.create( 800, 600 );

        //section texture
        sf::Texture menubg_texture;
        menubg_texture.create( 800, 600 );
        menubg_texture.loadFromFile( "Assets/Scenes/Default/background_section.png" );

        //section sprite
        sf::Sprite menubg_sprite( menubg_texture );
        menubg_sprite.setTextureRect( sf::IntRect( 0, 0, 800, 107 ) );
        menubg_sprite.setPosition( 0.f, 0.f );

        //draw menu to scene render texture
        scene_texture.clear( sf::Color( 0, 255, 0 ) );
        scene_texture.draw( menubg_sprite );
        scene_texture.display();

        sf::Sprite s( scene_texture.getTexture() );
        window->draw( s );
}

int main() {

        Test t;

        // Create the main SFML window
        sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "Temporary Window", sf::Style::Titlebar | sf::Style::Close );
        sf::RectangleShape background( sf::Vector2f(800.f, 600.f) );
        background.setPosition( 0.f, 0.f );
        background.setFillColor( sf::Color(255, 0, 0) );

        // Start the game loop
        while ( app_window.isOpen() ) {


                // Process events
                sf::Event event;

                while ( app_window.pollEvent( event ) ) {

                        // Close window : exit
                        if ( event.type == sf::Event::Closed ) {
                                app_window.close();
                        }

                }

                // Clear screen
                app_window.clear( sf::Color( 0, 255, 0) );

                //draw stuff
                app_window.draw( background );
               
                t.load_texture( &app_window );

                // Update the window
                app_window.display();
        }
       
        return EXIT_SUCCESS;
}

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
God okay I swear I tried this before, created a blank project and just sent the renderwindow reference to the function and it worked.

Even with your small changes you still have the same problem as before. Reread the tutorial and pay close attention to "the white square problem" just as Laurent already said.

You shouldn't be loading the texture over and over again every time you draw your sprite.
« Last Edit: July 31, 2013, 01:19:35 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hanmac

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
There is the Tutorial link
infomation shorted: you need to define the Texture outside of the loop or you use an Pointer instead than an reference (and create the Texture with new)