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();
}
}