SDL_Rect help

Started by
5 comments, last by kuramayoko10 12 years, 7 months ago
I am learning SDL right now and i was just wondering what the SDL_Rect Function does?
I was reading the lazy foo tutorials (http://lazyfoo.net/SDL_tutorials/lesson02/index.php)
In the tut its in an apply surface function and i dont understand what is does or why its needed...
Can someone plz explain it to me as it doesnt really tell alot about the function.
Advertisement
SDL_Rect is a structure like below:

typedef struct
{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;


It defines a rectangle where:
a) x and y are the coordinates of the top-left vertex of the rectangle
b) w and h are the width and height of the rectangle

On the case of the tutorial, he utilizes the rectangle to tell SDL in which part of the screen he wants the image to be drawn, this is offset.x and offset.y
Later on the tutorials you will see that the SDL_Rect will also serve as a cut perimeter for your image. It will tell what part of the image you want to draw (good for clipping tiles or spritesheets).
Programming is an art. Game programming is a masterpiece!

SDL_Rect is a structure like below:

typedef struct
{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;


It defines a rectangle where:
a) x and y are the coordinates of the top-left vertex of the rectangle
b) w and h are the width and height of the rectangle

On the case of the tutorial, he utilizes the rectangle to tell SDL in which part of the screen he wants the image to be drawn, this is offset.x and offset.y
Later on the tutorials you will see that the SDL_Rect will also serve as a cut perimeter for your image. It will tell what part of the image you want to draw (good for clipping tiles or spritesheets).


So he puts the image in the rect?
So he puts the image in the rect?
This don't make any sense without a context. With which function you using the SDL_Rect structure?

For example, in the SDL_BlitSurface function:
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

There is a 'source rect' and a 'destiny rect'.
The source one indicates what portion (rectangle) of the source surface will be used, passing null sdl uses the entire source surface on the blit. This rectangle is defined by the x, y, w, h members of the SDL_Rect(x/y as top/left, w/h as bottom/right).
The destiny one indicates only where the source surface will be blittled on the destiny one. The x and y members of the SDL_Rect indicates the location. This location is where the source surface starts, that is, indicates where the coordinate 0, 0 of the source portion will be on the destiny surface.

[quote name='kuramayoko10' timestamp='1315619352' post='4859851']
SDL_Rect is a structure like below:

typedef struct
{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;


It defines a rectangle where:
a) x and y are the coordinates of the top-left vertex of the rectangle
b) w and h are the width and height of the rectangle

On the case of the tutorial, he utilizes the rectangle to tell SDL in which part of the screen he wants the image to be drawn, this is offset.x and offset.y
Later on the tutorials you will see that the SDL_Rect will also serve as a cut perimeter for your image. It will tell what part of the image you want to draw (good for clipping tiles or spritesheets).


So he puts the image in the rect?
[/quote]

It puts the lotion on the skin,
or else it gets the hose again.




(Sorry, couldn't resist. I just read your comment in Buffalo Bill's voice. I'm done now)

Hmm...Still not getting this :/
I've read the same tutorial at least 5 times and i still dont understand the SDL_rect stuff
Please halp me out as im hesitant to move onto the next tut, cuz i want to know this first
A picture explaining it would help

Hmm...Still not getting this :/
I've read the same tutorial at least 5 times and i still dont understand the SDL_rect stuff
Please halp me out as im hesitant to move onto the next tut, cuz i want to know this first
A picture explaining it would help

I don't understand what you don't understand xD. Can you be more specific?

The lazyfoo definition is pretty straight forward:
[quote='lazyfoo']
First we take the offsets and put them inside an SDL_Rect. We do this because SDL_BlitSurface() only accepts the offsets inside of an SDL_Rect.
[/quote]
This says that you need to tell SDL_BlitSurface (the function that draws on a surface) in which position (x,y) you want to draw the image into.
In this case, SDL programmer decided to make the function receive a SDL_Rect structure that already has an (x,y) value, instead of int posX, int posY.
Programming is an art. Game programming is a masterpiece!

This topic is closed to new replies.

Advertisement