SFML community forums

Help => Graphics => Topic started by: paul.mihaita on March 14, 2013, 05:15:09 pm

Title: Error, window.draw
Post by: paul.mihaita on March 14, 2013, 05:15:09 pm
Hey guys, i'm new at sfml. I want to draw 2 images but i get 2 errors:
25|error: 'desenbackground' was not declared in this scope|
26|error: 'desenpatrat' was not declared in this scope|

here is the code

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    while (window.isOpen())
    {
         sf::Texture texture;
         if (!texture.loadFromFile("Background.png"))
            return EXIT_FAILURE;
         sf::Sprite desenbackground(texture);
         if (!texture.loadFromFile("Patrat.png"))
            return EXIT_FAILURE;
         sf::Sprite desenpatrat(texture);
    }
    while (window.isOpen())
     {
         sf::Event event;
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed)
                 window.close();
         }

         window.clear();
         window.draw(desenbackground);
         window.draw(desenpatrat);
         window.display();
     }

     return EXIT_SUCCESS;
 }
Title: Re: Error, window.draw
Post by: eXpl0it3r on March 14, 2013, 05:23:16 pm
Welcome to SFML! :)

For posting C++ code here on the forum, you should always use the code=cpp tag.

As for your problem, the compiler tells you exactly what the problem is. desenbackground and desenpatrat are not defined in the scope you're trying to use them in.
If you don't know/understand what a scope is in C++, then you've missed some major part of C++ itself and I advise you to first learn C++ to get a good understand of the language. Here's a list (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) of recommended and good C++ books. ;)

You basically define the two variables (including the textures) in the scope of your first while-loop, but as soon as you get to the end of the loop the variables will get destroyed and either recreated for the next iteration or the code will move on.
Do you even know what you're doing here? Because the first while loop is completely useless and misplaced. ;)
Title: Re: Error, window.draw
Post by: paul.mihaita on March 14, 2013, 05:26:25 pm
Sorry...I know c++ but i didn't noticed that while:|...Sry again for my post