SFML community forums
Help => General => Topic started by: ineed.help on August 11, 2014, 02:18:07 pm
-
I've been trying to use stb_image.h to load PNG files, and it works fine when including it in one .cpp file, but if I try to include it in a .hpp file to which another .cpp file is linked, I can't call the methods without getting a linker error.
I noticed that SFML is using this file for most of its image loading, which is why I posted this here, since I would assume that some of SFML's many header files use it (?). I apologize if this is the wrong category or I shouldn't have posted this here, but I would like to know how, if possible, I could "fix" this.
-
Read the stb_image.h documentation, everything is explained very well. To keep things simple and self-contained, the author chose to write everything in a single header, which is uncommon and requires some tricks that he explains.
And no, SFML only includes stb_image.h in a single file. It's then encapsulated so that the rest of the code never has to know about it.
-
Read the stb_image.h documentation
Do you mean the one that's in the header file?
And no, SFML only includes stb_image.h in a single file. It's then encapsulated so that the rest of the code never has to know about it.
I don't understand how you could include it in only one file yet use it in several...
-
Do you mean the one that's in the header file?
Yes. The very first lines.
I don't understand how you could include it in only one file yet use it in several...
If I include it only in a single file, obviously it means that I use it only in this file ;)
The file is the definition of the ImageLoader class, which is in turn used only by a single class (Image), and it's Image which is used by all the other classes that need image loading/saving. I never use stb_image directly outside its encapsulation.
-
Oh, do you mean that the methods themselves can't be used outside of one file, but the stb_image.h file can be included by multiple files? That would make sense.
-
Do this:
#define STB_IMAGE_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
Include it in one file and do your implementation there, then use your implementation in all other places, without ever having to touch stb_image.
This (https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/ImageLoader.cpp) is how SFML does it.
So what was so hard to look up these things on your own? ;)
-
I did look it up, but I didn't understand it.
I do understand it now, though, so thank you for the help!