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

Author Topic: Cant load texture from file.  (Read 12344 times)

0 Members and 1 Guest are viewing this topic.

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Cant load texture from file.
« on: February 04, 2018, 11:02:34 pm »
Hello, I have just started using SFML, and also Visual studio...

So I was following a guide to load a single texture and create an sprite from a file... Probably the most simple tutorial ever... but I am having problems nonetheless.

My code based in the tutorial just bellow, I am currently using the C++14 and x64 libraries:

Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{

sf::Event event;
//window creation
sf::RenderWindow MainWindow(sf::VideoMode(400, 300), "Window");


//texture creation
sf::Texture textureTest1;
system("dir");
//if (!textureTest1.loadFromFile("img\test.png")) {
if (!textureTest1.loadFromFile("test.png")) {
std::cout << "the texture does not load" << std::endl;
}
else {
std::cout << "the texture does load" << std::endl;
}

//Sprite creation
sf::Sprite sprtTest1(textureTest1);

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

MainWindow.clear();
MainWindow.draw(sprtTest1);
MainWindow.display();
}

return 0;
}

For some reason loadFromFile() is unable to load the texture from the file, even when I am in the correct working directory. From what I have been able to read online the most likely issue seems to be that the picture cant be open for some reason, but if that was the case I should be getting an error message in the console, which I am not getting. I saw in another topic that u have to set the target subsystem to console in order to be able to see the messages, and I have done this (Image bellow) but it is still likely that I am missing something because I dont see any error message... in any case, "test.png" is a simple image I made in paint, which is a red square inside a green square.




For most of my life I have been using Linux, but now for work reasons I must use windows, so that also could be an issue.

Please let me know if u have any insights to my predicament, I would really appreciate the help.
Thank you.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Cant load texture from file.
« Reply #1 on: February 05, 2018, 12:46:17 am »
When Visual Studio uses local paths for programs run from within the IDE, it uses its Current Working Directory; this can be set separately to the actual location of the executable or the source code. However, its default location is usually the project's source code so placing the image in the same folder/directory as the cpp file should help.
If you run the executable directly - outside of the IDE - then it should use the executable's location as its CWD. This means that you may need to duplicate resources for different targets as well as IDE execution (if referenced by local paths).


One other thing to note is that it is often simpler to start with an empty project instead of a Console Application project. This removes the added stuff that you probably don't need (most notably the stdafx bit). Being used to Linux, I expect you'd probably appreciate this approach anyway :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #2 on: February 05, 2018, 08:33:41 am »
When Visual Studio uses local paths for programs run from within the IDE, it uses its Current Working Directory; this can be set separately to the actual location of the executable or the source code. However, its default location is usually the project's source code so placing the image in the same folder/directory as the cpp file should help.
If you run the executable directly - outside of the IDE - then it should use the executable's location as its CWD. This means that you may need to duplicate resources for different targets as well as IDE execution (if referenced by local paths).


One other thing to note is that it is often simpler to start with an empty project instead of a Console Application project. This removes the added stuff that you probably don't need (most notably the stdafx bit). Being used to Linux, I expect you'd probably appreciate this approach anyway :)

To the first point I assume that something like this should be the issue,  if u see in the image, the solution and the project files are in the same folder, and I have set the working directory to $(ProjectDir), is there anything else I should be changing for setting this correctly?.

I will try with an empty project later, thanks A lot.



Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Cant load texture from file.
« Reply #3 on: February 05, 2018, 05:37:00 pm »
I see now that your project is actually inside that very directory so a working directory of $(ProjectDir) should indeed point to that directory.

I think the first thing to try should be using an absolute path to see if the image is really the problem.
Also you might try a different image and/or allow others to test your image.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #4 on: February 05, 2018, 06:49:04 pm »
I see now that your project is actually inside that very directory so a working directory of $(ProjectDir) should indeed point to that directory.

I think the first thing to try should be using an absolute path to see if the image is really the problem.
Also you might try a different image and/or allow others to test your image.


I have tried using an absolute path and still nothing happens. Additionally, I have changed $(ProjectDir) accordingly.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Cant load texture from file.
« Reply #5 on: February 06, 2018, 01:48:09 am »
If it doesn't successfully load when using an absolute path that is sure to be correct, the problem then must be with the image.
Have you tried a different image? If you need one to test, you could always use the SFML logo from here: https://www.sfml-dev.org/download/goodies/ :)
(not the SVG, of course)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #6 on: February 06, 2018, 08:20:54 am »
If it doesn't successfully load when using an absolute path that is sure to be correct, the problem then must be with the image.
Have you tried a different image? If you need one to test, you could always use the SFML logo from here: https://www.sfml-dev.org/download/goodies/ :)
(not the SVG, of course)

Well that worked. I actually tried before with 3 other png I found online and had the same problem. Like I said before I made the png I was testing in the first post in paint which as 2 square one inside the other, so... what was I missing? is there some particular limitations to the type of image we can use? i mean something in terms of compression, or other saving parameters?


FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #7 on: February 06, 2018, 02:21:29 pm »
It'd help if you provided such an image that fails. You can PM it to me to let me check if you don't want it out in the public.

And yes, failure to load an image should result in a message to terminal, it's interesting it doesn't.

SFML interally uses STB image, you can see what it supports in the comments in the header itself: https://github.com/SFML/SFML/blob/master/extlibs/headers/stb_image/stb_image.h

Additionally in your commented out code you use "img\test.png" as a path but that should be "img\\test.png" or "img/test.png" since \ is an escape character so \t is a tab and \\ is a \.
« Last Edit: February 06, 2018, 02:28:17 pm by FRex »
Back to C++ gamedev with SFML in May 2023

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #8 on: February 06, 2018, 07:17:22 pm »
It'd help if you provided such an image that fails. You can PM it to me to let me check if you don't want it out in the public.

And yes, failure to load an image should result in a message to terminal, it's interesting it doesn't.

SFML interally uses STB image, you can see what it supports in the comments in the header itself: https://github.com/SFML/SFML/blob/master/extlibs/headers/stb_image/stb_image.h

Additionally in your commented out code you use "img\test.png" as a path but that should be "img\\test.png" or "img/test.png" since \ is an escape character so \t is a tab and \\ is a \.

Sent...

Yes I also was expecting  a error message, in fact if I had an error code that would had helped me searching for a solution to my problem online.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #9 on: February 06, 2018, 07:49:10 pm »
That file is a BMP file renamed to have a png extension. It also loads for me without a problem, which is interesting.
Back to C++ gamedev with SFML in May 2023

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #10 on: February 07, 2018, 08:04:43 am »
That file is a BMP file renamed to have a png extension. It also loads for me without a problem, which is interesting.
What I did to create it was, created a new bitmap image, then rename it to PNG, edit it in paint and save is as  PNG... so when I saved it to PNG I should have had no problems with the image, unless I missed something weird.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Cant load texture from file.
« Reply #11 on: February 07, 2018, 08:19:14 am »
You can't just change file extensions and expect things to miraculously work. Changing a file extension doesn't change the format. I wouldn't know what paint does, but maybe it realized it was a BMP before and saved it in the same format.

Not sure why or how that would happen, but if you get no error on the cli, then the either sf::err() or std::cerr has been redirected.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #12 on: February 07, 2018, 11:58:13 am »
Windows warns you when you change an extension by hand. Paint sees it's a BMP and doesn't change it while editing it. The extension doesn't seem to matter. It also doesn't matter while loading images for SFML so it's still unusual it didn't work.
« Last Edit: February 07, 2018, 12:03:27 pm by FRex »
Back to C++ gamedev with SFML in May 2023

XCOMMAN

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Cant load texture from file.
« Reply #13 on: February 08, 2018, 07:59:10 pm »
You can't just change file extensions and expect things to miraculously work. Changing a file extension doesn't change the format. I wouldn't know what paint does, but maybe it realized it was a BMP before and saved it in the same format.

Not sure why or how that would happen, but if you get no error on the cli, then the either sf::err() or std::cerr has been redirected.

I did it so because I was too lazy to search pain in the start menu (I formatted this PC like 1 month ago so I had not shortcut)... as I said, I "Save as" method which should reformat the image...

now this becomes even more weird, because after I tried the SFML logo, which worked, now the original image giving me problems works fine... so I have no idea what happened, I changed absolutely nothing in the configuration, and now it works... 

so... magic.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Cant load texture from file.
« Reply #14 on: February 08, 2018, 09:31:13 pm »
Sounds more like a PEBCAK. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/