Author Topic: stb libraries  (Read 370 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2688
  • Karma: 483
    • View Profile
    • http://www.rbraz.com/
stb libraries
« on: April 15, 2018 »
For those looking for a simple image loader, take a look at "stb image library" by Sean Barrett
https://github.com/nothings/stb

It's simple to use:

Code: [Select]
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG     //You can request *only* certain decoders and suppress all other
#include "stb_image.h"

    int x,y,n;
    unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
    // ... process data if not NULL ...
    // ... x = width, y = height, n = # 8-bit components per pixel ...
    // ... replace '0' with '1'..'4' to force that many components per pixel
    // ... but 'n' will always be the number that it would have been if you said 0

    stbi_image_free(data)

// Standard parameters:
//    int *x                 -- outputs image width in pixels
//    int *y                 -- outputs image height in pixels
//    int *channels_in_file  -- outputs # of image components in image file
//    int desired_channels   -- if non-zero, # of image components requested in result
Challenge Trophies Won:

Offline flightcrank

  • C= 64
  • **
  • Posts: 31
  • Karma: 24
    • View Profile
Re: stb libraries
« Reply #1 on: April 16, 2018 »
nice, i might use this as i use sdl, and that only works with bmp files
Challenge Trophies Won:

Offline flightcrank

  • C= 64
  • **
  • Posts: 31
  • Karma: 24
    • View Profile
Re: stb libraries
« Reply #2 on: April 16, 2018 »
looks like it can do allot more than just images.

how do you actually compile and include it ? there are no .c files ! its all in the header files ? ive never seen something like that before
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2688
  • Karma: 483
    • View Profile
    • http://www.rbraz.com/
Re: stb libraries
« Reply #3 on: April 16, 2018 »
I also scratched my head on the first time I was using it, but in the end it is very simple.

Just all you need to do is to include this lines on your texture ".CPP"  file:
Code: [Select]
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

It includes those functions on your source code and no other file or library is necessary, besides of course it uses the standard C runtime, needed for open file, memory allocation and etc.


Ps. I've only tested the image loader, but the other libs should be also simple to use, just take a look on the header file for example usage.

« Last Edit: April 16, 2018 by Rbz »
Challenge Trophies Won: