This is the drawing method of my tile engine.
The point is, when a button is pressed (left,right,up,down) the x and y offsets are changed. if the offsets are changed the map is updated and goes through the 3 nested for loops. If the offsets have not been changed then that last draw will be redrawn. So when idle I get 999+ fps when I move the map the fps goes down to about 55 because of the expensive content of the 3 nested for loops. I know that somehow I need to get rid of the if statements and the multiplies in the loops but I can't think of how I would do it.
Any help is appreciated.
Code:
int CMap::DrawMap(SDL_Surface *dest_surf, SDL_Surface *src_surf){
SDL_Rect dest;
SDL_Rect src;
int y,x,lyr;
/* previous offsets */
static int pre_x_offset = -1;
static int pre_y_offset = -1;
/* Checks if map needs updating */
if(x_offset != pre_x_offset || y_offset != pre_y_offset){
for(lyr=0;lyr<LAYERS;lyr++){
for(y=0;y<y_tiles;y++){
for(x=0;x<x_tiles;x++){
dest.x = (x * TILESIZEX) - x_offset;
dest.y = (y * TILESIZEY) - y_offset;
/* Does not draw outside the display_size box */
if(dest.x > display_size_x) continue;
if(dest.y > display_size_y) continue;
if(dest.x < -32) continue;
if(dest.y < -32) continue;
/* The source coordinates of the tile map */
src.x = MAP[lyr][y][x].xpos;
src.y = MAP[lyr][y][x].ypos;
src.w = TILESIZEX;
src.h = TILESIZEY;
/* Blit one tile at a time to a buffer */
SDL_BlitSurface(src_surf, &src, buf, &dest);
}
}
}
}
/* Nullify dest rect blit buffer to destination surface*/
dest.x = 0;
dest.y = 0;
SDL_BlitSurface(buf, NULL, dest_surf, &dest);
/* Updates previous offsets */
pre_x_offset = x_offset;
pre_y_offset = y_offset;
return 0;
}