Jump to content
• Advertisement

Public Group

# SDL_Rect help

This topic is 2643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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.

#### Share this post

##### Share on other sites
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).

#### Share this post

##### Share on other sites

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?

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

[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)

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites

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.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14
• Advertisement

• 9
• 11
• 11
• 23
• 10
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013149
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!