#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

#include "..\comp\Graphics\graph.h"
void CaptureDisplay(char* szFile)
{
	FILE* fpFile = fopen(szFile,"w");
	int szColorMap[16] =
	{
		0x0000,		//BLACK
		0x0010,		//BLUE
		0x0200,		//GREEN
		0x0310,		//CYAN
		0x4000,		//RED
		0x4010,		//MAGENTA
		0x4100,		//BROWN
		0x6318,		//LIGHTGRAY
		0x4210,		//DARKGRAY
		0x001F,		//LIGHTBLUE
		0x03E0,		//LIGHTGREEN
		0x033F,		//LIGHTCYAN
		0x7C00,		//LIGHTRED
		0x7C1F,		//LIGHTMAGENTA
		0x7FE0,		//YELLOW
		0x7FFF		//WHITE
	};
	char cPixel,cPixel1;
	STgaHeader Header;
	Header.ucIdentSize = 0;
	Header.ucColorMapType = 0;
	Header.ucImageType = 2;
	Header.usColorMapStart = 0;
	Header.usColorMapLenght = 0;
	Header.usXStart = 0;
	Header.usYStart = 0;
	Header.usWidth = 640;
	Header.usHeight = 480;
	Header.ucBitsPerPixel = 16;
	Header.ucDescriptor = 0;
	fwrite(&Header,sizeof(STgaHeader),1,fpFile);
	for(int i=480; i >=0;i--)
	{
		for(int y = 0; y <640;y++)
		{
			cPixel = getpixel(y,i);
			fwrite(&szColorMap[cPixel],2,1,fpFile);
		}
	}
	fflush(fpFile);
	fclose(fpFile);
}

CGraphics::CGraphics()
{
	int iMode,iDriver = DETECT, iError;
	registerbgidriver(EGAVGA_driver);
	initgraph(&iDriver,&iMode,"");
	if((iError = graphresult()) != grOk)
	{
		printf("Graphics Error; %s\n",grapherrormsg(iError));
		exit(0);
	}
	//Install font
	registerbgifont(triplex_font);
}
CGraphics::~ CGraphics()
{
	closegraph();
}
//We can't use cleardevice here because we wont be abel to display anything in black
void CGraphics::ClearScreen(void)
{
	CSize chSize;
	setfillstyle(SOLID_FILL,BackGroundColor);

	bar(0,0,getmaxx(),getmaxy());
// Border
	// Border while
	setcolor(WHITE);
	line(0,0,0,getmaxy());
	line(0,0,getmaxx(),0);
	line(getmaxx(),0,getmaxx(),getmaxy());
	line(0,getmaxy(),getmaxx(),getmaxy());

	// Border LIGHTGRAY
	setcolor(LIGHTGRAY);
	line(1,1,1,getmaxy()-1);
	line(1,1,getmaxx()-1,1);
	line(getmaxx()-1,1,getmaxx()-1,getmaxy()-1);
	line(1,getmaxy()-1,getmaxx()-1,getmaxy()-1);

	// Border DARKGRAY
	setcolor(DARKGRAY);
	line(2,2,2,getmaxy()-2);
	line(2,2,getmaxx()-2,2);
	line(getmaxx()-2,2,getmaxx()-2,getmaxy()-2);
	line(2,getmaxy()-2,getmaxx()-2,getmaxy()-2);

	// Border BLACK
	setcolor(BLACK);
	line(3,3,3,getmaxy()-3);
	line(3,3,getmaxx()-3,3);
	line(getmaxx()-3,3,getmaxx()-3,getmaxy()-3);
	line(3,getmaxy()-3,getmaxx()-3,getmaxy()-3);

// 3 part

//ToolsBar, separator
	setcolor(White);
	line(0,50,getmaxx(),50);
	
	setcolor(LightGray);
	line(1,49,getmaxx() - 1,49);
	line(1,51,getmaxx() - 1, 51);
	
	setcolor(DarkGray);
	line(2,48,getmaxx() -2,48); 
	line(2,52,getmaxx() - 2, 52);
	
	setcolor(Black);
	line(3, 47,getmaxx() - 3, 47);
	line(3, 53,getmaxx() - 2, 53);

// Task bar, separator
	setcolor(White);
	line(0,getmaxy() - 50,getmaxx(),getmaxy() - 50); 

	setcolor(LightGray);
	line(1,getmaxy() - 51,getmaxx() - 1, getmaxy() - 51);
	line(1,getmaxy() - 49, getmaxx() - 1, getmaxy() - 49);

	setcolor(DarkGray);
	line(2,getmaxy() - 48,getmaxx()-2,getmaxy()-48); 
	line(2,getmaxy() - 52,getmaxx() -2,getmaxy()-52);

	setcolor(Black);
	line(3, getmaxy() - 53, getmaxx() -3, getmaxy() - 53);
	line(3, getmaxy() - 47, getmaxx() - 3, getmaxy() -47);
}

int CGraphics::Line3D(CSize& chSize,unsigned char ucBorder)
{
	int iGx, iDx, iBx;
	int iGy, iDy, iBy;

	iGx =  1; iDx =  2; iBx =  3; iGy =  1;	iDy =  2; iBy =  3;

	if(chSize.m_iX < 0 || chSize.m_iX > getmaxx()-3)
		return -1;
	
	if(chSize.m_iMaxX < 3 || chSize.m_iMaxX > getmaxx() + 1)
		return -1;

	if(chSize.m_iY < 3 || chSize.m_iY > getmaxy()-3)
		return -1;

	if(chSize.m_iMaxY < 3 || chSize.m_iMaxY > getmaxy()-3)
		return -1;

	if(ucBorder == 0)
	{
		if(chSize.m_iX == chSize.m_iMaxX)
		{ // verti
			iGy =  0;
			iDy =  0;
			iBy =  0;
		}
		else if(chSize.m_iY == chSize.m_iMaxY)
		{ // Hori
			iGx =  0;
			iDx =  0;
			iBx =  0;
		}
	}
	setcolor(White);
	line(chSize.m_iX , chSize.m_iY , chSize.m_iMaxX, chSize.m_iMaxY); 

	setcolor(LightGray);
	line(chSize.m_iX + iGx, chSize.m_iY + iGy, chSize.m_iMaxX - iGx, chSize.m_iMaxY + iGy);
	line(chSize.m_iX + iGx, chSize.m_iY - iGy, chSize.m_iMaxX - iGx, chSize.m_iMaxY - iGy);

	setcolor(DarkGray);
	line(chSize.m_iX + iDx, chSize.m_iY + iDy, chSize.m_iMaxX - iDx, chSize.m_iMaxY + iDy); 
	line(chSize.m_iX + iDx, chSize.m_iY - iDy, chSize.m_iMaxX - iDx, chSize.m_iMaxY - iDy);

	setcolor(Black);
	line(chSize.m_iX + iBx, chSize.m_iY + iBy, chSize.m_iMaxX - iBx, chSize.m_iMaxY + iBy);
	line(chSize.m_iX + iBx, chSize.m_iY - iBy, chSize.m_iMaxX - iBx, chSize.m_iMaxY - iBy);
	return 0;
}

CSize CGraphics::GetSizeTitle(void)
{
	CSize chReturn;
	chReturn.m_iX		= 4;
	chReturn.m_iY		= 4;
	chReturn.m_iMaxX	= getmaxx()-4;
	chReturn.m_iMaxY	= 46;
	return chReturn;
}

CSize CGraphics::GetSizeBarTask(void)
{
	CSize chReturn;
	chReturn.m_iX		= 4;
	chReturn.m_iY		= getmaxy()-46;
	chReturn.m_iMaxX	= getmaxx()-4;
	chReturn.m_iMaxY	= getmaxy()-4;
	return chReturn;
}

CSize CGraphics::getSizeBody(void)
{
	CSize chReturn;
	chReturn.m_iX		= 4;
	chReturn.m_iY		= 54;
	chReturn.m_iMaxX	= getmaxx()-4;
	chReturn.m_iMaxY	= getmaxy()-54;
	return chReturn;
}






