SFML community forums

Help => General => Topic started by: Whovian1701 on October 30, 2021, 08:40:14 pm

Title: Texture wont load
Post by: Whovian1701 on October 30, 2021, 08:40:14 pm
Hey, I want a Window with a green background and a texture in the middle, but my texture wont load, doess anyone know how to fix this?
This is the code:

#include <iostream>
#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
   RenderWindow Window(VideoMode(1000, 700), "MyGame");
   Texture texture;
   Sprite sprite;
   if (!texture.loadFromFile("Bug1.png"))
      std::cout << "nicht geladen" << std::endl;
      return -1;
   sprite.setTexture(texture);
   sprite.setScale(0.5f, 0.5f);
   sprite.setPosition(450, 320);
   
   while (Window.isOpen())
   {
      Event event;
      while (Window.pollEvent(event))
         if (event.type == Event::Closed) Window.close();
   }
   Window.clear(Color::Green);
   Window.draw(sprite);
   Window.display();
   

   return 0;
}
Title: Re: Texture wont load
Post by: G. on October 30, 2021, 09:54:39 pm
Your clear/draw/display NEEDS to be inside your main loop (the while (Window.isOpen()) loop)
Title: Re: Texture wont load
Post by: Skaldi on November 02, 2021, 12:34:25 pm
Hello,

also, this code will always return -1. After an if statement without {} only the next statement will be in the if branch. This means, no matter what, return -1 will be executed.
if (!texture.loadFromFile("Bug1.png"))
      std::cout << "nicht geladen" << std::endl;
      return -1;
 

I am quite sure your intention is this:
if (!texture.loadFromFile("Bug1.png"))
{
      std::cout << "nicht geladen" << std::endl;
      return -1;
}