SFML community forums

Help => General => Topic started by: Catalyst on April 01, 2016, 04:36:45 pm

Title: Why isn't the image being displayed?
Post by: Catalyst on April 01, 2016, 04:36:45 pm
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
int main(){
sf::VideoMode videomode(800, 800);
sf::RenderWindow window(videomode, "game");
sf::Sprite sprite;
sf::Texture texture;
texture.loadFromFile("Images\catz.png");
sprite.setTexture(texture);
sprite.setPosition(200, 200);
while (window.isOpen()){
    window.clear();
    window.draw(sprite);
    window.display();
    sf::Event event;
    while (window.pollEvent(event)){
        if ((event.type == sf::Event::Closed))
            window.close();
        }

    }
}


Image is in:-
 Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Images

The working directory is the project directory.
Why isn't this working? I've tried Images '//' and '\\' and '/' and '\' but none is working, the program displays a black window instead,could anyone please help?
Title: Re: Why isn't the image being displayed?
Post by: nicox11 on April 01, 2016, 04:40:51 pm
The first thing you should do is to check for the return value when you are loading the texture. Then, check for your directory. I'm pretty sure you developpement tool create separate folder for release and debug, meaning that the link to the image should be "../Images/catz.png".
Title: Re: Why isn't the image being displayed?
Post by: eXpl0it3r on April 01, 2016, 04:41:27 pm
Most likely because you're not adjusting the texture rect of the sprite.
Also you should always check the return value of the loading function.
Title: Re: Why isn't the image being displayed?
Post by: Hapax on April 01, 2016, 04:51:45 pm
Have you tried the fully-qualified path?:
if (!texture.loadFromFile("C:/Users/YOUR_USERNAME/My Documents/Visual Studio 2013/Projects/ConsoleApplication1/ConsoleApplication1/Images/catz.png"))
    return EXIT_FAILURE;
If that works, it's a problem with it looking in the wrong folder.
If it doesn't (and you're certain that the fully-qualified path is correct), then the file may be corrupt (or unsupported).

p.s. I find it odd that you're using the ConsoleApplication project to draw to a graphical window  ;D
p.p.s. Consider creating an empty project to start with.

Most likely because you're not adjusting the texture rect of the sprite.
I was thinking that too but I looked again and it seems that the texture is set (after loading) on a sprite that had no previous texture so should automatically adjust the rectangle.
Title: Re: Why isn't the image being displayed?
Post by: Catalyst on April 01, 2016, 04:54:37 pm
I added setTextureRect and a console Error if it failed to load, now it's displaying a white rectangle inside the black rectangle but still no image, and it's showing the console error. I put the image in the project directory directly so I don't have to worry about the '/'s for now too.I tried the full path and I tried using multiple images but the issue doesn't go away.
Title: Re: Why isn't the image being displayed?
Post by: Mr_Blame on April 02, 2016, 10:35:42 am
What was displayed in console ?
Title: Re: Why isn't the image being displayed?
Post by: Catalyst on April 02, 2016, 12:20:05 pm
Just the error I  put "Error".
Title: AW: Why isn't the image being displayed?
Post by: eXpl0it3r on April 02, 2016, 01:22:54 pm
If the loading really failed, the load function returns false, then there will be an error printed to cerr, which should be displayed on the console.