#include "includes.h"

////////////////////////////////////////////////////////////////////////////////

int install_gfx()
{
	set_color_depth(8);
	
	if (0!=set_gfx_mode(MODE, SCR_W, SCR_H, 0, 0))
	{
		char buffer[5000]; 
		sprintf(buffer, "COULDN'T SET_GFX_MODE!\n %d x %d x %dbpp\nEDIT CONFIG.INI\n SORRY!\nALLEGRO_ERROR:\n %s", SCR_W, SCR_H, 8, allegro_error);
		allegro_message(buffer);
		allegro_exit();
		return(1);
	}
		

	if (MODE==GFX_AUTODETECT_FULLSCREEN)
	{FULLSCREEN=true;}
	else
	{FULLSCREEN=false;}
	
	apage=create_bitmap(320, 200);
	page1=create_bitmap(SCR_W, SCR_H);
	page2=NULL;
	
	if ((!apage) || (!page1))
	{
		allegro_message("Could not create backbuffers!");
		allegro_exit();
		return(1);
	}
	
	set_palette(MASTER_PAL);
	show_loading(0, LOADING_STEPS);
	calc_colors();
	show_loading(25, LOADING_STEPS);
			
	tileset=load_bitmap("gfx/master.pcx", MASTER_PAL);
	set_palette(MASTER_PAL);
	fix_tileset_and_palette();
	set_palette(MASTER_PAL);
			
	if (!tileset)
	{
		allegro_message("Couldn't Load Tileset: gfx/master.pcx!");
		allegro_exit();
		return(1);
	}
	
	calc_colors();
	show_loading(50, LOADING_STEPS);
	if (cut_tileset())
	{
		return(1);
	}
	
	show_loading(75, LOADING_STEPS);

	
	return (0);
}

////////////////////////////////////////////////////////////////////////////////

void page_flip()
{
	static int dx, dy;
	static int first_time=true;
	
	if (first_time)
	{
		dx=(SCR_W/2)-(DIS_W/2);
		dy=(SCR_H/2)-(DIS_H/2);
		first_time=false;	
	}
	
	if (DIS_W==SCR_W && DIS_H==SCR_H)
	{
		if (VSYNC){vsync();}
		blit(apage, screen, 0, 0, 0, 0, apage->w, apage->h);	
	}
	else
	{
		clear_to_color(page1, BLACK);
		stretch_blit(apage, page1, 0, 0, apage->w, apage->h, dx, dy, DIS_W, DIS_H);	
		if (VSYNC){vsync();}
		blit(page1, screen, 0, 0, 0, 0, page1->w, page1->h);
	}
	
	if (key[KEY_PRTSCR])
	{
		char buffer[100];
		int ok=false;
		int i=0;
		while (!ok)
		{
			i++;
			sprintf(buffer, "SCRSHOT%d.PCX", i);
			ok = (!file_exists(buffer));
		}
		
		if (DIS_W==SCR_W && DIS_H==SCR_H)
		{
			save_bitmap(buffer, apage, MASTER_PAL);			
		}
		else
		{	
			save_bitmap(buffer, page1, MASTER_PAL);
		}
		
		while (key[KEY_PRTSCR]);
		tmcLogic=0;	
	}
}

////////////////////////////////////////////////////////////////////////////////

int file_exists(char * filename)
{
	FILE * fp=fopen(filename, "r");
	if (fp)
	{
		fclose(fp);
		return(true);
	}
	
	return (false);	
}

////////////////////////////////////////////////////////////////////////////////

BITMAP * load_bitmap2(char * filename)
{
	PALETTE tmpPal;
	BITMAP * bmp=load_bitmap(filename, tmpPal);
	
	if (!bmp){return(NULL);}
	
	for (int i=0; i<bmp->w; i++)
	{
		for (int j=0; j<bmp->h; j++)
		{
			int c=getpixel(bmp, i,j);
			int r,g,b;
			r=((tmpPal[c].r+1)*4)-1;
			g=((tmpPal[c].g+1)*4)-1;
			b=((tmpPal[c].b+1)*4)-1;
			
			putpixel(bmp, i, j, makecol(r,g,b));
		}
	}
	
	return(bmp);
}

////////////////////////////////////////////////////////////////////////////////

void calc_colors(void)
{
	// Whites
	BLACK=makecol(0,0,0);
	DK_GRAY=makecol(128,128,128);
	GRAY=makecol(192,192,192);
	WHITE=makecol(255,255,255);

	// Low Intensity
	DK_BLUE=makecol(0,0,128);
	DK_GREEN=makecol(0,128,0);
	DK_CYAN=makecol(0,128,128);
	DK_RED=makecol(128,0,0);
	DK_PURPLE=makecol(128,0,128);
	DK_YELLOW=makecol(128,128,0);	

	// High Intensity
	BLUE=makecol(0,0,255);
	GREEN=makecol(0,255,0);
	CYAN=makecol(0,255,255);
	RED=makecol(255,0,0);
	PURPLE=makecol(255,0,255);
	YELLOW=makecol(255,255,0);
}

////////////////////////////////////////////////////////////////////////////////

void show_loading(int step, int max_steps)
{
	clear_to_color(apage, makecol(0,0,0));
	
	if (quitting)
	{
		textprintf_centre_ex(apage, font, apage->w/2,
		(apage->h/2)-(text_height(font)/2),
		makecol(255,255,255), -1, "Shutting Down...");
		page_flip();
		return;
	}
	
	textprintf_centre_ex(apage, font, apage->w/2,
		(apage->h/2)-(text_height(font)/2),
		makecol(255,255,255), -1, "Loading...");
	
	if (step>0)
	{
		if (step>=max_steps)
		{
			textprintf_right_ex(apage, font, apage->w-10, 10, makecol(255,255,255), -1, "100%%");
		}
		else
		{
			double n, d, p;
			n=(double)step;
			d=(double)max_steps;
			
			p=n/d;
			p *= 100;
			
			int a=(int)p;
			
			if (a>0)
			{
				textprintf_right_ex(apage, font, apage->w-10, 10, makecol(255,255,255), -1, "%d%%", a);
			}
		}
		
	}
	page_flip();
}

////////////////////////////////////////////////////////////////////////////////

void fix_tileset_and_palette()
{
	int zero_goes_here=makecol(255,0,255);
	
	if (!zero_goes_here){return;}
	
	MASTER_PAL[zero_goes_here].r=MASTER_PAL[0].r;
	MASTER_PAL[zero_goes_here].g=MASTER_PAL[0].g;
	MASTER_PAL[zero_goes_here].b=MASTER_PAL[0].b;
	
	MASTER_PAL[0].r=255;
	MASTER_PAL[0].g=0;
	MASTER_PAL[0].b=255;
	
	set_palette(MASTER_PAL);
	
	if (tileset)
	{
		destroy_bitmap(tileset);
	}
	
	tileset=load_bitmap2("gfx/master.pcx");
	
}

////////////////////////////////////////////////////////////////////////////////

int cut_tileset()
{
	int i=0;
	int x=0;
	int y=0;
	int w=16;
	int h=16;
	
	//tiles[512]
	while (i<512 && y<tileset->h)
	{
		tiles[i]=create_bitmap(w,h);
		if (!tiles[i])
		{
			tiles[i]=create_sub_bitmap(tileset, x, y, w, h);
		}
		else
		{
			blit(tileset, tiles[i], x, y, 0, 0, w, h);
		}
		
		if (!tiles[i])
		{
			allegro_message("Not enough memory to load tileset!");
			allegro_exit();
			return(1);
		}
		
		x+=w;
		if (x>=tileset->w)
		{
			x=0;
			y+=h;
		}
		i++;
	}
	
	return(0);
}

////////////////////////////////////////////////////////////////////////////////

PMASK * create_box_pmask(BITMAP * bmp)
{	
	int x1, y1;
	int x2, y2;
	int t=makecol(255,0,255);
	int nt=makecol(0,0,0);
	
	x1=0;
	for (int i=0; i<bmp->w; i++)
	{
		int ok=true;
		for (int j=0; j<bmp->h; j++)
		{
			if (getpixel(bmp, i,j) !=t){ok=false;}
		}
		if (ok)
		{x1=i;}
		else
		{i=bmp->w;}
	}
	
	x2=bmp->w-1;
	for (int i=bmp->w-1; i>=0; i--)
	{
		int ok=true;
		for (int j=0; j<bmp->h; j++)
		{
			if (getpixel(bmp, i,j) !=t){ok=false;}
		}
		if (ok)
		{x2=i;}
		else
		{i=0;}
	}
	
	y1=0;
	for (int i=0; i<bmp->h; i++)
	{
		int ok=true;
		for (int j=0; j<bmp->w; j++)
		{
			if (getpixel(bmp, j,i) !=t){ok=false;}
		}
		if (ok)
		{y1=i;}
		else
		{i=bmp->h;}
	}
	
	y2=bmp->w-1;
	for (int i=bmp->h-1; i>=0; i--)
	{
		int ok=true;
		for (int j=0; j<bmp->w; j++)
		{
			if (getpixel(bmp, j,i) !=t){ok=false;}
		}
		if (ok)
		{y2=i;}
		else
		{i=0;}
	}
	
	BITMAP * b2=create_bitmap(bmp->w, bmp->h);
	clear_to_color(b2, t);
	for (int i=x1; i<=x2; i++)
	{
		for (int j=y1; j<=y2; j++)
		{
			putpixel(b2, i,j, nt);	
		}	
	}
	
	PMASK * p=create_allegro_pmask(b2);
	destroy_bitmap(b2);
	
	return p;
}

////////////////////////////////////////////////////////////////////////////////

