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

#define max(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) {
	
	// pocet "krokov" pri 100%
	
	int steps = width;
	
	if (steps > 255) steps = 255;
	
	// krok pre RGB
	
	double step_r = max(start_r, end_r) / steps;
	double step_g = max(start_g, end_g) / steps;
	double step_b = max(start_b, end_b) / steps;	
	
	// pricitat ci odcitat? 
	
	bool add_r = (max(start_r, end_r) == end_r) ? TRUE : FALSE;
	bool add_g = (max(start_g, end_g) == end_g) ? TRUE : FALSE;
	bool add_b = (max(start_b, end_b) == end_b) ? TRUE : FALSE;	
	
	// pozadie
	
	rectfill(bmp, x, y, x + width, y + height, background_col);
	
	// cyklusove premenne
	
	int i;
	int stop = (progress * width) / 100;
	
	// docasne pomocne premenne
	
	int sx, ex, sy, ey;
	int r,g,b;
	
	for (i = 0; i < stop; i++) {
		
		// nastavenie zaciatku a konca
		
		sx = i; // start x
		ex = i; // end x
		
		sy = 0; // start y
		ey = height; // end y
		
		// posun
		
		sx = sx + x;
		ex = ex + x;
		
		sy = sy + y;
		ey = ey + y;
		
		// spocitanie RGB
		
		r = (int) step_r * i;
		g = (int) step_g * i;
		b = (int) step_b * i;
		
		// pripociatat (alebo odpocitat) zaciatok 
		
		r = (add_r) ? (r + start_r) : (start_r - r);
		g = (add_g) ? (g + start_g) : (start_g - g);
		b = (add_b) ? (b + start_b) : (start_b - b);
		
		if (i > steps) {
			
			r = end_r;
			g = end_g;
			b = end_b;
			
		}
		
		// nakreslenie
		
		line (bmp, sx, sy, ex, ey, makecol(r, g, b));
		
	}
	
	if (border) {
		
		// ramcek
		
		line(bmp, x, y, x + width, y, border_col); // top
		line(bmp, x, y + height, x + width, y + height, border_col); // bottom
		line(bmp, x, y, x, y + height, border_col); // left
		line(bmp, x + width, y, x + width, y + height, border_col); // right
		
	}
	
}

/*
Nakresli cely status bar (sirka = SCREEN_W, vyska = SREEN_H / 10);

@param bmp Bitmapa kam kreslit
@param health zdravie (0 - 100)
@param reload_navnada, reload_torpedo1-4 Reloading navnady a torpedovych komor
@param pocet_torped, pocet_navnad
@param cas Cas od novej hry
@param cash Peniaze
@param border, border_col Ramcek, farba (makecol)
@param background_col Farba pozadia (makecol);
*/

void drawStatusBar(BITMAP* bmp, int health, int reload_navnada, int reload_torpedo1, int reload_torpedo2, int reload_torpedo3, int reload_torpedo4, int pocet_torped, int pocet_navnad, int cas, int cash, bool border, int border_col, int background_col) {
	
	int width = SCREEN_W;
	int height = SCREEN_H / 10;
	
	int y = SCREEN_H - height;
	
	double kw = SCREEN_W / 1024; // konstanta - pomer vypocitanej a aktualnej sirky
	double kh = SCREEN_H / 768; // konstanta - ^
	
	// sirka & vyska veci
	
	
	// health
	int health_height = (int) (23 * kh);
	int health_hspace = (int) (10 * kh);
	int health_width  = (int) (300 * kw);
	int health_wspace = (int) (20 * kw);
	
	// reload torped
	int reloadt_height = (int) (12 * kh);
	int reloadt_hspace = (int) (10 * kh);
	int reloadt_width = (int) (150 * kw);
	int reloadt_wspace = (int) (20 * kw);
	
	// reload nevnady
	int reloadn_height = (int) (12 * kh);
	int reloadn_hspace = (int) (10 * kh);
	int reloadn_width = (int) (200 * kw);
	int reloadn_wspace = (int) (80 * kw);
	
	// koniec vypoctov ( konecne :) ... )
	
	// pozadie, ramcek
	
	rectfill(bmp, 0, y, width, y + height, background_col);
	line(bmp, 0, y, width, y, border_col);
	
	// progress bars ...
	
	// health progressbar
	
	printf("%d", health_width);
	
	drawProgressBar(bmp, health_wspace, y + health_hspace, health_width, health_height, 255, 0, 0, 0, 255, 0, health, 1, makecol(255,255,255), makecol(0,0,255));

	// torpedo reload progressbars
	
	drawProgressBar(bmp, width - 2*reloadt_width - 2*reloadt_wspace, y + reloadt_hspace, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo1, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - reloadt_width - reloadt_wspace, y + reloadt_hspace, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo2, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - 2*reloadt_width - 2*reloadt_wspace, y + 2*reloadt_hspace + reloadt_height, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo3, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - reloadt_width - reloadt_wspace, y + 2*reloadt_hspace + reloadt_height, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo4, 0, 0, makecol(0,0,0));
	
	// navnada reload progressbar
	
	drawProgressBar(bmp, width - reloadn_width - reloadn_wspace, y + 3*reloadt_hspace + 2*reloadt_height, reloadn_width, reloadn_height, 255, 0, 0, 0, 255, 0, reload_torpedo4, 0, 0, makecol(0,0,0));
		
}

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,300,10,255,0,0,0,255,0,50,1,makecol(255,255,255), makecol(0,0,255));
	/**/
	
	drawStatusBar(screen, 100, 75, 50, 50, 25, 75, 0, 0, 0, 0, 1, makecol(255,255,0), makecol(128,128,128));
	
	while (!key[KEY_ESC]) {};
	
	return 0;
	
}
END_OF_MAIN();
