~artemis/svg

c5d7154cec1228392eae22744b196678477e834a — Artemis 1 year, 3 months ago 453fec2
Added image scaling
1 files changed, 29 insertions(+), 14 deletions(-)

M svg.c
M svg.c => svg.c +29 -14
@@ 29,15 29,28 @@ int get_padding(int container, int item)
	return (container > item ? container - item : item - container) / 2;
}

void recompute_image_position(SDL_Rect *dest, vect window)
float get_ratio(float item, float container)
{
	if (window.h == dest->h && window.w == dest->w)
	return (1 / item) * container;
}

void recompute_image_position(SDL_Rect *dest_original, SDL_Rect *dest, vect *window)
{
	if (window->h == dest->h && window->w == dest->w)
	{
		dest->x = 0;
		dest->y = 0;
	}
	dest->x = get_padding(window.w, dest->w);
	dest->y = get_padding(window.h, dest->h);

	float w_ratio = get_ratio(dest_original->w, window->w);
	float h_ratio = get_ratio(dest_original->h, window->h);
	float target_ratio = w_ratio < h_ratio ? w_ratio : h_ratio;

	dest->w = (int)(target_ratio * dest_original->w);
	dest->h = (int)(target_ratio * dest_original->h);

	dest->x = get_padding(window->w, dest->w);
	dest->y = get_padding(window->h, dest->h);
}

typedef struct load_image_res


@@ 47,6 60,7 @@ typedef struct load_image_res
	unsigned char *img;
	SDL_Surface *surface;
	SDL_Rect box;
	SDL_Rect scaled_box;
} load_image_res;

bool load_image(const char *filename, load_image_res *res)


@@ 80,10 94,11 @@ bool load_image(const char *filename, load_image_res *res)
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface: %s", SDL_GetError());
		return false;
	}
	res->box.x = 0;
	res->box.y = 0;
	res->box.w = res->image->width;
	res->box.h = res->image->height;
	res->scaled_box.x = 0;
	res->scaled_box.y = 0;
	res->scaled_box.w = res->image->width;
	res->scaled_box.h = res->image->height;
	res->box = res->scaled_box;

	return true;
}


@@ 246,11 261,11 @@ int main(int argc, char **argv)
				.w = w,
				.h = h,
			};
			recompute_image_position(&image.box, win_coords);
			border_position.x = image.box.x - 1;
			border_position.y = image.box.y - 1;
			border_position.w = image.box.w + 2;
			border_position.h = image.box.h + 2;
			recompute_image_position(&image.box, &image.scaled_box, &win_coords);
			border_position.x = image.scaled_box.x - 1;
			border_position.y = image.scaled_box.y - 1;
			border_position.w = image.scaled_box.w + 2;
			border_position.h = image.scaled_box.h + 2;
			needs_redraw = true;
		}



@@ 279,7 294,7 @@ int main(int argc, char **argv)
			SDL_SetRenderDrawColor(renderer, 0xDD, 0xDD, 0xDD, 0xFF);
			SDL_RenderClear(renderer);

			SDL_RenderCopy(renderer, texture, NULL, &image.box);
			SDL_RenderCopy(renderer, texture, NULL, &image.scaled_box);

			if (display_borders)
			{