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

Author Topic: Function Returning White Sprite  (Read 2343 times)

0 Members and 2 Guests are viewing this topic.

Haku

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Function Returning White Sprite
« 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;
}
 
« Last Edit: May 31, 2012, 06:50:29 pm by Laurent »

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Function Returning White Sprite
« Reply #1 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

 

anything