/* progname is colorwv.c */
/* By R W Clement 11/20/86 */
/* & put into the public domain */
/********************************************************************/
/*  This LOW RES program prints the drawdown  for a 4-harness loom, */
/*  based on the input threading and treadling sequences.           */
/*  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();
weft();
}

/*---------------------------------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[] = "WEAVING";
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 + 15, 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 );
}

/*-------------------------WEFT---------------------------------*/

weft()
{
char     *thrdptr;
char     threads[81];
char     *liftptr;
char     lifts[81];
char     c,colorindex[3],ev_k;
short    sx=0,sy=0,ax=4,ay=3,color;
vst_height(handle,4,&dum1,&dum1,&dum1,&dum1);
vst_color(handle,GREEN);
v_gtext(handle,10,159,"weft color, (0-15), Return");
color = atoi(gets(colorindex));
v_gtext(handle,10,166,"Enter threading sequentially from left");
vs_curaddress(handle,20,2);
gets(threads);
v_gtext(handle,10,173,"Enter lifts sequentially");
vs_curaddress(handle,23,2);
gets(lifts);
for(liftptr=lifts;  *liftptr!=0; liftptr+=2)   {
   ;
   for(thrdptr=threads; *thrdptr!=0; thrdptr++)    {
if((*thrdptr == *liftptr) || (*thrdptr == *(liftptr+1)))   {
   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;
   }
else   {
   sx+=4; ax+=4;
   }
   }
sx=0; ax=4; sy+=3;ay+=3;
}
ev_k = evnt_keybd();
c=getchar();
}

