-
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();
}
}
-
Ok I managed to get the image to load, however it stops working almost immediately afterwards.
-
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);
-
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();
}
}
-
Hi oOhttpOo, You need to read the tutorials again, It's all really well explained. Start with the Drawing 2D stuff (http://www.sfml-dev.org/tutorials/2.2/graphics-draw.php)
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();
}
}