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

Author Topic: Texture wont load  (Read 4627 times)

0 Members and 1 Guest are viewing this topic.

Whovian1701

  • Newbie
  • *
  • Posts: 1
    • View Profile
Texture wont load
« 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;
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Texture wont load
« Reply #1 on: October 30, 2021, 09:54:39 pm »
Your clear/draw/display NEEDS to be inside your main loop (the while (Window.isOpen()) loop)

Skaldi

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Texture wont load
« Reply #2 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;
}
 

 

anything