SFML community forums

Help => Graphics => Topic started by: Haku on May 31, 2012, 06:17:55 pm

Title: Function Returning White Sprite
Post by: Haku on May 31, 2012, 06:17:55 pm
I know there are much better ways to implement intro screens and such, but I'm just messing around with SFML first to see its power. The problem with the code below is that the drawIntro() function seems to be returning a blank sprite. If anyone can figure out why, it would be greatly appreciated. I'm using SFML 2.0 RC 25.

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

using namespace std;

sf::Sprite drawIntro();

int main()
{
        // Variable declaration
        int gameState = 0;

        // Create the main window
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
       
        sf::Text text("Hello SFML");

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        window.close();
                }

                // Clear screen
                window.clear();

                // Game logic
                switch(gameState)
                {
                        case 0:
                                window.draw(drawIntro());
                                break;
                        case 1:
                                break;
                        case 3:
                                break;
                }
                //window.draw(text);

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

        return 0;
}

sf::Sprite drawIntro()
{
        // Create a new render-texture
        sf::RenderTexture texture;
        //texture.create(800, 600);
        if (!texture.create(800, 600))
                cout<< "Unable to create texture" << endl;
                //return -1;

        // Declare and load a font
        //sf::Font font;
        //font.loadFromFile("resources/fonts/lazy.ttf");       

        //sf::Text text("test");
        /*text.setFont(font);
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setColor(sf::Color::Red);*/


        texture.clear(sf::Color::Black);
        //texture.draw(text);
        texture.display();

        sf::Sprite sprite(texture.getTexture());
       
        return sprite;
}
 
Title: Re: Function Returning White Sprite
Post by: Lo-X on May 31, 2012, 06:36:10 pm
You create a texture in this function, you say to the sprite to refer on this texture and then it's destroyed...

You should learn C++ a bit more, re-read about variable scopes