/* progname is clrname.c */
/* By R W Clement  11/20/86 */
/* & put into the public domain */
/*****************************************************/
/* This LOW RES program develops the threading and   */
/* treadling sequences for a 4-harness loom and      */
/* prints the drawdown.  It permits the choice of    */
/* individual warp colors and a weft color.          */
/* Note: It does not terminate properly and you may  */
/* have to go to medium res and then return to low   */
/* res to reset it for each new run.                 */
/*    Acknowledgement is made to Jim Luczak's "VDI   */
/* Sampler and C Primer" from Antic, for a fine      */
/*introduction to color programing the ST.           */
/*****************************************************/

#include "ctype.h"
#include "portab.h"
#include "stdio.h"
#include "osbind.h"
#include "gemlib.h"

short   buffer[5];
short   contrl[12], intin[128], ptsin[128], intout[128], ptsout[128];
short   rgb_in[3], pxy[4], dum1[2], dum2[6];
short   handle, i, gr_2, px, py;
short   prx = 65, pry = 190, tsz = 4, cell_w, cell_h, cell_h1;
short   newc[48] = {0,0,0,1000,1000,1000,1000,0,0,0,1000,0,0,0,1000,0,1000,1000,
                  1000,1000,0,1000,0,1000,714,714,714,428,428,428,1000,428,
                  428,428,1000,428,428,428,1000,428,1000,1000,1000,1000,428,
                  1000,428,1000};

static char    prompt[] = "PRESS ANY KEY TO CONTINUE";

double   num1;

main()
{
   short   l_intin[11], l_out[57], rgb_out[3];
   short   index;
   short   gr_1, gr_3, gr_4;
   short   oldc[48];

appl_init();

handle=graf_handle(&gr_1, &gr_2, &gr_3, &gr_4);
v_hide_c(handle);
v_clrwk(handle);

/*-------------------------OPEN WORKSTATION------------------------------*/

l_intin[0] = 1;
for(i=1; i<10; i++)
   l_intin[i]=1;
l_intin[10]=2;
v_opnvwk(l_intin, &handle, l_out);

/*------------------------SAVE OLD COLORS AND SET TO NEW VALUES----------*/

px=0;
for(i=0; i<16; i++)   {
   vq_color(handle, i, 1, rgb_out);
   oldc[px]=rgb_out[0]; ++px;
   oldc[px]=rgb_out[1]; ++px;
   oldc[px]=rgb_out[2]; ++px;
}
px=0;
for(i=0; i<16; i++)   {
   rgb_in[0]=newc[px]; px++;
   rgb_in[1]=newc[px]; px++;
   rgb_in[2]=newc[px]; px++;
   index=i;
   vs_color(handle, index, rgb_in);
}
vst_height(handle, gr_2, &dum1, &dum1, &dum1, &cell_h1);

/*----------------------CHECK TO SEE IF WE ARE IN MED REZ-----------------*/

if (l_out[13] < 5)   {
   vst_height(handle, 20, &dum1, &dum1, &cell_w, &dum1);
   vst_color(handle, 1);
   v_gtext(handle, 100+(cell_w * 5), 75, "Sorry this is a LOW RES demo");
   v_gtext(handle, 100, 100, "Switch to LOW RES and restart program");
   v_gtext(handle, 100+(cell_w * 14), 125, "THANK YOU");
   key_wait();
}
else

/*-----------------------------PROGRAM MODULE AREA-----------------------*/

{
intro();
warp();
name();
}

/*---------------------------------RESET COLORS-------------------------*/

px=0;
for(i=0; i<16; i++)   {
   rgb_in[0] = oldc[px]; px++;
   rgb_in[1] = oldc[px]; px++;
   rgb_in[2] = oldc[px]; px++;
   index=i;
   vs_color(handle, index, rgb_in);
   }

/*-------------------------------CLOSE WORKSTATION---------------------*/

v_clrwk(handle);
v_clsvwk(handle);
appl_exit();

}  /* end main */
/*------------------------------END MAIN--------------------------*/

/******************************KEY WAIT**********************************/
key_wait()
{
char  ev_k;
vst_height(handle,tsz,&dum1,&dum1,&dum1,&dum1);
vst_color(handle, GREEN);
v_gtext(handle,prx,pry,prompt);
ev_k = evnt_keybd();
vst_height(handle,gr_2,&dum1,&dum1,&dum1,&dum1);
v_clrwk(handle);
}
/*-------------END KEY WAIT----------*/

/**************************** INTRODUCTION SCREEN *****************************/

intro()
{
                           
static char   line1[] = "COLOR NAME";
static char   line2[] = "by R W CLEMENT";
static char   line3[] = "for the";
static char   line4[] = "ATARI";
static char   line5[] = "1040 ST";

v_clrwk(handle);
px=80;

/*---------------------------- DRAW BACKGROUND -------------------------------*/

for (i=5; i<100; i+=5)
{
vsl_color(handle, YELLOW);                            /* SET POLYLINE COLOR */
v_arc(handle, 0, 0, i, 2700, 0);                 /* DRAW ARC */
v_arc(handle, 0, 200, i, 0, 900);
v_arc(handle, 320, 200, i, 900, 200);
v_arc(handle, 320, 0, i, 1800, 2700);
}

vswr_mode(handle, 2);                        /* SET WRITE MODE TO TRANSPARENT */
vsf_interior(handle, 2);                     /* SET INTERIOR TYPE TO PATTERN */
vsf_style(handle, 13);                       /* SET INTERIOR FILL STYLE */
vsf_color(handle, MAGENTA);                  /* SET INTERIOR COLOR */
v_circle(handle, 160, 100, 110);             /* DRAW CIRCLE */

/*--------------------------- WRITE HEADER ON SCREEN ------------------------*/

vst_height(handle, 30, &dum1, &dum1, &dum1, &dum1);
vst_color(handle, CYAN);
vst_effects(handle, 4);                         /* SET TEXT EFFECTS TO SKEWED */
v_gtext(handle, px - 10, 50, line1);
v_gtext(handle, px - 35, 80, line2);
vst_height(handle, gr_2, &dum1, &dum1, &dum1, &dum1);
vst_color(handle, 1);
vst_effects(handle, 8);                     /* SET TEXT EFFECTS TO UNDERLINED */
v_gtext(handle, px + 45, 97, line3);
vst_effects(handle, 1);                     /* SET TEXT EFFECTS TO THICKENED */
vst_height(handle, 30, &dum1, &dum1, &dum1, &dum1);
vst_color(handle, LYELLOW);
v_gtext(handle, px + 40, 140, line4);
v_gtext(handle, px + 33, 170, line5);
vst_height(handle, tsz, &dum1, &dum1, &dum1, &dum1);
vst_effects(handle, 0);                     /* RESET TEXT EFFECTS TO NORMAL */
vswr_mode(handle, 1);                       /* RESET WRITE MODE TO REPLACE */

/*------------------------- WAIT FOR KEY PRESS -------------------------------*/

key_wait();

/*--------------------------- CLEAR SCREEN & RESET ---------------------------*/

rgb_in[0] = 1000;
rgb_in[1] = 1000;
rgb_in[2] = 1000;
vs_color(handle, 1, rgb_in);

v_clrwk(handle);
vst_height(handle, gr_2, &dum1, &dum1, &dum1, &dum1);

}

/************************ END INTRODUCTION SCREEN *****************************/

/*---------------------------WARP----------------------------------------*/

warp()
{
static char line6[]="0  1   2  3  4   5  6  7  8   9  10  11 12 13  14  15";
short    i, sx=0, sy=184, ax=15, ay=199;
char     colorindex[3],number[81];
short    color,warps;

for(i=0; i<16; i++)   {
   vswr_mode(handle,1);
   vsf_interior(handle,1);
   vsf_color(handle,i);
   pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
   vr_recfl(handle,pxy);
   sx+=20; ax+=20;
}
sx=0; sy=0; ax=3; ay=175;
vst_color(handle,GREEN);
vst_height(handle,4,&dum1,&dum1,&dum1,&dum1);
v_gtext(handle,1,180,line6);
do   {
v_gtext(handle,5,145,"what color warp? (0-15), Return.  Type 20 to quit");
vs_curaddress(handle,20,2);
color = atoi(gets(colorindex));
v_gtext(handle,5,152,"how many? (1-80), Return");
warps = atoi(gets(number));
vswr_mode(handle,1);
vsf_interior(handle,1);
for(i=0; i<warps; i++)   {
   vsf_color(handle,color);
   pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
   v_bar(handle,pxy);
   sx+=4; ax+=4;
   }
} while( color < 16 );
}

/*-------------------------NAME---------------------------------*/

name()
{
   int name[81],name2[81],name3[81],name4[81];
   get_name(name);      /* input name */
   letrthrd(name,name2);   /* sub thrd num for name letr */
   odd_even(name2,name3);  /* make sequence odd,even */
   ends(name3);         /* make start and finish odd,even */
   concat(name3);       /* repeat twice */
   lifts(name3,name4);
   drawdown(name3,name4);
}     /* end name() */

get_name(caps)
   int   caps[];
{
   vst_height(handle,4,&dum1,&dum1,&dum1,&dum1);
   vst_color(handle,GREEN);
v_gtext(handle,10,159,"type name in caps with no spaces (max 24)");
vs_curaddress(handle,23,2);
   gets(caps);
}
letrthrd(letrs,num)
   int   letrs[], num[];
{
   char  *ptr;
   int   *point;
   for(ptr=letrs, point=num; *ptr!='\0'; ptr++,point++)  {
      if(*ptr=='A'||*ptr=='E'||*ptr=='I'||*ptr=='M'||*ptr=='Q'||
      *ptr=='U'||*ptr=='Y')
         *point=1;
      else if(*ptr=='B'||*ptr=='F'||*ptr=='J'||*ptr=='N'||*ptr==
      'R'||*ptr=='V'||*ptr=='Z')
         *point=2;
      else if(*ptr=='C'||*ptr=='G'||*ptr=='K'||*ptr=='O'||*ptr==
      'S'||*ptr=='W')
         *point=3;
      else if(*ptr=='D'||*ptr=='H'||*ptr=='L'||*ptr=='P'||*ptr==
      'T'||*ptr=='X')
         *point=4;
      else printf("not a capital letter");
   }
}

odd_even(str2,str3)
   int   *str2,*str3;
{
   int   *ptr1, *ptr2, *ptr3;
   ptr1=str2;
   ptr2=ptr1+1;
   ptr3=str3;
   *(ptr3++)=*ptr1;
   for(; *ptr2!=0; ptr1++,ptr2++,ptr3++)  {
      switch (*ptr1-*ptr2) {
         case -1:
         case -3:
         case  1:
         case  3:
            *ptr3=*ptr2;
            break;
         case -2:
            *ptr3=*ptr1+1;
            *(++ptr3)=*ptr2;
            break;
         case  2:
            *ptr3=*ptr2+1;
            *(++ptr3)=*ptr2;
            break;
         case  0:
            if(*ptr2==4)
               *ptr3=*ptr2-1;
            else *ptr3=*ptr2+1;
            *(++ptr3)=*ptr2;
            break;
      }
   }
}

ends(str3)
   int   *str3;
{
   int   *ptr1,*ptr2;
   ptr1=str3;
   ptr2=str3;
      for(; *ptr2!=0; ptr2++)
         ;
   ptr2--;
      switch(*ptr1-*ptr2)  {
         case -1: case -3: case 1: case 3:
            break;
         case 2:
            *(ptr2+1)=*ptr2+1;
            break;
         case -2:
            *(ptr2+1)=*ptr2-1;
            break;
         case 0:
            if(*ptr2==4)
               *(ptr2+1)=*ptr2-1;
            else *(ptr2+1)=*ptr2+1;
      }
}

concat(str4)
   int   *str4;
{
   int   *ptr1,*ptr2,i,count;
   ptr1=str4;
   ptr2=str4;
      for(count=0; *ptr2!=0; count++,ptr2++)
         ;
      for(i=0; i<count*2 && i<40; i++,ptr1++,ptr2++)
         *ptr2=*ptr1;
}

lifts(str5,str6)
   int   *str5,*str6;
{
   int   *ptr1,*ptr2,*ptr3;
   ptr1=str5;
   ptr2=ptr1+1;
   ptr3=str6;
   for(; *ptr2!=0; ptr1++,ptr2++,ptr3++)
      switch((*ptr1) + (*ptr2))  {
         case 3:
            *ptr3=1;
            break;
         case 5:
            if(((*ptr1) * (*ptr2)) == 4)
               *ptr3=4;
            else *ptr3=2;
            break;
         case 7:
            *ptr3=3;
            break;
      }
}

drawdown(strt,strh)
   int   *strt,*strh;
{
   int   *ptrt,*ptrh;
   char  c,colorindex[3],ev_k;
   short sx=0, sy=0, ax=4, ay=3, color;
ptrt = strt;
ptrh = strh;
vst_height(handle,4,&dum1,&dum1,&dum1,&dum1);
vst_color(handle,GREEN);
v_gtext(handle,10,166,"weft color?, (0-15), Return");
color = atoi(gets(colorindex));
         for(; *ptrh!=0; ptrh++) {
            for(; *ptrt!=0; ptrt++)
               switch(*ptrt - *ptrh)   {
                  case 0:
                  case 1:
                  case-3:
               sx+=4; ax+=4;
                     break;

                  default:
   vswr_mode(handle,1);
   vsf_color(handle,color);
   pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
   vr_recfl(handle,pxy);
   sx+=4; ax+=4;
                     break;
               }
               ptrt=strt; sx=0; ax=4; sy+=3; ay+=3;
         }
ev_k = evnt_keybd();
c=getchar();
}



