www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 1 more header file conversion question

reply Oliver <Oliver_member pathlink.com> writes:
Hello and thanks for the help I already received !

I have another problem concerning converting a C header file to D.

I have :

#define BLACK           rgb(0x00,0x00,0x00);
#define WHITE           rgb(0xFF,0xFF,0xFF);
#define BLUE            rgb(0x00,0x00,0xFF);
...

where Color rgb(int,int,int) is a function defined in the same file.
Any ideas ? I tried with aliases and typedefs but no success...

Oliver
May 17 2005
next sibling parent "Walter" <newshound digitalmars.com> writes:
"Oliver" <Oliver_member pathlink.com> wrote in message
news:d6d7sb$9d4$1 digitaldaemon.com...
 Hello and thanks for the help I already received !

 I have another problem concerning converting a C header file to D.

 I have :

 #define BLACK           rgb(0x00,0x00,0x00);
 #define WHITE           rgb(0xFF,0xFF,0xFF);
 #define BLUE            rgb(0x00,0x00,0xFF);
 ...

 where Color rgb(int,int,int) is a function defined in the same file.
 Any ideas ? I tried with aliases and typedefs but no success...
int BLACK() { return rgb(0,0,0); }
May 17 2005
prev sibling next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Oliver" <Oliver_member pathlink.com> wrote in message 
news:d6d7sb$9d4$1 digitaldaemon.com...
 Hello and thanks for the help I already received !

 I have another problem concerning converting a C header file to D.

 I have :

 #define BLACK           rgb(0x00,0x00,0x00);
 #define WHITE           rgb(0xFF,0xFF,0xFF);
 #define BLUE            rgb(0x00,0x00,0xFF);
 ...

 where Color rgb(int,int,int) is a function defined in the same file.
 Any ideas ? I tried with aliases and typedefs but no success...

 Oliver
Let me mention just in case: if you also have extern(C) global variables in the header be sure to look at http://www.digitalmars.com/d/htomodule.html the section about extern variables. You won't be able to have one module contain the "macros" (that Walter's post contains) and the extern variables since the macros need to be linked in and the extern vars can't be linked in.
May 17 2005
prev sibling parent reply pragma <pragma_member pathlink.com> writes:
In article <d6d7sb$9d4$1 digitaldaemon.com>, Oliver says...
Hello and thanks for the help I already received !

I have another problem concerning converting a C header file to D.

I have :

#define BLACK           rgb(0x00,0x00,0x00);
#define WHITE           rgb(0xFF,0xFF,0xFF);
#define BLUE            rgb(0x00,0x00,0xFF);
...

where Color rgb(int,int,int) is a function defined in the same file.
Any ideas ? I tried with aliases and typedefs but no success...
Here, I think you're at the limits of what D's templates and aliases can provide you. You're stuck either using functions (and relying on the compiler to inline for efficency) or go up the call chain to what 'rgb' is returing. Color WHITE(){ return rgb(0xFF,0xFF,0xFF); } In case of the latter, most graphics libs recognize certain bit-packed formats for color that are pretty easy to declare w/o need of a funciton. // assuming AARRGGBB (where AA = alpha channel) static const uint BLACK = 0xFF000000; static const uint WHITE = 0xFFFFFFFF; static const uint BLUE = 0xFF0000FF; Even if this isn't the color format you had in mind, I'd highly reccomend it as it makes working with existing graphics libs (like openGL for instance) much easier. If you must have the ability to access the components by name, then I'd suggest something like the following:
 struct ARGB{
     byte alpha,red,green,blue;
 }
 
 struct Color{
     union{
         uint bits;
         ARGB argb;
     }
     byte alpha(){ return argb.alpha; }
     byte red(){ return argb.red; }
     byte green(){ return argb.green; }
     byte blue(){ return argb.blue; }
 
     Color create(uint bits){ Color value; value.bits=bits; return value; }
 }
 
 static Color BLACK = Color.create(0x00000000);
- EricAnderton at yahoo
May 17 2005
parent James Dunne <james.jdunne gmail.com> writes:
In article <d6ddk5$eo1$1 digitaldaemon.com>, pragma says...
In article <d6d7sb$9d4$1 digitaldaemon.com>, Oliver says...
Hello and thanks for the help I already received !

I have another problem concerning converting a C header file to D.

I have :

#define BLACK           rgb(0x00,0x00,0x00);
#define WHITE           rgb(0xFF,0xFF,0xFF);
#define BLUE            rgb(0x00,0x00,0xFF);
...

where Color rgb(int,int,int) is a function defined in the same file.
Any ideas ? I tried with aliases and typedefs but no success...
Here, I think you're at the limits of what D's templates and aliases can provide you. You're stuck either using functions (and relying on the compiler to inline for efficency) or go up the call chain to what 'rgb' is returing. Color WHITE(){ return rgb(0xFF,0xFF,0xFF); } In case of the latter, most graphics libs recognize certain bit-packed formats for color that are pretty easy to declare w/o need of a funciton. // assuming AARRGGBB (where AA = alpha channel) static const uint BLACK = 0xFF000000; static const uint WHITE = 0xFFFFFFFF; static const uint BLUE = 0xFF0000FF; Even if this isn't the color format you had in mind, I'd highly reccomend it as it makes working with existing graphics libs (like openGL for instance) much easier. If you must have the ability to access the components by name, then I'd suggest something like the following:
 struct ARGB{
     byte alpha,red,green,blue;
 }
I think you want that a ubyte (unsigned) so as to get the full 0-255 range. Simple typo, I know. But for those learning the language, best to not confuse :)
 
 struct Color{
     union{
         uint bits;
         ARGB argb;
     }
     byte alpha(){ return argb.alpha; }
     byte red(){ return argb.red; }
     byte green(){ return argb.green; }
     byte blue(){ return argb.blue; }
 
     Color create(uint bits){ Color value; value.bits=bits; return value; }
 }
 
 static Color BLACK = Color.create(0x00000000);
- EricAnderton at yahoo
Regards, James Dunne
May 22 2005