#include "tile.h"
#include "render_struct.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int render_set_tile (struct render *render_info, u_int32_t view_w, u_int32_t view_h, u_int32_t x, u_int32_t y, u_int16_t zoom) {
// figure out how many tiles will fit onto the view area
u_int16_t tiles = MIN(
MAX(view_w / TILE_WIDTH, 1),
MAX(view_h / TILE_HEIGHT, 1)
) + 1;
// have it be square
view_w = tiles * TILE_WIDTH;
view_h = tiles * TILE_HEIGHT;
// the image width is fixed
render_info->img_w = TILE_WIDTH;
render_info->img_h = TILE_HEIGHT;
// calcuate how wide each pixel is
double width_scale = REGION_W/(view_w << zoom);
double height_scale = REGION_H/(view_h << zoom);
// calcuate the region
render_info->x1 = REGION_X1 + x * width_scale;
render_info->y1 = REGION_Y1 + y * height_scale;
render_info->x2 = render_info->x1 + TILE_WIDTH * width_scale;
render_info->y2 = render_info->y1 + TILE_HEIGHT * height_scale;
// ok
return 0;
}