home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 064.lha / LineDrawer / linedrawer.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  6KB  |  222 lines

  1. /****************************************************************
  2.  * LineDrawer by John M. Olsen. V1.0 July 5, 1987
  3.  *
  4.  * John M. Olsen
  5.  * 1547 Jamestown Drive
  6.  * Salt Lake City, UT  84121-2051
  7.  *
  8.  * Network addresses:
  9.  * u-jmolse@ug.utah.edu  or  ...!{seismo,ihnp4}!utah-cs!utah-ug!u-jmolse
  10.  *
  11.  * This is public domain software.  Do whatever you want with it.
  12.  * Just don't complain to me when it breaks after a few hundred people
  13.  * have hacked on it. :^)
  14.  *
  15.  * This program takes a data file as a parameter and makes a line drawing
  16.  * based on the commands in the file.  The instruction format is listed
  17.  * below.  There should be two data files included.  One is a Mercator
  18.  * projection of the USA, and the other is a really short demo of how to use
  19.  * the color changing and line patterning commands.
  20.  *
  21.  * Manx Instructions:
  22.  * cc LineDrawer.c
  23.  * ln LineDrawer.o -lc
  24.  *
  25.  * It should (no guarantees) work fine with Lettuce C since I used only
  26.  * longs, and have (hopefully) everything type cast correctly.
  27.  ****************************************************************/
  28.  
  29. #include <exec/types.h>
  30. #include <graphics/gfxbase.h>
  31. #include <graphics/gfxmacros.h>
  32. #include <intuition/intuition.h>
  33. #include <stdio.h>
  34.  
  35. void *OpenLibrary();
  36. struct Window *OpenWindow(), *w;
  37. struct IntuiMessage *GetMsg(), *WaitPort(), *mesg;
  38. struct IntuitionBase *IntuitionBase;
  39. struct GfxBase *GfxBase;
  40.  
  41. struct NewWindow nw =
  42. {
  43.     0, 10, 320, 100, /* window corner and size */
  44.     -1, -1,
  45.     CLOSEWINDOW | NEWSIZE, /* messages I want */
  46.     ACTIVATE | NOCAREREFRESH | WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH
  47.     | WINDOWSIZING | GIMMEZEROZERO,
  48.     NULL, NULL, (UBYTE *) "LineDrawer by John M. Olsen V1.0",
  49.     NULL, NULL,
  50.     50,20,-1,-1,
  51.     WBENCHSCREEN
  52. };
  53.  
  54. main(argc,argv)
  55. int argc;
  56. char *argv[];
  57. {
  58.     char stuff[80];
  59.     FILE *temp;
  60.  
  61.     if(!argc)
  62.     {
  63.         exit(1); /* not run from the cli */
  64.     }
  65.     if(!(temp = fopen(argv[1],"r")))
  66.     {
  67.         printf("Usage: %s <datafile>\n\n",argv[0]);
  68.         puts("Valid instructions:");
  69.         puts("1 minx miny maxx maxy");
  70.         puts("2 x y   (draw to a point)");
  71.         puts("3 x y   (move to a point)");
  72.         puts("4 color (change foreground color)");
  73.         puts("5 color (change background color)");
  74.         puts("6 mode  (change drawmode)");
  75.         puts("Lines beginning with a letter are comments.");
  76.         puts("Comments may also follow instructions.");
  77.         exit(1); /* file does not exist. */
  78.     }
  79.     fclose(temp);
  80.     if(GfxBase = OpenLibrary("graphics.library",32L)) /* 1.1 is okay */
  81.     {
  82.         if(IntuitionBase = OpenLibrary("intuition.library",32L))
  83.         {
  84.             if(w = OpenWindow(&nw))
  85.             {
  86.                 while(1)
  87.                 {
  88.                     body(argc,argv);
  89.                     if(!mesg)
  90.                     { /* message not found in body */
  91.                         mesg = WaitPort(w->UserPort);
  92.                         mesg = GetMsg(w->UserPort);
  93.                     }
  94.                     if (mesg->Class == CLOSEWINDOW)
  95.                         break; /* die gracefully */
  96.                     ReplyMsg(mesg);
  97.                     mesg = NULL;
  98.                 }
  99.                 ReplyMsg(mesg);  /* reply after break */
  100.                 CloseWindow(w);
  101.             }
  102.             CloseLibrary(IntuitionBase);
  103.         }
  104.         CloseLibrary(GfxBase);
  105.     }
  106.     exit(0);
  107. }
  108.  
  109. /****************************************************************
  110.  * All of the setup work is done, so do the picture now.  Argv and argc have
  111.  * just been passed on to this routine for simplicity.
  112.  *
  113.  * Data file format:
  114.  * lines may have white space just about anywhere.  Each command line begins
  115.  * with a number followed by a number of parameters.  Any line not starting
  116.  * with an appropriate number (after white space, if any) is considered to
  117.  * be a comment.  Lines may also have a comment after the data.
  118.  *
  119.  * 1: minx miny maxx maxy.  These tell what range the coordinates fall into.
  120.  * 2: x y.  Draw a line from current position to x,y.
  121.  * 3: x y.  Move pen to position x,y.
  122.  * 4: color.  Foreground pen color.
  123.  * 5: color.  Background pen color.
  124.  * 6: pattern.  Line pattern.  This is an unsigned word 0 to 65536 (0 to $ffff)
  125.  * 7: mode.  This can change the way 4 to 6 appear by using only foreground,
  126.  *           or complimenting colors, etc.  It can do some strange stuff.
  127.  *           The current (unguaranteed values) usable here are:
  128.  *           JAM1:       0
  129.  *           JAM2:       1
  130.  *           COMPLIMENT: 2
  131.  *           INVERSVID:  4
  132.  *           You can add these, so 3 = JAM2 and COMPLIMENT.
  133.  ****************************************************************/
  134.  
  135. body(argc,argv)
  136. int argc;
  137. char *argv[];
  138. {
  139.     FILE *input;
  140.     long minx, miny, maxx, maxy, cmd, x, y;
  141.     struct RastPort *r;
  142.     char str[100];
  143.  
  144.     r = w->RPort;
  145.     SetRast(r,0L);
  146.     SetAPen(r,1L);
  147.     SetBPen(r,0L);
  148.     if (input = fopen(argv[1],"r"))
  149.     {
  150.         while((cmd = fgetc(input)) != EOF)
  151.         {
  152.             mesg = GetMsg(w->UserPort);
  153.             if(mesg) /* break out if a message recvd */
  154.                 break;
  155.             switch(cmd)
  156.             {
  157.                 case '1': /* min and max x,y */
  158.                 {
  159.                     fscanf(input,"%ld%ld%ld%ld",
  160.                     &minx,&miny,&maxx,&maxy);
  161.                     break;
  162.                 }
  163.                 case '2': /* draw to */
  164.                 {
  165.                     fscanf(input,"%ld%ld",&x,&y);
  166.                     Draw(r,(((x-minx)
  167.                     *(w->GZZWidth))/(maxx - minx)),
  168.                     (((y-miny)
  169.                     *(w->GZZHeight))/(maxy-miny)));
  170.                     break;
  171.                 }
  172.                 case '3': /* move to */
  173.                 {
  174.                     fscanf(input,"%ld%ld",&x,&y);
  175.                     Move(r,(((x-minx)
  176.                     *(w->GZZWidth))/(maxx - minx)),
  177.                     (((y-miny)
  178.                     *(w->GZZHeight))/(maxy-miny)));
  179.                     break;
  180.                 }
  181.                 case '4': /* foreground color */
  182.                 {
  183.                     fscanf(input,"%ld",&x);
  184.                     SetAPen(r, x);
  185.                     break;
  186.                 }
  187.                 case '5': /* background color */
  188.                 {
  189.                     fscanf(input,"%ld",&x);
  190.                     SetBPen(r,x);
  191.                     break;
  192.                 }
  193.                 case '6': /* draw pattern */
  194.                 {
  195.                     fscanf(input,"%ld",&x);
  196.                     SetDrPt(r,x);
  197.                     break;
  198.                 }
  199.                 case '7': /* draw mode  */
  200.                 {
  201.                     fscanf(input,"%ld",&x);
  202.                     SetDrMd(r,x);
  203.                     break;
  204.                 }
  205.                 case ' ': /* leading white space */
  206.                 case '\n':
  207.                 case '\t':
  208.                 case '\r':
  209.                 {
  210.                     break;
  211.                 }
  212.                 default: /* must be a comment */
  213.                 {
  214.                     fgets(str,100,input);
  215.                     break;
  216.                 }
  217.             }
  218.         }
  219.         fclose(input);
  220.     }
  221. }
  222.