SDL_Rect Rounding

I’m trying to subtract to values inside a SDL_Rect but the results a rounding as if the ints in the Rect are unsigned.
Here is a simple example.

#include <SDL/SDL.h>

int main()
{
SDL_Rect x;
x.x=20;
x.y=40;
x.w=0;

x.w=x.x-x.y;
printf("%d\n",x.w);

return(0);

}

the result is 65516 when it should be -20.

I tried making my own structure with int x,y,w,h; with the same results. But when I use ints that are not in a structure it works fine.

Any ideas,
Thanks,
Image

Hi,

width and height are unsigned short ints. They cannot become negative
and a negative values wrap over the maximum value.


If you have no clue what this means, it is a good time to read a good
C/C++/Programming book.
By the way: Your calculation is totally wrong.Am 17.01.2011 23:22, schrieb image28:

I’m trying to subtract to values inside a SDL_Rect but the results a
rounding as if the ints in the Rect are unsigned.
Here is a simple example.

#include

int main()
{
SDL_Rect x;
x.x=20;
x.y=40;
x.w=0;

x.w=x.x-x.y;
printf("%d\n",x.w);

return(0);
}

the result is 65516 when it should be -20.

I tried making my own structure with int x,y,w,h; with the same
results. But when I use ints that are not in a structure it works fine.


Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt

Read in the wiki that SDL_Rect was made up with signed ints.
That still doesnt explain why it doesnt work when I make my own structure of signed ints.
Found a quick fix anyway.

Thanks,
Image

I have no clue where you are reading this…

The most authorative source, the source code:
http://hg.libsdl.org/SDL/file/930614179450/include/SDL_video.h#l50
50 typedef struct SDL_Rect {
51 Sint16 x, y;
52 Uint16 w, h;
53 } SDL_Rect;

The wiki:
http://sdl.beuc.net/sdl.wiki/SDL_Rect
1 typedef struct{
2 Sint16 x, y;
3 Uint16 w, h;
4 } SDL_Rect;

I am curious what your fix is.Am 18.01.2011 00:56, schrieb image28:

Read in the wiki that SDL_Rect was made up with signed ints.

Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt

In SDL 1.2, “w” and “h” are unsigned:
http://hg.libsdl.org/SDL/file/930614179450/include/SDL_video.h#l50
In SDL 1.3 (and hence the wiki), they are signed:
http://hg.libsdl.org/SDL/file/06be4b33029d/include/SDL_video.h#l54

This explains the confusion I thinkOn 18 January 2011 07:15, Christoph Nelles wrote:

Am 18.01.2011 00:56, schrieb image28:

Read in the wiki that SDL_Rect was made up with signed ints.

I have no clue where you are reading this…