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