home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cuj9301.zip / 1101029A < prev    next >
Text File  |  1992-11-06  |  4KB  |  96 lines

  1. #include <stdio.h>
  2.  
  3. #define PENUP()       pen_up=1
  4. #define PENDOWN()     pen_up=0
  5. #define SETCOLOR(x)   color=x
  6. #define SETWIDTH(x)   width=x
  7. #define BLACK         0
  8. #define RED           1
  9. #define GREEN         2
  10. #define BLUE          3
  11. #define FAT           0
  12. #define MEDIUM        1
  13. #define THIN          2
  14.  
  15. #define TRUE          1
  16. #define FALSE         0
  17.  
  18. int pen_up;
  19. int color;
  20. int width;
  21. int x1, x2, y1, y2;
  22. int gotx1, gotx2, goty1, goty2;
  23.  
  24. /* Function Draw - demonstrates use of the Simple Drawing Language
  25. Does not actually draw lines, simple parses the first parameter and
  26. prints commands on screen */
  27.  
  28. Draw (char *str, int val)
  29. {
  30. int *pval=&val;      //points to optional parameters
  31.                      //restore defaults
  32.     gotx1=goty1=gotx2=goty2=FALSE;
  33.     x1=x2=y1=y2=0;
  34.     pen_up=1;
  35.     color = BLACK;
  36.     width=MEDIUM;
  37.                      //loop to parse the command string
  38.     while (TRUE)
  39.     {
  40.         switch (*str++)
  41.         {
  42.             case 'p': PENUP(); break;      //pick pen up
  43.             case 'P': PENDOWN(); break;    //put pen down
  44.             case 'c':                      //set color
  45.                 switch (*str++)
  46.                 {
  47.                     case 'R':  SETCOLOR(RED); printf ("Color set to red\n");break;
  48.                     case 'G':  SETCOLOR(GREEN); printf ("Color set to green\n");break;
  49.                     case 'B':  SETCOLOR(BLUE); printf ("Color set to blue\n");break;
  50.                     case 'b':  SETCOLOR(BLACK); printf ("Color set to black\n");break;
  51.                     default:   return -1;
  52.                 } //End switch (on character)
  53.                 break;
  54.             case 'w':                      //set width
  55.                 switch (*str++)
  56.                 {
  57.                     case 'T':  SETWIDTH(THIN); printf ("Width set to thin\n");break;
  58.                     case 'M':  SETWIDTH(MEDIUM); printf ("Width set to medium\n");break;
  59.                     case 'F':  SETWIDTH(FAT); printf ("Width set to fat\n");break;
  60.                     default:   return -1;
  61.                 } //End switch (on character)
  62.                 break;
  63.             case '%':                      //get next optional parameter
  64.                 switch (*str++)
  65.                 {
  66.                     case 'x':              //set x coordinate
  67.                         if (gotx2) {x1=x2; x2=*pval++; break;}
  68.                         if (!gotx1) {x1=*pval++; gotx1=TRUE; break;}
  69.                         if (!gotx2) {x2=*pval++; gotx2=TRUE; break;}
  70.                         break;
  71.                     case 'y':              //set y coordinate
  72.                         if (goty2) {y1=y2; y2=*pval++; break;}
  73.                         if (!goty1) {y1=*pval++; goty1=TRUE; break;}
  74.                         if (!goty2) {y2=*pval++; goty2=TRUE; break;}
  75.                         break;
  76.                     default:   return -1;
  77.                 } //End switch (token)
  78.                     //do we have enough info to draw the line?
  79.                 if (gotx2 && goty2 && !pen_up)
  80.                 {
  81.                     printf ("Drawing line <%d,%d>-<%d,%d>\n", x1,y1,x2,y2);
  82.                     x1 = x2;
  83.                     y1 = y2;
  84.                     goty2 = FALSE;
  85.                     gotx2 = FALSE;
  86.                 } //end if (got both coordinates - draw line)
  87.                 break;
  88.             case '\0':               //end of command string
  89.                 return 0;
  90.             default:   return -1;
  91.         } //End switch (on character)
  92.     } //End while (TRUE)
  93.     return 0;
  94. } //End function (Draw)
  95. WRAP_EOF
  96.