www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - importc and stb

reply stb <ssstttbbb hotmail.com> writes:
are we going to be able use https://github.com/nothings/stb with 
importc?
these are header only libraries and i use stb (and similar 
libraries) all the time.
there is no reason for me to use c/c++ for tools if importc can 
pull this off.
Dec 16 2021
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
In theory.

Recently Iain let me know that zlib's test suite is now passing with 
ImportC.

So the things that are missing are the macro preprocessor and whatever 
extensions those files use. Once they have been sorted out (the macro 
preprocessor is the big one of the two), then sure, they should work, if 
not either it isn't C or its a bug.
Dec 16 2021
parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/16/2021 2:44 AM, rikki cattermole wrote:
 In theory.
 
 Recently Iain let me know that zlib's test suite is now passing with ImportC.
 
 So the things that are missing are the macro preprocessor and whatever 
 extensions those files use. Once they have been sorted out (the macro 
 preprocessor is the big one of the two), then sure, they should work, if not 
 either it isn't C or its a bug.
We are having a problem with what to do with all the C extensions the system .h files rely on.
Dec 18 2021
prev sibling next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 16 December 2021 at 10:38:38 UTC, stb wrote:
 are we going to be able use https://github.com/nothings/stb 
 with importc?
 these are header only libraries and i use stb (and similar 
 libraries) all the time.
 there is no reason for me to use c/c++ for tools if importc can 
 pull this off.
note that some of these have already been ported to D like https://github.com/adamdruppe/arsd/blob/master/ttf.d though i haven't updated it back from upstream for a while. but i think the import C should work with those if you can set up the macros in a separate file ahead of time.
Dec 16 2021
next sibling parent reply Guillaume Piolat <first.last gmail.com> writes:
On Thursday, 16 December 2021 at 13:38:06 UTC, Adam D Ruppe wrote:
 On Thursday, 16 December 2021 at 10:38:38 UTC, stb wrote:
 are we going to be able use https://github.com/nothings/stb 
 with importc?
 these are header only libraries and i use stb (and similar 
 libraries) all the time.
 there is no reason for me to use c/c++ for tools if importc 
 can pull this off.
note that some of these have already been ported to D like https://github.com/adamdruppe/arsd/blob/master/ttf.d though i haven't updated it back from upstream for a while. but i think the import C should work with those if you can set up the macros in a separate file ahead of time.
More STB translations: - `stb_image.h` v2.27 => [D translation](https://github.com/AuburnSounds/Dplug/blob/master/graphics/dplug/graphics/pngload.d) I believe this is the fastest/resource efficient PNG loading available in D. That one is stripped for only PNG decoding only, it's useful if you want 16-bit PNG and the latest STB optimizations. - `stb_image_resize.h` v0.96: => [D translation](https://github.com/AuburnSounds/Dplug/blob/master/graphics/dplug/graphics/stb_image_resize.d) I believe this is the fastest/memory-aware image resizing available in D. It is an image resizer with an excellent quality. - `stb_truetype.h` v0.7: => [D translation](https://github.com/AuburnSounds/Dplug/blob/master/graphics/dplug/graphics/stb_truetype.d) - `stb_vorbis.h` v1.22: => [D translation](https://github.com/AuburnSounds/audio-formats/blob/master/source/audioformats/stb_vorbis2.d) Most of the translations can't be used as-is in our codebase anyway, so they were all tweaked to fit their D purpose (eg: original might have ARM intrinsics).
Dec 16 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 16 December 2021 at 16:04:34 UTC, Guillaume Piolat 
wrote:
  - `stb_image_resize.h` v0.96:
I might just steal that from you my image resizer has nice quality but is awfully slow...
  - `stb_truetype.h` v0.7:
But you might want to steal my truetype thing cuz i have a much newer version than you do.
  - `stb_vorbis.h` v1.22:
hah my stb_vorbis is 1.10 so maybe i'll take yours. though i've done bug fixes to mine.... come to think of it i think we worked on this together before, the bugfix was upstream, you updated and i just cherry picked it.
Dec 16 2021
parent reply Guillaume Piolat <first.last gmail.com> writes:
On Thursday, 16 December 2021 at 16:47:00 UTC, Adam D Ruppe wrote:
  - `stb_truetype.h` v0.7:
But you might want to steal my truetype thing cuz i have a much newer version than you do.
Yup! STB do receive improvements over time for example more recent stb_image had faster PNG loading. It could be nice if stb_truetype has eg. better antialiasing.
Dec 16 2021
parent Guillaume Piolat <first.last gmail.com> writes:
On Thursday, 16 December 2021 at 17:03:30 UTC, Guillaume Piolat 
wrote:
 On Thursday, 16 December 2021 at 16:47:00 UTC, Adam D Ruppe 
 wrote:
  - `stb_truetype.h` v0.7:
But you might want to steal my truetype thing cuz i have a much newer version than you do.
Yup! STB do receive improvements over time for example more recent stb_image had faster PNG loading. It could be nice if stb_truetype has eg. better antialiasing.
So: another future win for ImportC! It is already receiving lots of attention for a new feature.
Dec 16 2021
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/16/2021 5:38 AM, Adam D Ruppe wrote:
 note that some of these have already been ported to D like
 https://github.com/adamdruppe/arsd/blob/master/ttf.d
 
 though i haven't updated it back from upstream for a while.
The updating problem is one of the major motivations for ImportC.
Dec 18 2021
prev sibling next sibling parent reply bachmeier <no spam.net> writes:
On Thursday, 16 December 2021 at 10:38:38 UTC, stb wrote:
 are we going to be able use https://github.com/nothings/stb 
 with importc?
 these are header only libraries and i use stb (and similar 
 libraries) all the time.
 there is no reason for me to use c/c++ for tools if importc can 
 pull this off.
I just tried the first three files: stb_c_lexer.h, stb_divide.h and stb_ds.h. The first two compiled without issues: ``` gcc -E -P stb_c_lexer.h > stb_c_lexer_d.c ldmd2 -c stb_c_lexer_d.c gcc -E -P stb_divide.h > stb_divide_d.c ldmd2 -c stb_divide_d.c ``` The last gave errors: ``` gcc -E -P stb_ds.h > stb_ds_d.c ldmd2 -c stb_ds_d.c ``` The errors are due to the insertion of stuff in the preprocessing step that's not C11. There are solutions for some of these (such as https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in) but I didn't dig into it any further, since I have never used those libraries. The first error that comes up is due to `__restrict` rather than plain `restrict`.
Dec 16 2021
next sibling parent reply Elronnd <elronnd elronnd.net> writes:
On Thursday, 16 December 2021 at 14:26:42 UTC, bachmeier wrote:
 https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in
 #define __builtin_bswap16(x) ((u_int16_t) ((((x) >> 8) & 0xff) 
 | (((x) & 0xff) << 8)))
Oh, my, that's bad. GCC __builtins don't generally evaluate more than once. And this could just be an inline function anyway.
Dec 16 2021
parent reply bachmeier <no spam.net> writes:
On Thursday, 16 December 2021 at 18:38:10 UTC, Elronnd wrote:
 On Thursday, 16 December 2021 at 14:26:42 UTC, bachmeier wrote:
 https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in
 #define __builtin_bswap16(x) ((u_int16_t) ((((x) >> 8) & 0xff) 
 | (((x) & 0xff) << 8)))
Oh, my, that's bad. GCC __builtins don't generally evaluate more than once. And this could just be an inline function anyway.
This is why most of us should stay away from C. I honestly don't know what that incantation is doing, even though I've written a fair amount of C over the years.
Dec 16 2021
next sibling parent Abdulhaq <alynch4047 gmail.com> writes:
On Thursday, 16 December 2021 at 19:04:21 UTC, bachmeier wrote:
 On Thursday, 16 December 2021 at 18:38:10 UTC, Elronnd wrote:
 On Thursday, 16 December 2021 at 14:26:42 UTC, bachmeier wrote:
 https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in
 #define __builtin_bswap16(x) ((u_int16_t) ((((x) >> 8) & 
 0xff) | (((x) & 0xff) << 8)))
Oh, my, that's bad. GCC __builtins don't generally evaluate more than once. And this could just be an inline function anyway.
This is why most of us should stay away from C. I honestly don't know what that incantation is doing, even though I've written a fair amount of C over the years.
:-) it rotates the MSB right 8 places into the LSB, rotates the LSB left 8 places, and ORs/overlays them both together, to effectively swap the MSB and LSB bytes
Dec 16 2021
prev sibling parent reply Patrick Schluter <Patrick.Schluter bbox.fr> writes:
On Thursday, 16 December 2021 at 19:04:21 UTC, bachmeier wrote:
 On Thursday, 16 December 2021 at 18:38:10 UTC, Elronnd wrote:
 On Thursday, 16 December 2021 at 14:26:42 UTC, bachmeier wrote:
 https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in
 #define __builtin_bswap16(x) ((u_int16_t) ((((x) >> 8) & 
 0xff) | (((x) & 0xff) << 8)))
Oh, my, that's bad. GCC __builtins don't generally evaluate more than once. And this could just be an inline function anyway.
This is why most of us should stay away from C. I honestly don't know what that incantation is doing, even though I've written a fair amount of C over the years.
You're not serious, aren't you? Except for the typecast syntax the expression is also valid D.
Dec 17 2021
parent Temtaime <temtaime gmail.com> writes:
There is https://github.com/Temtaime/stb also
Outdated a little and missing some define macros, but i'll update 
it if someone needs it
Dec 18 2021
prev sibling parent reply bachmeier <no spam.net> writes:
On Thursday, 16 December 2021 at 14:26:42 UTC, bachmeier wrote:
 On Thursday, 16 December 2021 at 10:38:38 UTC, stb wrote:
 are we going to be able use https://github.com/nothings/stb 
 with importc?
 these are header only libraries and i use stb (and similar 
 libraries) all the time.
 there is no reason for me to use c/c++ for tools if importc 
 can pull this off.
I just tried the first three files: stb_c_lexer.h, stb_divide.h and stb_ds.h. The first two compiled without issues: ``` gcc -E -P stb_c_lexer.h > stb_c_lexer_d.c ldmd2 -c stb_c_lexer_d.c gcc -E -P stb_divide.h > stb_divide_d.c ldmd2 -c stb_divide_d.c ``` The last gave errors: ``` gcc -E -P stb_ds.h > stb_ds_d.c ldmd2 -c stb_ds_d.c ``` The errors are due to the insertion of stuff in the preprocessing step that's not C11. There are solutions for some of these (such as https://github.com/ibuclaw/importC/blob/main/src/keywords.c.in) but I didn't dig into it any further, since I have never used those libraries. The first error that comes up is due to `__restrict` rather than plain `restrict`.
So over lunch I decided to take another look. I downloaded Ian's fixes as fixes.h and then did ``` gcc -E -P stb_ds.h > stb_ds_d.c gcc -E -P -include fixes.h stb_ds_d.c > stb_ds_d2.c ldmd2 -c stb_ds_d2.c ``` Compiled without any error messages or warnings.
Dec 16 2021
parent reply bachmeier <no spam.net> writes:
On Thursday, 16 December 2021 at 19:00:20 UTC, bachmeier wrote:

 So over lunch I decided to take another look. I downloaded 
 Ian's fixes as fixes.h and then did

 ```
 gcc -E -P stb_ds.h > stb_ds_d.c
 gcc -E -P -include fixes.h stb_ds_d.c > stb_ds_d2.c
 ldmd2 -c stb_ds_d2.c
 ```

 Compiled without any error messages or warnings.
Since I'm looking for an opportunity to procrastinate, I created a repo. One file still results in errors from LDC. The others all compile. https://github.com/bachmeil/stb
Dec 16 2021
parent bachmeier <no spam.net> writes:
On Thursday, 16 December 2021 at 20:56:26 UTC, bachmeier wrote:
 On Thursday, 16 December 2021 at 19:00:20 UTC, bachmeier wrote:

 So over lunch I decided to take another look. I downloaded 
 Ian's fixes as fixes.h and then did

 ```
 gcc -E -P stb_ds.h > stb_ds_d.c
 gcc -E -P -include fixes.h stb_ds_d.c > stb_ds_d2.c
 ldmd2 -c stb_ds_d2.c
 ```

 Compiled without any error messages or warnings.
Since I'm looking for an opportunity to procrastinate, I created a repo. One file still results in errors from LDC. The others all compile. https://github.com/bachmeil/stb
All but two functions in stb compile. Repo has been updated with the preprocessed C files in the unmodified/ directory.
Dec 19 2021
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 12/16/2021 2:38 AM, stb wrote:
 are we going to be able use https://github.com/nothings/stb with importc?
 these are header only libraries and i use stb (and similar libraries) all the
time.
 there is no reason for me to use c/c++ for tools if importc can pull this off.
That is indeed the point of ImportC.
Dec 18 2021
parent reply stb <stbeeeee hotmail.com> writes:
On Saturday, 18 December 2021 at 21:49:31 UTC, Walter Bright 
wrote:
 On 12/16/2021 2:38 AM, stb wrote:
 are we going to be able use https://github.com/nothings/stb 
 with importc?
 these are header only libraries and i use stb (and similar 
 libraries) all the time.
 there is no reason for me to use c/c++ for tools if importc 
 can pull this off.
That is indeed the point of ImportC.
i was waiting for this for about 10 years, better late than never. some still won't understand how huge this feature is, talking about complexity added to the language. compared to its benefits the complexity (which could probably be deferred to llvm etc..) added is nothing. when matured, wrappers/wrapper-versions will be obsolete for 100% of the battle tested libraries, and going to reduce the work needed for the obscure/extension-heavy libraries. one of the bigger if not the biggest starting point of c++ was its c backwards compatibility. with importc you have best of both worlds. you have backwards/foreign compatibility with c libraries and since this is just an `include "path-to-c"` you do not contaminate d with c syntax. thank you walter!
Dec 20 2021
parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/20/2021 3:01 AM, stb wrote:
 one of the bigger if not the biggest starting point of c++ was its c backwards 
 compatibility. with importc you have best of both worlds. you have 
 backwards/foreign compatibility with c libraries and since this is just an 
 `include "path-to-c"` you do not contaminate d with c syntax.
 
 thank you walter!
You're welcome! I'm mad at myself for not doing this long ago.
Dec 20 2021