Public Group

# SDL Drawing Functions

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

## Recommended Posts

Hello! I am not very clear about the drawing functions in SDL. From the tutorial on Cone3D I was given 2:
[SOURCE]

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest)
}

[/SOURCE]
[/source][/source] And...
[SOURCE]

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}

[/SOURCE]
[/source][/source] I dont understand the difference between them, but I think that the 1st one only takes x and y and then the rest of the source surface, but the 2nd one can take x, y, w, h to specify what to blit... Secondly, I would like to know the purpose of SDL_Flip(screen). I see it like this: 2nd screen (not visible) 1st screeen (visible) So if I draw an O for example it appears (though you cant see it) on screen 2. 2nd screen (not visible) (with an O now) 1st screen (visible) So then I have to call SDL_Flip(screen) to swap them to make what I drew visible. 1st screen (not visible) 2nd screen (visible) (with the O) Is this correct or is it not? Thanks in advance!

##### Share on other sites
The reason you have two version of your drawing routines is that the first one (with just the x,y coordinates) draws the whole of the surface to the screen. The second version lets you draw part of the surface to the screen. Imagine if you had a bitmap with four different frames of animation one after the other left to right. You could use the second version to draw just one of those frames on screen.

Regarding, SDL_Flip, yes your explanation is basically correct. If you want to read more about this lookup the concept of backbuffering on google. Simple, you have two screen. One is currently being drawn to the monitor, the other is for you to draw on. When you have finished drawing on the second, hidden screen you call SDL_Flip and it swaps the two surfaces so now the second screen is being shown on the monitor and the first is ready to be drawn on. This is done to prevent an effect called tearing; where as you are drawing to the screen the monitor is physically drawing it and it draws part of the previous frame (because it had already drawn that before it got to the part you were updating) and part of the new frame.

Alan

##### Share on other sites
According to the SDL documentation, SDL_BlitSurface blits from one surface to another. With DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2), it is taking the information from a certain spot on a surface and putting it onto a certain spot on another surface. This is good for blitting one tile from a bunch of tiles on one surface.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000091
×