www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - is this C struct possible in D?

reply clayasaurus <clayasaurus gmail.com> writes:
Hello all. I've been under the impression D can do everything C can, and 
more, but what about this structure?

typedef struct{
   Uint32 hw_available:1;
   Uint32 wm_available:1;
   Uint32 blit_hw:1;
   Uint32 blit_hw_CC:1;
   Uint32 blit_hw_A:1;
   Uint32 blit_sw:1;
   Uint32 blit_sw_CC:1;
   Uint32 blit_sw_A:1;
   Uint32 blit_fill;
   Uint32 video_mem;
   SDL_PixelFormat *vfmt;
} SDL_VideoInfo;

This structure is defined in the C library SDL (www.libsdl.org), and 
while this structure is possible in C, how can D define it so it can 
interface to SDL.dll?

so far the current solution is to just use

struct SDL_VideoInfo
{
    Uint32 flags;
    Uint32 blit_fill;
    Uint32 video_mem;
    SDL_PixelFormat *vfmt;
}

Just so it won't break SDL.dll, but the flags are useless.
Oct 21 2004
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
clayasaurus wrote:

 Hello all. I've been under the impression D can do everything C can, and 
 more, but what about this structure?
 
 typedef struct{
   Uint32 hw_available:1;
   Uint32 wm_available:1;
   Uint32 blit_hw:1;
   Uint32 blit_hw_CC:1;
   Uint32 blit_hw_A:1;
   Uint32 blit_sw:1;
   Uint32 blit_sw_CC:1;
   Uint32 blit_sw_A:1;
   Uint32 blit_fill;
   Uint32 video_mem;
   SDL_PixelFormat *vfmt;
 } SDL_VideoInfo;
 
 This structure is defined in the C library SDL (www.libsdl.org), and 
 while this structure is possible in C, how can D define it so it can 
 interface to SDL.dll?
 
 so far the current solution is to just use
 
 struct SDL_VideoInfo
 {
    Uint32 flags;
    Uint32 blit_fill;
    Uint32 video_mem;
    SDL_PixelFormat *vfmt;
 }
 
 Just so it won't break SDL.dll, but the flags are useless.

Can't you set them by OR'ing 0x001, 0x002, 0x004, etc? E.g. flag = 0x001 | 0x004; Lars Ivar Igesund
Oct 21 2004
next sibling parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
Lars Ivar Igesund wrote:
 clayasaurus wrote:
 
 Hello all. I've been under the impression D can do everything C can, 
 and more, but what about this structure?

 typedef struct{
   Uint32 hw_available:1;
   Uint32 wm_available:1;
   Uint32 blit_hw:1;
   Uint32 blit_hw_CC:1;
   Uint32 blit_hw_A:1;
   Uint32 blit_sw:1;
   Uint32 blit_sw_CC:1;
   Uint32 blit_sw_A:1;
   Uint32 blit_fill;
   Uint32 video_mem;
   SDL_PixelFormat *vfmt;
 } SDL_VideoInfo;

 This structure is defined in the C library SDL (www.libsdl.org), and 
 while this structure is possible in C, how can D define it so it can 
 interface to SDL.dll?

 so far the current solution is to just use

 struct SDL_VideoInfo
 {
    Uint32 flags;
    Uint32 blit_fill;
    Uint32 video_mem;
    SDL_PixelFormat *vfmt;
 }

 Just so it won't break SDL.dll, but the flags are useless.

Can't you set them by OR'ing 0x001, 0x002, 0x004, etc? E.g. flag = 0x001 | 0x004; Lars Ivar Igesund

How about a bit array, can't that be the solution? I don't know if it actually works, but it should. struct SDL_VideoInfo { const static uint hw_available = 0; const static uint wm_available = 1; const static uint blit_hw = 2; const static uint blit_hw_CC = 3; const static uint blit_hw_A = 4; const static uint blit_sw = 5; const static uint blit_sw_CC = 6; const static uint blit_sw_A = 7; bit[8] flags; ulong blit_fill; ulong video_mem; SDL_PixelFormat *vfmt; } Regards, Sjoerd
Oct 21 2004
prev sibling next sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Lars Ivar Igesund wrote:
 Can't you set them by OR'ing 0x001, 0x002, 0x004, etc?
 
 E.g.
 
 flag = 0x001 | 0x004;

Better yet, use properties to set & get them, so that the complexity is hidden from the user.
Oct 21 2004
prev sibling parent Mike Parker <aldacron71 yahoo.com> writes:
 
 
 Can't you set them by OR'ing 0x001, 0x002, 0x004, etc?
 
 E.g.
 
 flag = 0x001 | 0x004;

That's the obvious solution. The problem is ordering. There is nothing in the C standard that specifies the ordering of bit field members. Compilers are free to order them anyway they like. For example: struct some_struct { short one_bit:1; short two_bit:1; short three_bit:1; short four_bit:1; }; In this struct, which value is at 0x0001? While it would be nice to say that one_bit is, it could be four_bit instead. This can vary from compiler to compiler even on the same platform. I haven't investigated to see exactly how each compiler handles it (they could all be the same for all I know), but the fact that it /could/ be different makes bitmasks an unsuitable substitute for bitfields when interfacing with a C shared library (such as SDL.dll). You can never gaurantee the shared lib was compiled with a compiler that conforms to a certain bitfield ordering. When linking with static C libs you will often know which compiler was used to compile the lib (and can use flags based on that), and if you aren't interfacing with C code at all you can do what you like.
Oct 21 2004