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

Author Topic: SFML_T.exe has stopped working error  (Read 2304 times)

0 Members and 1 Guest are viewing this topic.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
SFML_T.exe has stopped working error
« on: April 14, 2015, 10:43:45 pm »
Hello, I am trying to draw a sprite I made with the texture, however when I run it I get "SFML_T.exe has stopped working. I am new to this by the way. However the texture seems to 'load', because if you don't include the draw sprite bit it works as it should. I read the tutorials, but they didn't really mention my problem. Here is my code:
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window1(sf::VideoMode(400, 400), "Window");
        window1.setVerticalSyncEnabled(true); // call it once, after creating the window
        while (window1.isOpen())
        {
                sf::Event event;
                while (window1.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window1.close();
                }
                window1.clear(sf::Color::Black);
               
                sf::Texture texture;
                if (!texture.loadFromFile("pic.jpg"))
                {
                        //error
                }
       
                sf::Sprite sprite1;
                sprite1.setTexture(texture);
                window1.draw(sprite1);
       
window1.display();

        }
       
}
 
« Last Edit: April 14, 2015, 11:13:59 pm by oOhttpOo »

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: SFML_T.exe has stopped working error
« Reply #1 on: April 14, 2015, 11:13:22 pm »
Ok I managed to get the image to load, however it stops working almost immediately afterwards.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: SFML_T.exe has stopped working error
« Reply #2 on: April 16, 2015, 10:55:14 am »
Texture is destroyed at the final of loop. Try to put everything between .clear and .display out of the main loop and keep only
window1.draw(sprite1);
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: SFML_T.exe has stopped working error
« Reply #3 on: April 16, 2015, 07:02:57 pm »
Texture is destroyed at the final of loop. Try to put everything between .clear and .display out of the main loop and keep only
window1.draw(sprite1);

I did (or at least tried) to do what you said, however I still get the same error  :-/ By "put everything between .clear and .display, do you mean in an if statement or something else? Here is my code. Cheers
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>



int main()
{
        sf::RenderWindow window1(sf::VideoMode(400, 400), "Window");
        window1.setVerticalSyncEnabled(true); // call it once, after creating the window
        while (window1.isOpen())
        {
                sf::Event event;
                while (window1.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window1.close();
                }
                window1.clear(sf::Color::Black);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sf::Texture texture;
                        if (!texture.loadFromFile("pic.jpg"))
                        {
                                //error
                        }

                        sf::Sprite sprite1;
                        sprite1.setTexture(texture);
                        window1.draw(sprite1);
                }
               

                window1.display();

        }

}      

Ivan

  • Newbie
  • *
  • Posts: 32
  • IT geek
    • View Profile
Re: SFML_T.exe has stopped working error
« Reply #4 on: April 17, 2015, 01:16:59 pm »
Hi oOhttpOo, You need to read the tutorials again, It's all really well explained. Start with the Drawing 2D stuff



               
                window1.clear(sf::Color::Black);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sf::Texture texture;
                        if (!texture.loadFromFile("pic.jpg"))
                        {
                                //error
                        }

                        sf::Sprite sprite1;
                        sprite1.setTexture(texture);
                        window1.draw(sprite1);
                }
               
                window1.display();
 


The problem with your code is that you are creating the texture and sprite inside the if, and it's destroyed when the if finishes. You must define it before the game loop (while (window1.isOpen())).

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window1(sf::VideoMode(400, 400), "Window");
    window1.setVerticalSyncEnabled(true); // call it once, after creating the window

    sf::Texture texture;
    if (!texture.loadFromFile("pic.jpg"))
    {
        //error
    }

    sf::Sprite sprite1;
    sprite1.setTexture(texture);

    while (window1.isOpen())
    {
        sf::Event event;
        while (window1.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window1.close();
        }
        window1.clear(sf::Color::Black);
        window1.draw(sprite1);
        window1.display();
    }
}