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 / Circles / circles.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  9KB  |  437 lines

  1. /*
  2.  * circles.c : Color circle pattern generator experimental interface
  3.  *
  4.  * this stuf adapted from mackie.c on fish disk 131.
  5.  *  by Joel Swank 11-16-88
  6.  *
  7.  *   Version 1.1 5/27/89
  8.  *
  9.  *
  10.  */
  11.  
  12. #include "window.h"
  13.  
  14. /*
  15.  *   The maximum number of lines on the screen at once.
  16.  */
  17. #define MAXLINES (100)
  18.  
  19. #define XRAS 100
  20. #define YRAS 100
  21.  
  22. #define INTUITION_REV 1L
  23.  
  24. /*
  25.  *   The global variables.
  26.  */
  27. struct GfxBase    *GfxBase = NULL;
  28. struct RastPort *rastport ;
  29. int screenheight, screenwidth;
  30. int erasecolor, curcolor = 1 ;
  31. struct Screen *scr = NULL, *OpenScreen() ;
  32. struct Window    *Wind = NULL, *OpenWindow() ;
  33. struct MsgPort    *CreatePort() ;
  34. struct IntuitionBase    *IntuitionBase = NULL;
  35. struct IntuiMessage    *msg, *GetMsg();
  36. struct Library *OpenLibrary();
  37. WORD areabuffer[250];
  38. struct TmpRas tmpras, *InitTmpRas();
  39. struct AreaInfo myAreaInfo;
  40. PLANEPTR planeptr = NULL, AllocRaster();
  41. struct TextFont *OpenFont();
  42. struct Window *wH = NULL;        /* help window */
  43. struct RastPort *rpH = NULL;     /* help RastPort */
  44. struct TextFont *font = NULL;
  45.  
  46. USHORT keycode, stop, rotcolor, bkgnd, freeze, erase;
  47. ULONG class;
  48.  
  49. /*
  50.  *   MainLine
  51.  */
  52. main() {
  53.    long i ;
  54.    char buf[100];
  55.  
  56.    screenheight = YSIZE;
  57.    screenwidth = XSIZE;
  58.  
  59.    if ((IntuitionBase = (struct IntuitionBase *)
  60.        OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  61.       done(21);
  62.  
  63.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  64.    if (GfxBase == NULL) done(22);
  65.  
  66.    if ( (scr = OpenScreen(&newscreen)) == NULL)
  67.        {
  68.     AutoRequest(0L,&scrfailtxt,0L,&oktxt,0L,0L,300L,75L);
  69.     done(26);
  70.     }
  71.    New_Window.Screen = scr;
  72.  
  73.    if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  74.       done(25);
  75.    rastport = Wind->RPort;
  76.  
  77.    InitArea(&myAreaInfo,areabuffer,100);
  78.    rastport->AreaInfo = &myAreaInfo;
  79.    if ((planeptr=AllocRaster(XRAS,YRAS)) == NULL)
  80.        done(18);
  81.    rastport->TmpRas = InitTmpRas(&tmpras,planeptr,RASSIZE(XRAS,YRAS));
  82.  
  83.    LoadRGB4(&(scr->ViewPort),&Palette,PaletteColorCount);
  84.  
  85.    do_help();
  86.    clr_grf();
  87.  
  88.    startlines() ;
  89.    /*for (i=0; i<MAXLINES; i++) {
  90.       advancelines() ;
  91.       drawnew() ;
  92.    } */
  93.    colors() ;
  94.    stop = FALSE;
  95.    freeze = FALSE;
  96.    erase = TRUE;
  97.    rotcolor = TRUE;
  98.  
  99.    while (1) {
  100.  
  101.     while((msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) ||
  102.          (msg = (struct IntuiMessage *) GetMsg(wH->UserPort))) {
  103.         keycode = msg->Code;
  104.         class = msg->Class;
  105.         ReplyMsg(msg);
  106.         switch(class) {
  107.             case VANILLAKEY:
  108.                 switch (keycode)
  109.                     {
  110.                     case ' ':        /* start/stop */
  111.                       if (stop == TRUE) stop = FALSE;
  112.                       else stop = TRUE;
  113.                       break;
  114.                     case 'o':        /* one iteration */
  115.                       stop = TRUE;
  116.                       goto once;
  117.                     case 'r':        /* toggle pallete changes */
  118.                       if (rotcolor == TRUE)  rotcolor = FALSE;
  119.                       else rotcolor = TRUE;
  120.                       break;
  121.                     case 'b':        /* Black/white background */
  122.                       if (bkgnd)
  123.                           {
  124.                         bkgnd = FALSE;
  125.                           SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L);
  126.                       } else {
  127.                           SetRGB4(&(scr->ViewPort), 0L, 15L, 15L, 15L);
  128.                         bkgnd = TRUE;
  129.                         }
  130.                       break;
  131.                     case 'f':        /* freeze/thaw color changing */
  132.                       if (freeze == TRUE) freeze = FALSE;
  133.                       else freeze = TRUE;
  134.                       break;
  135.                     case 'a':        /* do about requester  */
  136.                       about();
  137.                       break;
  138.                     case 'q':        /* Exit */
  139.                     case 'x':
  140.                     case '\033':
  141.                       done(0);
  142.                       break;
  143.                     }
  144.                 continue;
  145.             case REFRESHWINDOW :
  146.                 draw_help();
  147.                 break;
  148.             case CLOSEWINDOW:
  149.                 done(0);
  150.             }
  151.         }
  152.       if (stop == TRUE) 
  153.           {
  154.         Wait(( 1L << Wind->UserPort->mp_SigBit) |    /* wait on mesg */
  155.             ( 1L << wH->UserPort->mp_SigBit));
  156.         continue;
  157.         }
  158.  
  159.    once:
  160.       advancelines() ;
  161.       drawnew() ;
  162.       colors() ;
  163.       advancelines() ;
  164.       drawnew() ;
  165.       colors() ;
  166.       advancelines() ;
  167.       drawnew() ;
  168.       colors() ;
  169.       advancelines() ;
  170.       drawnew() ;
  171.       colors() ;
  172.       advancelines() ;
  173.       drawnew() ;
  174.       colors() ;
  175.       advancelines() ;
  176.       drawnew() ;
  177.       colors() ;
  178.    }
  179. }
  180. /*
  181.  * End of MainLine 
  182.  */
  183.  
  184.  
  185. /*
  186.  * done - just clean up that which is open, and then leave.
  187.  */
  188. done(how)
  189. int how;
  190.     {
  191.     if (planeptr) FreeRaster(planeptr,XRAS,YRAS);
  192.     if (wH) CloseWindow(wH) ;
  193.     if (Wind) CloseWindow(Wind) ;
  194.     if (scr) CloseScreen(scr) ;
  195.     if (font) CloseFont(font) ;
  196.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  197.     if (GfxBase) CloseLibrary(GfxBase) ;
  198.  
  199. /*    OpenWorkBench() ;        /* As requested */
  200.     exit(how) ;
  201.     }
  202.  
  203. /*
  204.  *   This routine returns a random value from 0 to n-1.
  205.  */
  206. int randm(i)
  207. int i ;
  208. {
  209.    static long seed ;
  210.    long rval ;
  211.  
  212.    if (seed == 0)
  213.       seed = 323214521 + scr->MouseX +
  214.               scr->MouseY ;
  215.    seed = seed * 123213 + 121 ;
  216.    rval = (seed >> 5) & 65535 ;
  217.    return ((i * rval) >> 16) ;
  218. }
  219. /*
  220.  *   This routine sets x and y values to a random number.
  221.  */
  222. static long x, y ;
  223. randomxy() {
  224.    x = randm(screenwidth) ;
  225.    y = randm(screenheight) ;
  226. }
  227. /*
  228.  *   Main routines are always fun.
  229.  */
  230. short x1store[MAXLINES], y1store[MAXLINES] ;
  231. short x2store[MAXLINES], y2store[MAXLINES] ;
  232. short ptr ;
  233. short dx1, dy1, dx2, dy2 ;
  234. short ox1, oy1, ox2, oy2 ;
  235. short nx1, ny1, nx2, ny2 ;
  236. short dr[8], dg[8], db[8] ;
  237. short or, og, ob ;
  238. short nr[8], ng[8], nb[8] ;
  239. /*
  240.  *   Initialize things for the first lines.
  241.  */
  242. startlines() {
  243.    int i;
  244.  
  245.    ptr = 0 ;
  246.    ox1 = randm(screenwidth) ;
  247.    ox2 = randm(screenwidth) ;
  248.    oy1 = randm(screenheight) ;
  249.    oy2 = randm(screenheight) ;
  250.    dx1 = 13 ;
  251.    dx2 = 14 ;
  252.    dy1 = 11 ;
  253.    dy2 = 16 ;
  254.    for (i=0;i < 8; i++)
  255.       {
  256.       nr[i] = (short) ((Palette[i] >>8) & 15) <<3;
  257.       ng[i] = (short) ((Palette[i] >>4) & 15) <<3;
  258.       nb[i] = (short) (Palette[i] & 15) <<3;
  259.       dr[i] = -3 ;
  260.       dg[i] = 5 ;
  261.       db[i] = 7 ;
  262.       }
  263. }
  264. /*
  265.  *   Advance the number by the delta, and check the boundaries.
  266.  */
  267. adv(o, d, n, w)
  268. short *o, *d, *n ;
  269. long w ;
  270. {
  271.    *n = *o + *d ;
  272.    if (*n < 0) {
  273.       *n = 0 ;
  274.       *d = randm(12) + 7 ;
  275.    } else if (*n >= w) {
  276.       *n = w - 1 ;
  277.       *d = - randm(12) - 7 ;
  278.    }
  279. }
  280. /*
  281.  *   Advance the number by the delta, and check the boundaries.
  282.  */
  283. advc(o, d, n, w)
  284. short *o, *d, *n ;
  285. long w ;
  286. {
  287.    *n = *o + *d ;
  288.    if (*n < 0) {
  289.       *n = 0 ;
  290.       *d = randm(6) + 1 ;
  291.    } else if (*n >= w) {
  292.       *n = w - 1 ;
  293.       *d = - randm(6) - 1 ;
  294.    }
  295. }
  296. /*
  297.  *   Advance the two points which make up the lines.
  298.  */
  299. advancelines() {
  300.    adv(&ox1, &dx1, &nx1, screenwidth) ;
  301.    adv(&ox2, &dx2, &nx2, screenwidth) ;
  302.    adv(&oy1, &dy1, &ny1, screenheight) ;
  303.    adv(&oy2, &dy2, &ny2, screenheight) ;
  304. }
  305. /*
  306.  *   Draw a new set of filled circles.
  307.  */
  308. drawnew() {
  309.    x1store[ptr] = ox1 = nx1 ;
  310.    x2store[ptr] = ox2 = nx2 ;
  311.    y1store[ptr] = oy1 = ny1 ;
  312.    y2store[ptr] = oy2 = ny2 ;
  313.    DrawSpot((long)ox2, (long)oy2) ;
  314.    DrawSpot((long)(screenwidth-ox1-1), (long)(screenheight-oy1-1)) ;
  315.    DrawSpot((long)(screenwidth-ox2-1), (long)(screenheight-oy2-1)) ;
  316.    DrawSpot((long)ox1, (long)oy1) ;
  317.    ptr++ ;
  318.    if (ptr == MAXLINES)
  319.       ptr = 0 ;
  320. }
  321. /*
  322.  *   Draw a filled circle at given location with clipping
  323.  */
  324. DrawSpot(x,y)
  325. long x,y;
  326. {
  327.    long radi;
  328.    radi = randm(30)+10;
  329.    if (x<radi) radi = x;
  330.    if (x+radi>XSIZE) radi = XSIZE-x;
  331.    if (y<radi) radi = y;
  332.    if (y+radi>YSIZE) radi = YSIZE-y;
  333.    if (radi > 1)
  334.       {
  335.       AreaEllipse(rastport,x,y,radi,radi);
  336.       AreaEnd(rastport);
  337.       }
  338. }
  339.  
  340. /*
  341.  *   This routine mucks with the colors.
  342.  */
  343. colors() {
  344.  
  345.    if (!freeze)
  346.       {
  347.       if (++curcolor > 7) curcolor = 1 ;
  348.       SetAPen(rastport, (long)curcolor) ;
  349.       }
  350.  
  351.    if (rotcolor )
  352.       {
  353.       or = nr[curcolor] ;
  354.       og = ng[curcolor] ;
  355.       ob = nb[curcolor] ;
  356.       advc(&or, &dr[curcolor], &nr[curcolor], 128) ;
  357.       advc(&og, &dg[curcolor], &ng[curcolor], 128) ;
  358.       advc(&ob, &db[curcolor], &nb[curcolor], 128) ;
  359.       SetRGB4(&(scr->ViewPort), (long)curcolor, (long)(nr[curcolor] >> 3),
  360.                 (long)(ng[curcolor] >> 3), (long)(nb[curcolor] >> 3)) ;
  361.       }
  362. }
  363.  
  364.  
  365. /*
  366.  * do_help - display help text
  367.  */
  368.  
  369. do_help()
  370. {
  371.  
  372.     NewWindowStructureHelp.Screen = scr;
  373.  
  374.     wH = OpenWindow(&NewWindowStructureHelp);    /* open the window */
  375.     if ( wH == NULL )
  376.         {
  377.         AutoRequest(Wind,&winfailtxt,0L,&oktxt,0L,0L,300L,75L);
  378.         done(20);
  379.         }
  380.  
  381.     rpH = wH->RPort;    /* get a rastport pointer for the window */
  382.  
  383.     font = OpenFont(&TOPAZ80); /* make sure correct size font */
  384.     if (font) SetFont(rpH,font);
  385.     SetAPen(rpH,1L);
  386.     draw_help();
  387. }
  388.  
  389. draw_help()
  390. {
  391.     int i;
  392.     for (i=0; i<MAXHELP; i++)    /* dump the whole help text array */
  393.         {
  394.         if (!HelpText[i]) break;
  395.         Move(rpH,5L,(long) (i+1)*9+20);
  396.         Text(rpH,HelpText[i], (long) strlen(HelpText[i]));
  397.         }
  398. }
  399.  
  400.  
  401.  
  402. /*
  403.  *  Handle 'about' request
  404.  */
  405.  
  406.  
  407. about()
  408. {
  409.     AutoRequest(wH,&aboutmsg,0L,&oktxt,0L,0L,300L,75L);
  410. }
  411.  
  412.  
  413.  
  414. clr_grf()
  415. {
  416.    SetAPen(rastport, 0L) ;
  417.    Forbid() ;
  418.    RectFill(rastport, 0L, 0L, (long)screenwidth-1, (long)screenheight-1) ;
  419.    Permit() ;
  420.    SetAPen(rastport, 1L) ;
  421. }
  422.  
  423. #ifdef AZTEC_C
  424. /* 
  425.  * Gracefull exit
  426.  */
  427. _abort()
  428. {
  429.     done(13);
  430. }
  431.  
  432. _wb_parse()
  433. {
  434. }
  435.  
  436. #endif
  437.