Advertisement

[SDL] Blitting with negative coordinates

Started by June 08, 2008 02:37 PM
3 comments, last by MuriloCruz 13 years, 4 months ago
As stated by the SDL documentation, if a SDL_Rect with negative coordinates is passed onto the SDL_BlitSurface function the image will be clipped accordingly. However, I tried setting an image with a negative position of (0,-300) and the image would be fixed at (0,0), not going out of the bounds of the window. Here is a small snippet of the code:

image.SetPosY(-300);
image.Display();

void D_Image::SetPosY(int newPosY)
{
	position.y = newPosY;
}

void D_Image::Display()
{
	SDL_BlitSurface(image, NULL, window, &position);
}
image and position are an SDL_Surface and an SDL_Rect respectively. They are members of the class D_Image. What would be causing this problem? Do I need to make a cropping SDL_Rect and calculate it everytime I change the position?
I'm confused, what happens when you run this and what did you expect to happen?

If I recall rightly, the SDL_BlitSurface function will actually change the rectangles you passed in, so it is not a good idea to pass the rectangle you are using to keep track of your object's position. Try this:
void D_Image::Display( ){   SDL_Rect temp = position;   SDL_BlitSurface(image, NULL, window, &temp);}
Advertisement
What I'm trying to do is to blit the surface so that it's on a negative coordinate, as in cropped by the upper boundary of the window (I want to make it scroll up).

I've now tried your suggestion, and it worked perfectly. Thanks!
You're welcome!

What was happening was that SDL_BlitSurface was changing the Rectangles x and y to (0, 0) so that it could blit the portion that should appear. Since you are passing the rectangle by pointer, however, you were making that correction permanent.
Sorry for reviving an old post, but this helped me and i have a correction to do.

SDL's blitting will set the position to 0 just if it is negative.

This topic is closed to new replies.

Advertisement