#include <allegro.h>
#include <math.h>
#include <iostream>

#define max(a, b)  (((a) > (b)) ? (a) : (b))
#define min(a, b)  (((a) < (b)) ? (a) : (b))

using namespace std;

/*
Nakresli progress bar

@param bmp Bitmapa do ktorej kreslit (BITMAP*)
@param x,y Suradnice zaciatku
@param width,height Sirka a vyska
@param start_r, start_g, start_b RGB pre zaciatok (0 - 255)
@param end_r, end_g, end_b RGB pre koniec (0 - 255)
@param progress Vyfarbena cast (0 - 100%)
@param border Urcuje ci sa nakresli ramcek (bool)
@param border_col Farba ramceku (makecol)
@param background_col Farba pozadia (makecol)
*/
void drawProgressBar(BITMAP* bmp, int x, int y, int width, int height, int start_r, int start_g, int start_b, int end_r, int end_g, int end_b, int progress, bool border, int border_col, int background_col) {
	
	// rozdiel v RGB zlozkach
	
	int diff_r = max(start_r, end_r) - min(start_r, end_r);
	int diff_g = max(start_g, end_g) - min(start_g, end_g);	
	int diff_b = max(start_b, end_b) - min(start_b, end_b);
	
	// preskakovat rovnake zlozky
	
	bool no_r = (diff_r == 0) ? TRUE : FALSE;
	bool no_g = (diff_g == 0) ? TRUE : FALSE;
	bool no_b = (diff_b == 0) ? TRUE : FALSE;
	
	// krok pre jednotlive zlozky
	
	double step_r = (no_r) ? NULL : (diff_r / min(width, diff_r));
	double step_g = (no_g) ? NULL : (diff_g / min(width, diff_g));
	double step_b = (no_b) ? NULL : (diff_b / min(width, diff_b));
	
	// koniec gradientu
	
	bool endg_r = (no_r) ? TRUE : FALSE;
	bool endg_g = (no_g) ? TRUE : FALSE;
	bool endg_b = (no_b) ? TRUE : FALSE;
	
	// pozadie
	
	rectfill(bmp, x, y, x + width, y + height, background_col);
	
	// premenne pre cyklus
	
	int i;
	int stop = (progress * width) / 100;
	
	int cx, sy, ey;
	int r,g,b;
	
	// cyklus
	
	for (i = 0; i < stop; i++) {
		
		// vypocet suradnic ciary
		
		cx = x + i;
		sy = y;
		ey = y + height;
		
		// vypocet RGB
		
		if (!endg_r) {
			
			r = start_r + (int) (step_r * i);
			if (r == end_r) {
			endg_r = TRUE;
			printf("Koncim r, i = %d\n", i);
			}
			
		}
		
		if (!endg_g) {
			
			g = start_g + (int) (step_g * i);
			if (g == end_g) {
			endg_g = TRUE;
			printf("Koncim g, i = %d\n", i);
			}
						
		}
		
		if (!endg_b) {
			
			b = start_b + (int) (step_b * i);
			if (b == end_b) endg_b = TRUE;
			printf("Koncim b, i = %d\n", i);			
			
		}
		
		line (bmp, cx, sy, cx, ey, makecol(r, g, b));
		
	}
	
}

int main () {
	
	//...
	
	allegro_init();
	install_keyboard();
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,1024,768,0,0);
	
	clear_to_color(screen, makecol(0, 0, 0));
	
	drawProgressBar(screen,100,200,100,10,255,0,0,0,255,0,50,1,makecol(255,255,255), makecol(0,0,255));
		
	while (!key[KEY_ESC]) {};
	
	return 0;
	
}
END_OF_MAIN();
