SFML community forums

Help => General => Topic started by: ineed.help on August 11, 2014, 02:18:07 pm

Title: stb_image
Post 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.
Title: Re: stb_image
Post by: Laurent on August 11, 2014, 02:26:00 pm
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.
Title: Re: stb_image
Post by: ineed.help on August 11, 2014, 02:31:52 pm
Quote
Read the stb_image.h documentation
Do you mean the one that's in the header file?

Quote
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...
Title: Re: stb_image
Post by: Laurent on August 11, 2014, 02:36:44 pm
Quote
Do you mean the one that's in the header file?
Yes. The very first lines.

Quote
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.
Title: Re: stb_image
Post by: ineed.help on August 11, 2014, 02:40:18 pm
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.
Title: Re: stb_image
Post by: eXpl0it3r on August 11, 2014, 02:50:57 pm
Quote
   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? ;)
Title: Re: stb_image
Post by: ineed.help on August 11, 2014, 02:53:38 pm
I did look it up, but I didn't understand it.

I do understand it now, though, so thank you for the help!