/*  (c) 1990 S.Hawtin.
  Permission is granted to copy this file provided
   1) It is not used for commercial gain
   2) This notice is included in all copies
   3) Altered copies are marked as such

  No liability is accepted for the contents of the file.

*/

/* Window interface stuff for gnu-go */

#include <stdio.h>
#include <intuition/intuition.h>
#include <graphics/rastport.h>

#define BOX_LEFT 23
#define BOX_TOP  23
#define BOX_WIDTH 15
#define BOX_HEIGHT 7
#define AVOID_RIGHT 35
#define AVOID_BOTTOM 15
#define ELIPSE_WIDTH  (BOX_WIDTH/3)
#define ELIPSE_HEIGHT (BOX_HEIGHT/3)

struct NewWindow init_win;
struct Window *mywin;
char   area_buf[100];

/* Set up the console if called from WB */
char *_WBConsole="newcon:20/20/500/150/GNU Go";

extern struct Window *OpenWindow();

StartGraphics()
   {/* Open up a window to output the graphics into */
    int i;
    /* Load up the libraries */
    StartLow();
    /* Set up the window */
    init_win.LeftEdge = 280;
    init_win.TopEdge  = 10;
    init_win.Width    = BOX_LEFT + 19*BOX_WIDTH + AVOID_RIGHT;
    init_win.Height   = BOX_TOP  + 19*BOX_HEIGHT + AVOID_BOTTOM;
    /* Window cannot resize so just fix it */
    init_win.MaxWidth = init_win.Width;
    init_win.MaxHeight= init_win.Height;
    init_win.MinWidth = init_win.Width;
    init_win.MinHeight= init_win.Height;
    /* Workbench, so we have four colours to pick from */
    init_win.DetailPen= 2;
    init_win.BlockPen = 1;
    init_win.Title    = "GNU Go";
    init_win.Type     = 1;	/* Workbench Window */
    init_win.Flags    = WINDOWDRAG | WINDOWDEPTH; /* | WINDOWCLOSE; */
    /* Now try and make it */
    mywin = OpenWindow(&init_win);
    if(mywin==NULL)
       {printf("Failed to open window\n");
	exit(10);
        }
    SetAPen(mywin->RPort,3);
    for(i=0;i<19;i++)
       {char temp;
        char num[3];
        temp = 'A'+(i<8?i:i+1);
        Move(mywin->RPort,i*BOX_WIDTH+BOX_LEFT+3,20);
        Text(mywin->RPort,&temp,1);
        Move(mywin->RPort,i*BOX_WIDTH+BOX_LEFT+3,20*BOX_HEIGHT+BOX_TOP);
        Text(mywin->RPort,&temp,1);
        sprintf(num,"%2d",19-i);
        Move(mywin->RPort,5,BOX_TOP+i*BOX_HEIGHT+7);
        Text(mywin->RPort,&num[0],2);
        Move(mywin->RPort,19*BOX_WIDTH+BOX_LEFT,BOX_TOP+i*BOX_HEIGHT+7);
        Text(mywin->RPort,&num[0],2);
        }
    }

StopGraphics()
   {/* Close down the graphics window */
    if(mywin)
        CloseWindow(mywin);
    /* Shut down the libraries */
    EndLow();
    }
    
AmPutPiece(y,x,colour)
    int y,x,colour;
   {/* Draw a piece on the board, could be empty */
    int real_x,real_y;

    real_x = BOX_LEFT + x * BOX_WIDTH;
    real_y = BOX_TOP  + y * BOX_HEIGHT;

    /* Clear off square */
    SetAPen(mywin->RPort,0);
    RectFill(mywin->RPort,real_x,real_y,real_x+BOX_WIDTH,real_y+BOX_HEIGHT);
    /* Draw the board */
    SetAPen(mywin->RPort,3);

    if(x==0)
        Move(mywin->RPort,real_x +BOX_WIDTH/2,real_y+BOX_HEIGHT/2);
      else
        Move(mywin->RPort,real_x,real_y+BOX_HEIGHT/2);

    if(x==18)
        Draw(mywin->RPort,real_x+BOX_WIDTH/2,real_y+BOX_HEIGHT/2);
      else
        Draw(mywin->RPort,real_x+BOX_WIDTH,real_y+BOX_HEIGHT/2);

    if(y==0)
        Move(mywin->RPort,real_x +BOX_WIDTH/2,real_y+BOX_HEIGHT/2);
      else
        Move(mywin->RPort,real_x +BOX_WIDTH/2,real_y);

    if(y==18)
        Draw(mywin->RPort,real_x+BOX_WIDTH/2,real_y+BOX_HEIGHT/2);
      else
        Draw(mywin->RPort,real_x+BOX_WIDTH/2,real_y+BOX_HEIGHT);

    if((x==3 || x==9 || x==15) && (y==3 || y==9 || y==15))
       {Move(mywin->RPort,real_x+BOX_WIDTH/4,real_y+BOX_HEIGHT/4);
        Draw(mywin->RPort,real_x+(3*BOX_WIDTH)/4,real_y+(3*BOX_HEIGHT)/4);
        Move(mywin->RPort,real_x+(3*BOX_WIDTH)/4,real_y+BOX_HEIGHT/4);
        Draw(mywin->RPort,real_x+BOX_WIDTH/4,real_y+(3*BOX_HEIGHT)/4);
        }

    if(colour!=0)
       {
	SetAPen(mywin->RPort,colour);
        RectFill(mywin->RPort,real_x+BOX_WIDTH/4,real_y+BOX_HEIGHT/4,
                    real_x+(3*BOX_WIDTH)/4,real_y+(3*BOX_HEIGHT)/4);
        DrawEllipse(mywin->RPort,real_x+BOX_WIDTH/2,real_y+BOX_HEIGHT/2,
			ELIPSE_WIDTH,ELIPSE_HEIGHT);
#ifdef DONT
        /* Should fill in piece, but this code does't work */
        InitArea(mywin->RPort->AreaInfo,&area_buf[0],20);
        AreaEllipse(mywin->RPort,real_x+BOX_WIDTH/2,real_y+BOX_HEIGHT/2,
			ELIPSE_WIDTH,ELIPSE_HEIGHT);
        AreaEnd(mywin->RPort);
#endif
        }
    }
