Is there some way to stop SDL_BlitSurface from modifying the destination Rectangle I pass it

I’m working on a very simple Tile engine that depends on the value in
the destination rectangle I pass SDL_BlitSurface. Each time through the loop
that draws the tiles I increment the x and y values of the destination
rectangle so that the next tile will be drawn in the next spot. Here’s the
function:

int DrawTiles(SDL_Rect* WorldCoordsRect){

SDL_Rect destpts, Blit_Tiles_Extents, SDL_Blit_Rect;
int first_destpts_x;

destpts.x = -(WorldCoordsRect->x % X_TILE_SIZE);
destpts.y = -(WorldCoordsRect->y % Y_TILE_SIZE);

first_destpts_x = destpts.x;

Blit_Tiles_Extents.x = WorldCoordsRect->x / X_TILE_SIZE;
Blit_Tiles_Extents.y = WorldCoordsRect->y / Y_TILE_SIZE;
Blit_Tiles_Extents.w = WorldCoordsRect->w / X_TILE_SIZE;
Blit_Tiles_Extents.h = WorldCoordsRect->h / Y_TILE_SIZE;

for( int j = Blit_Tiles_Extents.y; j < Blit_Tiles_Extents.h; j++){

	for( int i = Blit_Tiles_Extents.x; i < Blit_Tiles_Extents.w; i++){
		SDL_Blit_Rect.x = destpts.x;
		SDL_Blit_Rect.y = destpts.y;	
		SDL_BlitSurface(Tiles[ TileMap[i][j] ], NULL, screen,&SDL_Blit_Rect);

		destpts.x += X_TILE_SIZE;

	}//end for loop i

	destpts.y += Y_TILE_SIZE;

	printf("[destpts.y]: %d \n", destpts.y);
	destpts.x = first_destpts_x;

}//end for loop j

return 0;

}

The funtion isn't complete but you can probably see my problem and the

solution to it (use another SDL_Rect stucture and let SDL overright it, it’s
in the for loop with the blit). Is there a better/more elagant solution. BTW,
thanks for everyone who chimed in with my last question about scrolling. I can’t
believe it didn’t occure to me to just erase the sprite with tiles.–
Jeremy Gregorio
jgreg at azstarnet.com

I don’t think there is. SDL modifies the rect when you pass it to suit its
needs.

I’m sure you know that setting up a rect really isn’t that expensive in
the big scheme of things (when, for example, comparing to the blit
itself)… You could even write a macro like SDL_SetRect to make it 1 line
of code (at least to the untrained eye ;-)) If you want to keep things a
little more readable.

HTH,
Darrell

The funtion isn’t complete but you can probably see my problem and the
solution to it (use another SDL_Rect stucture and let SDL overright it, it’s
in the for loop with the blit). Is there a better/more elagant solution.

SDL clips the rectangle because the update routines will choke if you pass
in unclipped rectangles. Since the clipping needs to be done in the blit
anyway, SDL passes that information back to you. There are two ways to
handle this. If you absolutely know for sure that you are not passing
unclipped rectangles to the blit, you can use SDL_LowerBlit() instead of
the normal blitting function. The other, simpler way, is to make a copy
of the rectangle that you pass to the blit:

SDL_Rect dst, blitrect;

while ( moving_sprite ) {
	dst.x += 10;
	blitrect = dst;
	SDL_BlitSurface(..., &blitrect);
}

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software