/*
 *  Load a grey circle, split it diagonally into two colours.
 *  Overlay the appropriate mana symbols in the correct halves.
 *  Save each image to a split-mana-symbol filename.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "app.h"

int gen_split_mana(Image *grey, Image *upper, Colour uppercol,
				Image *lower, Colour lowercol, char *filename)
{
	int i;
	Image *img;
	Image *smaller;
	Colour c;
	int x, y;
	Rect area, centred;
	Graphics *src, *dst;

	/* Create a copy of the base image. */
	img = app_copy_image(grey);
	if (img == NULL)
		return 2;

	/* Now colour the halves diagonally. */
	for (y = 0; y < img->height; y++)
	{
	 for (x = 0; x < img->width; x++)
	 {
		c = img->data32[y][x];
		if (c.alpha > 0x7F)
			continue;
		else if (img->width - x > y)
			img->data32[y][x] = uppercol;
		else
			img->data32[y][x] = lowercol;
	 }
	}

	/* Now overlay the symbols. */
	dst = app_get_image_graphics(img);

	smaller = app_scale_image(upper,
		rect(0, 0, upper->width * 48/100, upper->height * 48/100),
		rect(0, 0, upper->width,          upper->height));
	if (smaller)
	{
		area = app_get_image_area(smaller);
		x = grey->width * 6 / 60;
		y = grey->height * 6 / 60;

		src = app_get_image_graphics(smaller);
		app_copy_rect(dst, pt(x,y), src, area);
		app_del_graphics(src);
		app_del_image(smaller);
	}

	smaller = app_scale_image(lower,
		rect(0, 0, lower->width * 47/100, lower->height * 47/100),
		rect(0, 0, lower->width,     lower->height));
	if (smaller)
	{
		area = app_get_image_area(smaller);
		x = grey->width * 27 / 60;
		y = grey->height * 27 / 60;

		src = app_get_image_graphics(smaller);
		app_copy_rect(dst, pt(x,y), src, area);
		app_del_graphics(src);
		app_del_image(smaller);
	}

	app_del_graphics(dst);

	/* Save the generated image. */
	if (app_write_image(img, filename))
		printf("Created image file: %s\n", filename);

	return 0;
}

int main(int argc, char *argv[])
{
	Image *grey;
	Image *W, *U, *B, *R, *G;
	Colour Wbg, Ubg, Bbg, Rbg, Gbg;

	grey = app_read_image("symbols196/a-grey-circle.png", 32);
	W = app_read_image("symbols196/w.png", 32);
	U = app_read_image("symbols196/u.png", 32);
	B = app_read_image("symbols196/b.png", 32);
	R = app_read_image("symbols196/r.png", 32);
	G = app_read_image("symbols196/g.png", 32);
	Wbg = rgb(235,225,176);
	Ubg = rgb(178,210,239);
	Bbg = rgb(204,204,205);
	Rbg = rgb(244,166,137);
	Gbg = rgb(151,184,132);

	gen_split_mana(grey, W, Wbg, U, Ubg, "symbols196/WU.png");
	gen_split_mana(grey, W, Wbg, B, Bbg, "symbols196/WB.png");
	gen_split_mana(grey, U, Ubg, B, Bbg, "symbols196/UB.png");
	gen_split_mana(grey, U, Ubg, R, Rbg, "symbols196/UR.png");
	gen_split_mana(grey, B, Bbg, R, Rbg, "symbols196/BR.png");
	gen_split_mana(grey, B, Bbg, G, Gbg, "symbols196/BG.png");
	gen_split_mana(grey, R, Rbg, G, Gbg, "symbols196/RG.png");
	gen_split_mana(grey, R, Rbg, W, Wbg, "symbols196/RW.png");
	gen_split_mana(grey, G, Gbg, W, Wbg, "symbols196/GW.png");
	gen_split_mana(grey, G, Gbg, U, Ubg, "symbols196/GU.png");

	return 0;
}

