
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <k:\pruebas\mouse15\mouse.h>

int huge detectEGA(void);
void main(){
   int gdriver = DETECT, gmode, errorcode;

   gdriver = installuserdriver("egavga",detectEGA);
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
   errorcode = graphresult();
   if (errorcode != grOk){
      printf("Error al iniciar el modo gr fico: %s\n", grapherrormsg(errorcode));
      printf("Pulse cualquier tecla para finalizar...");
      getch();
      exit(1);
   }

   MouseInit();
   MShowCursor();
   setcolor(1);
   rectangle(-1,-1,640,480);setfillstyle(9,1);floodfill(2,2,1);
   setcolor(15);
   rectangle(288,88,612,412);rectangle(290,90,610,410);
   int i=0;
   for (i=1;i<9;i++)
     line(290+(i*40),90,290+(i*40),410);
   for (i=1;i<9;i++)
     line(290,90+(i*40),610,90+(i*40));
   for (int y=90,con=1,con2=1;y<410;y=y+40,con2++)
     for (int x=290;x<610;x=x+40,con++){
       if (con%2==0 && con2%2!=0)
	 setfillstyle(1,6);
       else
	 if (con%2!=0 && con2%2==0)
	   setfillstyle(1,6);
	 else
	 setfillstyle(1,14);
       floodfill(x+1,y+1,15);
     }
   for (y=110,con=1,con2=1;y<230;y=y+40,con2++)
     for (i=310;i<610;i=i+40,con++)
       if ((con%2==0 && con2%2!=0) || (con%2!=0 && con2%2==0)){
	 setcolor(0);setfillstyle(1,0);circle(i,y,10);floodfill(i,y,0);
       }
   for (y=310,con=1,con2=6;y<410;y=y+40,con2++)
     for (i=310;i<610;i=i+40,con++)
       if ((con%2==0 && con2%2!=0) || (con%2!=0 && con2%2==0)){
	   setcolor(15);setfillstyle(1,15);circle(i,y,10);floodfill(i,y,15);
       }

  int sw=0,cursor=0,col=0,fil=0,a=0,color=6;
  col=370;fil=210;setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
  rectangle(col+2,fil+2,col+38,fil+38);rectangle(col+3,fil+3,col+37,fil+37);

  while(sw==0){
    cursor=getche();
    if (cursor!=13){
      setcolor(color);rectangle(col+1,fil+1,col+39,fil+39);
      rectangle(col+2,fil+2,col+38,fil+38);rectangle(col+3,fil+3,col+37,fil+37);
    }
    if(cursor==0){
      a=getch();
      switch(a){
	case 72:
	  if (col>289 && col<571 && fil>89 && fil<371){
	    fil=fil-40;
	    if (fil<89)
	      fil=fil+40;
	    color=getpixel(col+1,fil+1);
	    setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
	    rectangle(col+2,fil+2,col+38,fil+38);
	    rectangle(col+3,fil+3,col+37,fil+37);
	  }
	  break;
	 case 80:
	  if (col>289 && col<571 && fil>89 && fil<371){
	    fil=fil+40;
	    if (fil>371)
	      fil=fil-40;
	    color=getpixel(col+1,fil+1);
	    setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
	    rectangle(col+2,fil+2,col+38,fil+38);
	    rectangle(col+3,fil+3,col+37,fil+37);
	  }
	  break;
	 case 77:
	  if (col>289 && col<571 && fil>89 && fil<371){
	    col=col+40;
	    if (col>571)
	      col=col-40;
	    color=getpixel(col+1,fil+1);
	    setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
	    rectangle(col+2,fil+2,col+38,fil+38);
	    rectangle(col+3,fil+3,col+37,fil+37);
	  }
	  break;
	 case 75:
	  if (col>289 && col<571 && fil>89 && fil<371){
	    col=col-40;
	    if (col<289)
	      col=col+40;
	    color=getpixel(col+1,fil+1);
	    setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
	    rectangle(col+2,fil+2,col+38,fil+38);
	    rectangle(col+3,fil+3,col+37,fil+37);
	  }
	  break;
      }
    }
    else
      if (cursor==13){
	int color2=0;
	color2=getpixel(col+20,fil+20);
	setcolor(color);setfillstyle(1,color);floodfill(col+20,fil+20,color);
	circle(col+20,fil+20,color);
	setcolor(8);setfillstyle(1,8);circle(col+22,fil+22,10);
	floodfill(col+22,fil+22,8);
	setcolor(color2);setfillstyle(1,color2);circle(col+18,fil+18,10);
	floodfill(col+18,fil+18,color2);
	col=370;fil=210;setcolor(13);rectangle(col+1,fil+1,col+39,fil+39);
	rectangle(col+2,fil+2,col+38,fil+38);
	rectangle(col+3,fil+3,col+37,fil+37);
      }
      else
	sw=1;
  }
   getch();
   MouseClose();
   closegraph();
}
/* detects EGA or VGA cards */
int huge detectEGA(void)
{
   int driver, mode, sugmode = 0;
   sugmode++;

   detectgraph(&driver, &mode);
   if ((driver == EGA) || (driver == VGA))
      /* return suggested video mode number */
      return 4;
   else
      /* return an error code */
      return grError;
}
