/* progname is clrinkle.c */
/* By R W Clement 11/20/86 */
/* & put into the public domain */
/********************************************************************/
/*  This LOW RES program prints the drawdown  for an inkle loom,    */
/*  based on the input threading.                                   */
/*  It permits the choice of three warp colors.                     */
/*  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.                                                          */
/*     sample top row is XXXOOXXXXXOOOOOOXXXXXOOXXX                 */
/*     sample bottom row XXOOOOXXXOOOZZOOOXXXOOOOXX                 */
/* X represents the dark color, O (capital O) the medium, and Z the */
/* light color.  Input the rows, when prompted, using X,O,& Z for   */
/* colors.  Ignore all spaces.  Add appropriate character at end of */
/* the shorter row.                                                 */
/********************************************************************/

#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";

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();
}

/*---------------------------------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[] = "INKLE WEAVE";
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    sx=0, sy=184, ax=15, ay=199, D, M, L, i, j, k,colorindex[3];
char     toprow[50], botrow[50], *tptr, *bptr;

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;
}
vst_color(handle,GREEN);
vst_height(handle,4,&dum1,&dum1,&dum1,&dum1);
v_gtext(handle,1,180,line6);

v_gtext(handle,5,145,"dark warp color? (0-15)   Return");
vs_curaddress(handle,20,2);
D=atoi(gets(colorindex));
v_gtext(handle,5,152,"medium warp color? (0-15)   Return");
vs_curaddress(handle,20,5);
M=atoi(gets(colorindex));
v_gtext(handle,5,159,"light warp color? (0-15)   Return");
vs_curaddress(handle,20,8);
L=atoi(gets(colorindex));

sx=0; sy=0; ax=4; ay=3;
v_gtext(handle,5,166,"type in top row,   Return");
vs_curaddress(handle,19,2);
gets(toprow);
v_gtext(handle,5,173,"type in bottom row,   Return");
vs_curaddress(handle,22,2);
gets(botrow);

for(i=0; i<6; i++)   {
   for(j=0; j<2; j++)   {
      for(tptr=toprow; *tptr!=0; tptr++)   {
         if(*tptr=='X')   {
            vswr_mode(handle,1);
            vsf_color(handle,D);
            pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
            vr_recfl(handle,pxy);
            sx+=4; ax+=4;
            }
         else if(*tptr=='O')   {
            vswr_mode(handle,1);
            vsf_color(handle,M);
            pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
            vr_recfl(handle,pxy);
            sx+=4; ax+=4;
            }
         else if(*tptr=='Z')   {
            vswr_mode(handle,1);
            vsf_color(handle,L);
            pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
            vr_recfl(handle,pxy);
            sx+=4; ax+=4;
            }
         }
      sx=0; ax=4; sy+=3; ay+=3;
      }
      for(k=0; k<2; k++)   {
         for(bptr=botrow; *bptr!=0; bptr++)   {
            if(*bptr=='X')   {
               vswr_mode(handle,1);
               vsf_color(handle,D);
               pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
               vr_recfl(handle,pxy);
               sx+=4; ax+=4;
               }
            else if(*bptr=='O')   {
               vswr_mode(handle,1);
               vsf_color(handle,M);
               pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
               vr_recfl(handle,pxy);
               sx+=4; ax+=4;
               }
            else if(*bptr=='Z')   {
               vswr_mode(handle,1);
               vsf_color(handle,L);
               pxy[0]=sx; pxy[1]=sy; pxy[2]=ax; pxy[3]=ay;
               vr_recfl(handle,pxy);
               sx+=4; ax+=4;
                 }
            }
            sx=0; ax=4; sy+=3; ay+=3;
            }
         }
   key_wait();
}




