SFML community forums

Help => General => Topic started by: coolian on June 30, 2018, 07:25:47 pm

Title: Image wont load
Post by: coolian on June 30, 2018, 07:25:47 pm
hello guys :)
i have a problem with well obviously an  sfml project my image wont load but the path should be correct if sfml dosnt do anything with it and the file format is .png and the names are right in code now i want to know why it isnt loading here i ahve some code
/code #include<iostream>
#include<fstream>
#include<string>
#include"SFML\Graphics.hpp"

sf::Texture loadImage(std::string);
sf::Sprite createSprite(sf::Texture,float,float);

int main() {

   sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "menu");

   float pos = 0.0f;
   float pos1 = 0.0f;

   sf::Texture dirt = loadImage("dirt.png");
   sf::Texture grass = loadImage("grass.png");
   sf::Texture player = loadImage("dirt.png");

   sf::Sprite s_player = createSprite(player, pos, pos1);

   sf::Event event;

   while (renderWindow.isOpen()) {
      while (renderWindow.pollEvent(event)) {
         if (event.type == sf::Event::EventType::Closed) {
            renderWindow.close();
         }
      }

      renderWindow.clear(sf::Color::White);
      renderWindow.draw(s_player);
      renderWindow.display();
   }

   return 0;
}

sf::Texture loadImage(std::string fileName) {
   sf::Texture texture;
   if (!texture.loadFromFile(fileName)) {
      std::cout << "failed to load image" + fileName << std::endl;
   }
   return texture;
}

sf::Font loadFont(std::string fileName, sf::Font font) {
   if (!font.loadFromFile(fileName)) {
      std::cout << "failed to load font" + fileName << std::endl;
   }
   return font;
}

sf::Sprite createSprite(sf::Texture texture, float pos, float pos2) {
   sf::Sprite sprite(texture);
   sprite.setPosition(pos, pos2);
   return sprite;
}

btw if anyone could tell me how to mark some text as code then please do it
Title: Re: Image wont load
Post by: Stauricus on June 30, 2018, 09:19:25 pm
the problem is that you are creating a texture inside this function
sf::Texture loadImage(std::string fileName) {
   sf::Texture texture;
   if (!texture.loadFromFile(fileName)) {
      std::cout << "failed to load image" + fileName << std::endl;
   }
   return texture;
}
when the function ends, the texture is destroyed.

why don't you just use
sf::Texture dirt;
dirt.loadFromFile("dirt.png");

instead of
sf::Texture dirt = loadImage("dirt.png");

?
Title: Re: Image wont load
Post by: Rosme on July 03, 2018, 03:45:13 pm
The issue is with your createSprite function. You copy the texture. If you pass it as a const reference (const sf::Texture&) it will be good. However, I don't see the usefulness of your loadImage and createSprite. Like Stauricus said, you copy the texture, which is kinda heavy.

Overall, your code could use some const correctness. You should read up on it :) Google has ton of info about it!
Title: Re: Image wont load
Post by: Hapax on July 03, 2018, 11:53:40 pm
if anyone could tell me how to mark some text as code then please do it
Since you asked nicely :)

You can highlight the code and then select the language from the Code drop-down menu.

Or you can just type the tags [code] & [/code] yourself (use code=cpp for c++):
[code=cpp]
#include <iosteam>
[/code]

becomes:
#include <iosteam>
Title: Re: Image wont load
Post by: Dschoonmaker on July 17, 2018, 03:39:40 pm
Is the image in the SFML folder?

I use this code:
sf::RectangleShape shape(sf::Vector2f(10.0f, 10.0f));
sf::Texture texture;
texture.loadFromFile("imageName.png");

 
Title: Re: Image wont load
Post by: Hapax on July 17, 2018, 03:43:46 pm
Their code for loading the texture was correct; you should be checking its result.

The problem was that the texture was passed to the sprite by value rather than reference. Did you read the thread?