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

Author Topic: stb_image  (Read 5573 times)

0 Members and 1 Guest are viewing this topic.

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
stb_image
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: stb_image
« Reply #1 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.
Laurent Gomila - SFML developer

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: stb_image
« Reply #2 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...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: stb_image
« Reply #3 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.
Laurent Gomila - SFML developer

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: stb_image
« Reply #4 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: stb_image
« Reply #5 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 is how SFML does it.

So what was so hard to look up these things on your own? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: stb_image
« Reply #6 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!
« Last Edit: August 11, 2014, 04:45:16 pm by ineed.help »