home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Assembler / dse-src5.dms / in.adf / Moire.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-07  |  6.0 KB  |  251 lines

  1. /************************************************************************
  2.  * Moire.c -- Yet another graphics dazzler for the Amiga.  This one draws
  3.  *            Moire Patterns in Black and White - they tend to look better
  4.  *            that way.  Uses a borderless backdrop window to make life
  5.  *            easier, and so we get the whole screen if we want it.
  6.  *
  7.  *            Copyright (c) 1985 by Scott Ballantyne
  8.  *            (I.E. Ok to give away for nothing )
  9.  ************************************************************************/
  10.  
  11.                         /* Not all of these are used -- I include them */
  12.                         /* 'cause I'm sick of compiler warnings.       */
  13. #include <exec/types.h>
  14. #include <exec/nodes.h>
  15. #include <exec/lists.h>
  16. #include <exec/exec.h>
  17. #include <exec/execbase.h>
  18. #include <exec/ports.h>
  19. #include <exec/devices.h>
  20. #include <exec/memory.h>
  21. #include <hardware/blit.h>
  22. #include <graphics/copper.h>
  23. #include <graphics/regions.h>
  24. #include <graphics/rastport.h>
  25. #include <graphics/gfxbase.h>
  26. #include <graphics/gfxmacros.h>
  27. #include <graphics/gels.h>
  28. #include <intuition/intuition.h>
  29.  
  30. struct IntuitionBase *IntuitionBase = NULL;
  31. struct GfxBase *GfxBase = NULL;
  32.  
  33.  
  34. #define MAXX   640
  35. #define MAXY   200
  36. struct NewScreen MyScreen =
  37. { 0,0,MAXX,MAXY,1,0,1,HIRES, CUSTOMSCREEN, NULL, "Moire Patterns", 0,0,};
  38.  
  39.    struct NewWindow DrawWindow = {
  40.       0,0,MAXX,MAXY,
  41.       0,1,
  42.       MENUPICK,
  43.       BORDERLESS | BACKDROP | ACTIVATE,
  44.       NULL,
  45.       NULL,
  46.       NULL,
  47.       NULL,
  48.       NULL,
  49.       0,0,0,0,
  50.       CUSTOMSCREEN,
  51.    };
  52.  
  53. struct Screen *Screen = NULL;
  54. struct Window *Backdrop = NULL;
  55. struct RastPort *DrawRP;
  56. struct ViewPort *DrawVP;
  57. struct IntuiMessage  *message;
  58.  
  59. struct MenuItem OnlyMenuItems[4];
  60. struct IntuiText OnlyMenuText[4];
  61. struct Menu OnlyMenu[1];
  62.  
  63. #define SHOW      1
  64. #define ERASE     0
  65.  
  66. main()
  67. {
  68.    ULONG class;
  69.    USHORT code, ItemNum;
  70.  
  71.    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  72.          exit(1);
  73.  
  74.    if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  75.       cleanitup(2);
  76.  
  77.    if(!(Screen = (struct Screen *)OpenScreen(&MyScreen)))
  78.       cleanitup(3);
  79.  
  80.    DrawWindow.Screen = Screen;
  81.  
  82.    if(!(Backdrop = (struct Window *)OpenWindow(&DrawWindow)))
  83.       cleanitup(4);
  84.  
  85.  
  86.    DrawRP = Backdrop->RPort;     /* Draw into backdrop window */
  87.    DrawVP = &Screen->ViewPort;   /* Set colors in Screens VP  */
  88.    SetBPen(DrawRP, 0);           /* For clean text */
  89.    setcolors();
  90.  
  91.    initmenuitems();
  92.    initmenu();
  93.    SetMenuStrip(Backdrop, &OnlyMenu[0]);
  94.    moire();
  95.  
  96.    FOREVER {
  97.       while(message = (struct IntuiMessage *)GetMsg(Backdrop->UserPort)) {
  98.          class = message->Class;
  99.          code = message->Code;
  100.          ReplyMsg(message);
  101.  
  102.          if (class == MENUPICK && code != MENUNULL) {
  103.             ItemNum = ITEMNUM( code );
  104.             switch (ItemNum) {
  105.                case 0:
  106.                   clearRP();
  107.                   moire();
  108.                   break;
  109.                case 1:
  110.                   ShowTitle(Screen, FALSE);
  111.                   break;
  112.                case 2:
  113.                   ShowTitle(Screen, TRUE);
  114.                   break;
  115.                case 3:
  116.                   ClearMenuStrip(Backdrop);
  117.                   cleanitup(0);
  118.             }
  119.          }
  120.       }
  121.    }
  122. }
  123.  
  124. moire()
  125. {
  126.    register int x, y;
  127.    int endx;
  128.    UBYTE endy;
  129.    double rnd();
  130.  
  131.    OffMenu(Backdrop, NOITEM << 5);
  132.  
  133.    endx = MAXX * rnd();
  134.    endy = MAXY * rnd();
  135.  
  136.    for(x = 0; x <= MAXX; x++) {
  137.       doline(SHOW, x, 0, endx, endy, x, MAXY);
  138.       x++;
  139.       doline(ERASE, x, 0, endx, endy, x, MAXY);
  140.    }
  141.    for(y = 0; y <= MAXY; y++) {
  142.       doline(SHOW, 0, y, endx, endy, MAXX, y);
  143.       y++;
  144.       doline(ERASE, 0, y, endx, endy, MAXX, y);
  145.    }
  146.       OnMenu(Backdrop, NOITEM << 5);
  147. }
  148.  
  149. clearRP()
  150. {
  151.    SetAPen(DrawRP, ERASE);
  152.    SetDrMd(DrawRP, JAM1);
  153.    RectFill(DrawRP, 0, 0, MAXX, MAXY);
  154. }
  155.  
  156. doline(color, x0, y0, x1, y1, x2, y2)
  157. UBYTE color, y0, y1, y2;
  158. int x0, x1, x2;
  159. {
  160.    SetAPen(DrawRP, color);
  161.    SetDrMd(DrawRP, JAM1);
  162.    Move(DrawRP, x0, y0);
  163.    Draw(DrawRP, x1, y1);
  164.    Draw(DrawRP, x2, y2);
  165. }
  166.  
  167. cleanitup(error_code)
  168. int error_code;
  169. {
  170.    if (Backdrop)
  171.       CloseWindow(Backdrop);
  172.    if (Screen)
  173.       CloseScreen(Screen);
  174.    if (GfxBase)
  175.       CloseLibrary(GfxBase);
  176.    if (IntuitionBase)
  177.       CloseLibrary(IntuitionBase);
  178.    exit(error_code);
  179. }
  180.  
  181.  
  182. setcolors()
  183. {
  184.    SetRGB4(DrawVP, 0, 0, 0, 0);
  185.    SetRGB4(DrawVP, 1, 15, 15, 15);
  186. }
  187.  
  188. initmenuitems()
  189. {
  190.    short n;
  191.  
  192.    for(n = 0; n < 4; n++) {  /* One struct for each item */
  193.       OnlyMenuItems[n].NextItem = &OnlyMenuItems[n + 1]; /* next item */
  194.       OnlyMenuItems[n].LeftEdge = 0;
  195.       OnlyMenuItems[n].TopEdge = 10 * n;
  196.       OnlyMenuItems[n].Width = 112;
  197.       OnlyMenuItems[n].Height = 10;
  198.       OnlyMenuItems[n].Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  199.       OnlyMenuItems[n].MutualExclude = 0;
  200.       OnlyMenuItems[n].ItemFill = (APTR)&OnlyMenuText[n];
  201.       OnlyMenuItems[n].SelectFill = NULL;
  202.       OnlyMenuItems[n].Command = 0;
  203.       OnlyMenuItems[n].SubItem = NULL;
  204.       OnlyMenuItems[n].NextSelect = 0;
  205.  
  206.       OnlyMenuText[n].FrontPen = 0;
  207.       OnlyMenuText[n].BackPen = 1;
  208.       OnlyMenuText[n].DrawMode = JAM2;
  209.       OnlyMenuText[n].LeftEdge = 0;
  210.       OnlyMenuText[n].TopEdge = 1;
  211.       OnlyMenuText[n].ITextFont = NULL;
  212.       OnlyMenuText[n].NextText = NULL;
  213.    }
  214.    OnlyMenuItems[3].NextItem = NULL; /* Last item */
  215.  
  216.    OnlyMenuText[0].IText = (UBYTE *)"New Moire";
  217.    OnlyMenuText[1].IText = (UBYTE *)"Hide Title Bar";
  218.    OnlyMenuText[2].IText = (UBYTE *)"Show Title Bar";
  219.    OnlyMenuText[3].IText = (UBYTE *)"QUIT!";
  220. }
  221. initmenu()
  222. {
  223.    OnlyMenu[0].NextMenu = NULL;                 /* No more menus */
  224.    OnlyMenu[0].LeftEdge = 0;
  225.    OnlyMenu[0].TopEdge = 0;
  226.    OnlyMenu[0].Width = 85;
  227.    OnlyMenu[0].Height = 10;
  228.    OnlyMenu[0].Flags = MENUENABLED;             /* All items selectable */
  229.    OnlyMenu[0].MenuName = "Actions";
  230.    OnlyMenu[0].FirstItem = &OnlyMenuItems[0];   /* Pointer to first item */
  231. }
  232.  
  233. double
  234. rnd ()
  235. {
  236.     static int seed;
  237.     register int i, n;
  238.     double d;
  239.  
  240.     i = (seed & 1<<27) ? 1 : 0;
  241.     n = (seed & 1<<30) ? 1 : 0;
  242.     seed <<= 1;
  243.     seed |= (i ^ n);
  244.     if (seed == 0)
  245.         d = 1;
  246.     else
  247.         d = seed;
  248.     return (1.0 / d);
  249. }
  250.  
  251.