home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff304.lzh / Lines / lines.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  8KB  |  374 lines

  1. /*
  2.  * lines.c : Line pattern generator experimental interface
  3.  *
  4.  * this stuf adapted from mackie.c on fish disk 131.
  5.  *  by Joel Swank 11-13-88
  6.  *
  7.  *
  8.  *
  9.  *
  10.  */
  11.  
  12. /*
  13.  *   The maximum number of lines on the screen at once.
  14.  */
  15. #define MAXLINES (100)
  16.  
  17. #define XSIZE 670
  18. #define YSIZE 440
  19. #define MAXHELP 20
  20.  
  21. #define INTUITION_REV 1L
  22.  
  23. /*
  24.  *   The global variables.
  25.  */
  26. struct GfxBase    *GfxBase;
  27. struct RastPort *rastport ;
  28. int screenheight, screenwidth ;
  29. struct Screen *scr, *OpenScreen() ;
  30. struct Window    *Wind = NULL, *OpenWindow() ;
  31. struct MsgPort    *CreatePort() ;
  32. struct IntuitionBase    *IntuitionBase ;
  33. struct IntuiMessage    *msg, *GetMsg();
  34. struct Library *OpenLibrary();
  35.  
  36. /*
  37.  * definition of the main window
  38.  */
  39.  
  40. static struct NewWindow    New_Window = {
  41.     0, 2,            /* Take all except the */
  42.     XSIZE, YSIZE,    /* top two lines       */
  43.     -1, -1,            /* Default pens */
  44.     CLOSEWINDOW+VANILLAKEY,    /* Inputs acceppeted */
  45.     WINDOWCLOSE        /* Fairly standard window */
  46.     | BACKDROP | SIMPLE_REFRESH | BORDERLESS ,
  47.     NULL,            /* no gadgets */
  48.     (struct Image *) NULL,
  49.     (UBYTE *) "h for Help",    /* title */
  50.     (struct Screen *) NULL,        /* filled at startup */
  51.     (struct BitMap *) NULL,
  52.     0, 0, 0, 0,        /* no change sizes, doesn't matter */
  53.     CUSTOMSCREEN
  54.     } ;
  55.  
  56.  
  57.  
  58.  
  59. static struct NewScreen newscreen = {
  60.    0, 0,
  61.    XSIZE, YSIZE+2,
  62.    1,
  63.    0, 1,
  64.    HIRES | LACE | SCREENQUIET,
  65.    CUSTOMSCREEN,
  66.    NULL,
  67.    NULL,
  68.    NULL,
  69.    NULL } ;
  70.  
  71. char *HelpText[40] = {
  72. "    Lines - line pattern generator",
  73. "       by Joel Swank 11/13/88",
  74. " ",
  75. "   Control from keyboard as follows:",
  76. " ",
  77. " space   Start/Stop generation",
  78. " o       Do one generation",
  79. " f       Freeze Thaw color rotation",
  80. " h       Help Screen",
  81. " x,q     Exit Program",
  82. " Escape  Exit Program",
  83. " ",
  84. "    Return to continue",
  85. NULL };
  86.  
  87.  
  88. /*
  89.  *   MainLine
  90.  */
  91. main() {
  92.    long i ;
  93.    USHORT keycode, stop, freeze;
  94.  
  95.    screenheight = YSIZE;
  96.    screenwidth = XSIZE;
  97.  
  98.    if ((IntuitionBase = (struct IntuitionBase *)
  99.        OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  100.       done(21);
  101.  
  102.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  103.    if (GfxBase == NULL) done(22);
  104.  
  105.    if ( (scr = OpenScreen(&newscreen)) == NULL)  done(26);
  106.    New_Window.Screen = scr;
  107.  
  108.    if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  109.       done(25);
  110.    rastport = Wind->RPort;
  111.  
  112.    SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L) ;
  113.    clr_grf();
  114.  
  115.    startlines() ;
  116.    for (i=0; i<MAXLINES; i++) {
  117.       advancelines() ;
  118.       drawnew() ;
  119.    }
  120.    colors() ;
  121.  
  122.    stop = FALSE;
  123.    freeze = FALSE;
  124.    while (1) {
  125.  
  126.     while(msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) {
  127.         switch(msg->Class) {
  128.             case VANILLAKEY:
  129.                 keycode = msg->Code;
  130.                 ReplyMsg(msg);
  131.                 switch (keycode)
  132.                     {
  133.                     case ' ':        /* start/stop */
  134.                       if (stop == TRUE) stop = FALSE;
  135.                       else stop = TRUE;
  136.                       break;
  137.                     case 'h':        /* Help */
  138.                       /* wait for this message */
  139.                       if (!stop) Wait( 1L << Wind->UserPort->mp_SigBit);
  140.                       do_help();
  141.                          /* wait on next mesg */
  142.                       Wait( 1L << Wind->UserPort->mp_SigBit);
  143.                       clr_grf();
  144.                       break;
  145.                     case 'o':        /* one iteration */
  146.                       stop = TRUE;
  147.                       goto once;
  148.                     case 'f':        /* freeze/thaw color changing */
  149.                       if (freeze == TRUE) freeze = FALSE;
  150.                       else freeze = TRUE;
  151.                       break;
  152.                     case 'q':        /* Exit */
  153.                     case 'x':
  154.                     case '\033':
  155.                       done(0);
  156.                       break;
  157.                     }
  158.                 continue;
  159.             case CLOSEWINDOW:
  160.                 ReplyMsg(msg);
  161.                 done(0);
  162.             }
  163.             ReplyMsg(msg);
  164.         }
  165.       if (stop == TRUE) 
  166.           {
  167.         Wait( 1L << Wind->UserPort->mp_SigBit);    /* wait on mesg */
  168.         continue;
  169.         }
  170.  
  171.    once:
  172.       eraseold() ;
  173.       advancelines() ;
  174.       drawnew() ;
  175.       eraseold() ;
  176.       advancelines() ;
  177.       drawnew() ;
  178.       eraseold() ;
  179.       advancelines() ;
  180.       drawnew() ;
  181.       eraseold() ;
  182.       advancelines() ;
  183.       drawnew() ;
  184.       eraseold() ;
  185.       advancelines() ;
  186.       drawnew() ;
  187.       eraseold() ;
  188.       advancelines() ;
  189.       drawnew() ;
  190.       if (!freeze) colors() ;
  191.    }
  192. }
  193. /*
  194.  * End of MainLine 
  195.  */
  196.  
  197.  
  198. /*
  199.  * done - just clean up that which is open, and then leave.
  200.  */
  201. done(how)
  202. int how;
  203.     {
  204.     if (Wind) CloseWindow(Wind) ;
  205.     if (scr) CloseScreen(scr) ;
  206.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  207.     if (GfxBase) CloseLibrary(GfxBase) ;
  208.  
  209.     OpenWorkBench() ;        /* As requested */
  210.     exit(how) ;
  211.     }
  212.  
  213. /*
  214.  *   This routine returns a random value from 0 to n-1.
  215.  */
  216. int randm(i)
  217. int i ;
  218. {
  219.    static long seed ;
  220.    long rval ;
  221.  
  222.    if (seed == 0)
  223.       seed = 323214521 + scr->MouseX +
  224.               scr->MouseY ;
  225.    seed = seed * 123213 + 121 ;
  226.    rval = (seed >> 5) & 65535 ;
  227.    return ((i * rval) >> 16) ;
  228. }
  229. /*
  230.  *   This routine sets x and y values to a random number.
  231.  */
  232. static long x, y ;
  233. randomxy() {
  234.    x = randm(screenwidth) ;
  235.    y = randm(screenheight) ;
  236. }
  237. /*
  238.  *   Main routines are always fun.
  239.  */
  240. short x1store[MAXLINES], y1store[MAXLINES] ;
  241. short x2store[MAXLINES], y2store[MAXLINES] ;
  242. short ptr ;
  243. short dx1, dy1, dx2, dy2 ;
  244. short ox1, oy1, ox2, oy2 ;
  245. short nx1, ny1, nx2, ny2 ;
  246. short dr, dg, db ;
  247. short or, og, ob ;
  248. short nr, ng, nb ;
  249. /*
  250.  *   Initialize things for the first lines.
  251.  */
  252. startlines() {
  253.    ptr = 0 ;
  254.    if (dx1 == 0) {
  255.       ox1 = randm(screenwidth) ;
  256.       ox2 = randm(screenwidth) ;
  257.       oy1 = randm(screenheight) ;
  258.       oy2 = randm(screenheight) ;
  259.       dx1 = 3 ;
  260.       dx2 = 4 ;
  261.       dy1 = 1 ;
  262.       dy2 = 6 ;
  263.       nr = 53 ;
  264.       ng = 33 ;
  265.       nb = 35 ;
  266.       dr = -3 ;
  267.       dg = 5 ;
  268.       db = 7 ;
  269.    }
  270.    SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L) ;
  271.    SetRGB4(&(scr->ViewPort), 1L, (long)(nr >> 3),
  272.                                  (long)(ng >> 3), (long)(nb >> 3)) ;
  273. }
  274. /*
  275.  *   Advance the number by the delta, and check the boundaries.
  276.  */
  277. adv(o, d, n, w)
  278. short *o, *d, *n ;
  279. long w ;
  280. {
  281.    *n = *o + *d ;
  282.    if (*n < 0) {
  283.       *n = 0 ;
  284.       *d = randm(6) + 1 ;
  285.    } else if (*n >= w) {
  286.       *n = w - 1 ;
  287.       *d = - randm(6) - 1 ;
  288.    }
  289. }
  290. /*
  291.  *   Advance the two points which make up the lines.
  292.  */
  293. advancelines() {
  294.    adv(&ox1, &dx1, &nx1, screenwidth) ;
  295.    adv(&ox2, &dx2, &nx2, screenwidth) ;
  296.    adv(&oy1, &dy1, &ny1, screenheight) ;
  297.    adv(&oy2, &dy2, &ny2, screenheight) ;
  298. }
  299. /*
  300.  *   Draw a new set of lines.
  301.  */
  302. drawnew() {
  303.    x1store[ptr] = ox1 = nx1 ;
  304.    x2store[ptr] = ox2 = nx2 ;
  305.    y1store[ptr] = oy1 = ny1 ;
  306.    y2store[ptr] = oy2 = ny2 ;
  307.    Move(rastport, (long)ox1, (long)oy1) ;
  308.    Draw(rastport, (long)ox2, (long)oy2) ;
  309.    Draw(rastport, (long)(screenwidth-ox1-1), (long)(screenheight-oy1-1)) ;
  310.    Draw(rastport, (long)(screenwidth-ox2-1), (long)(screenheight-oy2-1)) ;
  311.    Draw(rastport, (long)ox1, (long)oy1) ;
  312.    ptr++ ;
  313.    if (ptr == MAXLINES)
  314.       ptr = 0 ;
  315. }
  316. /*
  317.  *   Erase the old line.
  318.  */
  319. eraseold() {
  320.    short oldpen ;
  321.  
  322.    oldpen = rastport->FgPen ;
  323.    SetAPen(rastport, 0L) ;
  324.    Move(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
  325.    Draw(rastport, (long)x2store[ptr], (long)y2store[ptr]) ;
  326.    Draw(rastport, (long)(screenwidth-x1store[ptr]-1),
  327.                   (long)(screenheight-y1store[ptr]-1)) ;
  328.    Draw(rastport, (long)(screenwidth-x2store[ptr]-1), 
  329.                   (long)(screenheight-y2store[ptr]-1)) ;
  330.    Draw(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
  331.    SetAPen(rastport, (long)oldpen) ;
  332. }
  333. /*
  334.  *   This routine mucks with the colors.
  335.  */
  336. colors() {
  337.    or = nr ;
  338.    og = ng ;
  339.    ob = nb ;
  340.    adv(&or, &dr, &nr, 128) ;
  341.    adv(&og, &dg, &ng, 128) ;
  342.    adv(&ob, &db, &nb, 128) ;
  343.    SetRGB4(&(scr->ViewPort), 1L, (long)(nr >> 3),
  344.                                     (long)(ng >> 3), (long)(nb >> 3)) ;
  345. }
  346.  
  347. /*
  348.  * do_help - display help text
  349.  */
  350.  
  351. do_help()
  352. {
  353.     int i;
  354.     clr_grf();
  355.  
  356.     for (i=0; i<MAXHELP; i++)    /* dump the whole help text array */
  357.         {
  358.         if (!HelpText[i]) break;
  359.         Move(rastport,50L,(long) (i+1)*9+20);
  360.         Text(rastport,HelpText[i], (long) strlen(HelpText[i]));
  361.         }
  362.  
  363. }
  364.  
  365.  
  366. clr_grf()
  367. {
  368.    SetAPen(rastport, 0L) ;
  369.    Forbid() ;
  370.    RectFill(rastport, 0L, 0L, (long)screenwidth-1, (long)screenheight-1) ;
  371.    Permit() ;
  372.    SetAPen(rastport, 1L) ;
  373. }
  374.