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

Author Topic: Why isn't the image being displayed?  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

Catalyst

  • Newbie
  • *
  • Posts: 3
    • View Profile
Why isn't the image being displayed?
« 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?

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Why isn't the image being displayed?
« Reply #1 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".

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10875
    • View Profile
    • development blog
    • Email
Re: Why isn't the image being displayed?
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why isn't the image being displayed?
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Catalyst

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Why isn't the image being displayed?
« Reply #4 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.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Why isn't the image being displayed?
« Reply #5 on: April 02, 2016, 10:35:42 am »
What was displayed in console ?

Catalyst

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Why isn't the image being displayed?
« Reply #6 on: April 02, 2016, 12:20:05 pm »
Just the error I  put "Error".

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10875
    • View Profile
    • development blog
    • Email
AW: Why isn't the image being displayed?
« Reply #7 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/