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

Author Topic: Image wont load  (Read 2700 times)

0 Members and 1 Guest are viewing this topic.

coolian

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Image wont load
« 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

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Image wont load
« Reply #1 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");

?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Image wont load
« Reply #2 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!
« Last Edit: July 03, 2018, 04:06:29 pm by Rosme »
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image wont load
« Reply #3 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>
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Dschoonmaker

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Image wont load
« Reply #4 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");

 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image wont load
« Reply #5 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything