#include<stdio.h>
#include<time.h>
#include<process.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#define d 80
#define TRUE  1

struct REGPACK reg, REGS ;

//for showing contents in different fonts
char *fname[] = { "PROGRAM TO DEAL WITh :",
		  "RASSAM USING C-LANG",
		  "COMPUTER GRAPHICS",
		  "FINAL PROJECT FOR ARAB TEAM 2000",
		  "DONE BY : ",
		  "STUDENT:IBRAHIM AL-SHARIF",
		};



char *str[8] = {"Line"     ,
	      "Rectangle",
	      "Circle"   ,
	      "Ellipse"  ,
	      "Polygon"  ,
	      "Sector"   ,
	      "Clear"    ,
	      "Exit"};

void Choose();
void Call();
void pos_mouse();
void show_mouse();
void hide_mouse();
void basic_paint();

void Interface();
void Display();
void font();

void get_point();
void draw_pixel();


void INITIALIZE();
void BOX();

int P_Box();
int R_Box();

int label=0, x=0, y=0, sx=0, sy=0;
int color=1;
char msg[80];

/*Shapes to be draw*/
void Line();
void sector();
void Rectangle();
void Polygon();
void Circle();
void Ellipse();

/************************************<Main>**********************************/
int main(void)
{
/*reg.r_ax=0;
  reg.r_bx=0;
  reg.r_cx=0;
  reg.r_dx=0;
*/

Interface();
setviewport(105, 0, getmaxx()-50, getmaxy()-50,1);
  Call();

  return 0;
}
/*************************************<Mmain>********************************/
void Call()
{
       INITIALIZE();
	pos_mouse();
	hide_mouse();
	show_mouse();
	basic_paint();
	color = 20;
	BOX();
	show_mouse();
	while(TRUE)
	{
       if(P_Box() > 0)//depends on register
	 {
	 get_point();
	 if(reg.r_cx > 15 && reg.r_cx < 625 && reg.r_dx > 450 && reg.r_dx < 465)
		color = (x-91)/(534/16);
	 if(reg.r_cx > 15 && reg.r_cx < 95 && reg.r_dx > 15 && reg.r_dx < 465)
		Choose();
	 if(reg.r_cx < 25 && reg.r_dx < 25)
		break;
	}//end of if

	}//end of while
	closegraph();
}
/********************************<Color appear>******************************/
void basic_paint()
{
	int k = 90;
	setcolor(8);
	setfillstyle(1, 8);
	bar3d(  0  , 10 ,  105  ,getmaxy()/2, 0     , 10 );
	bar3d(  0  , 11 ,  105  ,getmaxy()/2, 0     , 11 );

	line(15, 450, 625, 450);//start of beginig line for color (down)
	for(int i=0;i<10;i++)//number of colors
	{
		setfillstyle(1,i);
		bar(k+1,451,k+610/16,464);
		k = k+534/16;
	}
}
/******************************<pushing Box>*********************************/
int P_Box()
{
	reg.r_ax = 5;
	reg.r_bx = 2;
	intr(0x33, &reg);
	return reg.r_bx;
}
/********************************<lifting Box>*******************************/
int R_box()
{
	reg.r_ax = 6;
	reg.r_bx = 2;
	intr(0x33, &reg);
	return reg.r_bx;
}
/********************************<get_point>*********************************/
void get_point()
{
	reg.r_ax = 3;
	intr(0x33, &reg);
	x = reg.r_cx;
	y = reg.r_dx;
}
/*******************************<position_mouse>*****************************/
void pos_mouse()
{
	reg.r_ax=4;
	reg.r_cx=reg.r_dx=0;
	reg.r_ax = 0;         //initialize mouse
	intr(0x33, &reg);
}
/********************************<show_mouse>********************************/
void show_mouse()
{
	reg.r_ax = 1;
	intr(0x33, &reg);
	reg.r_ax = 3;
}
/********************************<hide_mouse>********************************/
void hide_mouse()
{
	reg.r_ax = 2;
	intr(0x33, &reg);
	reg.r_ax = 3;
}
/************************************<BOX>**********************************/
void BOX()          //Drawing buttons on the screen
{
float i;
int l=20;

	line(105, 15, 105, 500);//vertical line between boxes
	for(i=1;i<9;i++)//number of buttons
	{//need one more for appearing the name of the shape dealing with
	setfillstyle(1,i);//color with different background for boxes
	bar(0,l+1,104,l+20);//showing dimensions of colored region
	setcolor(YELLOW);//colored fonts
	outtextxy(18, l+8, str[i-1]);//take the fonts
	l=l+450/16;//division of vertical left font
	}//if

}
/*******************************<Draw Polygon>*******************************/
void Polygon()
{
	if(x>91 && x<625 && y>15 && y<450)
	{
	hide_mouse();
	putpixel(x,y,color);
	if(sx != 0 && sy != 0)
	{
	 setcolor(color);
	 line(sx, sy, x, y);
	}
	sx = x;sy = y;
	show_mouse();
	}
	sx = x;
	sy = y;
}
/********************************<Drawing Circle>****************************/
void Circle()
{
	hide_mouse();
	if(sx!=0 && sy!=0)
	{
	  setcolor(color);
	  circle((abs(sx+x))/2, (abs(sy+y))/2, (abs(sx-x))/2);
	  sx = 0;sy = 0;
	}
	else
	 {
	 sx = x;sy = y;
	 }
	show_mouse();
}
/****************************<Drawing Ellipse>******************************/
void Ellipse()
{
	int sx, sy;
	get_point();
	sx = x;
	sy = y;
	while(reg.r_bx&1 == 1)
	{
		if(x>91 && x<625 && y>16 && y<449)
		   get_point();

		else
		   break;
	}
	hide_mouse();
	setcolor(color);
	ellipse((abs(sx+x))/2, (abs(sy+y))/2, 0, 360, (abs(sx-x))/2, (abs(sy-y))/2);
	show_mouse();
}
/*******************************<Drawing Rectangle>**************************/
void Rectangle()
{
	int sx, sy;
	get_point();
	sx = x;
	sy = y;
	while(reg.r_bx&1 == 1)
	{
		if(x>91 && x<625 && y>16 && y<449)
		  get_point();

		else
		 break;
	}
	hide_mouse();
	setcolor(color);
	rectangle(sx, sy, x, y);
	show_mouse();
	if(P_Box() > 0)
	  if(x>15 && x<91 && y>15 && y<450)
	    Choose();
}
/*********************************<Draw Line>*******************************/
void Line()
{
	int sx,sy;
	get_point();
	sx = x;sy = y;
	while(reg.r_bx&1 == 1)
	{
		if(x > 91 && x < 625 && y > 16 && y < 449)
		  get_point();

		else
		  break;
	}
	hide_mouse();
	setcolor(color);
	line(sx,sy,x,y);
	show_mouse();
	if(P_Box() > 0)
	  if(x > 15 && x < 91 && y > 15 && y < 450)
	    Choose();
}
/********************************<Drawing Sector>***************************/
void sector()
{
	int sx,sy;
	get_point();
	sx = x;sy = y;
	while(reg.r_bx&1 == 1)
	{if(x > 91 && x < 625 && y > 16 && y < 449)
	 get_point();
	 else
	 break;
	}
	hide_mouse();
	setcolor(color);
	sector((abs(sx+x))/2,(abs(sy+y))/2,0,360,(abs(sx-x))/2,(abs(sy-y))/2);
	show_mouse();

}
/*****************************<Functions>***********************************/
void Choose()
{
	setcolor(0);outtextxy(18, 255, str[label]);
	label = (y-15)/(450/16);
	setcolor(12);outtextxy(18, 255, str[label]);
	switch(label)
	{
		case 6: clrscr();Call(); break;
		case 7:
		     clrscr();INITIALIZE();settextstyle(7,HORIZ_DIR,5); /* select the text style */
		     outtextxy(200,100,"GOOD BYE"); /* output a message */
		     outtextxy(100,200,"THE BEST FOR YOU"); /* output a message */
		     outtextxy(130,300,"IBRAHIM AL-SHARIF"); /* output a message */
		     getch();
		     exit(1);//leave the executable program

		exit(0);
		default: break;
	}
	sx = 0;sy = 0;
	while(TRUE)
	{
		if(P_Box() > 0)
		{
			get_point();
			if(x > 91 && x < 625 && y > 15 && y < 450)
			{
				switch(label)
				{
					case 0: Line(); break;
					case 1: Rectangle(); break;
					case 2: Circle(); break;
					case 3: Ellipse(); break;
					case 4:	Polygon(); break;
					case 5:	sector();break;
					default: break;
				}
			}
			else if(x > 15 && x < 625 && y > 450 && y < 465)
				color = (x-91)/(534/16);
			else
				break;
		}
	}
}
/*******************************<INITIALIZE>********************************/
void INITIALIZE()
{
/* FORCE AN ERROR TO OCCUR */
int gd = DETECT,errorcode, gm;
/* initialize graphics mode */
initgraph(&gd, &gm, "c:\\tc\\bgi");

/* read result of initialization */
errorcode = graphresult();

/* if an error occurred, then output a */
/* descriptive error message.          */
if (errorcode != grOk)
  {
   printf("Graphics error: %s\n", grapherrormsg(errorcode));
   printf("Press any key to halt:");
   getch();
   exit(1); /* terminate with an error code */
  }

}
/**************************<DISPLAY>***************************************/
void Display()
{
char ch;      //for entered character
clrscr();        //clear the screen
gotoxy(30, 1); cprintf("旼컴컴컴컴컴컴컴�");
gotoxy(30, 2); cprintf("�  Show  ASSAM  �");
gotoxy(30, 3); cprintf("읕컴컴컴컴컴컴컴�");
gotoxy(35, 19); cprintf("旼컴컴커");
gotoxy(35, 20); cprintf("�  xit �");
gotoxy(35, 21); cprintf("읕컴컴켸");
textcolor(15);
gotoxy(38, 2); textcolor(RED);cprintf("R");
gotoxy(37,20); cprintf("E");
ch=getch();//take the entered character and comparing it

if(ch=='R' || ch=='r') //if entered character is T or t
{clrscr();INITIALIZE();Call();}//end of else if

if (ch=='E'||ch=='e') //if entered character is E or e
{
clrscr();INITIALIZE();settextstyle(7,HORIZ_DIR,5); /* select the text style */
		     outtextxy(200,100,"GOOD BYE"); /* output a message */
		     outtextxy(100,200,"THE BEST FOR YOU"); /* output a message */
		     outtextxy(130,300,"IBRAHIM AL-SHARIF"); /* output a message */
getch();
exit(1);//leave the executable program
}
else //any character except (E or e) or (X or x) will return to same display
Display();  //choices between Display file, Transformation, Exit
}//Dispaly file

/**************************<Display file>************************************/
void Interface()
{
clrscr();     //clear the screen
delay(d);    //delay with value (d) can change its value at define section
textcolor(WHITE);
gotoxy(43, 10); cprintf("4");//start of appearing HomeWork 4 in returning side
delay(d);//delay with value (d) can change its value at define section
gotoxy(43, 10); cprintf(" ");
gotoxy(41, 10); cprintf("K");
delay(d); //delay with value (d) can change its value at define section
gotoxy(41, 10); cprintf(" ");
gotoxy(40, 10); cprintf("R");
delay(d); //delay with value (d) can change its value at define section
gotoxy(40, 10); cprintf(" ");
gotoxy(39, 10); cprintf("O");
delay(d); //delay with value (d) can change its value at define section
gotoxy(39, 10); cprintf(" ");
gotoxy(38, 10); cprintf("W");
delay(d); //delay with value (d) can change its value at define section
gotoxy(38, 10); cprintf(" ");
gotoxy(37, 10); cprintf("E");
delay(d); //delay with value (d) can change its value at define section
gotoxy(39, 10); cprintf(" ");
gotoxy(38, 10); cprintf("M");
delay(d); //delay with value (d) can change its value at define section
gotoxy(38, 10); cprintf(" ");
gotoxy(37, 10); cprintf("O");
delay(d); //delay with value (d) can change its value at define section
gotoxy(37, 10); cprintf(" ");
gotoxy(36, 10); cprintf("H");
clrscr(); //delay with value (d) can change its value at define section
gotoxy(36, 10); cprintf(" ");
delay(d); //delay with value (d) can change its value at define section
textcolor(15);
gotoxy(36, 10); cprintf("H");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(36, 10); cprintf("H");
textcolor(15);
gotoxy(37, 10); cprintf("O");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(37, 10); cprintf("O");
textcolor(15);
gotoxy(38, 10); cprintf("M");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(38, 10); cprintf("M");
textcolor(15);
gotoxy(39, 10); cprintf("E");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(39, 10); cprintf("E");
textcolor(15);
gotoxy(40, 10); cprintf("W");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(40, 10); cprintf("W");
textcolor(15);
gotoxy(41, 10); cprintf("O");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(41, 10); cprintf("O");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(42, 10); cprintf("R");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(42, 10); cprintf("R");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(43, 10); cprintf("K");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(43, 10); cprintf("K");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
gotoxy(44, 10); cprintf(" ");
delay(d); //delay with value (d) can change its value at define section
textcolor(8);
textcolor(15);
gotoxy(45, 10); cprintf("4");
delay(d); //delay with value (d) can change its value at define section
textcolor(15);
gotoxy(45, 10); cprintf("4");            //end of HomeWork 4 appearing
sleep(1); //delay with value (2) can change its value

textcolor(15);
gotoxy(43, 13); cprintf("1");
delay(100);
gotoxy(43, 13); cprintf(" ");
gotoxy(41, 13); cprintf("T");
delay(100);
gotoxy(41, 13); cprintf(" ");
gotoxy(40, 13); cprintf("N");
delay(100);
gotoxy(40, 13); cprintf(" ");
gotoxy(39, 13); cprintf("I");
delay(100);
gotoxy(39, 13); cprintf(" ");
gotoxy(38, 13); cprintf("A");
delay(100);
gotoxy(38, 13); cprintf(" ");
gotoxy(37, 13); cprintf("P");
delay(100);
gotoxy(37, 13); cprintf(" ");
delay(100);
textcolor(15);
gotoxy(37, 13); cprintf("P");
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf("P");
textcolor(15);
gotoxy(38, 13); cprintf("A");
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf("A");
textcolor(15);
gotoxy(39, 13); cprintf("I");
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf("I");
textcolor(15);
gotoxy(40, 13); cprintf("N");
delay(100);
textcolor(8);
gotoxy(40, 13); cprintf("N");
textcolor(15);
gotoxy(41, 13); cprintf("T");
delay(100);
textcolor(8);
gotoxy(41, 13); cprintf("T");
textcolor(15);
gotoxy(43, 13); cprintf("1");
sleep(2);
INITIALIZE();
font();//to change Fonts and show the functions dealed with
Display(); //choices between Display file, Transformation, Exit
}
/*********************************<Font>************************************/
void font()
{
   delay(d+20);//delay with value (d) can change its value at define section
   int style, midx, midy;  //to change fonts
   int size = 1;
  midx = getmaxx() / 2;    //take the size of x of the screen divided by two
  midy = getmaxy() / 2;    //take the size of y of the screen divided by two

  settextjustify(CENTER_TEXT, CENTER_TEXT);//far text

   /* loop through the available text styles */
   for (style=DEFAULT_FONT; style<=GOTHIC_FONT+3; style++)
   {
      cleardevice(); //clear the device
      if (style == TRIPLEX_FONT)
	 size = 4;
  settextstyle(style, HORIZ_DIR, size);/* select the text style */
  outtextxy(midx, midy, fname[style]); /* output a message */
      getch();   //wait for a key
   }

  closegraph();    //close down the graphics system
  Display(); //choices between Display file, Transformation, Exit
}//font



