View previous topic :: View next topic |
Author |
Message |
DarthCthulhu
Joined: 25 Aug 2010 Posts: 1
|
Posted: Wed Aug 25, 2010 10:59 am Post subject: SDL_Rect ushort? |
|
|
Hey there.
Derelict is a truly awesome library and I absolutely love the D language. Recently, I encountered somewhat strange behavior, though; the SDL_Rect structure appears to be made of ushorts rather than ints. Is this by design?
I'm curious because one of the major uses of SDL_Rects is as clipping rectangles for textures, but the SDL_Surface .w and .h members are ints which means you can't set an SDL_Rect's members to that of a SDL_Surface without an explicit cast to ushort. |
|
Back to top |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Wed Aug 25, 2010 12:08 pm Post subject: |
|
|
On the SDL reference you can see this:
Code: |
typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;
|
and as you can see in the sdltypes.d file:
Code: |
alias ubyte Uint8;
alias byte Sint8;
alias ushort Uint16;
alias short Sint16;
|
are x and y short and w and h are ushort.
Also you must cast like this:
Code: | SDL_Rect rect;
rect.x = cast(short) 180;
rect.y = cast(short) 200; |
|
|
Back to top |
|
|
aldacron
Joined: 05 May 2004 Posts: 1322 Location: Seoul, South Korea
|
Posted: Wed Aug 25, 2010 8:05 pm Post subject: Re: SDL_Rect ushort? |
|
|
DarthCthulhu wrote: | Hey there.
Derelict is a truly awesome library and I absolutely love the D language. Recently, I encountered somewhat strange behavior, though; the SDL_Rect structure appears to be made of ushorts rather than ints. Is this by design?
I'm curious because one of the major uses of SDL_Rects is as clipping rectangles for textures, but the SDL_Surface .w and .h members are ints which means you can't set an SDL_Rect's members to that of a SDL_Surface without an explicit cast to ushort. |
As mutable mentioned above, the SDL C headers declare 16-bit integers for the SDL_Rect fields. There's no choice but to use short/ushort on the D side, otherwise it's not binary compatible with the C library.
In C, you can go from bigger integers to smaller integers (like int to short) without casting. However, this can truncate larger values and cause silent bugs that are hard to track down. A part of Walter's philosophy with D is that it should avoid known causes of silent bugs. Therefore, we have to explicitly cast from int to short. _________________ The One With D | The One With Aldacron | D Bits |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|